You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by Bill Speirs <bi...@gmail.com> on 2011/08/23 16:32:20 UTC
Getting Remote IP from IOEventDispatch
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
Re: Getting Remote IP from IOEventDispatch
Posted by Oleg Kalnichevski <ol...@apache.org>.
On Tue, 2011-08-23 at 10:32 -0400, Bill Speirs wrote:
> 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?
>
You can cast any HttpConnection object to HttpInetConnection in order to
get access to its endpoint details. Alternatively, you can override the
IOEventdispatch#connected method(), get the remote address off the
actual IOSession instance and add it an attribute to the same session by
using IOSession#setAttribute(String, Object). The attribute will show up
in the context of the connection associated with that session.
Hope this helps
Oleg
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org