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 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;
27 public class FilterProcessor {
28 private static final Logger logger = LoggerFactory.getLogger(FilterProcessor.class);
30 private final Host host;
32 public FilterProcessor(Host host) {
36 public void process(Request request, Response response, FilterChain nextValveFilterChain)
37 throws IOException, ServletException {
39 String contextPath = request.getContext().getPath();
40 String pathInfo = request.getPathInfo();
42 Optional<Context> maybeContext = host.findContext(contextPath);
43 logger.trace("Processing context {} path {}, found {}", contextPath, pathInfo, maybeContext);
44 if (maybeContext.isPresent()) {
46 Context context = maybeContext.get();
47 List<Filter> matchingFilters = context.findMatchingFilters(pathInfo);
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() {
56 public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
58 logger.trace("Applying {}", currentFilter);
60 javax.servlet.Filter actualFilter = currentFilter.getActualFilter();
61 actualFilter.doFilter(request, response, copy);
66 fromLast.doFilter(request, response);
69 nextValveFilterChain.doFilter(request, response);