3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
|
import uk.org.iscream.cms.server.util.*; |
6 |
< |
import java.util.LinkedList; |
7 |
< |
import java.util.Iterator; |
6 |
> |
import java.util.ArrayList; |
7 |
|
import java.net.InetAddress; |
8 |
+ |
import java.io.Serializable; |
9 |
|
|
10 |
|
/** |
11 |
|
* Access Control List for use primarily |
18 |
|
* @author $Author$ |
19 |
|
* @version $Id$ |
20 |
|
*/ |
21 |
< |
public class ACL { |
21 |
> |
public class ACL implements Serializable { |
22 |
|
|
23 |
|
//---FINAL ATTRIBUTES--- |
24 |
|
|
87 |
|
* @return whether the address was permitted by the ACL |
88 |
|
*/ |
89 |
|
public boolean check(String address) { |
90 |
< |
Iterator i = _acl.iterator(); |
91 |
< |
while(i.hasNext()) { |
92 |
< |
ACLRule item = (ACLRule) i.next(); |
93 |
< |
if(StringUtils.wildcardCheck(address, item._expression)) { |
94 |
< |
return item._allow; |
90 |
> |
for(int i=0; i < _acl.size(); i++) { |
91 |
> |
ACLRule rule = (ACLRule) _acl.get(i); |
92 |
> |
if(StringUtils.wildcardCheck(address, rule._expression)) { |
93 |
> |
return rule._allow; |
94 |
|
} |
95 |
|
} |
96 |
|
return _defaultMode; |
108 |
|
* @return whether the InetAddress was permitted by the ACL |
109 |
|
*/ |
110 |
|
public boolean check(InetAddress address) { |
111 |
< |
Iterator i = _acl.iterator(); |
112 |
< |
while(i.hasNext()) { |
113 |
< |
ACLRule item = (ACLRule) i.next(); |
114 |
< |
if(StringUtils.wildcardCheck(address.getHostName(), item._expression)) { |
116 |
< |
return item._allow; |
111 |
> |
for(int i=0; i < _acl.size(); i++) { |
112 |
> |
ACLRule rule = (ACLRule) _acl.get(i); |
113 |
> |
if(StringUtils.wildcardCheck(address.getHostName(), rule._expression)) { |
114 |
> |
return rule._allow; |
115 |
|
} |
116 |
< |
if(StringUtils.wildcardCheck(address.getHostAddress(), item._expression)) { |
117 |
< |
return item._allow; |
116 |
> |
if(StringUtils.wildcardCheck(address.getHostAddress(), rule._expression)) { |
117 |
> |
return rule._allow; |
118 |
|
} |
119 |
|
} |
120 |
|
return _defaultMode; |
121 |
|
} |
122 |
|
|
123 |
|
/** |
124 |
< |
* Gets the ACL as a String for debugging. |
124 |
> |
* Gives a String representation of this ACL. |
125 |
|
* |
126 |
|
* @return A String representation of this ACL. |
127 |
|
*/ |
128 |
< |
public String getStringACL() { |
129 |
< |
String acl = ""; |
130 |
< |
Iterator i = _acl.iterator(); |
131 |
< |
while(i.hasNext()) { |
132 |
< |
ACLRule item = (ACLRule) i.next(); |
133 |
< |
if(item._allow) { |
136 |
< |
acl += "ALLOW:" + item._expression + " "; |
137 |
< |
} |
138 |
< |
else { |
139 |
< |
acl += "DENY:" + item._expression + " "; |
140 |
< |
} |
128 |
> |
public String toString() { |
129 |
> |
StringBuffer acl = new StringBuffer(); |
130 |
> |
acl.append("{"); |
131 |
> |
for(int i=0; i < _acl.size(); i++) { |
132 |
> |
acl.append((ACLRule) _acl.get(i)); |
133 |
> |
acl.append(","); |
134 |
|
} |
135 |
< |
return acl.substring(0, acl.length()-1); |
135 |
> |
if(_defaultMode) { |
136 |
> |
acl.append("DEFAULT=ALLOW"); |
137 |
> |
} |
138 |
> |
else { |
139 |
> |
acl.append("DEFAULT=DENY"); |
140 |
> |
} |
141 |
> |
acl.append("}"); |
142 |
> |
return acl.toString(); |
143 |
|
} |
144 |
– |
|
145 |
– |
/** |
146 |
– |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
147 |
– |
* method to provide clean logging (every class should have this). |
148 |
– |
* |
149 |
– |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
150 |
– |
* to format the toString() |
151 |
– |
* |
152 |
– |
* @return the name of this class and its CVS revision |
153 |
– |
*/ |
154 |
– |
public String toString() { |
155 |
– |
return FormatName.getName( |
156 |
– |
_name, |
157 |
– |
getClass().getName(), |
158 |
– |
REVISION); |
159 |
– |
} |
144 |
|
|
145 |
|
//---PRIVATE METHODS--- |
146 |
|
|
160 |
|
private String _name = null; |
161 |
|
|
162 |
|
/** |
163 |
< |
* The ACL is stored in this LinkedList. |
180 |
< |
* This is ideal as the list is always searched |
181 |
< |
* from beginning to end in an iterative fashion. |
163 |
> |
* The ACL is stored in this ArrayList. |
164 |
|
*/ |
165 |
< |
private LinkedList _acl = new LinkedList(); |
165 |
> |
private ArrayList _acl = new ArrayList(); |
166 |
|
|
167 |
|
/** |
168 |
|
* The default mode of this ACL. |
176 |
|
/** |
177 |
|
* Wrapper class for an ACL rule. |
178 |
|
*/ |
179 |
< |
private class ACLRule { |
179 |
> |
private class ACLRule implements Serializable { |
180 |
|
|
181 |
|
/** |
182 |
|
* Construct an ACL rule. |
187 |
|
private ACLRule(boolean allow, String expression) { |
188 |
|
_allow = allow; |
189 |
|
_expression = expression; |
190 |
+ |
} |
191 |
+ |
|
192 |
+ |
/** |
193 |
+ |
* Returns a String representation of this rule. |
194 |
+ |
* |
195 |
+ |
* @return A String representation of this rule. |
196 |
+ |
*/ |
197 |
+ |
public String toString() { |
198 |
+ |
if(_allow) { |
199 |
+ |
return _expression + "=ALLOW"; |
200 |
+ |
} |
201 |
+ |
else { |
202 |
+ |
return _expression + "=DENY"; |
203 |
+ |
} |
204 |
|
} |
205 |
|
|
206 |
|
/** |