Remove unnecessary @SuppressWarnings
[controller.git] / opendaylight / commons / filter-valve / src / main / java / org / opendaylight / controller / filtervalve / cors / FilterValve.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;
10
11 import java.io.File;
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;
27
28 /**
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.
32  */
33 public class FilterValve extends ValveBase {
34     private static final Logger logger = LoggerFactory.getLogger(FilterValve.class);
35     private FilterProcessor filterProcessor;
36
37     public void invoke(final Request request, final Response response) throws IOException, ServletException {
38         if (filterProcessor == null) {
39             throw new IllegalStateException("Initialization error");
40         }
41
42         FilterChain nextValveFilterChain = new FilterChain() {
43             @Override
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");
52                 }
53                 getNext().invoke(request, response);
54             }
55         };
56         filterProcessor.process(request, response, nextValveFilterChain);
57     }
58
59     /**
60      * Called by Tomcat when configurationFile attribute is set.
61      * @param fileName path to xml file containing valve configuration
62      * @throws Exception
63      */
64     public void setConfigurationFile(String fileName) throws Exception {
65         File configurationFile = new File(fileName);
66         if (configurationFile.exists() == false || configurationFile.canRead() == false) {
67             throw new IllegalArgumentException(
68                     "Cannot read 'configurationFile' of this valve defined in tomcat-server.xml: " + fileName);
69         }
70         String xmlContent;
71         try {
72             xmlContent = FileUtils.readFileToString(configurationFile);
73         } catch (IOException e) {
74             logger.error("Cannot read {} of this valve defined in tomcat-server.xml", fileName, e);
75             throw new IllegalStateException("Cannot read " + fileName, e);
76         }
77         Host host;
78         try {
79             host = Parser.parse(xmlContent, fileName);
80         } catch (Exception e) {
81             logger.error("Cannot parse {} of this valve defined in tomcat-server.xml", fileName, e);
82             throw new IllegalStateException("Error while parsing " + fileName, e);
83         }
84         filterProcessor = new FilterProcessor(host);
85     }
86
87     /**
88      * @see org.apache.catalina.valves.ValveBase#getInfo()
89      */
90     public String getInfo() {
91         return getClass() + "/1.0";
92     }
93 }