Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

how to read short Urls from long url /content/en in sling servlet. ?

Avatar

Level 1

request.getHeader('referer') returns only long url. 

 

9 Replies

hi,

thanks for the links. But I want to read the shortened url (cut url) available in the browser address bar in my sling servlet or sling models 

Avatar

Employee Advisor

In your example code you are trying to read the "referer" header, which is obviously not what you want. Do you want to read the "long" (probably you mean "unshortened") path of the requested resource or the "long" path of the referer?

 

In any way, the links @Saravanan_Dharmaraj  provided are good ones, check them to understand the mapping.

If you want to reverse the shortening in Java code, you can use these API:

resourceresolver.map (longpath)

resourceresolver.resolver (request, shortpath) 

 

Both return resources, for which you can use "resource.getPath()" to get their actual repository path. See https://sling.apache.org/apidocs/sling11/org/apache/sling/api/resource/ResourceResolver.html

 

 

Avatar

Level 1

hi,

thanks for the reply. I want to read the shortened url which is visible to user in browser address bar (without /content/en ) in my sling servlet.

request.getRequestURL() and getRequestURI,request.getHeader('referer') are returning only long urls /content/en which I dont want. I want the one which is visible to user in my sling servlet or sling model

Avatar

Employee Advisor

In that case you need the

resouceResolver.map()

API call.

Avatar

Community Advisor

Hi,

Could you please explain the use case, I believe in servlet you can shorten the url by chopping off contextpath substring.



Arun Patidar

hi,
So my use case is something like, I have a page with path (say longURL) "http://localhost:4502/content/en/test/temp/apple.html", but the url which is visible to user is (shortURL) "www.apple.com/home". I want to read this shortURL in my servlet.If I use request.getPath or getReferer or getRequestURL are returning longurl, but I need shortURL 

Avatar

Community Advisor

Where are the mappings from long to short? in AEM? How you are transforming links.

/content/en/test/temp/apple.html --> www.apple.com/home

 

you can use the same mapping here to get short url from long url.



Arun Patidar

Avatar

Employee Advisor

If you want to write the short-path as URL within the page itself (or a different page), just write the "long" path there. If a mapping exists AEM will do that for you automatically. At least for pages, not for JSON payloads etc.

 

For these you need to use the ResourceResolver as indicated above.