Honestly, in this case, it might be getter to just call:

PhaseInterceptorChain.getCurrentMessage()

and grab the HTTPRequest object out of there.  That would work with both JAX-
WS and JAXRS and avoids the mismatch of injections and such.  

It ties it to CXF API's, but that may not matter too much for you.


Dan


On Monday, June 13, 2011 6:52:07 AM myerscb wrote:
> it appears that I spoke to soon.
> 
> The jaxrsContext is null.  So when I attempt to get the ip address when
> running a REST query i get a null pointer.
> 
> What i have figured out is if i switch jaxrsContext to a
> org.apache.cxf.jaxrs.ext.MessgeContext from a
> javax.xml.ws.handler.MessageContext the rest side start to work.  But, the
> SOAP side no longer works (which it does if I use the
> javax.xml.ws.handler.MessageContext.
> 
> I am not sure why changing the package of the MessageContext for
> jaxrsContext affects the jaxwsContext but it does.
> 
> so to be clear i am including the relevant code again with package info.
> 
>        @Resource javax.xml.ws.WebServiceContext jaxwsContext;
>        @Resource javax.xml.ws.handler.MessageContext jaxrsContext;
> 
>       private String getIpAddress() {
>               String ipAddress = " unknown";
>               javax.servlet.http.HttpServletRequest request = null;
> 
>               if (jaxrsContext != null) {
>                       request = (HttpServletRequest)
> jaxrsContext.get(org.apache.cxf.transport.http.AbstractHTTPDestination.HTTP_
> REQUEST); } else if (jaxwsContext != null) {
>                       request = (HttpServletRequest)
> jaxwsContext.getMessageContext().get(javax.xml.ws.handler.MessageContext.SER
> VLET_REQUEST); } else {
>                       // TODO can not get ip address
>                       // should we through and exception
>               }
> 
>               if (request != null){
>                       if 
> (StringUtils.isNotBlank(request.getHeader("X-Forwarded-
For"))) {
>                               ipAddress = 
> request.getHeader("X-Forwarded-For");
>                       } else {
>                               ipAddress = request.getRemoteAddr();
>                       }
>               }
> 
>               // System.out.println("IP ADDRESS: " + ipAddress);
>               return ipAddress;
>       }
> 
> any help would be greatly appreciated.
> 
> Thanks,
> 
> Chris
> 
> --
> View this message in context:
> http://cxf.547215.n5.nabble.com/Getting-the-IP-Address-of-SOAP-Clients-work
> s-for-REST-clients-but-not-for-SOAP-clients-tp4476334p4484400.html Sent from
> the cxf-user mailing list archive at Nabble.com.
-- 
Daniel Kulp
[email protected]
http://dankulp.com/blog
Talend - http://www.talend.com

Reply via email to