Expand my Community achievements bar.

SOLVED

hostname or ip address of the system we are triggering a request (aem6.5)

Avatar

Level 8

Hi,

 

I will be uploading an  image to aem server using a service that aem or coustom code provides (www.abc.com/upload/image.json)

 

Now this request is triggered from https://uploadservice.com . How could i get the host name in aem servlet (uploadservice.com) and ip address of this when it is triggred from (uploadservice.com).

 

What will code for this??

 

 

Thanks

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

What is this request? How will it invoke the above code if you access the URL.

Can you please provide info how are you integrating this code?

 

Thanks!

View solution in original post

4 Replies

Avatar

Community Advisor

Hi @srinivas_chann1 ,

 

Host name - Host name can be obtained from the SlingHTTPServletRequest object header "referer". Sample - 

request.getHeader("referer");

 

IP address - There are few methods that help here like getRemoteAddr()/getRemoteHost() of SlingHTTPServletRequest object or the request header value "X-Forwarded-For". But, they can't be entirely relied on, because for most of the scenarios you may be receiving the IP address of the Default Gateway or proxy.

 

Thanks,

Fani

Avatar

Community Advisor

Hi @srinivas_chann1 

 

Please use the below code. I use it and it works perfectly fine.

 

public static String getClientIPAddress(HttpServletRequest request) {
String xForwardedForHeader = request.getHeader(HttpHeaders.X_FORWARDED_FOR);
if (xForwardedForHeader == null) {
return request.getRemoteAddr();
} else {
return new StringTokenizer(xForwardedForHeader, ",").nextToken().trim();
}
}

 

Thanks! 

Avatar

Level 8

Hi ,

I tried it my local with the above code using postman  to trigger the reuqest( http://localhost:4502/upload/image.json) .In the logs it does not print any value for this ??

 

What could be the reason

 

Thanks

Avatar

Correct answer by
Community Advisor

What is this request? How will it invoke the above code if you access the URL.

Can you please provide info how are you integrating this code?

 

Thanks!