I use Grizzly to apply for a Jersey, using Logback for my registration needs. Please, not that there is no Servlet , I run everything "manually" using a piece of code:
final URI uri = this.server = new HttpServer(); final NetworkListener nl = new NetworkListener( "grizzly", uri.getHost(), uri.getPort()); server.addListener(nl); final GuiceComponentProviderFactory gcpf = new GuiceComponentProviderFactory(rc, inj); final HttpHandler processor = ContainerFactory.createContainer( HttpHandler.class, rc, gcpf); this.server.getServerConfiguration().addHttpHandler( processor, uri.getPath()); server.start();
Now I would like to use the Logback MDC function to make the client socket address visible in the log entries. To do this, I need a place to connect the listener to HTTP processing, which receives notification of incoming requests (where I can put the address in the MDC) and when the request is completed (so that I can clear the MDC). One of my approaches is to connect an instance of Container*Filter to a Jersey that looks like this:
class MdcFilter implements ContainerRequestFilter, ContainerResponseFilter { @Override public ContainerRequest filter(ContainerRequest request) { MDC.put("http-client", "foo" ); return request; } @Override public ContainerResponse filter( ContainerRequest request, ContainerResponse response) { MDC.remove("http-client"); return response; } }
Unfortunately, Jersey ContainerRequest does not provide information about the connected client (which cam is like a real surprise).
I suspect that such an interface should exist with the grizzly himself, but I could not dig it out.
source share