Return-Path: X-Original-To: apmail-hc-dev-archive@www.apache.org Delivered-To: apmail-hc-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5BF3177F1 for ; Tue, 23 Aug 2011 15:27:59 +0000 (UTC) Received: (qmail 74266 invoked by uid 500); 23 Aug 2011 15:27:59 -0000 Delivered-To: apmail-hc-dev-archive@hc.apache.org Received: (qmail 74213 invoked by uid 500); 23 Aug 2011 15:27:58 -0000 Mailing-List: contact dev-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list dev@hc.apache.org Received: (qmail 74205 invoked by uid 99); 23 Aug 2011 15:27:58 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 23 Aug 2011 15:27:58 +0000 X-ASF-Spam-Status: No, hits=0.7 required=5.0 tests=SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (athena.apache.org: local policy) Received: from [92.42.190.144] (HELO ok2cons2.nine.ch) (92.42.190.144) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 23 Aug 2011 15:27:50 +0000 Received: from [192.168.42.77] (unknown [213.55.131.184]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ok2cons2.nine.ch (Postfix) with ESMTPSA id E7BE4245E478 for ; Tue, 23 Aug 2011 17:27:28 +0200 (CEST) Subject: Re: Getting Remote IP from IOEventDispatch From: Oleg Kalnichevski To: HttpComponents Project In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Date: Tue, 23 Aug 2011 17:27:08 +0200 Message-ID: <1314113228.1969.60.camel@ubuntu> Mime-Version: 1.0 X-Mailer: Evolution 2.30.3 Content-Transfer-Encoding: 7bit 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