Skip navigation.
Home

Obtaining web service client IP address

some times it is important to obtain the IP address of a web service client for logging

in case of Servlet End Point
 All what you have to do it the follwing:

 1- Make your class implements ServiceLifecycle inteface
 2- Add ServletEndpointContext variable to your endpoint class "endPtCntxt"
 3- Add init method to your end point class "public void init(Object context) throws ServiceException"
 4- Assign that context object to the declared ServletEndpointContext in setp 2 "endPtCntxt = (ServletEndpointContext)context;"
 5- Now, go to the place where you wnat to obtain the ip and add this code
  MessageContext msgCtx = endPtCntxt.getMessageContext ();
  String remoteAddress = (String)msgCtx.getProperty("REMOTE_ADDR"):

 Here is a complete example:

 import javax.xml.rpc.server.ServletEndpointContext;
 import javax.xml.rpc.server.ServiceLifecycle;
 import javax.xml.rpc.ServiceException;

 public class MyWebServiceEndPoint implements MyWebService, ServiceLifecycle {
 
   ServletEndpointContext endPtCntxt;

   public void init(Object context) throws ServiceException{

       endPtCntxt = (ServletEndpointContext)context;
   }

   public String helloWorld()
   {
  MessageContext msgCtx = endPtCntxt.getMessageContext ();
  String remoteAddress = (String)msgCtx.getProperty("REMOTE_ADDR");
  return remoteAddress; 
  }
 }

2-if you're using EJB endpoint
 1- Add member variable 'SessionContext ejbContext'
 2- add implementation for setSessionContext method
 3- in the set session, do the follwoing

   public void setSessionContext(SessionContext cntxt){
     ejbContext = cntxt;
   }

 4-in your function, do this
   public String helloWorld()
   { 
          javax.xml.rpc.handler.soap.SOAPMessageContext msgCntxt = (SOAPMessageContext)
          ejbContext .getMessageContext();

  String remoteAddress = (String)msgCtx.getProperty("REMOTE_ADDR");
  return remoteAddress; 
   }