Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2012 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.catalina.core;
 
 
 import static org.jboss.web.CatalinaMessages.MESSAGES;
 
 
 
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(s):
Craig R. McClanahan
Version:
$Revision: 1728 $ $Date: 2011-05-27 18:12:11 +0200 (Fri, 27 May 2011) $
 
 
 public final class ApplicationFilterChain implements FilterChainHttpEventFilterChain {
 
     // Used to enforce requirements of SRV.8.2 / SRV.14.2.5.1
     private final static ThreadLocal lastServicedRequest;
     private final static ThreadLocal lastServicedResponse;
 
     static {
         if (.) {
              = new ThreadLocal();
              = new ThreadLocal();
         } else {
              = null;
              = null;
         }
     }
 
     
     // -------------------------------------------------------------- Constants
 
 
     public static final int INCREMENT = 10;
 
 
     // ----------------------------------------------------------- Constructors
 

    
Construct a new chain instance with no defined filters.
 
     public ApplicationFilterChain() {
 
         super();
 
     }
 
 
     // ----------------------------------------------------- Instance Variables
 

    
Associated request facade.
    private RequestFacade requestFacade = null;
    
    
    
Filters.
    private ApplicationFilterConfig[] filters = 
        new ApplicationFilterConfig[0];


    
The int which is used to track the component currently executed.
    private int pointer = 0;


   
The int which is used to maintain the current position in the filter chain.
    private int pos = 0;


    
The int which gives the current number of filters in the chain.
    private int filterCount = 0;


    
The wrapper to be executed by this chain.
    private Wrapper wrapper = null;


    
The servlet to be executed by this chain.
    private Servlet servlet = null;


    
Static class array used when the SecurityManager is turned on and doFilter is invoked.
    private static Class[] classType = new Class[]{ServletRequest.class
                                                   ServletResponse.class,
                                                   FilterChain.class};
                                                   
    
Static class array used when the SecurityManager is turned on and service is invoked.
                                                 
    private static Class[] classTypeUsedInService = new Class[]{
                                                         ServletRequest.class,
                                                         ServletResponse.class};

    
Static class array used when the SecurityManager is turned on and doFilterEvent is invoked.
    private static Class[] cometClassType = 
        new Class[]{ HttpEvent.classHttpEventFilterChain.class};
                                                   
    
Static class array used when the SecurityManager is turned on and event is invoked.
                                                 
    private static Class[] classTypeUsedInEvent = 
        new Class[] { HttpEvent.class };
    // ---------------------------------------------------- FilterChain Methods


    
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.

Parameters:
request The servlet request we are processing
response The servlet response we are creating
Throws:
java.io.IOException if an input/output error occurs
javax.servlet.ServletException if a servlet exception occurs
    public void doFilter(ServletRequest requestServletResponse response)
        throws IOExceptionServletException {
        if. ) {
            final ServletRequest req = request;
            final ServletResponse res = response;
            try {
                java.security.AccessController.doPrivileged(
                    new java.security.PrivilegedExceptionAction() {
                        public Object run() 
                            throws ServletExceptionIOException {
                            internalDoFilter(req,res);
                            return null;
                        }
                    }
                );
            } catchPrivilegedActionException pe) {
                Exception e = pe.getException();
                if (e instanceof ServletException)
                    throw (ServletExceptione;
                else if (e instanceof IOException)
                    throw (IOExceptione;
                else if (e instanceof RuntimeException)
                    throw (RuntimeExceptione;
                else
                    throw new ServletException(e.getMessage(), e);
            }
        } else {
            internalDoFilter(request,response);
        }
    }
    private void internalDoFilter(ServletRequest request
                                  ServletResponse response)
        throws IOExceptionServletException {
        InstanceSupport support = .getInstanceSupport();
        Throwable throwable = null;
        // Call the next filter if there is one
        if ( < ) {
            ApplicationFilterConfig filterConfig = [++];
            ++;
            Filter filter = null;
            try {
                filter = filterConfig.getFilter();
                support.fireInstanceEvent(.,
                                          filterrequestresponse);
                if. ) {
                    final ServletRequest req = request;
                    final ServletResponse res = response;
                    Principal principal = 
                        ((HttpServletRequestreq).getUserPrincipal();
                    Object[] args = new Object[]{reqresthis};
                    SecurityUtil.doAsPrivilege
                        ("doFilter"filterargsprincipal);
                    
                    args = null;
                } else {  
                    filter.doFilter(requestresponsethis);
                }
            } catch (IOException e) {
                throwable = e;
                throw e;
            } catch (ServletException e) {
                throwable = e;
                throw e;
            } catch (RuntimeException e) {
                throwable = e;
                throw e;
            } catch (Throwable e) {
                throwable = e;
                throw new ServletException(.filterException(), e);
            } finally {
                --;
                if (filter != null) {
                    support.fireInstanceEvent(.,
                                              filterrequestresponsethrowable);
                }
            }
            return;
        }
        // We fell off the end of the chain -- call the servlet instance
        ++;
        try {
                    requestresponse);
            if (.) {
                .set(request);
                .set(response);
            }
            if ((request instanceof HttpServletRequest) &&
                (response instanceof HttpServletResponse)) {
                    
                if. ) {
                    final ServletRequest req = request;
                    final ServletResponse res = response;
                    Principal principal = 
                        ((HttpServletRequestreq).getUserPrincipal();
                    Object[] args = new Object[]{reqres};
                    SecurityUtil.doAsPrivilege("service",
                                               ,
                                               
                                               args,
                                               principal);   
                    args = null;
                } else {  
                    .service((HttpServletRequestrequest,
                                    (HttpServletResponseresponse);
                }
            } else {
                .service(requestresponse);
            }
        } catch (IOException e) {
            throwable = e;
            throw e;
        } catch (ServletException e) {
            throwable = e;
            throw e;
        } catch (RuntimeException e) {
            throwable = e;
            throw e;
        } catch (Throwable e) {
            throwable = e;
            throw new ServletException(.servletException(), e);
        } finally {
            --;
            if (.) {
                .set(null);
                .set(null);
            }
            support.fireInstanceEvent(.,
                    requestresponsethrowable);
        }
    }


    
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.

Parameters:
request The servlet request we are processing
response The servlet response we are creating
Throws:
java.io.IOException if an input/output error occurs
javax.servlet.ServletException if a servlet exception occurs
    public void doFilterEvent(HttpEvent event)
        throws IOExceptionServletException {
        if. ) {
            final HttpEvent ev = event;
            try {
                java.security.AccessController.doPrivileged(
                    new java.security.PrivilegedExceptionAction() {
                        public Object run() 
                            throws ServletExceptionIOException {
                            internalDoFilterEvent(ev);
                            return null;
                        }
                    }
                );
            } catchPrivilegedActionException pe) {
                Exception e = pe.getException();
                if (e instanceof ServletException)
                    throw (ServletExceptione;
                else if (e instanceof IOException)
                    throw (IOExceptione;
                else if (e instanceof RuntimeException)
                    throw (RuntimeExceptione;
                else
                    throw new ServletException(e.getMessage(), e);
            }
        } else {
            internalDoFilterEvent(event);
        }
    }

    
    
The last request passed to a servlet for servicing from the current thread.

Returns:
The last request to be serviced.
    public static ServletRequest getLastServicedRequest() {
        return (ServletRequest.get();
    }

    
    
The last response passed to a servlet for servicing from the current thread.

Returns:
The last response to be serviced.
    public static ServletResponse getLastServicedResponse() {
        return (ServletResponse.get();
    }
    
    
    private void internalDoFilterEvent(HttpEvent event)
        throws IOExceptionServletException {
        InstanceSupport support = .getInstanceSupport();
        Throwable throwable = null;
        // Call the next filter if there is one
        if ( < ) {
            ApplicationFilterConfig filterConfig = [++];
            ++;
            HttpEventFilter filter = null;
            try {
                filter = (HttpEventFilterfilterConfig.getFilter();
                support.fireInstanceEvent(.,
                        filterevent);
                if. ) {
                    final HttpEvent ev = event;
                    Principal principal = 
                        ev.getHttpServletRequest().getUserPrincipal();
                    Object[] args = new Object[]{evthis};
                    SecurityUtil.doAsPrivilege
                        ("doFilterEvent", (Filterfilterargsprincipal);
                    args = null;
                } else {  
                    filter.doFilterEvent(eventthis);
                }
            } catch (IOException e) {
                throwable = e;
                throw e;
            } catch (ServletException e) {
                throwable = e;
                throw e;
            } catch (RuntimeException e) {
                throwable = e;
                throw e;
            } catch (Throwable e) {
                throwable = e;
                throw new ServletException(.filterException(), e);
            } finally {
                --;
                if (filter != null) {
                    support.fireInstanceEvent
                        (.filtereventthrowable);
                }
            }
            return;
        }
        // We fell off the end of the chain -- call the servlet instance
        ++;
        try {
                    event);
            if. ) {
                final HttpEvent ev = event;
                Principal principal = 
                    ev.getHttpServletRequest().getUserPrincipal();
                Object[] args = new Object[]{ ev };
                SecurityUtil.doAsPrivilege("event",
                        ,
                        
                        args,
                        principal);
                args = null;
            } else {  
                ((HttpEventServlet).event(event);
            }
        } catch (IOException e) {
            throwable = e;
            throw e;
        } catch (ServletException e) {
            throwable = e;
            throw e;
        } catch (RuntimeException e) {
            throwable = e;
            throw e;
        } catch (Throwable e) {
            throwable = e;
            throw new ServletException(.servletException(), e);
        } finally {
            --;
            support.fireInstanceEvent(.,
                    eventthrowable);
        }
    }
    // -------------------------------------------------------- Package Methods



    
Add a filter to the set of filters that will be executed in this chain.

Parameters:
filterConfig The FilterConfig for the servlet to be executed
    void addFilter(ApplicationFilterConfig filterConfig) {
        if ( == .) {
            ApplicationFilterConfig[] newFilters =
                new ApplicationFilterConfig[ + ];
            System.arraycopy(, 0, newFilters, 0, );
             = newFilters;
        }
        [++] = filterConfig;
    }


    
Release references to the filters and wrapper executed by this chain.
    void release() {
        for (int i = 0; i < i++) {
            [i] = null;
        }
         = 0;
         = 0;
         = 0;
         = null;
         = null;
    }


    
Prepare for reuse of the filters and wrapper executed by this chain.
    void reuse() {
         = 0;
         = 0;
    }


    
Set the wrapper that will be executed at the end of this chain.

Parameters:
wrapper The Wrapper to be executed
    void setWrapper(Wrapper wrapper) {
        this. = wrapper;
    }


    
Set the servlet that will be executed at the end of this chain.

Parameters:
servlet The Servlet to be executed
    void setServlet(Servlet servlet) {
        this. = servlet;
    }


   
Set the RequestFacade object used for removing the association of the chain from the request facade.

Parameters:
requestFacade The RequestFacade object
    void setRequestFacade(RequestFacade requestFacade) {
        this. = requestFacade;
    }
    
    public ApplicationFilterConfig[] getFilters() {
        return ;
    }
    public int getFilterCount() {
        return ;
    }
    public int getPointer() {
        return ;
    }
    public Wrapper getWrapper() {
        return ;
    }
    public Servlet getServlet() {
        return ;
    }
New to GrepCode? Check out our FAQ X