FileDocCategorySizeDatePackage
ApplicationFilterFactory.javaAPI DocGlassfish v2 API16615Fri May 04 22:31:54 BST 2007org.apache.catalina.core

ApplicationFilterFactory

public final class ApplicationFilterFactory extends Object
Factory for the creation and caching of Filters and creationg of Filter Chains.
author
Greg Murray
author
Remy Maucherat
version
$Revision: 1.0

Fields Summary
public static final int
ERROR
public static final Integer
ERROR_INTEGER
public static final int
FORWARD
public static final Integer
FORWARD_INTEGER
public static final int
INCLUDE
public static final Integer
INCLUDE_INTEGER
public static final int
REQUEST
public static final Integer
REQUEST_INTEGER
public static final String
DISPATCHER_TYPE_ATTR
public static final String
DISPATCHER_REQUEST_PATH_ATTR
private static ApplicationFilterFactory
factory
Constructors Summary
private ApplicationFilterFactory()



    // ----------------------------------------------------------- Constructors


    /*
     * Prevent instanciation outside of the getInstanceMethod().
     */
      
    
Methods Summary
public ApplicationFilterChaincreateFilterChain(javax.servlet.ServletRequest request, org.apache.catalina.Wrapper wrapper, javax.servlet.Servlet servlet)
Construct and return a FilterChain implementation that will wrap the execution of the specified servlet instance. If we should not execute a filter chain at all, return null.

param
request The servlet request we are processing
param
servlet The servlet instance to be wrapped

        
        HttpServletRequest hreq = null;
        /* GlassFish 6386229
        if (request instanceof HttpServletRequest) 
            hreq = (HttpServletRequest)request;
        */
        // START GlassFish 6386229
        hreq = (HttpServletRequest)request;
        // END GlassFish 6386229
        // If there is no servlet to execute, return null
        if (servlet == null)
            return (null);

        // Create and initialize a filter chain object
        ApplicationFilterChain filterChain = null;
        /** IASRI 4665318
        if ((securityManager == null) && (request instanceof Request)) {
            Request req = (Request) request;
            filterChain = (ApplicationFilterChain) req.getFilterChain();
            if (filterChain == null) {
                filterChain = new ApplicationFilterChain();
                req.setFilterChain(filterChain);
            }
        } else {
            // Security: Do not recycle
            filterChain = new ApplicationFilterChain();
        }

        filterChain.setServlet(servlet);

        filterChain.setSupport
            (((StandardWrapper)wrapper).getInstanceSupport());
        */

        // Acquire the filter mappings for this Context
        StandardContext context = (StandardContext) wrapper.getParent();
        FilterMap filterMaps[] = context.findFilterMaps();

        // If there are no filter mappings, we are done
        if ((filterMaps == null) || (filterMaps.length == 0))
            return (filterChain);

        // get the dispatcher type
        int dispatcher = -1; 
        if (request.getAttribute(DISPATCHER_TYPE_ATTR) != null) {
            Integer dispatcherInt = 
                (Integer) request.getAttribute(DISPATCHER_TYPE_ATTR);
            dispatcher = dispatcherInt.intValue();
        }
        String requestPath = null;
        Object attribute = request.getAttribute(DISPATCHER_REQUEST_PATH_ATTR);
        if (attribute != null){
            requestPath = attribute.toString();
        }

        // Acquire the information we will need to match filter mappings
        String servletName = wrapper.getName();

        int n = 0;

        // Add the relevant path-mapped filters to this filter chain
        for (int i = 0; i < filterMaps.length; i++) {
            if (!matchDispatcher(filterMaps[i] ,dispatcher)) {
                continue;
            }
            /* SJSWS 6324431
            if (!matchFiltersURL(filterMaps[i], requestPath))
                continue;
            */
            // START SJSWS 6324431
            if (!matchFiltersURL(filterMaps[i], requestPath, 
                                context.isCaseSensitiveMapping()))
                continue;
            // END SJSWS 6324431
            ApplicationFilterConfig filterConfig = (ApplicationFilterConfig)
                context.findFilterConfig(filterMaps[i].getFilterName());
            if (filterConfig == null) {
                ;       // FIXME - log configuration problem
                continue;
            }
            // START IASRI 4665318
            // Create a filter chain only when there are filters to add
            if (filterChain == null)
                filterChain = internalCreateFilterChain(request, wrapper, servlet);
            // END IASRI 4665318
            filterChain.addFilter(filterConfig);
            n++;
        }

        // Add filters that match on servlet name second
        for (int i = 0; i < filterMaps.length; i++) {
            if (!matchDispatcher(filterMaps[i] ,dispatcher)) {
                continue;
            }
            if (!matchFiltersServlet(filterMaps[i], servletName))
                continue;
            ApplicationFilterConfig filterConfig = (ApplicationFilterConfig)
                context.findFilterConfig(filterMaps[i].getFilterName());
            if (filterConfig == null) {
                ;       // FIXME - log configuration problem
                continue;
            }
            // START IASRI 4665318
            // Create a filter chain only when there are filters to add
            if (filterChain == null)
                filterChain = internalCreateFilterChain(request, wrapper, servlet);
            // END IASRI 4665318
            filterChain.addFilter(filterConfig);
            n++;
        }

        // Return the completed filter chain
        return (filterChain);

    
public static org.apache.catalina.core.ApplicationFilterFactorygetInstance()
Return the fqctory instance.

        return factory;
    
