Merge "Bug 1367 - Fix NPE in cors filter"
[controller.git] / opendaylight / commons / filter-valve / src / main / java / org / opendaylight / controller / filtervalve / cors / model / FilterProcessor.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.filtervalve.cors.model;
10
11 import com.google.common.base.Optional;
12 import java.io.IOException;
13 import java.util.List;
14 import java.util.ListIterator;
15 import javax.servlet.FilterChain;
16 import javax.servlet.ServletException;
17 import javax.servlet.ServletRequest;
18 import javax.servlet.ServletResponse;
19 import org.apache.catalina.connector.Request;
20 import org.apache.catalina.connector.Response;
21 import org.opendaylight.controller.filtervalve.cors.jaxb.Context;
22 import org.opendaylight.controller.filtervalve.cors.jaxb.Filter;
23 import org.opendaylight.controller.filtervalve.cors.jaxb.Host;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class FilterProcessor {
28     private static final Logger logger = LoggerFactory.getLogger(FilterProcessor.class);
29
30     private final Host host;
31
32     public FilterProcessor(Host host) {
33         this.host = host;
34     }
35
36     public void process(Request request, Response response, FilterChain nextValveFilterChain)
37             throws IOException, ServletException {
38
39         String contextPath = request.getContext().getPath();
40         String path = request.getDecodedRequestURI();
41
42         Optional<Context> maybeContext = host.findContext(contextPath);
43         logger.trace("Processing context {} path {}, found {}", contextPath, path, maybeContext);
44         if (maybeContext.isPresent()) {
45             // process filters
46             Context context = maybeContext.get();
47             List<Filter> matchingFilters = context.findMatchingFilters(path);
48             FilterChain fromLast = nextValveFilterChain;
49             ListIterator<Filter> it = matchingFilters.listIterator(matchingFilters.size());
50             final boolean trace = logger.isTraceEnabled();
51             while (it.hasPrevious()) {
52                 final Filter currentFilter = it.previous();
53                 final FilterChain copy = fromLast;
54                 fromLast = new FilterChain() {
55                     @Override
56                     public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
57                         if (trace) {
58                             logger.trace("Applying {}", currentFilter);
59                         }
60                         javax.servlet.Filter actualFilter = currentFilter.getActualFilter();
61                         actualFilter.doFilter(request, response, copy);
62                     }
63                 };
64             }
65             // call first filter
66             fromLast.doFilter(request, response);
67         } else {
68             // move to next valve
69             nextValveFilterChain.doFilter(request, response);
70         }
71     }
72 }