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