Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Getting the client ip in a web service call.

843833Apr 27 2005 — edited Jan 9 2006
Hi!

I found a very old post here and tried and looked around some myself in the documentation also, and finally managed to put together some code to do this,
and had problems finding it when I googled for it, so I figured I'd post an example
for others. I'm working under tomcat.

The thing to do is to implement the ServiceLifecycle interface, like so:
public class MyWSImpl implements MyWSIF, javax.xml.rpc.server.ServiceLifecycle {
    private javax.xml.rpc.server.ServletEndpointContext wsctx = null;

    public void init(Object ctx) {
        wsctx = (javax.xml.rpc.server.ServletEndpointContext)ctx;
        System.out.println("INIT CALLED: " + wsctx);
    }

    public void destroy() {
        System.out.println("DESTROY CALLED.");
    }

    private String getRemoteAddr() {
        String remoteHost = "unknown.remote.host";
        try {
            javax.xml.rpc.handler.MessageContext msgCtx = wsctx.getMessageContext();
            javax.servlet.http.HttpServletRequest request = null;
            request = (javax.servlet.http.HttpServletRequest)
                    msgCtx.getProperty("com.sun.xml.rpc.server.http.HttpServletRequest");
                       
            remoteHost = request.getRemoteAddr();
        } catch (Throwable t) { }
        return remoteHost;
    }

    public String reloadConfig(String msg) throws RemoteException {
        String addr = getRemoteAddr();
        System.out.println("Message from " + addr + ": " + msg);
        return "Hi " + addr;
    }
}
Search hits improvement: client ip, remote ip, remote address, client address, jwsdp.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 6 2006
Added on Apr 27 2005
1 comment
89 views