filter-valve: use lambdas
[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 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;
24
25 public class FilterProcessor {
26     private static final Logger logger = LoggerFactory.getLogger(FilterProcessor.class);
27
28     private final Host host;
29
30     public FilterProcessor(Host host) {
31         this.host = host;
32     }
33
34     public void process(Request request, Response response, FilterChain nextValveFilterChain)
35             throws IOException, ServletException {
36
37         String contextPath = request.getContext().getPath();
38         String path = request.getDecodedRequestURI();
39
40         Optional<Context> maybeContext = host.findContext(contextPath);
41         logger.trace("Processing context {} path {}, found {}", contextPath, path, maybeContext);
42         if (maybeContext.isPresent()) {
43             // process filters
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) -> {
53                     if (trace) {
54                         logger.trace("Applying {}", currentFilter);
55                     }
56                     javax.servlet.Filter actualFilter = currentFilter.getActualFilter();
57                     actualFilter.doFilter(request1, response1, copy);
58                 };
59             }
60             // call first filter
61             fromLast.doFilter(request, response);
62         } else {
63             // move to next valve
64             nextValveFilterChain.doFilter(request, response);
65         }
66     }
67 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.