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

ApplicationFilterChain

public final class ApplicationFilterChain extends Object implements FilterChain
Implementation of javax.servlet.FilterChain used to manage the execution of a set of filters for a particular request. When the set of defined filters has all been executed, the next call to doFilter() will execute the servlet's service() method itself.
author
Craig R. McClanahan
version
$Revision: 1.7 $ $Date: 2007/05/05 05:31:53 $

Fields Summary
public static final int
INCREMENT
private ApplicationFilterConfig[]
filters
Filters.
private int
pos
The int which is used to maintain the current position in the filter chain.
private int
n
The int which gives the current number of filters in the chain.
private Servlet
servlet
The servlet instance to be executed by this chain.
private static final org.apache.catalina.util.StringManager
sm
The string manager for our package.
private org.apache.catalina.util.InstanceSupport
support
The InstanceSupport instance associated with our Wrapper (used to send "before filter" and "after filter" events.
private static Class[]
classType
Static class array used when the SecurityManager is turned on and doFilter
private static Class[]
classTypeUsedInService
Static class array used when the SecurityManager is turned on and service
Constructors Summary
public ApplicationFilterChain()
Construct a new chain instance with no defined filters.



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


                  
      

        super();

    
Methods Summary
voidaddFilter(ApplicationFilterConfig filterConfig)
Add a filter to the set of filters that will be executed in this chain.

param
filterConfig The FilterConfig for the servlet to be executed


        if (n == filters.length) {
            ApplicationFilterConfig[] newFilters =
                new ApplicationFilterConfig[n + INCREMENT];
            System.arraycopy(filters, 0, newFilters, 0, n);
            filters = newFilters;
        }
        filters[n++] = filterConfig;

    
public voiddoFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response)
Invoke the next filter in this chain, passing the specified request and response. If there are no more filters in this chain, invoke the service() method of the servlet itself.

param
request The servlet request we are processing
param
response The servlet response we are creating
exception
IOException if an input/output error occurs
exception
ServletException if a servlet exception occurs


    // ---------------------------------------------------- FilterChain Methods


                                                                      
         
           

        if (Globals.IS_SECURITY_ENABLED) {
            final ServletRequest req = request;
            final ServletResponse res = response;
            try {
                java.security.AccessController.doPrivileged(
                    new java.security.PrivilegedExceptionAction() {
                        public Object run() 
                            throws ServletException, IOException {
                            internalDoFilter(req,res);
                            return null;
                        }
                    }
                );
            } catch( PrivilegedActionException pe) {
                Exception e = pe.getException();
                if (e instanceof ServletException)
                    throw (ServletException) e;
                else if (e instanceof IOException)
                    throw (IOException) e;
                else if (e instanceof RuntimeException)
                    throw (RuntimeException) e;
                else
                    throw new ServletException(e.getMessage(), e);
            }
        } else {
            internalDoFilter(request,response);
        }
    
private voidinternalDoFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response)


        // Call the next filter if there is one
        if (pos < n) {
            ApplicationFilterConfig filterConfig = filters[pos++];
            Filter filter = null;
            try {
                filter = filterConfig.getFilter();
                support.fireInstanceEvent(InstanceEvent.BEFORE_FILTER_EVENT,
                                          filter, request, response);
                
                if( SecurityUtil.isPackageProtectionEnabled() ) {
                    final ServletRequest req = request;
                    final ServletResponse res = response;
                    Principal principal = 
                        ((HttpServletRequest) req).getUserPrincipal();
                    Object[] filterType = new Object[3];

                    filterType[0] = req;
                    filterType[1] = res;
                    filterType[2] = this;
                    SecurityUtil.doAsPrivilege
                        ("doFilter", filter, classType, filterType);

                    filterType = null;
                } else {  
                    filter.doFilter(request, response, this);
                }

                support.fireInstanceEvent(InstanceEvent.AFTER_FILTER_EVENT,
                                          filter, request, response);
            } catch (IOException e) {
                if (filter != null)
                    support.fireInstanceEvent(InstanceEvent.AFTER_FILTER_EVENT,
                                              filter, request, response, e);
                throw e;
            } catch (ServletException e) {
                if (filter != null)
                    support.fireInstanceEvent(InstanceEvent.AFTER_FILTER_EVENT,
                                              filter, request, response, e);
                throw e;
            } catch (RuntimeException e) {
                if (filter != null)
                    support.fireInstanceEvent(InstanceEvent.AFTER_FILTER_EVENT,
                                              filter, request, response, e);
                throw e;
            } catch (Throwable e) {
                if (filter != null)
                    support.fireInstanceEvent(InstanceEvent.AFTER_FILTER_EVENT,
                                              filter, request, response, e);
                throw new ServletException
                  (sm.getString("filterChain.filter"), e);
            }
            return;
        }

        // We fell off the end of the chain -- call the servlet instance
        /* IASRI 4665318
        try {
            support.fireInstanceEvent(InstanceEvent.BEFORE_SERVICE_EVENT,
                                      servlet, request, response);
            if ((request instanceof HttpServletRequest) &&
                (response instanceof HttpServletResponse)) {
                    
                // START SJS WS 7.0 6236329
                //if( System.getSecurityManager() != null) {
                if ( SecurityUtil.executeUnderSubjectDoAs() ){
                // END OF SJS WS 7.0 6236329
                    final ServletRequest req = request;
                    final ServletResponse res = response;
                    Principal principal = 
                        ((HttpServletRequest) req).getUserPrincipal();

                    Object[] serviceType = new Object[2];
                    serviceType[0] = req;
                    serviceType[1] = res;
                    
                    SecurityUtil.doAsPrivilege("service",
                                               servlet,
                                               classTypeUsedInService, 
                                               serviceType,
                                               principal);                                                   
                    serviceType = null;
                } else {  
                    servlet.service((HttpServletRequest) request,
                                    (HttpServletResponse) response);
                }
            } else {
                servlet.service(request, response);
            }
            support.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
                                      servlet, request, response);
        } catch (IOException e) {
            support.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
                                      servlet, request, response, e);
            throw e;
        } catch (ServletException e) {
            support.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
                                      servlet, request, response, e);
            throw e;
        } catch (RuntimeException e) {
            support.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
                                      servlet, request, response, e);
            throw e;
        } catch (Throwable e) {
            support.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
                                      servlet, request, response, e);
            throw new ServletException
              (sm.getString("filterChain.servlet"), e);
        }

        */
        // START IASRI 4665318
        servletService(request, response, servlet, support);
        // END IASRI 4665318
    
voidrelease()
Release references to the filters and wrapper executed by this chain.


        n = 0;
        pos = 0;
        servlet = null;
        support = null;

    
static voidservletService(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.Servlet serv, org.apache.catalina.util.InstanceSupport supp)

        try {
            supp.fireInstanceEvent(InstanceEvent.BEFORE_SERVICE_EVENT,
                                   serv, request, response);
            if ((request instanceof HttpServletRequest) &&
                (response instanceof HttpServletResponse)) {
                    
                if ( SecurityUtil.executeUnderSubjectDoAs() ){
                    final ServletRequest req = request;
                    final ServletResponse res = response;
                    Principal principal = 
                        ((HttpServletRequest) req).getUserPrincipal();

                    Object[] serviceType = new Object[2];
                    serviceType[0] = req;
                    serviceType[1] = res;
                    
                    SecurityUtil.doAsPrivilege("service",
                                               serv,
                                               classTypeUsedInService, 
                                               serviceType,
                                               principal);                                                   
                    serviceType = null;
                } else {  
                    serv.service((HttpServletRequest) request,
                                 (HttpServletResponse) response);
                }
            } else {
                serv.service(request, response);
            }
            supp.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
                                   serv, request, response);
        } catch (IOException e) {
            supp.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
                                   serv, request, response, e);
            throw e;
        } catch (ServletException e) {
            supp.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
                                   serv, request, response, e);
            throw e;
        } catch (RuntimeException e) {
            supp.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
                                   serv, request, response, e);
            throw e;
        } catch (Throwable e) {
            supp.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
                                   serv, request, response, e);
            throw new ServletException
              (sm.getString("filterChain.servlet"), e);
        }

    
voidsetServlet(javax.servlet.Servlet servlet)
Set the servlet that will be executed at the end of this chain.

param
wrapper The Wrapper for the servlet to be executed


        this.servlet = servlet;

    
voidsetSupport(org.apache.catalina.util.InstanceSupport support)
Set the InstanceSupport object used for event notifications for this filter chain.

param
support The InstanceSupport object for our Wrapper


        this.support = support;