private ApplicationFilterChaininternalCreateFilterChain(javax.servlet.ServletRequest request, org.apache.catalina.Wrapper wrapper, javax.servlet.Servlet servlet)

        ApplicationFilterChain filterChain = null;
        if (!Globals.IS_SECURITY_ENABLED && (request instanceof Request)) {
            Request req = (Request) request;
            filterChain = (ApplicationFilterChain) req.getFilterChain();
            if (filterChain == null) {
                filterChain = new ApplicationFilterChain();
                req.setFilterChain(filterChain);
            }
        } else {
            // Security: Do not recycle
            filterChain = new ApplicationFilterChain();
        }

        filterChain.setServlet(servlet);

        filterChain.setSupport
            (((StandardWrapper)wrapper).getInstanceSupport());

        return filterChain;
    
private booleanmatchDispatcher(org.apache.catalina.deploy.FilterMap filterMap, int dispatcher)
Convienience method which returns true if the dispatcher type matches the dispatcher types specified in the FilterMap

        switch (dispatcher) {
            case FORWARD : {
                if (filterMap.getDispatcherMapping() == FilterMap.FORWARD ||
                    filterMap.getDispatcherMapping() == FilterMap.FORWARD_ERROR ||
                    filterMap.getDispatcherMapping() == FilterMap.INCLUDE_FORWARD ||
                    filterMap.getDispatcherMapping() == FilterMap.INCLUDE_ERROR_FORWARD ||
                    filterMap.getDispatcherMapping() == FilterMap.REQUEST_FORWARD ||
                    filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_FORWARD ||
                    filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_FORWARD_INCLUDE ||
                    filterMap.getDispatcherMapping() == FilterMap.REQUEST_FORWARD_INCLUDE) {
                        return true;
                }
                break;
            }
            case INCLUDE : {
                if (filterMap.getDispatcherMapping() == FilterMap.INCLUDE ||
                    filterMap.getDispatcherMapping() == FilterMap.INCLUDE_ERROR ||
                    filterMap.getDispatcherMapping() == FilterMap.INCLUDE_FORWARD ||
                    filterMap.getDispatcherMapping() == FilterMap.INCLUDE_ERROR_FORWARD ||
                    filterMap.getDispatcherMapping() == FilterMap.REQUEST_INCLUDE ||
                    filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_INCLUDE ||
                    filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_FORWARD_INCLUDE ||
                    filterMap.getDispatcherMapping() == FilterMap.REQUEST_FORWARD_INCLUDE) {
                        return true;
                }
                break;
            }
            case REQUEST : {
                if (filterMap.getDispatcherMapping() == FilterMap.REQUEST ||
                    filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR ||
                    filterMap.getDispatcherMapping() == FilterMap.REQUEST_INCLUDE ||
                    filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_INCLUDE ||
                    filterMap.getDispatcherMapping() == FilterMap.REQUEST_FORWARD ||
                    filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_FORWARD ||
                    filterMap.getDispatcherMapping() == FilterMap.REQUEST_FORWARD_INCLUDE ||
                    filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_FORWARD_INCLUDE) {
                        return true;
                }
                break;
            }
            case ERROR : {
                if (filterMap.getDispatcherMapping() == FilterMap.ERROR ||
                    filterMap.getDispatcherMapping() == FilterMap.FORWARD_ERROR || 
                    filterMap.getDispatcherMapping() == FilterMap.INCLUDE_ERROR || 
                    filterMap.getDispatcherMapping() == FilterMap.INCLUDE_ERROR_FORWARD || 
                    filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR ||
                    filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_FORWARD ||
                    filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_FORWARD_INCLUDE ||
                    filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_INCLUDE) {
                        return true;
                }
                break;
            }
        }
        return false;
    
private booleanmatchFiltersServlet(org.apache.catalina.deploy.FilterMap filterMap, java.lang.String servletName)
Return true if the specified servlet name matches the requirements of the specified filter mapping; otherwise return false.

param
filterMap Filter mapping being checked
param
servletName Servlet name being checked


        if (servletName == null) {
            return (false);
        } else {
            if (servletName.equals(filterMap.getServletName())
                    || "*".equals(filterMap.getServletName())) {
                return (true);
            } else {
                return false;
            }
        }

    
private booleanmatchFiltersURL(org.apache.catalina.deploy.FilterMap filterMap, java.lang.String requestPath, boolean caseSensitiveMapping)
Return true if the context-relative request path matches the requirements of the specified filter mapping; otherwise, return null.

param
filterMap Filter mapping being checked
param
requestPath Context-relative request path of this request

    // END SJSWS 6324431

        if (requestPath == null)
            return (false);

        // Match on context relative request path
        String testPath = filterMap.getURLPattern();
        if (testPath == null)
            return (false);

        // START SJSWS 6324431
        if (!caseSensitiveMapping) {
            requestPath = requestPath.toLowerCase();
            testPath = testPath.toLowerCase();
        }
        // END SJSWS 6324431

        // Case 1 - Exact Match
        if (testPath.equals(requestPath))
            return (true);

        // Case 2 - Path Match ("/.../*")
        if (testPath.equals("/*"))
            return (true);
        if (testPath.endsWith("/*")) {
            if (testPath.regionMatches(0, requestPath, 0, 
                                       testPath.length() - 2)) {
                if (requestPath.length() == (testPath.length() - 2)) {
                    return (true);
                } else if ('/" == requestPath.charAt(testPath.length() - 2)) {
                    return (true);
                }
            }
            return (false);
        }

        // Case 3 - Extension Match
        if (testPath.startsWith("*.")) {
            int slash = requestPath.lastIndexOf('/");
            int period = requestPath.lastIndexOf('.");
            if ((slash >= 0) && (period > slash) 
                && (period != requestPath.length() - 1)
                && ((requestPath.length() - period) 
                    == (testPath.length() - 1))) {
                return (testPath.regionMatches(2, requestPath, period + 1,
                                               testPath.length() - 2));
            }
        }

        // Case 4 - "Default" Match
        return (false); // NOTE - Not relevant for selecting filters