FileDocCategorySizeDatePackage
RequestFilterValve.javaAPI DocApache Tomcat 6.0.149206Fri Jul 20 04:20:32 BST 2007org.apache.catalina.valves

RequestFilterValve

public abstract class RequestFilterValve extends ValveBase
Implementation of a Valve that performs filtering based on comparing the appropriate request property (selected based on which subclass you choose to configure into your Container's pipeline) against a set of regular expressions configured for this Valve.

This valve is configured by setting the allow and/or deny properties to a comma-delimited list of regular expressions (in the syntax supported by the jakarta-regexp library) to which the appropriate request property will be compared. Evaluation proceeds as follows:

  • The subclass extracts the request property to be filtered, and calls the common process() method.
  • If there are any deny expressions configured, the property will be compared to each such expression. If a match is found, this request will be rejected with a "Forbidden" HTTP response.
  • If there are any allow expressions configured, the property will be compared to each such expression. If a match is found, this request will be allowed to pass through to the next Valve in the current pipeline.
  • If one or more deny expressions was specified but no allow expressions, allow this request to pass through (because none of the deny expressions matched it).
  • The request will be rejected with a "Forbidden" HTTP response.

This Valve may be attached to any Container, depending on the granularity of the filtering you wish to perform.

author
Craig R. McClanahan
version
$Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $

Fields Summary
private static final String
info
The descriptive information related to this implementation.
protected static org.apache.catalina.util.StringManager
sm
The StringManager for this package.
protected String
allow
The comma-delimited set of allow expressions.
protected Pattern[]
allows
The set of allow regular expressions we will evaluate.
protected Pattern[]
denies
The set of deny regular expressions we will evaluate.
protected String
deny
The comma-delimited set of deny expressions.
Constructors Summary
Methods Summary
public java.lang.StringgetAllow()
Return a comma-delimited set of the allow expressions configured for this Valve, if any; otherwise, return null.



    // ------------------------------------------------------------- Properties


                          
       

        return (this.allow);

    
public java.lang.StringgetDeny()
Return a comma-delimited set of the deny expressions configured for this Valve, if any; otherwise, return null.


        return (this.deny);

    
public java.lang.StringgetInfo()
Return descriptive information about this Valve implementation.


        return (info);

    
public abstract voidinvoke(org.apache.catalina.connector.Request request, org.apache.catalina.connector.Response response)
Extract the desired request property, and pass it (along with the specified request and response objects) to the protected process() method to perform the actual filtering. This method must be implemented by a concrete subclass.

param
request The servlet request to be processed
param
response The servlet response to be created
exception
IOException if an input/output error occurs
exception
ServletException if a servlet error occurs

protected java.util.regex.Pattern[]precalculate(java.lang.String list)
Return an array of regular expression objects initialized from the specified argument, which must be null or a comma-delimited list of regular expression patterns.

param
list The comma-separated list of patterns
exception
IllegalArgumentException if one of the patterns has invalid syntax


        if (list == null)
            return (new Pattern[0]);
        list = list.trim();
        if (list.length() < 1)
            return (new Pattern[0]);
        list += ",";

        ArrayList reList = new ArrayList();
        while (list.length() > 0) {
            int comma = list.indexOf(',");
            if (comma < 0)
                break;
            String pattern = list.substring(0, comma).trim();
            try {
                reList.add(Pattern.compile(pattern));
            } catch (PatternSyntaxException e) {
                IllegalArgumentException iae = new IllegalArgumentException
                    (sm.getString("requestFilterValve.syntax", pattern));
                iae.initCause(e);
                throw iae;
            }
            list = list.substring(comma + 1);
        }

        Pattern reArray[] = new Pattern[reList.size()];
        return ((Pattern[]) reList.toArray(reArray));

    
protected voidprocess(java.lang.String property, org.apache.catalina.connector.Request request, org.apache.catalina.connector.Response response)
Perform the filtering that has been configured for this Valve, matching against the specified request property.

param
property The request property on which to filter
param
request The servlet request to be processed
param
response The servlet response to be processed
exception
IOException if an input/output error occurs
exception
ServletException if a servlet error occurs


        // Check the deny patterns, if any
        for (int i = 0; i < denies.length; i++) {
            if (denies[i].matcher(property).matches()) {
                response.sendError(HttpServletResponse.SC_FORBIDDEN);
                return;
            }
        }

        // Check the allow patterns, if any
        for (int i = 0; i < allows.length; i++) {
            if (allows[i].matcher(property).matches()) {
                getNext().invoke(request, response);
                return;
            }
        }

        // Allow if denies specified but not allows
        if ((denies.length > 0) && (allows.length == 0)) {
            getNext().invoke(request, response);
            return;
        }

        // Deny this request
        response.sendError(HttpServletResponse.SC_FORBIDDEN);

    
public voidsetAllow(java.lang.String allow)
Set the comma-delimited set of the allow expressions configured for this Valve, if any.

param
allow The new set of allow expressions


        this.allow = allow;
        allows = precalculate(allow);

    
public voidsetDeny(java.lang.String deny)
Set the comma-delimited set of the deny expressions configured for this Valve, if any.

param
deny The new set of deny expressions


        this.deny = deny;
        denies = precalculate(deny);