2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.filtervalve.cors.model;
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 org.apache.catalina.connector.Request;
18 import org.apache.catalina.connector.Response;
19 import org.opendaylight.controller.filtervalve.cors.jaxb.Context;
20 import org.opendaylight.controller.filtervalve.cors.jaxb.Filter;
21 import org.opendaylight.controller.filtervalve.cors.jaxb.Host;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public class FilterProcessor {
26 private static final Logger logger = LoggerFactory.getLogger(FilterProcessor.class);
28 private final Host host;
30 public FilterProcessor(Host host) {
34 public void process(Request request, Response response, FilterChain nextValveFilterChain)
35 throws IOException, ServletException {
37 String contextPath = request.getContext().getPath();
38 String path = request.getDecodedRequestURI();
40 Optional<Context> maybeContext = host.findContext(contextPath);
41 logger.trace("Processing context {} path {}, found {}", contextPath, path, maybeContext);
42 if (maybeContext.isPresent()) {
44 Context context = maybeContext.get();
45 List<Filter> matchingFilters = context.findMatchingFilters(path);
46 FilterChain fromLast = nextValveFilterChain;
47 ListIterator<Filter> it = matchingFilters.listIterator(matchingFilters.size());
48 final boolean trace = logger.isTraceEnabled();
49 while (it.hasPrevious()) {
50 final Filter currentFilter = it.previous();
51 final FilterChain copy = fromLast;
52 fromLast = (request1, response1) -> {
54 logger.trace("Applying {}", currentFilter);
56 javax.servlet.Filter actualFilter = currentFilter.getActualFilter();
57 actualFilter.doFilter(request1, response1, copy);
61 fromLast.doFilter(request, response);
64 nextValveFilterChain.doFilter(request, response);