In switching from the sync to async httpcore code I've lost a clean
way to inject the remote IP address into the HttpContext. My sync code
basically worked like this:

final HttpService httpService = new HttpService(...);
final HttpContext context = new BasicHttpContext();

while(connection.isOpen()) {
        final String remoteAddr = 
connection.getRemoteAddress().getHostAddress();

        context.setAttribute("REMOTE_ADDRESS",
connection.getRemoteAddress().getHostAddress());
                                
        httpService.handleRequest(connection, context);
}

This allowed me to get the remote address in the handler. I want to
reproduce this same functionality using the async code. My first
thought was to create an EventListener and in the connectionOpen
method, cast the NHttpConnection to a DefaultNHttpServerConnection,
then get both remote address & context:


public void connectionOpen(final NHttpConnection conn) {
    DefaultNHttpServerConnection serverConn =
(DefaultNHttpServerConnection)conn;

    final String remoteAddress = serverConn.getRemoteAddress().getHostAddress();

    serverConn.getContext().setAttribute("REMOTE_ADDRESS", remoteAddress);
}

The cast feels a bit clumsy, but since I'm always using a
DefaultServerIOEventDispatch, I think I'm probably alright.

Any thoughts on this? Is there a better way to go about this?

Thanks...

Bill-

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to