javajettyjetty-12

Porting from Jetty 11 to Jetty 12. Problem with immutable request


I am porting application that uses Jetty as a HTTP server from Jetty 11 to Jetty 12. I use my own handler that extends ResourceHandler and changes request. I need to change request because for some requests there is session identificator in URL, and I need to remove it from URL. I also map WOPI requests (LibreOffice) and I have to change the same query with GET method to "view document", and with POST method "save document" with proper QueryString.

In Jetty 12 I changed many things like request.getRemoteAddr() -> Request.getRemoteAddr(request) but request is immutable, so I have problems with methods: request.setQueryParameters() request.setMethod() request.setHttpURI() request.setContext()

My code that works in Jetty 11:

class my_jetty_handler extends ResourceHandler
    {

    @Override
    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException
        {
            StringBuilder fileURL = new StringBuilder(target);
            ...
            // remove session from URL and put it on env
            ProcesURLSubst(fileURL, env);
            ...
            // map WOPI query to new query with changed QueryString
            WOPIRedirectURL(baseRequest, fileURL, qs, session);
            if (qs.size() > 0)
                baseRequest.setQueryParameters(qs);

            ...
            // handle static file
                String new_path = sFileURL.toString();
                baseRequest.setMethod("GET");

                baseRequest.setHttpURI(HttpURI.build(new_path));
                baseRequest.setContext(baseRequest.getContext(), new_path);
                super.handle(new_path, baseRequest, request, response);
            ...
        } // handle

Do you have any idea how to make changed request in Jetty 12?


Solution

  • You use a Request.Wrapper to make a new Request object with the appropriate changes for the next call to super.handle(request, response, callback);

    Example:

    public class AlteredRequestHandler extends Handler.Wrapper
    {
        @Override
        public boolean handle(Request request, Response response, Callback callback) throws Exception
        {
            HttpURI alteredHttpURI = HttpURI.build(request.getHttpURI())
                .query("a=b");
            String alteredMethod = "COPY";
            
            Request alteredRequest = new Request.Wrapper(request)
            {
                @Override
                public HttpURI getHttpURI()
                {
                    return alteredHttpURI;
                }
    
                @Override
                public String getMethod()
                {
                    return alteredMethod;
                }
            };
            
            return super.handle(alteredRequest, response, callback);
        }
    }
    

    Note: the Request does not have the Context. Only the ContextHandler has that (even nested ContextHandler is supported in Jetty 12) You can access the current context via ContextHandler.getCurrentContext()

    Also, don't forget to look at the Jetty 11 to Jetty 12 porting guide.

    https://eclipse.dev/jetty/documentation/jetty-12/programming-guide/index.html#pg-migration-11-to-12