FileDocCategorySizeDatePackage
RequestFilterValve.javaAPI DocGlassfish v2 API12034Fri May 04 22:32:42 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: 1.6 $ $Date: 2007/05/05 05:32:41 $

Fields Summary
private static final String
info
The descriptive information related to this implementation.
protected static final 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 intinvoke(org.apache.catalina.Request request, org.apache.catalina.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
param
context The valve context used to invoke the next valve in the current processing pipeline
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 intprocess(java.lang.String property, org.apache.catalina.Request request, org.apache.catalina.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
param
context The valve context used to invoke the next valve in the current processing pipeline
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()) {
                ServletResponse sres = response.getResponse();
                /* GlassFish 6386229 
                if (sres instanceof HttpServletResponse) {
                    HttpServletResponse hres = (HttpServletResponse) sres;
                    hres.sendError(HttpServletResponse.SC_FORBIDDEN);
                    // START OF IASRI 4665318
                    // return;
                    return END_PIPELINE;
                    // END OF IASRI 4665318
                }
                */
                // START GlassFish 6386229 
                HttpServletResponse hres = (HttpServletResponse) sres;
                hres.sendError(HttpServletResponse.SC_FORBIDDEN);
                // END GlassFish 6386229                 
                // START OF IASRI 4665318
                // return;
                return END_PIPELINE;
                // END OF IASRI 4665318
                // GlassFish 638622                   
            }
        }

        // Check the allow patterns, if any
        for (int i = 0; i < allows.length; i++) {
            if (allows[i].matcher(property).matches()) {
                // START OF IASRI 4665318
                //context.invokeNext(renuest
                // return;
                return INVOKE_NEXT;
                // END OF IASRI 4665318
            }
        }

        // Allow if denies specified but not allows
        if ((denies.length > 0) && (allows.length == 0)) {
            // START OF IASRI 4665318
            //context.invokeNext(renuest
            // return;
            return INVOKE_NEXT;
            // END OF IASRI 4665318
        }

        // Deny this request
        ServletResponse sres = response.getResponse();
        /* GlassFish 6386229 
        if (sres instanceof HttpServletResponse) {
            HttpServletResponse hres = (HttpServletResponse) sres;
            hres.sendError(HttpServletResponse.SC_FORBIDDEN);
            // START OF IASRI 4665318
            // return;
            return END_PIPELINE;
            // END OF IASRI 4665318
        }
        */
        // START GlassFish 6386229 
        HttpServletResponse hres = (HttpServletResponse) sres;
        hres.sendError(HttpServletResponse.SC_FORBIDDEN);
        // END GlassFish 6386229        
        // START OF IASRI 4665318
        // return;
        return END_PIPELINE;
        // END OF IASRI 4665318

    
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);