If a java client calls a remote EJB on a different server, how can you get the client IP address? Note that it is important to get it from the server, because the client is likely behind a NAT firewall, and in this case we need the public IP address.
NOTE: Although it would preferably be a generic solution, at a minimum I could use one that retrieves the IP address from an EJB2 call on JBoss 4.2.2
Thanks to MicSim, I learned that the thread name stores the IP address. In JBoss 4.2.2 the thread name for EJB2 items look like this:
(I assume the http is optional, depending on the protocol actually used).
This can then be parsed with a regular expression as so:
Have you tried: java.rmi.server.RemoteServer.getClientHost() ?
http://java.sun.com/j2se/1.5.0/docs/api/java/rmi/server/RemoteServer.html#getClientHost()
This article on the JBoss community wiki addresses exactly your issue. Prior to JBoss 5 the IP address apparently has to be parsed from the worker thread name. And that seems to be the only way to do it on earlier versions. This is the code snippet doing it (copied from the above link):
I believe that name of the current worker thread contains an IP address of the server, but not the client's IP since threads are pooled rather than created for each call. In JBoss 4, one can use the following workaround to obtain an IP address of the client: