Skip to main content
Announcements
See what Drew Clarke has to say about the Qlik Talend Cloud launch! READ THE BLOG
cancel
Showing results for 
Search instead for 
Did you mean: 
Alpha549
Creator II
Creator II

Get client IP address when request

Hello everyone !

I would like to be able to do a simple thing : get the client ip address when requests ?

I searched for it but I didn't find relevant results...

 

Thanks !

Labels (2)
1 Solution

Accepted Solutions
Alpha549
Creator II
Creator II
Author

Stupid mistake... I put the code in a tJava instead of a tJavaRow. It was logical...

The problem is solved. I put here a complete and simple example of the solution for those who also want to get the IP of the service caller 0683p000009MA9p.png

 

Thank you vapukov !

 

The code of the tJavaRow :

//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.
//Display the IP Address
System.out.println(messageContext.getHttpServletRequest().getRemoteAddr());

//Save the IP in the schema
output_row.ipadr = messageContext.getHttpServletRequest().getRemoteAddr().toString();

Pictures :

 

0683p000009M38G.pngThe whole job0683p000009M2wk.pngtRestRequest0683p000009M38L.pngtRestRequest schema0683p000009M38Q.pngtXMLMap0683p000009M38V.png

 

0683p000009M2zh.pngConsole of the studio, the ip address is displayed0683p000009M38a.pngIn the browser 0683p000009MACn.png

View solution in original post

8 Replies
vapukov
Master II
Master II

Alpha549
Creator II
Creator II
Author

Hello

I found this page before but when I try this code in a tJava, I have a NullPointerException at the second line (with the comment //The acquire the Message Context)... 😕
vapukov
Master II
Master II

can't say anything without information

 

from studio:

Endpoint deployed at: http://127.0.0.1:8090/services/customers
[statistics] connecting to socket on port 3768
[statistics] connected
[WARN ]: org.eclipse.jetty.server.handler.AbstractHandler - No Server set for org.apache.cxf.transport.http_jetty.JettyHTTPServerEngine$1@587d83f6
Client ip - 127.0.0.1

from runtime:

2/15/19 2:50 PM | INFO | 248 - Talend ESB :: Job :: Controller | Adding job DemoREST. | 
2/15/19 2:50 PM | INFO | 248 - Talend ESB :: Job :: Controller | Starting job DemoREST | 
Client ip - 0:0:0:0:0:0:0:1
Alpha549
Creator II
Creator II
Author

Could you send a screen of your test job using this tJava code please?
vapukov
Master II
Master II


@Alpha549 wrote:
Could you send a screen of your test job using this tJava code please?

we are looking for errors in my code or in yours? 🙂

 

P.S.

I just copy past code and edit print message

Alpha549
Creator II
Creator II
Author

Stupid mistake... I put the code in a tJava instead of a tJavaRow. It was logical...

The problem is solved. I put here a complete and simple example of the solution for those who also want to get the IP of the service caller 0683p000009MA9p.png

 

Thank you vapukov !

 

The code of the tJavaRow :

//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.
//Display the IP Address
System.out.println(messageContext.getHttpServletRequest().getRemoteAddr());

//Save the IP in the schema
output_row.ipadr = messageContext.getHttpServletRequest().getRemoteAddr().toString();

Pictures :

 

0683p000009M38G.pngThe whole job0683p000009M2wk.pngtRestRequest0683p000009M38L.pngtRestRequest schema0683p000009M38Q.pngtXMLMap0683p000009M38V.png

 

0683p000009M2zh.pngConsole of the studio, the ip address is displayed0683p000009M38a.pngIn the browser 0683p000009MACn.png

vapukov
Master II
Master II

great! 

 

welcome onboard! 🙂

Lakshmi_M
Contributor
Contributor

Hi,

 

Can anyone help to get ip address for a soap service.

The above one is for rest.