Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would you mind posting the code you're using to do this? It might come in handy.
 
Arpit Goel
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(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.
reply
    Bookmark Topic Watch Topic
  • New Topic