cancel
Showing results for 
Search instead for 
Did you mean: 

How to get the consumer IP address : EJB exposed as web service

Sharadha1
Active Contributor
0 Kudos

Hi,

We have a ejb which is exposed as webservice in NW 7.3. We would like to know the ip of the caller who consumes the webservices. I cannot find any standard interfaces/class which can be accessed from ejb to read http headers.

Are there any methods available to read the http headers so that we can get the ip address of the consumer?

Thanks,

Sharadha

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos
@WebService()
public class Test {
@Resource WebServiceContext context;
@WebMethod(operationName = "getInfo")
public String getInfo() {
HttpServletRequest request = (HttpServletRequest)context.getMessageContext() .get(MessageContext.SERVLET_REQUEST);
return "IP: " + request.getRemoteAddr() + ", Port: " + request.getRemotePort() + ", Host: " + request.getRemoteHost(); 
} }
Sharadha1
Active Contributor
0 Kudos

Thanks Ahmed. I tried with this code but getting the following  exception. I am not able to compile the code.

javax.xml.ws.WebServiceContext is an interface. jaxb cannot handle interfaces

Any suggestions?

vijay_kumar49
Active Contributor
0 Kudos

try with below code...

import javax.servlet.http.HttpServletRequest;

@GET

@Path("/yourservice")

@Produces("text/xml")

public void activate(@Context HttpServletRequest requestContext,@Context SecurityContext context){

   String yourIP = requestContext.getRemoteAddr().toString();

   //If security is enabled

   Principal principal = context.getUserPrincipal();

   String userName = principal.getName();

}

-----------------------------------------------------

private SessionContext sessionContext;

     public void setSessionContext(SessionContext context)

     {

          sessionContext = context;

     }

public void method()

{

javax.xml.rpc.handler.soap.SOAPMessageContext msgCntxt = (SOAPMessageContext)sessionContext.getMessageContext();

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

}