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;
12 import java.io.IOException;
13 import java.util.Objects;
14 import javax.servlet.FilterChain;
15 import javax.servlet.ServletException;
16 import javax.servlet.ServletRequest;
17 import javax.servlet.ServletResponse;
18 import org.apache.catalina.connector.Request;
19 import org.apache.catalina.connector.Response;
20 import org.apache.catalina.valves.ValveBase;
21 import org.apache.commons.io.FileUtils;
22 import org.opendaylight.controller.filtervalve.cors.jaxb.Host;
23 import org.opendaylight.controller.filtervalve.cors.jaxb.Parser;
24 import org.opendaylight.controller.filtervalve.cors.model.FilterProcessor;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Valve that allows adding filters per context. Each context can have its own filter definitions.
30 * Main purpose is to allow externalizing security filters from application bundles to a single
31 * file per OSGi distribution.
33 public class FilterValve extends ValveBase {
34 private static final Logger logger = LoggerFactory.getLogger(FilterValve.class);
35 private FilterProcessor filterProcessor;
37 public void invoke(final Request request, final Response response) throws IOException, ServletException {
38 if (filterProcessor == null) {
39 throw new IllegalStateException("Initialization error");
42 FilterChain nextValveFilterChain = new FilterChain() {
44 public void doFilter(ServletRequest req, ServletResponse resp) throws IOException, ServletException {
45 boolean reqEquals = Objects.equals(request, req);
46 boolean respEquals = Objects.equals(response, resp);
47 if (reqEquals == false || respEquals == false) {
48 logger.error("Illegal change was detected by valve - request {} or " +
49 "response {} was replaced by a filter. This is not supported by this valve",
50 reqEquals, respEquals);
51 throw new IllegalStateException("Request or response was replaced in a filter");
53 getNext().invoke(request, response);
56 filterProcessor.process(request, response, nextValveFilterChain);
60 * Called by Tomcat when configurationFile attribute is set.
61 * @param fileName path to xml file containing valve configuration
64 @SuppressWarnings("UnusedDeclaration")
65 public void setConfigurationFile(String fileName) throws Exception {
66 File configurationFile = new File(fileName);
67 if (configurationFile.exists() == false || configurationFile.canRead() == false) {
68 throw new IllegalArgumentException(
69 "Cannot read 'configurationFile' of this valve defined in tomcat-server.xml: " + fileName);
73 xmlContent = FileUtils.readFileToString(configurationFile);
74 } catch (IOException e) {
75 logger.error("Cannot read {} of this valve defined in tomcat-server.xml", fileName, e);
76 throw new IllegalStateException("Cannot read " + fileName, e);
80 host = Parser.parse(xmlContent, fileName);
81 } catch (Exception e) {
82 logger.error("Cannot parse {} of this valve defined in tomcat-server.xml", fileName, e);
83 throw new IllegalStateException("Error while parsing " + fileName, e);
85 filterProcessor = new FilterProcessor(host);
89 * @see org.apache.catalina.valves.ValveBase#getInfo()
91 public String getInfo() {
92 return getClass() + "/1.0";