GeeCON Prague 2014*
The moose likes Web Services and the fly likes How do I get remote IP Address in My JAX-RPC Handler ?    Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Web Services
Bookmark "How do I get remote IP Address in My JAX-RPC Handler ?    " Watch "How do I get remote IP Address in My JAX-RPC Handler ?    " New topic
Author

How do I get remote IP Address in My JAX-RPC Handler ?

Arpit Goel
Greenhorn

Joined: Nov 03, 2008
Posts: 9
I have implemented my JAX-RPC Web Service and below is interface for my Service Class.

package com.xyz

public interface MyService extends java.rmi.Remote {
public com.xyz.MyResponse getMyResponse(com.xyz.MyRequest request) throws java.rmi.RemoteException;
}

I have written a handler by implementing interface javax.xml.rpc.handler.Handler. Now I need to get client IP address in this handler in init() or handleRequest() method. How do I get ref to HttpServletRequest ? I see couple of solution using ServiceLifeCycleContext but those are not in Handler.

void init(HandlerInfo config)

Please advice. Thanks


Arpit Goel
Arpit Goel
Greenhorn

Joined: Nov 03, 2008
Posts: 9
I found that there is no direct way to get refrence to HttpServetRequest in Jax-RPC Handler , hence I have used ServiceLifeCycle interface's init() method to access HttpServletRequest object.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42034
    
  64
Would you mind posting the code you're using to do this? It might come in handy.


Ping & DNS - my free Android networking tools app
Arpit Goel
Greenhorn

Joined: Nov 03, 2008
Posts: 9
Sure!


Below is JAX-RPC Service class as generated by WebSphere 5.1 .

****************START**********************
package com.xyz

public interface MyService extends java.rmi.Remote {
public com.xyz.MyResponse getMyResponse(com.xyz.MyRequest request) throws java.rmi.RemoteException;

// Service implementation goes here
}

}
*************** END ******************

Then Modify generated Service code to add servic life cycle as Belwo.

****************START***********************
package com.xyz

public interface MyService extends java.rmi.Remote , javax.xml.rpc.server.ServiceLifeCycle {

private javax.xml.rpc.server.ServletEndpointContext cyx=null;

public com.xyz.MyResponse getMyResponse(com.xyz.MyRequest request) throws java.rmi.RemoteException;
}


// Add new method as per contract

public void init(Object obj){

HttpServletRequest req = null;
try {
req = (HttpServletRequest) ctx.getMessageContext().getProperty (com.ibm.ws.webservices.engine.transport.http.HTTPConstants.MC_HTTP_SERVLETR EQUEST);
System.out.println("** Finally Got Client IP Address *** " +
req.getRemoteAddr());
} catch (JAXRPCException e) { // Not an HTTP based
//endpoint
}
} // End Of init()

public void destroy(){

}



****************END*********************

It works and I get remote client IP address. Please let me know if any one has any improvement comments.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42034
    
  64
Thanks for posting that. Unfortunately, it uses WebSphere-specific features, whereas I was hoping for something that's independent of the implementation.

Looking at the javadocs of ServiceLifecycle, I see no indication whether it will (or should) be called for each WS call (which would imply that each call results in the creation of a service object - something one may wish to avoid, or at least keep in mind).

Have you tried whether this works with successive calls from different IP addresses?
Arpit Goel
Greenhorn

Joined: Nov 03, 2008
Posts: 9
(1)Actually I couldnt find generic solution for this. Currently I am using WebSphere server. I am trying to locate some vendor independent solution.

(2) I was reading JAX RPC specification from SUN and got below explanation for the point highlighted by you regarding instances creation.

Instances of a specific service endpoint class are considered equivalent when not servicing a client invoked remote method invocation. A service endpoint instance does not maintain any client specific state across remote method invocations. So service endpoint instances are capable of pooling by the JAX-RPC runtime system. The JAXRPC runtime system can invoke a remote method on any available and method ready instances of a specific service endpoint class.

(3) Regarding IP Address,I have tried this from different client's IP address , it prints IP address correctly.
 
GeeCON Prague 2014
 
subject: How do I get remote IP Address in My JAX-RPC Handler ?