KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > util > xmlreader > AccessConstraintConfig


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
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 &lt;access-constraint&gt;...&lt;/access-constraint&gt;.
25  * @author Akshathkumar Shetty
26  * @since 1.3.3
27  */

28 public class AccessConstraintConfig implements Serializable {
29     private static Logger logger = Logger.getLogger(AccessConstraintConfig.class.getName());
30
31     private IpFilterConfig ipFilterConfig;
32     
33     
34     /**
35      * Returns the IpFilterConfig.
36      * @return IpFilterConfig
37      */

38     public IpFilterConfig getIpFilterConfig() {
39         return ipFilterConfig;
40     }
41     /**
42      * Sets the IpFilterConfig
43      * XML Tag: &lt;ip-filter&gt;&lt;/ip-filter&gt;
44      * @param ipFilterConfig
45      */

46     public void setIpFilterConfig(IpFilterConfig ipFilterConfig) {
47         this.ipFilterConfig = ipFilterConfig;
48     }
49
50     /**
51      * Returns XML config of this class.
52      */

53     public String JavaDoc toXML(String JavaDoc pad) {
54         if(pad==null) pad="";
55         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
56
57         sb.append(pad+"<access-constraint>\n");
58         if(getIpFilterConfig()!=null)
59             sb.append(getIpFilterConfig().toXML(pad+"\t"));
60         sb.append(pad+"</access-constraint>\n");
61         return sb.toString();
62     }
63
64     /**
65      * Finds if the socket has access to connect to server.
66      * Based on the access constrains set.
67      * @exception SecurityException if access not allowed.
68      */

69     public void checkAccept(Socket socket) {
70         if(socket==null || ipFilterConfig==null || ipFilterConfig.getEnable()==false)
71             return;
72         String JavaDoc remoteIp = socket.getInetAddress().getHostAddress();
73         boolean accessFlag = ipFilterConfig.getAllowAccess()==true;
74
75         if(ipFilterConfig.getIpCollection().contains(remoteIp)!=accessFlag) {
76             try {
77                 socket.close();
78             } catch(IOException e) {
79                 logger.warning("IOException : "+e.getMessage());
80             }
81             socket = null;
82             throw new SecurityException JavaDoc("Accept denied from "+remoteIp);
83         }
84     }
85 }
86
Popular Tags