hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bill Speirs <bill.spe...@gmail.com>
Subject Getting Remote IP from IOEventDispatch
Date Tue, 23 Aug 2011 14:32:20 GMT
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: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Mime
View raw message