|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.intercept.web; |
|
17 |
| |
|
18 |
| import org.acegisecurity.ConfigAttributeDefinition; |
|
19 |
| |
|
20 |
| import org.apache.commons.logging.Log; |
|
21 |
| import org.apache.commons.logging.LogFactory; |
|
22 |
| |
|
23 |
| import org.apache.oro.text.regex.MalformedPatternException; |
|
24 |
| import org.apache.oro.text.regex.Pattern; |
|
25 |
| import org.apache.oro.text.regex.PatternMatcher; |
|
26 |
| import org.apache.oro.text.regex.Perl5Compiler; |
|
27 |
| import org.apache.oro.text.regex.Perl5Matcher; |
|
28 |
| |
|
29 |
| import java.util.HashSet; |
|
30 |
| import java.util.Iterator; |
|
31 |
| import java.util.List; |
|
32 |
| import java.util.Set; |
|
33 |
| import java.util.Vector; |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| public class RegExpBasedFilterInvocationDefinitionMap |
|
61 |
| extends AbstractFilterInvocationDefinitionSource |
|
62 |
| implements FilterInvocationDefinitionMap { |
|
63 |
| |
|
64 |
| |
|
65 |
| private static final Log logger = LogFactory.getLog(RegExpBasedFilterInvocationDefinitionMap.class); |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| private List requestMap = new Vector(); |
|
70 |
| private boolean convertUrlToLowercaseBeforeComparison = false; |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
2
| public Iterator getConfigAttributeDefinitions() {
|
|
75 |
2
| Set set = new HashSet();
|
|
76 |
2
| Iterator iter = requestMap.iterator();
|
|
77 |
| |
|
78 |
2
| while (iter.hasNext()) {
|
|
79 |
2
| EntryHolder entryHolder = (EntryHolder) iter.next();
|
|
80 |
2
| set.add(entryHolder.getConfigAttributeDefinition());
|
|
81 |
| } |
|
82 |
| |
|
83 |
2
| return set.iterator();
|
|
84 |
| } |
|
85 |
| |
|
86 |
3
| public void setConvertUrlToLowercaseBeforeComparison(
|
|
87 |
| boolean convertUrlToLowercaseBeforeComparison) { |
|
88 |
3
| this.convertUrlToLowercaseBeforeComparison = convertUrlToLowercaseBeforeComparison;
|
|
89 |
| } |
|
90 |
| |
|
91 |
7
| public boolean isConvertUrlToLowercaseBeforeComparison() {
|
|
92 |
7
| return convertUrlToLowercaseBeforeComparison;
|
|
93 |
| } |
|
94 |
| |
|
95 |
4
| public int getMapSize() {
|
|
96 |
4
| return this.requestMap.size();
|
|
97 |
| } |
|
98 |
| |
|
99 |
22
| public void addSecureUrl(String perl5RegExp, ConfigAttributeDefinition attr) {
|
|
100 |
22
| Pattern compiledPattern;
|
|
101 |
22
| Perl5Compiler compiler = new Perl5Compiler();
|
|
102 |
| |
|
103 |
22
| try {
|
|
104 |
22
| compiledPattern = compiler.compile(perl5RegExp,
|
|
105 |
| Perl5Compiler.READ_ONLY_MASK); |
|
106 |
| } catch (MalformedPatternException mpe) { |
|
107 |
1
| throw new IllegalArgumentException("Malformed regular expression: "
|
|
108 |
| + perl5RegExp); |
|
109 |
| } |
|
110 |
| |
|
111 |
21
| requestMap.add(new EntryHolder(compiledPattern, attr));
|
|
112 |
| |
|
113 |
21
| if (logger.isDebugEnabled()) {
|
|
114 |
0
| logger.debug("Added regular expression: "
|
|
115 |
| + compiledPattern.getPattern().toString() + "; attributes: " |
|
116 |
| + attr); |
|
117 |
| } |
|
118 |
| } |
|
119 |
| |
|
120 |
7
| public ConfigAttributeDefinition lookupAttributes(String url) {
|
|
121 |
7
| PatternMatcher matcher = new Perl5Matcher();
|
|
122 |
| |
|
123 |
7
| Iterator iter = requestMap.iterator();
|
|
124 |
| |
|
125 |
7
| if (convertUrlToLowercaseBeforeComparison) {
|
|
126 |
1
| url = url.toLowerCase();
|
|
127 |
| |
|
128 |
1
| if (logger.isDebugEnabled()) {
|
|
129 |
0
| logger.debug("Converted URL to lowercase, from: '" + url
|
|
130 |
| + "'; to: '" + url + "'"); |
|
131 |
| } |
|
132 |
| } |
|
133 |
| |
|
134 |
7
| while (iter.hasNext()) {
|
|
135 |
7
| EntryHolder entryHolder = (EntryHolder) iter.next();
|
|
136 |
| |
|
137 |
7
| boolean matched = matcher.matches(url,
|
|
138 |
| entryHolder.getCompiledPattern()); |
|
139 |
| |
|
140 |
7
| if (logger.isDebugEnabled()) {
|
|
141 |
0
| logger.debug("Candidate is: '" + url + "'; pattern is "
|
|
142 |
| + entryHolder.getCompiledPattern().getPattern() |
|
143 |
| + "; matched=" + matched); |
|
144 |
| } |
|
145 |
| |
|
146 |
7
| if (matched) {
|
|
147 |
5
| return entryHolder.getConfigAttributeDefinition();
|
|
148 |
| } |
|
149 |
| } |
|
150 |
| |
|
151 |
2
| return null;
|
|
152 |
| } |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| protected class EntryHolder { |
|
157 |
| private ConfigAttributeDefinition configAttributeDefinition; |
|
158 |
| private Pattern compiledPattern; |
|
159 |
| |
|
160 |
21
| public EntryHolder(Pattern compiledPattern,
|
|
161 |
| ConfigAttributeDefinition attr) { |
|
162 |
21
| this.compiledPattern = compiledPattern;
|
|
163 |
21
| this.configAttributeDefinition = attr;
|
|
164 |
| } |
|
165 |
| |
|
166 |
1
| protected EntryHolder() {
|
|
167 |
1
| throw new IllegalArgumentException("Cannot use default constructor");
|
|
168 |
| } |
|
169 |
| |
|
170 |
7
| public Pattern getCompiledPattern() {
|
|
171 |
7
| return compiledPattern;
|
|
172 |
| } |
|
173 |
| |
|
174 |
7
| public ConfigAttributeDefinition getConfigAttributeDefinition() {
|
|
175 |
7
| return configAttributeDefinition;
|
|
176 |
| } |
|
177 |
| } |
|
178 |
| } |