Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>How to get the webservice client address for a jax-ws service depends on whether you are:</p> <ul> <li>Running your webservice as a servlet (in a Java EE container), or</li> <li>Running your webservice as a stand-alone application (Java SE 6 or 7).</li> </ul> <p><strong>Servlet Webservices</strong> If your webservice is a servlet then use the solution of the first post that contains the following:</p> <pre><code>HttpServletRequest req = (HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST); </code></pre> <p><strong>Application Webservices : JAX-WS 2.1</strong> If you are using a Java application (Java SE) you have no servlet context, so the <code>HttpServletRequest</code> will be null. You need to use the method of the later post, the one that has the following line:</p> <pre><code>HttpExchange exchange = (HttpExchange)msgx.get(JAXWSProperties.HTTP_EXCHANGE); </code></pre> <p>Note: this only works with the JAX-WS 2.1 stack/reference implementation.</p> <p><strong>Application Webservices : JAX-WS 2.2</strong></p> <p>In JAX-WS 2.2 the value of <code>JAXWSProperties.HTTP_EXCHANGE</code> 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 to </p> <pre><code>HttpExchange exchange = (HttpExchange)msgx.get(JAXWSProperties.HTTP_EXCHANGE); InetSocketAddress remoteAddress = exchange.getRemoteAddress(); String remoteHost = remoteAddress.getHostName(); </code></pre> <p>will return null in JAX-WS 2.2 and you'll get a <code>NullPointerException</code> on the second line, and more importantly, cannot get the remote address of the client.</p> <p>If you use the following call instead (using the literal value, ugh!):</p> <pre><code>HttpExchange exchange = (HttpExchange)msgx.get("com.sun.xml.ws.http.exchange"); InetSocketAddress remoteAddress = exchange.getRemoteAddress(); String remoteHost = remoteAddress.getHostName(); </code></pre> <p>you will get a non-null value and will be able to obtain the client address.</p> <p>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).</p> <p><strong>Recommendations</strong></p> <ul> <li><p><strong>Servlets</strong>: If you are deploying your JAX-WS webservice in a servlet you can always use the call to get the property <code>MessageContext.SERVLET_REQUEST</code> no matter what version of JAX-WS 2 you are using.</p></li> <li><p><strong>Applications</strong>: If you are deploying your JAX-WS webservice in an application you can always use the call <code>HttpExchange exchange = (HttpExchange)msgx.get("com.sun.xml.ws.http.exchange");</code> 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 symbolic <code>JAXWSProperties.HTTP_EXCHANGE</code>. </p></li> </ul> <p>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.</p> <p>I hope the JAX-WS team correct the issue sometime and restore the value of <code>JAXWSProperties.HTTP_EXCHANGE</code> to the useful value again.</p> <p>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 :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload