1   package ch.qos.logback.access.common.boolex;
2   
3   import ch.qos.logback.access.common.spi.IAccessEvent;
4   import ch.qos.logback.core.boolex.EvaluationException;
5   import ch.qos.logback.core.boolex.EventEvaluatorBase;
6   
7   import java.util.regex.Pattern;
8   import java.util.regex.PatternSyntaxException;
9   
10  /**
11   * A very simple {@link ch.qos.logback.core.boolex.EventEvaluator EventEvaluator}
12   * which checks whether the status of {@link IAccessEvent access event} is equal
13   * to a status code given as a parameter.
14   *
15   * @since 2.0.6
16   */
17  public class RequestURIEventEvaluator extends EventEvaluatorBase<IAccessEvent> {
18  
19      String regex;
20      private Pattern pattern;
21  
22      @Override
23      public void start() {
24          if (regex == null) {
25              addError("regex is required");
26              return;
27          }
28  
29          try {
30              addInfo("Compiling pattern [" + regex + "]");
31              this.pattern = Pattern.compile(regex);
32          } catch (PatternSyntaxException e) {
33              addError("Invalid regular expression: " + regex);
34              return;
35          }
36          super.start();
37      }
38  
39      @Override
40      public boolean evaluate(IAccessEvent iAccessEvent) throws NullPointerException, EvaluationException {
41          if (!isStarted()) {
42              throw new IllegalStateException("Evaluator [" + this + "] was called in stopped state");
43          }
44          String requestURI = iAccessEvent.getRequestURI();
45          if(requestURI == null) {
46              return false;
47          }
48  
49          java.util.regex.Matcher matcher = pattern.matcher(requestURI);
50          return matcher.find();
51      }
52  
53      public String getRegex() {
54          return regex;
55      }
56  
57      public void setRegex(String regex) {
58          this.regex = regex;
59      }
60  }