I have a class that extends AbstractHandler and overrides the handle method like this:
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
throws IOException {
// implementation details
}
However, I see that AbstractHandler is deprecated in Jetty 12 and the method signature for handle has changed:
@Override
public boolean handle(Request request, Response response, Callback callback) throws Exception {
}
My questions are:
- How can I access the
baseRequestandrequestobjects that I rely on in my Jetty 11 implementation? - What is the proper way to port my
AbstractHandlersubclass to work in Jetty 12?
Here is a summary of what my handler is doing:
- Checks
HttpServletRequestheaders - Sets
HttpServletResponseheaders based onRequestheaders
Any help or examples of properly migrating an AbstractHandler implementation from Jetty 11 to Jetty 12 would be greatly appreciated!
What you appear to want is either
Handler.AbstractorHandler.Wrapper.There is documentation on both at
And there are examples in the jetty-examples project at
Such as
FormEndpoints(along with others in the/embedded/tree)In short, you have an immutable
Requestand a mutableResponse.The
Requestis a special purposeContent.Sourcefor HTTP which handles the read/demand behaviors of the incoming request (be sure to check out the static methods on both of these types, as there are many handy convenience methods for common tasks)The
Responseis a special purposeContent.Sinkfor HTTP which handles the write behaviors of the outgoing request (also look at the static methods on these two types as well).The
boolean Handler.handle(Request, Response, Callback)method has the following contract.truemeans that this Handler is going to handle the request. This means no furtherHandlerimplementations will be called by the Server dispatch. (this was the equivalent ofbaseRequest.setHandled(true)in Jetty 11 and older)CallbackMUST eventually be completed by callingsuccess()orfailure()on it. (many of the methods can take the Callback and do the success/failure for you, eg:Response.write,Content.Sink.write, etc)truedoes not complete/finish the request/response, only the callback completes the request/response.If you want to mutate the request (to change something about it) for a different handler, then you must use a
Handler.Wrapperand wrap the incoming request in aRequest.Wrapperthat modifies things to suite your needs and then call the nested handler.Tip: if you can avoid using the Stream APIs from Content.Sink and Content.Source you'll have far better performance (and less resource use) in your webapp. But we know that sometimes that is not possible, so don't hesitate to use them.