Using Jetty Proxy in a Standalone Jetty Application

I am learning to use Jetty 9 as a proxy using standalone Jetty, rather than embedded Jetty. I have looked for help in many places:

Most of them are related to the built-in Jetty:

This question goes along the same lines:

... but the only answer is a link to a page that covers some parameters for the proxy, but there are no examples or other useful tips.

To the question ...

I created an extension for the Jetty ProxyServlet that overrides the method rewriteURI()to actually change the request to a different URL. This custom proxy works when Jetty starts, but when I use the web.xml file and the jetty-maven plugin to create a war for deployment, it no longer works.

When I make a request, I can debug the application and see that it falls into the method rewriteURI(), then it calls the Jetty ProxyServlet method service(), which runs until completion, but then nothing happens. The page remains blank, and eventually the ProxyServlet onResponseFailure()is thrown using the TimeoutException, "Expired General Timeout" exception. Dev tools show a request receiving a 504 Gateway Timeout.

, - , , , . . web.xml (ProxyServletExtension) .

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();
    }
}
+4
1

, - 403 . , , <servlet> web.xml.

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>

  • <servlet-class> ProxyServletExtension$Transparent, trasparent proxy.
  • <init-param> proxyTo, , . , ProxyServletExtension.java ( ) .

, , prefix <init-param>, -.

+2

Source: https://habr.com/ru/post/1547828/


All Articles