Using Jetty's proxy in standalone Jetty application

mnd

I'm investigating using Jetty 9 as a proxy, using standalone Jetty, not embedded Jetty. I've looked for help in many places:

Most of these are related to embedded Jetty:

This question is along the same lines:

...but the only answer is a link to a page that covers some parameters for the proxy, but no examples or other helpful hints.

On to the question...

I've created an extension to Jetty's ProxyServlet, which overrides the rewriteURI() method to actually change the request to a different URL. This custom proxy works when running Jetty embedded, but when I use a web.xml file and the jetty-maven-plugin to create a war to deploy, it no longer works.

When I make a request, I can debug the application and see that it gets into the rewriteURI() method, it then calls Jetty's ProxyServlet's service() method, which runs to completion, but then nothing happens. The page remains blank, and eventually ProxyServlet's onResponseFailure() is called with a TimeoutException, "Total timeout elapsed". Dev tools shows the request receiving a 504 Gateway Timeout.

It seems as though something is missing in how things are connected, but I can't tell what it might be. Any help would be greatly appreciated. I've included web.xml and the custom proxy (ProxyServletExtension) below.

web.xml

<servlet>
    <servlet-name>proxy</servlet-name>
    <servlet-class>org.example.ProxyServletExtension</servlet-class>
    <init-param>
        <param-name>maxThreads</param-name>
        <param-value>1</param-value>
    </init-param>
    <async-supported>true</async-supported>
</servlet>
<servlet-mapping>
    <servlet-name>proxy</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

ProxyServletExtension.java

...
import org.eclipse.jetty.proxy.ProxyServlet;
...
public class ProxyServletExtension extends ProxyServlet {
    @Override
    protected URI rewriteURI(HttpServletRequest request) {
        // Forward all requests to another port on this machine
        String uri = "http://localhost:8060";

        // Take the current path and append it to the new url
        String path = request.getRequestURI();
        uri += path;

        // Add query params
        String query = request.getQueryString();
        if (query != null && query.length() > 0) {
            uri += "?" + query;
        }
        return URI.create(uri).normalize();
    }
}
mnd

I found the hints I needed to solve this with jetty transparent proxy always returns 403 forbidden. The question didn't exactly pertain to my question, but the code snippet provided showed me what I needed in the <servlet> in web.xml.

Updated web.xml

<servlet>
    <servlet-name>proxy</servlet-name>
    <servlet-class>org.example.ProxyServletExtension$Transparent</servlet-class>
    <init-param>
        <param-name>maxThreads</param-name>
        <param-value>1</param-value>
    </init-param>
    <init-param>
        <param-name>proxyTo</param-name>
        <param-value>http://localhost:8060</param-value>
    </init-param>
    <async-supported>true</async-supported>
</servlet>
<servlet-mapping>
    <servlet-name>proxy</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

Required changes

  1. Set the <servlet-class> to ProxyServletExtension$Transparent, previously I wasn't using a trasparent proxy.
  2. Use an <init-param> of proxyTo using the address you would like to proxy the requests to. This also means that the ProxyServletExtension.java class above (in the question) is completely unnecessary.

Also, it is worth mentioning that there is a prefix <init-param> as well, which can be used to remove part of the incoming request before proxying to the proxied request.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章