View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
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   * A very simple {@link ch.qos.logback.core.boolex.EventEvaluator EventEvaluator}
26   * which checks whether the status of {@link IAccessEvent access event} is equal
27   * to a status code given as a parameter.
28   *
29   * @since 2.0.6
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  }