Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

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

srinivas_chann1
Level 6
Level 6

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
asutosh_jena
Correct answer by
Community Advisor
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
fanindras
Level 5
Level 5

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

asutosh_jena
Community Advisor
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! 

srinivas_chann1
Level 6
Level 6

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

asutosh_jena
Correct answer by
Community Advisor
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