• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NullPointerException Trying to Get RemoteIP (why?)

 
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to get the client IP for one of the methods in the web service, but the stated directions I've found don't seem to work.

Here's what I've done...

At the @WebService level, I've defined these:

@HandlerChain(file="handler-chain.xml")

// Enable dependency injection on web service context.
@Resource
WebServiceContext wsContex

----

Then, in the web service method, I try to get the message context like this:

@WebMethod
public ....
{
MessageContext msgCtxt = wsContext.getMessageContext();
...

}

===

The handler-chsin.xml file has this:



------

And the event MessageHandler code itself has this:



=====================

However, each time I run this from the client side, I get a NullPointerException on the ' MessageContext msgCtxt = wsContext.getMessageContext();' line of code.

I thought the @Resource would dependency-inject the context....???

So, how do I correctly get the client's IP?

I can't seem to find the documentation beyond what I've done here.

Do I need to re-generate the client or other code? The web service itself is automatically deployed.

Thanks in advance.

Mike


 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To clarify all the detail above, what I'm trying to do is the same thing as in this person's posting:

http://stackoverflow.com/questions/1641594/geting-the-ip-address-of-a-client-for-a-webservice

However, I get a NullPointerException on the line of code:

MessageContext mc = wsContext.getMessageContext();


Does anyone have....any ideas how to make this work?

Thanks.

-- M
 
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
Your code does look correct. What web service stack are you deploying to?
An idea that may help you to determine whether injection of the WebServiceContext takes place is to implement a (non-public) setter method which takes a parameter of the type WebServiceContext and that is annotated with the @Resource annotation. In the method you can add some form of logging that prints a message and the reference to the WebServiceContext (and, of course, stores the reference in a suitable instance field).
Also make sure that you import the javax.xml.ws.WebServiceContext and no other interface or class.
Finally, for reference, you may want to take a look at section 5.3 in the JAX-WS 2.1 specification.
Best wishes!
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ivan,

Thanks for your great reply.

This is a simple JAX-WS web service running on Tomcat 6.0.20.

I like your idea about trying to verify the injection is happening, even though it seems it isn't.

Thanks!

Mike
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
Since I finally have taken the time to install Metro into a Tomcat 6 installation, I tried to add the following in a Java-first endpoint implementation class:

In a web service method I then added the code from the page you provided a link to - the code that is supposed to print the IP of the client.
Sad to say(?), there are no problems whatsoever; the WebServiceContext is injected when the web service is deployed/started and when there is a request to the operation in question, it does print the IP (127.0.0.1) from where the request came.
Have you tried to run your web service without the handler, to see if it is causing any problems?
Best wishes!
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, I got rid of all the handler logic since at least the injection into the web service should happen correctly.

I think I'll have to create a brand new example and, if it doesn't work, I'll post all the code here.

Thanks very much...hope you check out the ws code when I post it.

-- Mike
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, just a thought here...

After I added the @Resource and the code to receive the context object, did I need to regenerate the web service or WSDL?

If so, that could be the problem.

- Mike
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I have only tried Java-first, without a WSDL or any deployment descriptors, on Tomcat 6 with Metro.
After having added the @Resource annotation, you definitely need to re-deploy the web service, but I cannot see how it would affect the WSDL.
If you don't have a WSDL, a new WSDL will be generated automatically when you deploy the web service.
Best wishes!
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Appreciate your replies.
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I noticed when I added the @Resource element and re-deployed (using MyEclipse), the jaxws folder did not get updated on the server. I also tried re-creating the web service, but I still get the NullPointerException when I try to reference the context.

Hmmmm......

-- Mike
 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic