aboutsummaryrefslogtreecommitdiffstats
path: root/workflow-designer-ui/src/main/java/org/onap/workflow/web/TransparentProxy.java
blob: 3dc652341362e1b31dd3b5676c32cd93c5e55940 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * Copyright © 2016-2018 European Support Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.workflow.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.eclipse.jetty.proxy.ProxyServlet;

/**
 * <p>A naive implementation of transparent proxy based on
 * <a href="https://www.eclipse.org/jetty/documentation/9.4.x/proxy-servlet.html">Jetty proxy servlet</a>.
 * The only difference is that the <code>proxyTo</code> configuration parameter is taken from a JVM argument (and can
 * be therefore injected via an environment variable), instead of an <code>init-param</code> in <i>web.xml</i>.</p>
 * <p>Example: <code>java -DproxyTo=http://172.17.0.9:8080 -jar $JETTY_HOME/start.jar</code></p>
 * <p>If you get a <i>502 Bad Gateway</i> error:</p>
 * <ul>
 *     <ol>
 *         Make sure that Jetty 'proxy' module
 *         <a href="https://www.eclipse.org/jetty/documentation/9.4.x/startup-modules.html">is not enabled</a>.
 *     </ol>
 *     <ol>
 *         Check the value of <code>proxyTo</code>. Make sure it does not redirect to the proxy server itself.
 *     </ol>
 *     <ol>
 *         Make sure there is no proxy (e.g. in a corporate environment) between the servlet and <code>proxyTo</code>.
 *     </ol>
 * </ul>
 *
 * @author evitaliy
 * @since 16 Jul 2018
 */
public class TransparentProxy extends ProxyServlet.Transparent {

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(new ServletConfigWrapper(config));
    }

    private class ServletConfigWrapper implements ServletConfig {

        private static final String PROXY_TO = "proxyTo";

        private final String proxyTo;
        private final ServletConfig config;

        ServletConfigWrapper(ServletConfig config) throws ServletException {

            this.proxyTo = System.getProperty(PROXY_TO);
            if (this.proxyTo == null) {
                throw new ServletException("-D" + PROXY_TO + " must be specified");
            }

            this.config = config;
        }

        @Override
        public String getServletName() {
            return config.getServletName();
        }

        @Override
        public ServletContext getServletContext() {
            return config.getServletContext();
        }

        @Override
        public String getInitParameter(String s) {
            return PROXY_TO.equals(s) ? this.proxyTo : config.getInitParameter(s);
        }

        @Override
        public Enumeration<String> getInitParameterNames() {
            ArrayList<String> params = Collections.list(config.getInitParameterNames());
            params.add(PROXY_TO);
            return Collections.enumeration(params);
        }
    }
}