Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2014 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 io.undertow.servlet.handlers;
 
 import java.util.List;
 import java.util.Map;
 
 

Author(s):
Stuart Douglas
 
 public class FilterHandler implements HttpHandler {
 
     private final Map<DispatcherTypeList<ManagedFilter>> filters;
     private final Map<DispatcherTypeBooleanasyncSupported;
     private final boolean allowNonStandardWrappers;
 
     private final HttpHandler next;
 
     public FilterHandler(final Map<DispatcherTypeList<ManagedFilter>> filtersfinal boolean allowNonStandardWrappersfinal HttpHandler next) {
         this. = allowNonStandardWrappers;
         this. = next;
         this. = new EnumMap<>(filters);
         Map<DispatcherTypeBooleanasyncSupported = new EnumMap<>(DispatcherType.class);
         for(Map.Entry<DispatcherTypeList<ManagedFilter>> entry : filters.entrySet()) {
             boolean supported = true;
             for(ManagedFilter i : entry.getValue()) {
                 if(!i.getFilterInfo().isAsyncSupported()) {
                     supported = false;
                     break;
                 }
             }
             asyncSupported.put(entry.getKey(), supported);
         }
         this. = asyncSupported;
     }
 
     @Override
     public void handleRequest(final HttpServerExchange exchangethrows Exception {
         final ServletRequestContext servletRequestContext = exchange.getAttachment(.);
         ServletRequest request = servletRequestContext.getServletRequest();
         ServletResponse response = servletRequestContext.getServletResponse();
         DispatcherType dispatcher = servletRequestContext.getDispatcherType();
         Boolean supported = .get(dispatcher);
         if(supported != null && ! supported) {
             exchange.putAttachment(.false    );
         }
 
         final List<ManagedFilterfilters = this..get(dispatcher);
         if(filters == null) {
             .handleRequest(exchange);
         } else {
             final FilterChainImpl filterChain = new FilterChainImpl(exchangefilters);
             filterChain.doFilter(requestresponse);
         }
     }
 
     private static class FilterChainImpl implements FilterChain {
 
         int location = 0;
         final HttpServerExchange exchange;
         final List<ManagedFilterfilters;
         final HttpHandler next;
         final boolean allowNonStandardWrappers;
 
         private FilterChainImpl(final HttpServerExchange exchangefinal List<ManagedFilterfiltersfinal HttpHandler nextfinal boolean allowNonStandardWrappers) {
             this. = exchange;
             this. = filters;
            this. = next;
            this. = allowNonStandardWrappers;
        }
        @Override
        public void doFilter(final ServletRequest requestfinal ServletResponse responsethrows IOExceptionServletException {
            final ServletRequestContext servletRequestContext = .getAttachment(.);
            final ServletRequest oldReq = servletRequestContext.getServletRequest();
            final ServletResponse oldResp = servletRequestContext.getServletResponse();
            try {
                if(!) {
                    if(oldReq != request) {
                        if(!(request instanceof ServletRequestWrapper)) {
                            throw ..requestWasNotOriginalOrWrapper(request);
                        }
                    }
                    if(oldResp != response) {
                        if(!(response instanceof ServletResponseWrapper)) {
                            throw ..responseWasNotOriginalOrWrapper(response);
                        }
                    }
                }
                servletRequestContext.setServletRequest(request);
                servletRequestContext.setServletResponse(response);
                int index = ++;
                if (index >= .size()) {
                    .handleRequest();
                } else {
                    .get(index).doFilter(requestresponsethis);
                }
            } catch (IOException e) {
                throw e;
            } catch (ServletException e) {
                throw e;
            } catch (RuntimeException e) {
                throw e;
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                --;
                servletRequestContext.setServletRequest(oldReq);
                servletRequestContext.setServletResponse(oldResp);
            }
        }
    }
New to GrepCode? Check out our FAQ X