title: How can I get my hands on client addresses for logging using Grizzly / Jersey?
body: <p>I'm using Grizzly to serve a Jersey application, while using Logback for my logging needs. Please not that there are no <code>Servlet</code>s involved here, I fire up everything "by hand" using a piece of code like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>final URI uri = /* this is a configuration option */        
  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();
  </code></pre>
  
  <p>Now I would like to use Logback's <a href="http://logback.qos.ch/manual/mdc.html" rel="nofollow">MDC</a> feature to make the socket address of the clients visible in the log records. For this purpose I would need some place to hook up a listener to the HTTP processing which gets notified about incoming requests (where I can put the address into the MDC) and when a request is done (so I can clean up the MDC). One approach I followed is to hook up a <code>Container*Filter</code> instance with Jersey, which looked like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>class MdcFilter implements
          ContainerRequestFilter, ContainerResponseFilter {
  
      @Override
      public ContainerRequest filter(ContainerRequest request) {
          MDC.put("http-client", "foo" /* no way to get the address here */);
          return request;
      }
  
      @Override
      public ContainerResponse filter(
              ContainerRequest request,
              ContainerResponse response) {
  
          MDC.remove("http-client");
          return response;
      }
  
  }
  </code></pre>
  
  <p>Unfortunately, a Jersey <code>ContainerRequest</code> does not provide information about the connected client (which cam as a real surprise).</p>
  
  <p>I suspect a similar interface should exist with Grizzly itself, but I was unable to dig it out.</p>