001package ch.qos.logback.access.common.boolex;
002
003import ch.qos.logback.access.common.spi.IAccessEvent;
004import ch.qos.logback.core.boolex.EvaluationException;
005import ch.qos.logback.core.boolex.EventEvaluatorBase;
006
007import java.util.regex.Pattern;
008import java.util.regex.PatternSyntaxException;
009
010/**
011 * A very simple {@link ch.qos.logback.core.boolex.EventEvaluator EventEvaluator}
012 * which checks whether the status of {@link IAccessEvent access event} is equal
013 * to a status code given as a parameter.
014 *
015 * @since 2.0.6
016 */
017public class RequestURIEventEvaluator extends EventEvaluatorBase<IAccessEvent> {
018
019    String regex;
020    private Pattern pattern;
021
022    @Override
023    public void start() {
024        if (regex == null) {
025            addError("regex is required");
026            return;
027        }
028
029        try {
030            addInfo("Compiling pattern [" + regex + "]");
031            this.pattern = Pattern.compile(regex);
032        } catch (PatternSyntaxException e) {
033            addError("Invalid regular expression: " + regex);
034            return;
035        }
036        super.start();
037    }
038
039    @Override
040    public boolean evaluate(IAccessEvent iAccessEvent) throws NullPointerException, EvaluationException {
041        if (!isStarted()) {
042            throw new IllegalStateException("Evaluator [" + this + "] was called in stopped state");
043        }
044        String requestURI = iAccessEvent.getRequestURI();
045        if(requestURI == null) {
046            return false;
047        }
048
049        java.util.regex.Matcher matcher = pattern.matcher(requestURI);
050        return matcher.find();
051    }
052
053    public String getRegex() {
054        return regex;
055    }
056
057    public void setRegex(String regex) {
058        this.regex = regex;
059    }
060}