1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package ch.qos.logback.access.common.boolex;
16
17 import ch.qos.logback.access.common.spi.IAccessEvent;
18 import ch.qos.logback.core.boolex.EvaluationException;
19 import ch.qos.logback.core.boolex.EventEvaluatorBase;
20
21 import java.util.regex.Pattern;
22 import java.util.regex.PatternSyntaxException;
23
24
25
26
27
28
29
30
31 public class RequestURIEventEvaluator extends EventEvaluatorBase<IAccessEvent> {
32
33 String regex;
34 private Pattern pattern;
35
36 @Override
37 public void start() {
38 if (regex == null) {
39 addError("regex is required");
40 return;
41 }
42
43 try {
44 addInfo("Compiling pattern [" + regex + "]");
45 this.pattern = Pattern.compile(regex);
46 } catch (PatternSyntaxException e) {
47 addError("Invalid regular expression: " + regex);
48 return;
49 }
50 super.start();
51 }
52
53 @Override
54 public boolean evaluate(IAccessEvent iAccessEvent) throws NullPointerException, EvaluationException {
55 if (!isStarted()) {
56 throw new IllegalStateException("Evaluator [" + this + "] was called in stopped state");
57 }
58 String requestURI = iAccessEvent.getRequestURI();
59 if(requestURI == null) {
60 return false;
61 }
62
63 java.util.regex.Matcher matcher = pattern.matcher(requestURI);
64 return matcher.find();
65 }
66
67 public String getRegex() {
68 return regex;
69 }
70
71 public void setRegex(String regex) {
72 this.regex = regex;
73 }
74 }