I'd like to log all incoming requests and outgoing responses coming into or going out of my server.
Is there an event listener that I can register that will provide a Request object before it is passed to any Handler and a Response object before it is sent out?
I already manually inserted log statements everywhere I could find, but some calls never reach my application's Handler. For example, if a user invokes HTTP GET https://example.com//quests/195 I get:
org.eclipse.jetty.http.BadMessageException: 400: Ambiguous URI empty segment
at [email protected]/org.eclipse.jetty.server.internal.HttpConnection$HttpStreamOverHTTP1.headerComplete(HttpConnection.java:1226)
at [email protected]/org.eclipse.jetty.server.internal.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:1012)
at [email protected]/org.eclipse.jetty.http.HttpParser.parseFields(HttpParser.java:1252)
at [email protected]/org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:1545)
at [email protected]/org.eclipse.jetty.server.internal.HttpConnection.parseRequestBuffer(HttpConnection.java:626)
at [email protected]/org.eclipse.jetty.server.internal.HttpConnection.onFillable(HttpConnection.java:460)
at [email protected]/org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:322)
at [email protected]/org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:100)
at [email protected]/org.eclipse.jetty.io.SelectableChannelEndPoint$1.run(SelectableChannelEndPoint.java:53)
at java.base/java.util.concurrent.ThreadPerTaskExecutor$TaskRunner.run(ThreadPerTaskExecutor.java:314)
In the above case, I have no where to get at a Request object so I can log the incoming request, nor the response that gets sent out automatically by Jetty. Any ideas?
You have 2 choices.
Choice 1: Create a custom Handler.Wrapper with custom Request.Wrapper and Response.Wrapper
Create a normal Handler.Wrapper, and put it somewhere early in your Handler tree.
Then you can just wrap the existing request/response with your own implementation and pass it into the wrapped handler.
Eg:
Choice 2: Create your EventsHandler to capture events
The abstract
EventsHandlerhas a few events that you can hook into at different points during the handling that might be useful for you.See