KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > IpRestriction


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.security;
21
22 /**
23  * Encapsulates a single <i>IP Restriction</i>.
24  * <p>
25  * IP restrictions are used to test if the IP address of of any incoming
26  * network request (i.e. load a page, connecting agent, connecting
27  * WebDav client) is allowed access.
28  * <p>
29  * Each restrictions consists of an <i>Address Pattern</i> that may
30  * either be :-
31  * <ul>
32  * <li>A fully qualified IPV4 address. E.g. 192.168.1.10 would only match when
33  * this exact IP address attempts access.</li>
34  * <li>A wildcard address. E.g. 192.168.1.* would match any host from the
35  * 192.168.1 subnet.</li>
36  * <ul>
37  * <p>
38  * Ip restrictions may either be <i>Allow</i> restrictions or <i>Deny</i>
39  * restrictions.
40  * <p>
41  * Ip restrictions have a <i>Priority</i> which determines the order in
42  * which the are processed.
43  *
44  *
45  * @author Karl Moore <a HREF="mailto:karl@3sp.com">&lt;karl@3sp.com&gt;</a>
46  */

47 public class IpRestriction implements Comparable JavaDoc<IpRestriction> {
48
49     /**
50      * The address pattern for this restriction <i>allows</i> any addresses
51      * that <i>exactly matches</i>.
52      */

53     public final static int ALLOWED = 1;
54     
55     /**
56      * The address pattern for this restriction <i>allows</i> any addresses
57      * that match the <i>wildcard</i>
58      */

59     public final static int ALLOWED_WILDCARD = 2;
60     
61     /**
62      * The address pattern for this restriction <i>denies</i> any addresses
63      * that <i>exactly matches</i>.
64      */

65     public final static int DENIED = 3;
66     
67     /**
68      * The address pattern for this restriction <i>denies</i> any addresses
69      * that match the <i>wildcard</i>
70      */

71     public final static int DENIED_WILDCARD = 4;
72     
73     // Private instance variables
74

75     private final int id;
76     private String JavaDoc addressPattern;
77     private int type;
78     private int priority;
79
80     /**
81      * Constructor.
82      *
83      * @param isAllow address is an allow pattern
84      */

85     public IpRestriction(boolean isAllow) {
86         this("", isAllow);
87     }
88
89     /**
90      * Constructor.
91      *
92      * @param addressPattern address pattern
93      * @param isAllow address is an allow pattern
94      */

95     public IpRestriction(String JavaDoc addressPattern, boolean isAllow) {
96         this(addressPattern, isAllow, Integer.MAX_VALUE);
97     }
98
99     /**
100      * Constructor.
101      *
102      * @param addressPattern address pattern
103      * @param isAllow address is an allow pattern
104      * @param priority priority of restriction
105      */

106     public IpRestriction(String JavaDoc addressPattern, boolean isAllow, int priority) {
107         this(-1, addressPattern, getType(addressPattern, isAllow), 0);
108     }
109     
110     /**
111      * Constructor.
112      *
113      * @param id id
114      * @param addressPattern address pattern
115      * @param type type
116      * @param priority priority of restriction
117      */

118     public IpRestriction(int id, String JavaDoc addressPattern, int type, int priority) {
119         this.addressPattern = addressPattern;
120         this.type = type;
121         this.id = id;
122         this.priority = priority;
123     }
124
125     /**
126      * Get the Id of this restriction
127      *
128      * @return restriction Id
129      */

130     public int getID() {
131         return id;
132     }
133
134     /**
135      * Get the address pattern of this restriction. See class description
136      * for details of what is allowed as an address.
137      *
138      * @return address
139      */

140     public String JavaDoc getAddress() {
141         return addressPattern;
142     }
143
144     /**
145      * Set the address pattern of this restriction. See class description
146      * for details of what is allowed as an address.
147      *
148      * @param addressPattern address pattern
149      */

150     public void setAddress(String JavaDoc addressPattern) {
151         this.addressPattern = addressPattern;
152     }
153     
154     /**
155      * Get if this is an <i>allowed</i> address. This will be true
156      * if the type is {@link #ALLOWED} or {@link #ALLOWED_WILDCARD}.
157      *
158      * @return true if the IpRestriction is an allow
159      */

160     public boolean getAllowed() {
161         return ALLOWED == type || ALLOWED_WILDCARD == type;
162     }
163
164     /**
165      * Get if this is an <i>denied</i> address. This will be true
166      * if the type is {@link #DENIED} or {@link #DENIED_WILDCARD}.
167      *
168      * @return true if the IpRestriction is a deny
169      */

170     public boolean getDenied() {
171         return DENIED == type || DENIED_WILDCARD == type;
172     }
173     
174     /**
175      * Get if this restriction contains a wildcard address pattern.
176      *
177      * @return true if the IpRestriction supports wildcard matching
178      */

179     public boolean isWildcardMatch() {
180         return ALLOWED_WILDCARD == getType() || DENIED_WILDCARD == getType();
181     }
182     
183     /**
184      * Get the type code for the given address pattern and allow / deny
185      * type.
186      *
187      * @param address address pattern
188      * @param isAllow <code>true</code> if this pattern is an allow type
189      * @return int type representing the IpRestriction
190      * @see #getType()
191      */

192     public static int getType(String JavaDoc address, boolean isAllow) {
193         if (address.indexOf('*') > -1) {
194             return isAllow ? ALLOWED_WILDCARD : DENIED_WILDCARD;
195         } else {
196             return isAllow ? ALLOWED : DENIED;
197         }
198     }
199
200     /**
201      * Get the priority of this restriction. The close to <code>zero</code>
202      * the higher the priority of the restriction.
203      *
204      * @return priority
205      */

206     public int getPriority() {
207         return priority;
208     }
209
210     /**
211      * Set the priority of this restriction. The close to <code>zero</code>
212      * the higher the priority of the restriction.
213      *
214      * @param priority priority
215      */

216     public void setPriority(int priority) {
217         this.priority = priority;
218     }
219
220     /**
221      * Get the type of Ip restriction address pattern. Will be one of
222      * {@link #ALLOWED}, {@link #ALLOWED_WILDCARD}, {@link #DENIED} or
223      * {@link #DENIED_WILDCARD}.
224      *
225      * @return ip restriction address pattern type
226      */

227     public int getType() {
228         return type;
229     }
230
231     /**
232      * Set the type of Ip restriction address pattern. Should be one of
233      * {@link #ALLOWED}, {@link #ALLOWED_WILDCARD}, {@link #DENIED} or
234      * {@link #DENIED_WILDCARD}.
235      *
236      * @param type type
237      * @see #getType(String, boolean)
238      */

239     public void setType(int type) {
240         this.type = type;
241     }
242
243     /* (non-Javadoc)
244      * @see java.lang.Object#equals(java.lang.Object)
245      */

246     public boolean equals(Object JavaDoc obj) {
247         return obj instanceof IpRestriction && ((IpRestriction)obj).getID() == getID();
248     }
249
250     /**
251      * Compare this restriction against another using the address.
252      *
253      * @param o other restriction to compare against
254      * @return comparsion (see {@link Comparable#compareTo(Object)}).
255      */

256     public int compareTo(IpRestriction o) {
257         return getAddress().compareTo(o.getAddress());
258     }
259
260     /**
261      * Get if this restriction is the default restriction (i.e. has an
262      * address pattern of <strong>*.*.*.*</strong>)
263      *
264      * @return default restriction
265      */

266     public boolean isDefault() {
267         return getAddress().equals("*.*.*.*");
268     }
269 }
Popular Tags