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) { |
134 |
< |
acl += "ALLOW:" + item._expression + " "; |
128 |
> |
public String toString() { |
129 |
> |
StringBuffer acl = new StringBuffer(); |
130 |
> |
acl.append("{"); |
131 |
> |
for(int i=0; i < _acl.size(); i++) { |
132 |
> |
ACLRule rule = (ACLRule) _acl.get(i); |
133 |
> |
if(rule._allow) { |
134 |
> |
acl.append(rule._expression + "=ALLOW"); |
135 |
|
} |
136 |
|
else { |
137 |
< |
acl += "DENY:" + item._expression + " "; |
137 |
> |
acl.append(rule._expression + "=DENY"); |
138 |
|
} |
139 |
+ |
acl.append(","); |
140 |
|
} |
141 |
< |
return acl.substring(0, acl.length()-1); |
141 |
> |
if(_defaultMode) { |
142 |
> |
acl.append("DEFAULT=ALLOW"); |
143 |
> |
} |
144 |
> |
else { |
145 |
> |
acl.append("DEFAULT=DENY"); |
146 |
> |
} |
147 |
> |
acl.append("}"); |
148 |
> |
return acl.toString(); |
149 |
|
} |
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 |
– |
} |
150 |
|
|
151 |
|
//---PRIVATE METHODS--- |
152 |
|
|
166 |
|
private String _name = null; |
167 |
|
|
168 |
|
/** |
169 |
< |
* 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. |
169 |
> |
* The ACL is stored in this ArrayList. |
170 |
|
*/ |
171 |
< |
private LinkedList _acl = new LinkedList(); |
171 |
> |
private ArrayList _acl = new ArrayList(); |
172 |
|
|
173 |
|
/** |
174 |
|
* The default mode of this ACL. |
182 |
|
/** |
183 |
|
* Wrapper class for an ACL rule. |
184 |
|
*/ |
185 |
< |
private class ACLRule { |
185 |
> |
private class ACLRule implements Serializable { |
186 |
|
|
187 |
|
/** |
188 |
|
* Construct an ACL rule. |