File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Services Certification (SCDJWS/OCEJWSD) and the fly likes The remote IP Address Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Services Certification (SCDJWS/OCEJWSD)
Bookmark "The remote IP Address" Watch "The remote IP Address" New topic
Author

The remote IP Address

haitham raik
Greenhorn

Joined: Aug 08, 2004
Posts: 17
Hi all

I have created a web service and I want the remote address of the request to log it in a file. any body can help me to get the remote address.

thanks


Haitham Raik<br />SCJP 1.4 SCWCD 1.4 SCBCD 1.3 SCJA 1.0
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42934
    
  68
If you're using Axis, then the Web Services FAQ -linked in my signature- shows how to retrieve the HttpServletRequest, which has the getRemoteAddr and getRemoteHost methods.
haitham raik
Greenhorn

Joined: Aug 08, 2004
Posts: 17
Thank you. but I am using the IBM WSAD not AXIS. and I am looking to make my projects vendor-independent.
haitham raik
Greenhorn

Joined: Aug 08, 2004
Posts: 17
Dear all

I guess that I found a way to get the remote IP address for a JSE (Java service endpoint) but this way is not useful if you are using the session bean as a webservice. this way depends on the idea that the JSE is wrapped by a JAX-RPC servlet as the following figure:


{client side} {server side}
============================= =================================
= ---------- ------ = = ----------- -------- =
= - Client - uses -stub- = soap = - JAX RPC - delegate - JSE - =
= - side - ------> - - = req = - servlet - ------> - - =
= ---------- ------ = ----> = ----------- -------- =
============================= =================================

by adding a javax.servlet.Filter to the JAX-RPC servlet I have an instance of the ServletRequest which I can use to get the remote address as the following:
public MyFilter implememts Filter{
...
public void doFilter(ServletRequest req, ServletResponse rs, FiterChain c){
String ipAddr = req.getRemoteAddr();
...
}
...
}

then add this address to the session as the following:
public MyFilter implememts Filter{
...
public void doFilter(ServletRequest req, ServletResponse rs, FiterChain c){
String ipAddr = req.getRemoteAddr();
request.getSession().setAttribute("ipAddress", ipAddr);
...
}
...
}

in the web service class implements the javax.xml.rpc.server.ServiceLifecycle interface which has two methods:
1) init(java.lang.Object context)
2) destroy()

in the init method the Object param is an instance of javax.xml.rpc.server.ServletEndpointContext which has a method called getHttpSession() which can be used to get the session instance that has been created in the filter. so by getting the session instance we can get the ipaddress attribute. as the following:

public class WebService1_Impl implements WebService1_Interface, ServiceLifecycle{
ServletEndpointContext ctx;
public void init(Object obj){
ctx = (ServletEndpointContext) obj;
}

public void theServiceMethod(){
String ipAddr=(String)ctx.getHttpSession().getAttribute("ipAddress");
System.out.println(ipAddr);
}

public void destroy(){
}
}

Regards
haitham raik
Greenhorn

Joined: Aug 08, 2004
Posts: 17
sorry every body the spaces has been removed and the figure has been damaged but the main idea from the figure is to say that the client side uses the stub which in turn sends a soap message to the JAX-RPC servlet. this servlet has the ability to read the SOAP message and make a method invokation on the web service endpoint:

client side -> stub -> JAX-RPC servlet -> JSE

Regards
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
Looks to me like you're almost there.

Now use the ServletEndpointContext.getMessageContext() and set a property with MessageContext.setProperty to store the address.
In the Session bean use SessionContext.getMessageContext().getProperty() to retrieve the address.
haitham raik
Greenhorn

Joined: Aug 08, 2004
Posts: 17
Dear Peer Reynders

you have said "Now use the ServletEndpointContext.getMessageContext() and set a property with MessageContext.setProperty to store the address." what do you mean by now and now relative to what and where can I use ServletEndpointContext.getMessageContext() to set the property. and another thing the getMessageContext method is not a static method.

can you please explain more?

thanks in advance.
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
Ultimately the SOAPMessageContext that you can get from the SessionContext's getMessageContext is the only way to any additional information to the SessionBean. A SOAP Handler could manipulate the incoming message - but I can't find a way to access HTTP request specific information in a SOAP handler.

Whats even more depressing is that an EJB Service Endpoint seems to bypass the Web container filter chain. I managed to set up a situation where the EJB endpoint is at /hello-ejb/hello. The SOAP client works fine. At the same time I'm running a filter on /hello-ejb/* Accesses with browser are intercepted with the filter - accesses with the SOAP client are not.

Otherwise you could hack the incoming SOAP message in a Filter by splicing additional information into the SOAP Header (or splicing one in if absent).

So it seems that all you can do is to create a JSE and then call the EJB from your JSE service method (supplying any information you wish through additional parameters).
[ October 28, 2005: Message edited by: Peer Reynders ]
 
wood burning stoves
 
subject: The remote IP Address