1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.filters;
35
36 import info.magnolia.cms.util.ServletUtil;
37 import info.magnolia.cms.util.SimpleUrlPattern;
38 import info.magnolia.context.MgnlContext;
39 import info.magnolia.context.WebContext;
40 import org.apache.commons.lang.StringUtils;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import javax.servlet.http.HttpServletRequest;
45 import java.util.ArrayList;
46 import java.util.Collection;
47 import java.util.Iterator;
48 import java.util.regex.Matcher;
49 import java.util.regex.Pattern;
50
51
52
53
54
55 public class Mapping {
56
57 private static Logger log = LoggerFactory.getLogger(Mapping.class);
58
59 private static final String METACHARACTERS = "([\\^\\(\\)\\{\\}\\[\\]*$+])";
60
61 protected static String escapeMetaCharacters(String str) {
62 return str.replaceAll(METACHARACTERS, "\\\\$1");
63 }
64
65 protected Collection<Pattern> mappings = new ArrayList<Pattern>();
66
67 public MatchingResult match(HttpServletRequest request) {
68 Matcher matcher = findMatcher(request);
69 boolean matches = matcher != null;
70 int matchingEndPosition = determineMatchingEnd(matcher);
71 return new MatchingResult(matcher, matches, matchingEndPosition);
72 }
73
74
75
76
77
78 private int determineMatchingEnd(Matcher matcher) {
79 if (matcher == null) {
80 return -1;
81 }
82 if (matcher.groupCount() > 0) {
83 return matcher.end(1);
84 }
85 return matcher.end();
86 }
87
88 private Matcher findMatcher(HttpServletRequest request) {
89 String uri = null;
90 WebContext ctx = MgnlContext.getWebContextOrNull();
91 if (ctx != null) {
92 uri = ctx.getAggregationState().getCurrentURI();
93 }
94 if (uri == null) {
95
96 uri = ServletUtil.stripPathParameters(request.getRequestURI());
97 String contextPath = request.getContextPath();
98
99 if (uri.startsWith(contextPath + "/")) {
100 uri = uri.substring(contextPath.length());
101 }
102 }
103 return findMatcher(uri);
104 }
105
106 private Matcher findMatcher(String uri) {
107 for (Iterator iter = mappings.iterator(); iter.hasNext();) {
108 final Matcher matcher = ((Pattern) iter.next()).matcher(uri);
109
110 if (matcher.find()) {
111 return matcher;
112 }
113 }
114
115 return null;
116 }
117
118 public Collection<Pattern> getMappings() {
119 return mappings;
120 }
121
122 public void setMappings(Collection<String> mappings) {
123 for (String mapping : mappings) {
124 this.addMapping(mapping);
125 }
126 }
127
128
129
130
131
132
133
134
135 public void addMapping(final String mapping) {
136 final String pattern;
137
138
139
140 if (isDefaultMapping(mapping)) {
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 private boolean isPathMapping(String mapping) {
176 return mapping.startsWith("/") && mapping.endsWith("/*");
177 }
178
179 private boolean isExtensionMapping(String mapping) {
180 return mapping.startsWith("*.");
181 }
182
183 private boolean isDefaultMapping(String mapping) {
184
185
186 return mapping.equals("/");
187 }
188
189 private boolean isRegexpMapping(String mapping) {
190 return mapping.startsWith("regex:");
191 }
192
193
194
195
196 public static class MatchingResult {
197
198 Matcher matcher;
199
200 boolean matches;
201
202 int matchingEndPosition;
203
204 public MatchingResult(Matcher matcher, boolean matches, int matchingEndPosition) {
205 this.matcher = matcher;
206 this.matches = matches;
207 this.matchingEndPosition = matchingEndPosition;
208 }
209
210 public Matcher getMatcher() {
211 return matcher;
212 }
213
214 public boolean isMatching() {
215 return matches;
216 }
217
218 public int getMatchingEndPosition() {
219 return matchingEndPosition;
220 }
221 }
222
223 }