title: How do I tell the remote IP address from within NHttpRequestHandler using Apache httpcomponents?
body: <p>I am implementing a comet-style (delayed response) http server using Apache httpcomponents. My code is very similar to the "Basic non-blocking HTTP server" example at <a href="http://hc.apache.org/httpcomponents-core-ga/examples.html" rel="nofollow">http://hc.apache.org/httpcomponents-core-ga/examples.html</a></p>
  
  <p>I use a DefaultServerIOEventDispatch and DefaultListeningIOReactor to dispatch requests, just like in the example code. Inside my NHttpRequestHandler I would like to log the IP address of each request.</p>
  
  <p>Inside an HttpRequestHandler you have access to an HttpRequest, an HttpResponse and an HttpContext. With an NHttpRequestHandler you also have an NHttpResponseTrigger. How do I get the remote IP address the request came from? I can't see how to do this with the objects available.</p>
  
  <p><strong>Update</strong>, here is the Scala code I ended up using:</p>
  
  <pre><code>def getIp(context: HttpContext): Option[String] = {
    val conn = context.getAttribute(ExecutionContext.HTTP_CONNECTION)
  
    conn match {
      case inet: HttpInetConnection =&gt;
        inet.getRemoteAddress match {
          case sock: java.net.InetSocketAddress =&gt; // HttpComponents 4.1
            Some(sock.getAddress.getHostAddress)
          case adr: java.net.InetAddress =&gt; // HttpComponents 4.2
            Some(adr.getHostAddress)
          case unknown =&gt;
            Some(unknown.toString)
        }
      case _ =&gt; None
    }
  }
  </code></pre>
  
  <p>As you can see there is an extra step in HttpComponents 4.1.</p>