Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.github.edgarespina.mwa;
  
  import static com.google.common.base.Preconditions.checkNotNull;
  import static java.util.Collections.enumeration;
  
 import java.util.List;
 import java.util.Set;
 
 
A forwarding filter that propagate to filter call to mappings registered in the org.springframework.web.context.WebApplicationContext.

Author(s):
edgar.espina
Since:
0.1.3
 
 class ForwardingFilter extends GenericFilterBean {

  
The logging system.
 
   private static final Logger logger = LoggerFactory
       .getLogger(ForwardingFilter.class);

  
The application context.
 
The list of FilterMapping.
 
   private List<FilterMappingmappings;

  
A forwarding filter.

Parameters:
webApplicationContext The application context.
 
   public ForwardingFilter(final WebApplicationContext webApplicationContext) {
     this. =
         checkNotNull(webApplicationContext,
             "The web application context is required.");
   }

  
 
   @Override
   public void doFilter(final ServletRequest request,
       final ServletResponse responsefinal FilterChain chain)
       throws IOExceptionServletException {
     wrapChain(chain)
         .doFilter(requestresponse);
   }

  
 
   @Override
   protected void initFilterBean() throws ServletException {
     this. = lookFor(FilterMapping.class);
     Set<Filterinitialized = new HashSet<Filter>();
     for (FilterMapping mapping : ) {
       Filter filter = mapping.getFilter();
       if (initialized.add(filter)) {
         String filterName = filter.getClass().getSimpleName();
         .trace("Initializing filter: {}"filterName);
         filter.init(wrapFilterConfig(getServletContext(), filterName));
       }
     }
   }

  
 
   @Override
   public void destroy() {
    if ( != null) {
      Set<Filterdestroyed = new HashSet<Filter>();
      for (FilterMapping mapping : ) {
        Filter filter = mapping.getFilter();
        if (destroyed.add(filter)) {
          String filterName = filter.getClass().getSimpleName();
          .trace("Destroying filter: {}"filterName);
          mapping.getFilter().destroy();
        }
      }
      .clear();
       = null;
    }
  }

  
Creates a javax.servlet.FilterChain and call a filter if a path matches if found.

Parameters:
mappings The list of filter mappings.
chain The filter chain.
Returns:
A filter chaing to apply all the registered filters.
  private static FilterChain wrapChain(final List<FilterMappingmappings,
      final FilterChain chain) {
    return new FilterChain() {

      
The current filter mapping.
      private final Iterator<FilterMappingit = mappings.iterator();

      
Execute the filter.

Parameters:
request The http servlet request.
response The http servlet response.
Throws:
java.io.IOException If something goes wrong.
javax.servlet.ServletException If something goes wrong.
      @Override
      public void doFilter(final ServletRequest request,
          final ServletResponse responsethrows IOException,
          ServletException {
        doFilter((HttpServletRequestrequest, (HttpServletResponseresponse);
      }

      
Execute the filter.

Parameters:
request The http servlet request.
response The http servlet response.
Throws:
java.io.IOException If something goes wrong.
javax.servlet.ServletException If something goes wrong.
      private void doFilter(final HttpServletRequest request,
          final HttpServletResponse responsethrows IOException,
          ServletException {
        if (.hasNext()) {
          FilterMapping current = .next();
          if (current.matches(request)) {
            .trace("Calling filter: {} for {}"current,
                request.getRequestURI());
            current.getFilter().doFilter(requestresponsethis);
          } else {
            .trace("Ignoring filter: {} for {}"current,
                request.getRequestURI());
            doFilter(requestresponse);
          }
        } else {
          chain.doFilter(requestresponse);
        }
      }
    };
  }

  
Creates a new filter config.

Parameters:
servletContext The servlet context.
filterName The filter's name.
Returns:
A new filter config.
  private static FilterConfig wrapFilterConfig(
      final ServletContext servletContextfinal String filterName) {
    return new FilterConfig() {
      @Override
      public ServletContext getServletContext() {
        return servletContext;
      }
      @Override
      public Enumeration<StringgetInitParameterNames() {
        List<StringnoParams = Collections.emptyList();
        return enumeration(noParams);
      }
      @Override
      public String getInitParameter(final String name) {
        return null;
      }
      @Override
      public String getFilterName() {
        return filterName;
      }
    };
  }

  
Look for bean of an specific type in the Application Context.

Parameters:
beanType The bean type to look for.
<T> The bean generic type.
Returns:
All the of the specific types found in the Application Context.
  private <T> List<T> lookFor(final Class<T> beanType) {
    List<T> result = new ArrayList<T>();
    Collection<T> customExceptionResolvers =
        .getBeansOfType(beanType).values();
    if (customExceptionResolvers != null) {
      result.addAll(customExceptionResolvers);
    }
    return result;
  }
New to GrepCode? Check out our FAQ X