Five Stars

How to get an IP from the service caller

I created a service in Talend job and I wonder is it possible to retrive the caller's(the one who is calling the service) IP from inside the service?

  • ESB
1 ACCEPTED SOLUTION

Accepted Solutions
Ten Stars

Re: How to get an IP from the service caller

I was intrigued by this, so followed the link supplied by @TRF and tried it out. It works. Here is the code I used in a tJava to return the IP address using a System.out call....

 

//You need to retrieve the restRequest Map from the globalMap
java.util.Map request = (java.util.Map)globalMap.get("restRequest");

//The acquire the MessageContext from that Map
org.apache.cxf.jaxrs.ext.MessageContext messageContext = (org.apache.cxf.jaxrs.ext.MessageContext)request.get("MESSAGE_CONTEXT");

//The acquire the the HttpServletRequest object and call the getRemoteAddr method from that.
System.out.println(messageContext.getHttpServletRequest().getRemoteAddr());
Rilhia Solutions
7 REPLIES
Nine Stars TRF
Nine Stars

Re: How to get an IP from the service caller

Nine Stars TRF
Nine Stars

Re: How to get an IP from the service caller

So, does this helps?

In this case, thanks to mark the topic as solved.

Kudos also accepted!


TRF
Nine Stars

Re: How to get an IP from the service caller


TRF wrote:

Does this helps?

https://stackoverflow.com/questions/3825084/rest-how-get-ip-address-of-caller


thanks for idea, but - are You try to do this?

-----------
Ten Stars

Re: How to get an IP from the service caller

I was intrigued by this, so followed the link supplied by @TRF and tried it out. It works. Here is the code I used in a tJava to return the IP address using a System.out call....

 

//You need to retrieve the restRequest Map from the globalMap
java.util.Map request = (java.util.Map)globalMap.get("restRequest");

//The acquire the MessageContext from that Map
org.apache.cxf.jaxrs.ext.MessageContext messageContext = (org.apache.cxf.jaxrs.ext.MessageContext)request.get("MESSAGE_CONTEXT");

//The acquire the the HttpServletRequest object and call the getRemoteAddr method from that.
System.out.println(messageContext.getHttpServletRequest().getRemoteAddr());
Rilhia Solutions
Nine Stars

Re: How to get an IP from the service caller

thank You!

 

java.util.Map request = (java.util.Map)globalMap.get("restRequest");

this is was - what I not guess

-----------
Ten Stars

Re: How to get an IP from the service caller

I had to trawl through the code in the code tab for a while to find it. Most things can be found in the globalMap though :-)

Rilhia Solutions
Five Stars

Re: How to get an IP from the service caller

Thanx, I will test it when I have the time and if successful I will mark it as resolved.