I'm trying to retrieve the client IP with JAX-WS, I used:
@Resource
WebServiceContext wsContext;
MessageContext mc = wsContext.getMessageContext();
HttpServletRequest req = (HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST);
log.info("["+req.getRemoteAddr()+"])
I get a NullPointerException
in req, mc is not null.
My question is which JAR to use for HttpServletRequest
because I'm using a Java stand-alone application?
Thanks
Here is an answer, I think you should initialize mc and req in WebMethod annotated method. Geting the IP Address Of A client For a webservice
The following code might work:
if we use an embedded Http server , we can get the client IP like this:
Then in the web method :
Hope it helps someone else
How to get the webservice client address for a jax-ws service depends on whether you are:
Servlet Webservices If your webservice is a servlet then use the solution of the first post that contains the following:
Application Webservices : JAX-WS 2.1 If you are using a Java application (Java SE) you have no servlet context, so the
HttpServletRequest
will be null. You need to use the method of the later post, the one that has the following line:Note: this only works with the JAX-WS 2.1 stack/reference implementation.
Application Webservices : JAX-WS 2.2
In JAX-WS 2.2 the value of
JAXWSProperties.HTTP_EXCHANGE
has changed from "com.sun.xml.ws.http.exchange" (the value it was in JAX-WS 2.1) to "com.sun.xml.internal.ws.http.exchange". That means that a call towill return null in JAX-WS 2.2 and you'll get a
NullPointerException
on the second line, and more importantly, cannot get the remote address of the client.If you use the following call instead (using the literal value, ugh!):
you will get a non-null value and will be able to obtain the client address.
So, how you get the remote address of the client depends on how you deploy your code (servlet or application) and which version of JAX-WS you are using (JAX-WS 2.1 or 2.2).
Recommendations
Servlets: If you are deploying your JAX-WS webservice in a servlet you can always use the call to get the property
MessageContext.SERVLET_REQUEST
no matter what version of JAX-WS 2 you are using.Applications: If you are deploying your JAX-WS webservice in an application you can always use the call
HttpExchange exchange = (HttpExchange)msgx.get("com.sun.xml.ws.http.exchange");
no matter whether you are using JAX-WS 2.1 or 2.2, therefore it is probably better to use the string literal in your code rather than the symbolicJAXWSProperties.HTTP_EXCHANGE
.As distasteful as using the literal is, it is better to have more robust code that works across JAX-WS versions rather than prettier code that doesn't.
I hope the JAX-WS team correct the issue sometime and restore the value of
JAXWSProperties.HTTP_EXCHANGE
to the useful value again.Many thanks to the earlier posters that showed the various ways of finding the remote address of JAX-WS clients. The information was very useful :)