gt:     String proxyRequestUri = rewriteUrlFromRequest(servletRequest);
context: package io.hops.hopsworks.api.kibana;
  
  /**
   * Copyright MITRE
   * <p>
   * 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
   * <p>
   * http://www.apache.org/licenses/LICENSE-2.0
   * <p>
   * 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.
   */
  import org.apache.http.Header;
  import org.apache.http.HttpEntity;
  import org.apache.http.HttpEntityEnclosingRequest;
  import org.apache.http.HttpHeaders;
  import org.apache.http.HttpHost;
  import org.apache.http.HttpRequest;
  import org.apache.http.HttpResponse;
  import org.apache.http.client.HttpClient;
  import org.apache.http.client.methods.AbortableHttpRequest;
  import org.apache.http.client.params.ClientPNames;
  import org.apache.http.client.params.CookiePolicy;
  import org.apache.http.client.utils.URIUtils;
  import org.apache.http.entity.InputStreamEntity;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
  import org.apache.http.message.BasicHeader;
  import org.apache.http.message.BasicHttpEntityEnclosingRequest;
  import org.apache.http.message.BasicHttpRequest;
  import org.apache.http.message.HeaderGroup;
  import org.apache.http.params.BasicHttpParams;
  import org.apache.http.params.HttpParams;
  import org.apache.http.util.EntityUtils;
  
  import javax.servlet.ServletException;
  import javax.servlet.http.Cookie;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import java.io.Closeable;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.lang.reflect.Constructor;
  import java.net.HttpCookie;
  import java.net.URI;
  import java.util.BitSet;
  import java.util.Enumeration;
  import java.util.Formatter;
  import java.util.List;
  
  /**
   * An HTTP reverse proxy/gateway servlet. It is designed to be extended for
   * customization
   * if desired. Most of the work is handled by
   * <a href="http://hc.apache.org/httpcomponents-client-ga/">Apache
   * HttpClient</a>.
   * <p>
   * There are alternatives to a servlet based proxy such as Apache mod_proxy if
   * that is available to you. However
   * this servlet is easily customizable by Java, secure-able by your web
   * application's security (e.g. spring-security),
   * portable across servlet engines, and is embeddable into another web
   * application.
   * </p>
   * <p>
   * Inspiration: http://httpd.apache.org/docs/2.0/mod/mod_proxy.html
   * </p>
   * <p>
   * David Smiley dsmiley@mitre.org
   */
  public class ProxyServlet extends HttpServlet {
  
    /*
     * INIT PARAMETER NAME CONSTANTS
     */
    /**
     * A boolean parameter name to enable logging of input and target URLs to the
     * servlet log.
     */
    public static final String P_LOG = "log";
  
    /**
     * A boolean parameter name to enable forwarding of the client IP
     */
    public static final String P_FORWARDEDFOR = "forwardip";
  
    /**
     * The parameter name for the target (destination) URI to proxy to.
     */
    protected static final String P_TARGET_URI = "targetUri";
    protected static final String ATTR_TARGET_URI = ProxyServlet.class.
        getSimpleName() + ".targetUri";
    protected static final String ATTR_TARGET_HOST = ProxyServlet.class.
        getSimpleName() + ".targetHost";
    protected static final String ATTR_URI_FINISH = ProxyServlet.class.
        getSimpleName() + ".uriFinish";
    protected static final String ATTR_HOST_PORT = ProxyServlet.class.
        getSimpleName() + ".hostPort";
  
    /*
     * MISC
     */
    protected boolean doLog = false;
    protected boolean doForwardIP = true;
    /**
     * User agents shouldn't send the url fragment but what if it does?
     */
    protected boolean doSendUrlFragment = true;
  
    //These next 3 are cached here, and should only be referred to in 
    //initialization logic. See the ATTR_* parameters.
    /**
     * From the configured parameter "targetUri".
     */
    protected String targetUri;
    protected URI targetUriObj;//new URI(targetUri)
    protected HttpHost targetHost;//URIUtils.extractHost(targetUriObj);
  
    protected HttpClient proxyClient;
  
    @Override
    public String getServletInfo() {
      return "A proxy servlet by David Smiley, dsmiley@apache.org";
    }
  
    protected String getTargetUri(HttpServletRequest servletRequest) {
      return (String) servletRequest.getAttribute(ATTR_TARGET_URI);
    }
  
    protected HttpHost getTargetHost(HttpServletRequest servletRequest) {
      return (HttpHost) servletRequest.getAttribute(ATTR_TARGET_HOST);
    }
  
    /**
     * Reads a configuration parameter. By default it reads servlet init
     * parameters but
     * it can be overridden.
     */
    protected String getConfigParam(String key) {
      return getServletConfig().getInitParameter(key);
    }
  
    @Override
    public void init() throws ServletException {
      String doLogStr = getConfigParam(P_LOG);
      if (doLogStr != null) {
        this.doLog = Boolean.parseBoolean(doLogStr);
      }
  
      String doForwardIPString = getConfigParam(P_FORWARDEDFOR);
      if (doForwardIPString != null) {
        this.doForwardIP = Boolean.parseBoolean(doForwardIPString);
      }
  
      initTarget();//sets target*
  
      HttpParams hcParams = new BasicHttpParams();
      hcParams.setParameter(ClientPNames.COOKIE_POLICY,
          CookiePolicy.IGNORE_COOKIES);
      readConfigParam(hcParams, ClientPNames.HANDLE_REDIRECTS, Boolean.class);
      proxyClient = createHttpClient(hcParams);
    }
  
    protected void initTarget() throws ServletException {
      // TODO - should get the Kibana URI from Settings.java
  //    targetUri = Settings.getKibanaUri();
      targetUri = getConfigParam(P_TARGET_URI);
      if (targetUri == null) {
        throw new ServletException(P_TARGET_URI + " is required.");
      }
      //test it's valid
      try {
        targetUriObj = new URI(targetUri);
      } catch (Exception e) {
        throw new ServletException("Trying to process targetUri init parameter: "
            + e, e);
      }
      targetHost = URIUtils.extractHost(targetUriObj);
    }
  
    /**
     * Called from {@link #init(javax.servlet.ServletConfig)}. HttpClient offers
     * many opportunities
     * for customization. By default,
     * <a href="http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/
     * org/apache/http/impl/client/SystemDefaultHttpClient.html">
     * SystemDefaultHttpClient</a> is used if available, otherwise it falls
     * back to:
     * <pre>new DefaultHttpClient(new ThreadSafeClientConnManager(),hcParams)</pre>
     * SystemDefaultHttpClient uses PoolingClientConnectionManager. In any case,
     * it should be thread-safe.
     */
    @SuppressWarnings({"unchecked", "deprecation"})
    protected HttpClient createHttpClient(HttpParams hcParams) {
      try {
        //as of HttpComponents v4.2, this class is better since it uses System
        // Properties:
        Class clientClazz = Class.forName(
            "org.apache.http.impl.client.SystemDefaultHttpClient");
        Constructor constructor = clientClazz.getConstructor(HttpParams.class);
        return (HttpClient) constructor.newInstance(hcParams);
      } catch (ClassNotFoundException e) {
        //no problem; use v4.1 below
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
  
      //Fallback on using older client:
      return new DefaultHttpClient(new ThreadSafeClientConnManager(), hcParams);
    }
  
    /**
     * The http client used.
     *
     * @return
     * @see #createHttpClient(HttpParams)
     */
    protected HttpClient getProxyClient() {
      return proxyClient;
    }
  
    /**
     * Reads a servlet config parameter by the name {@code hcParamName} of type
     * {@code type}, and
     * set it in {@code hcParams}.
     *
     * @param hcParams
     * @param hcParamName
     * @param type
     */
    protected void readConfigParam(HttpParams hcParams, String hcParamName,
        Class type) {
      String val_str = getConfigParam(hcParamName);
      if (val_str == null) {
        return;
      }
      Object val_obj;
      if (type == String.class) {
        val_obj = val_str;
      } else {
        try {
          //noinspection unchecked
          val_obj = type.getMethod("valueOf", String.class).invoke(type, val_str);
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      }
      hcParams.setParameter(hcParamName, val_obj);
    }
  
    @Override
    public void destroy() {
      //As of HttpComponents v4.3, clients implement closeable
      if (proxyClient instanceof Closeable) {//TODO AutoCloseable in Java 1.6
        try {
          ((Closeable) proxyClient).close();
        } catch (IOException e) {
          log("While destroying servlet, shutting down HttpClient: " + e, e);
        }
      } else {
        //Older releases require we do this:
        if (proxyClient != null) {
          proxyClient.getConnectionManager().shutdown();
        }
      }
      super.destroy();
    }
  
    @Override
    protected void service(HttpServletRequest servletRequest,
        HttpServletResponse servletResponse)
        throws ServletException, IOException {
      //initialize request attributes from caches if unset by a subclass by this point
      if (servletRequest.getAttribute(ATTR_TARGET_URI) == null) {
        servletRequest.setAttribute(ATTR_TARGET_URI, targetUri);
      }
      if (servletRequest.getAttribute(ATTR_TARGET_HOST) == null) {
        servletRequest.setAttribute(ATTR_TARGET_HOST, targetHost);
      }
  
      // Make the Request
      // note: we won't transfer the protocol version because I'm not 
      // sure it would truly be compatible
      String method = servletRequest.getMethod();
  