How to get an IP from the service caller

Six 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?


Accepted Solutions
Community Manager

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());

All Replies
Fifteen Stars TRF
Fifteen Stars

Re: How to get an IP from the service caller

Fifteen Stars TRF
Fifteen 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
Forteen 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?

-----------
Community Manager

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());
Forteen 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

-----------
Community Manager

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 :-)

Six 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.

15TH OCTOBER, COUNTY HALL, LONDON

Join us at the Community Lounge.

Register Now

2019 GARNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

An API-First Approach to Modernizing Applications

Learn how to use an API-First Approach to Modernize your Applications

Watch Now

Talend API Designer – Technical Overview

Take a look at this technical overview video of Talend API Designer

Watch Now

Getting Started with APIs

Find out how to get started with APIs

Read