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;
/**
*
A naive implementation of transparent proxy based on
* Jetty proxy servlet.
* The only difference is that the proxyTo
configuration parameter is taken from a JVM argument (and can
* be therefore injected via an environment variable), instead of an init-param
in web.xml.
* Example: java -DproxyTo=http://172.17.0.9:8080 -jar $JETTY_HOME/start.jar
* If you get a 502 Bad Gateway error:
*
*
* Make sure that Jetty 'proxy' module
* is not enabled.
*
*
* Check the value of proxyTo
. Make sure it does not redirect to the proxy server itself.
*
*
* Make sure there is no proxy (e.g. in a corporate environment) between the servlet and proxyTo
.
*
*
*
* @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 getInitParameterNames() {
ArrayList params = Collections.list(config.getInitParameterNames());
params.add(PROXY_TO);
return Collections.enumeration(params);
}
}
}