001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v2.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014
015package ch.qos.logback.access.common.boolex;
016
017import ch.qos.logback.access.common.spi.IAccessEvent;
018import ch.qos.logback.core.boolex.EvaluationException;
019import ch.qos.logback.core.boolex.EventEvaluatorBase;
020
021import java.util.regex.Pattern;
022import java.util.regex.PatternSyntaxException;
023
024/**
025 * A very simple {@link ch.qos.logback.core.boolex.EventEvaluator EventEvaluator}
026 * which checks whether the status of {@link IAccessEvent access event} is equal
027 * to a status code given as a parameter.
028 *
029 * @since 2.0.6
030 */
031public class RequestURIEventEvaluator extends EventEvaluatorBase<IAccessEvent> {
032
033    String regex;
034    private Pattern pattern;
035
036    @Override
037    public void start() {
038        if (regex == null) {
039            addError("regex is required");
040            return;
041        }
042
043        try {
044            addInfo("Compiling pattern [" + regex + "]");
045            this.pattern = Pattern.compile(regex);
046        } catch (PatternSyntaxException e) {
047            addError("Invalid regular expression: " + regex);
048            return;
049        }
050        super.start();
051    }
052
053    @Override
054    public boolean evaluate(IAccessEvent iAccessEvent) throws NullPointerException, EvaluationException {
055        if (!isStarted()) {
056            throw new IllegalStateException("Evaluator [" + this + "] was called in stopped state");
057        }
058        String requestURI = iAccessEvent.getRequestURI();
059        if(requestURI == null) {
060            return false;
061        }
062
063        java.util.regex.Matcher matcher = pattern.matcher(requestURI);
064        return matcher.find();
065    }
066
067    public String getRegex() {
068        return regex;
069    }
070
071    public void setRegex(String regex) {
072        this.regex = regex;
073    }
074}