01: /*
02: * This file is part of the QuickServer library
03: * Copyright (C) 2003-2005 QuickServer.org
04: *
05: * Use, modification, copying and distribution of this software is subject to
06: * the terms and conditions of the GNU Lesser General Public License.
07: * You should have received a copy of the GNU LGP License along with this
08: * library; if not, you can download a copy from <http://www.quickserver.org/>.
09: *
10: * For questions, suggestions, bug-reports, enhancement-requests etc.
11: * visit http://www.quickserver.org
12: *
13: */
14:
15: package org.quickserver.util.xmlreader;
16:
17: import java.util.*;
18: import java.net.*;
19: import java.io.*;
20: import java.util.logging.*;
21:
22: /**
23: * This class encapsulate the access constraints on servers running.
24: * The xml is <access-constraint>...</access-constraint>.
25: * @author Akshathkumar Shetty
26: * @since 1.3.3
27: */
28: public class AccessConstraintConfig implements Serializable {
29: private static Logger logger = Logger
30: .getLogger(AccessConstraintConfig.class.getName());
31:
32: private IpFilterConfig ipFilterConfig;
33:
34: /**
35: * Returns the IpFilterConfig.
36: * @return IpFilterConfig
37: */
38: public IpFilterConfig getIpFilterConfig() {
39: return ipFilterConfig;
40: }
41:
42: /**
43: * Sets the IpFilterConfig
44: * XML Tag: <ip-filter></ip-filter>
45: * @param ipFilterConfig
46: */
47: public void setIpFilterConfig(IpFilterConfig ipFilterConfig) {
48: this .ipFilterConfig = ipFilterConfig;
49: }
50:
51: /**
52: * Returns XML config of this class.
53: */
54: public String toXML(String pad) {
55: if (pad == null)
56: pad = "";
57: StringBuffer sb = new StringBuffer();
58:
59: sb.append(pad + "<access-constraint>\n");
60: if (getIpFilterConfig() != null)
61: sb.append(getIpFilterConfig().toXML(pad + "\t"));
62: sb.append(pad + "</access-constraint>\n");
63: return sb.toString();
64: }
65:
66: /**
67: * Finds if the socket has access to connect to server.
68: * Based on the access constrains set.
69: * @exception SecurityException if access not allowed.
70: */
71: public void checkAccept(Socket socket) {
72: if (socket == null || ipFilterConfig == null
73: || ipFilterConfig.getEnable() == false)
74: return;
75: String remoteIp = socket.getInetAddress().getHostAddress();
76: boolean accessFlag = ipFilterConfig.getAllowAccess() == true;
77:
78: if (ipFilterConfig.getIpCollection().contains(remoteIp) != accessFlag) {
79: try {
80: socket.close();
81: } catch (IOException e) {
82: logger.warning("IOException : " + e.getMessage());
83: }
84: socket = null;
85: throw new SecurityException("Accept denied from "
86: + remoteIp);
87: }
88: }
89: }
|