aspose file tools*
The moose likes Web Services and the fly likes NullPointerException Trying to Get RemoteIP (why?) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "NullPointerException Trying to Get RemoteIP (why?)" Watch "NullPointerException Trying to Get RemoteIP (why?)" New topic
Author

NullPointerException Trying to Get RemoteIP (why?)

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
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
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
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
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
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
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
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

Joined: Oct 04, 2006
Posts: 2198
    
    1
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
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
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
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
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

Joined: Oct 04, 2006
Posts: 2198
    
    1
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
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
Thanks. Appreciate your replies.
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: NullPointerException Trying to Get RemoteIP (why?)