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.util;
35
36 import info.magnolia.cms.core.AggregationState;
37 import info.magnolia.context.MgnlContext;
38 import info.magnolia.context.WebContext;
39
40 import java.lang.reflect.InvocationTargetException;
41 import java.lang.reflect.Method;
42 import java.util.regex.Pattern;
43
44 import org.apache.commons.lang.StringUtils;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48
49
50
51
52
53
54
55
56 public final class SimpleUrlPattern implements UrlPattern {
57
58 private static final Logger log = LoggerFactory.getLogger(SimpleUrlPattern.class);
59
60
61
62
63 private static final long serialVersionUID = 223L;
64
65
66
67
68 public static final String URL_CHAR_PATTERN = ".";
69
70
71
72
73 public static final String MULTIPLE_CHAR_PATTERN = URL_CHAR_PATTERN + "*";
74
75
76
77
78 public static final String SINGLE_CHAR_PATTERN = URL_CHAR_PATTERN + "?";
79
80
81
82
83 private Pattern pattern;
84
85
86
87
88 private int length;
89
90
91
92
93 private String patternString;
94
95 private String site;
96
97
98
99
100
101
102 public SimpleUrlPattern(String string) {
103 String newPattern = string;
104 if(newPattern.startsWith("<")){
105 this.site = StringUtils.substringAfter(StringUtils.substringBefore(newPattern, ">"), "<");
106 newPattern = StringUtils.substringAfter(newPattern, ">");
107 }
108 this.length = StringUtils.removeEnd(newPattern, "*").length();
109 this.pattern = Pattern.compile(getEncodedString(newPattern), Pattern.DOTALL);
110 this.patternString = newPattern;
111 }
112
113
114
115
116
117
118
119 public static String getEncodedString(String str) {
120 StringBuffer stringBuffer = new StringBuffer();
121 char[] chars = str.toCharArray();
122 int i = 0, last = 0;
123 while (i < chars.length) {
124 char c = chars[i];
125 if (c == '*') {
126 stringBuffer.append('(');
127 stringBuffer.append(chars, last, i - last);
128 stringBuffer.append(')');
129 stringBuffer.append(MULTIPLE_CHAR_PATTERN);
130 last = i + 1;
131 }
132 else if (c == '?') {
133 stringBuffer.append('(');
134 stringBuffer.append(chars, last, i - last);
135 stringBuffer.append(')');
136 stringBuffer.append(SINGLE_CHAR_PATTERN);
137 last = i + 1;
138 }
139 i++;
140 }
141 stringBuffer.append(chars, last, i - last);
142 return stringBuffer.toString();
143 }
144
145 public boolean shouldUseFullURI(String pattern, String fullURI){
146
147 if(StringUtils.isBlank(fullURI)){
148 return false;
149 }
150 String patternWithoutLastSlash = StringUtils.removeEnd(pattern, "/");
151
152 if(!fullURI.contains(patternWithoutLastSlash)){
153 return false;
154 }
155
156 if(StringUtils.isNotBlank(StringUtils.substringAfter(fullURI, patternWithoutLastSlash)) && StringUtils.isNotBlank(patternWithoutLastSlash)){
157 return false;
158 }
159
160 if(StringUtils.countMatches(StringUtils.substringBefore(fullURI, patternWithoutLastSlash), "/") > 1){
161 return false;
162 }
163 return true;
164 }
165
166
167
168
169 public boolean match(String str) {
170 String domainName = getDomainName();
171 String siteName = getSiteNameBasedOnDomain(domainName);
172 if (StringUtils.isNotBlank(siteName)) {
173
174 String fullURI = getFullCurrentURI();
175 if(shouldUseFullURI(str, fullURI) && StringUtils.isNotBlank(this.site)){
176 str = fullURI;
177 }
178 }
179 return this.pattern.matcher(str).matches() && matchSiteName(siteName);
180 }
181
182 public boolean matchSiteName(String siteName){
183
184 if(this.site == null && siteName == null){
185 return true;
186 }
187
188
189 if(this.site != null && siteName == null){
190 return false;
191 }
192
193
194 if (this.site == null && siteName != null) {
195 return true;
196 }
197
198
199 return site.equals(siteName);
200 }
201
202 private String getSiteNameBasedOnDomain(String domain) {
203 if(MgnlContext.getInstance() instanceof WebContext){
204 AggregationState aggregationState = MgnlContext.getAggregationState();
205 if (aggregationState == null) {
206 return null;
207 }
208
209 Method getSite;
210 try {
211 getSite = aggregationState.getClass().getMethod("getSiteBasedOnDomain", String.class);
212 Object siteName = getSite.invoke(aggregationState, domain);
213 if(null != siteName){
214 return siteName.toString();
215 }
216 return null;
217 } catch (NoSuchMethodException e) {
218 log.debug("Not proper ExtendedAggregationState provided", e);
219 } catch (IllegalArgumentException e) {
220 log.debug("Not proper ExtendedAggregationState provided", e);
221 } catch (IllegalAccessException e) {
222 log.debug("Not proper ExtendedAggregationState provided", e);
223 } catch (InvocationTargetException e) {
224 log.debug("Not proper ExtendedAggregationState provided", e);
225 }
226 return null;
227 }
228 return null;
229 }
230
231 private String getFullCurrentURI() {
232 if(MgnlContext.getInstance() instanceof WebContext){
233 AggregationState aggregationState = MgnlContext.getAggregationState();
234 if (aggregationState == null) {
235 return null;
236 }
237
238 Method getFullCurrentURI;
239 try {
240 getFullCurrentURI = aggregationState.getClass().getMethod("getFullCurrentURI");
241 Object fullURI = getFullCurrentURI.invoke(aggregationState);
242 if(null != fullURI){
243 return fullURI.toString();
244 }
245 return null;
246 } catch (NoSuchMethodException e) {
247 log.debug("Not proper ExtendedAggregationState provided", e);
248 } catch (IllegalArgumentException e) {
249 log.debug("Not proper ExtendedAggregationState provided", e);
250 } catch (IllegalAccessException e) {
251 log.debug("Not proper ExtendedAggregationState provided", e);
252 } catch (InvocationTargetException e) {
253 log.debug("Not proper ExtendedAggregationState provided", e);
254 }
255 return null;
256 }
257 return null;
258 }
259
260 private String getDomainName() {
261 if(MgnlContext.getInstance() instanceof WebContext){
262 AggregationState aggregationState = MgnlContext.getAggregationState();
263 if (aggregationState == null) {
264 return null;
265 }
266
267 Method getDomainName;
268 try {
269 getDomainName = aggregationState.getClass().getMethod("getDomainName");
270 Object domainName = getDomainName.invoke(aggregationState);
271 if(null != domainName){
272 return domainName.toString();
273 }
274 return null;
275 } catch (NoSuchMethodException e) {
276 log.debug("Not proper ExtendedAggregationState provided", e);
277 } catch (IllegalArgumentException e) {
278 log.debug("Not proper ExtendedAggregationState provided", e);
279 } catch (IllegalAccessException e) {
280 log.debug("Not proper ExtendedAggregationState provided", e);
281 } catch (InvocationTargetException e) {
282 log.debug("Not proper ExtendedAggregationState provided", e);
283 }
284 return null;
285 }
286 return null;
287 }
288
289
290
291
292 public int getLength() {
293 return this.length;
294 }
295
296 public String toString() {
297
298
299 return "SimpleUrlPattern{" + patternString + '}';
300 }
301 }