Get Absolute content path from short URL | Community
Skip to main content
Level 2
Dec 21, 2020 8:49 AM UTC
Solved

Get Absolute content path from short URL

Hi Guys,

 

We are using etc mapping to shorten the URL's, We have a servlet where we will pass shortened URL

 

Param = "/en/testPage.html"

 

is there any way i can get absolute content path in java given short URL?

 

Excepted output : "/content/mywebsite/en/testPage.html"

 

Ay input helps, Thanks in advance!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Kunal_Gaba_
Kunal_Gaba_Accepted solution
New Participant
Dec 21, 2020 9:37 AM UTC

You can get the reference to a resource from a shortened absolute path. The API method for that is resolve() which does a reverse mapping of path to resource - https://helpx.adobe.com/experience-manager/6-5/sites/developing/using/reference-materials/javadoc/org/apache/sling/api/resource/ResourceResolver.html#resolve-java.lang.String- 

After you get reference to a resource, you can call resource.getPath() to get the full path of the resource in repository. 

 

For examples see this thread-  https://stackoverflow.com/questions/21105300/cq5-how-to-programmatically-find-out-the-resource-given-a-url 

10 replies

Manjunath_K
Level 7
Dec 21, 2020 3:22 PM UTC

Hi @vinodprathipati 

Please try following steps

 

Access Resource Resolver console http://localhost:4502/system/console/jcrresolver

 

To get absolute path using relative/shorten url: add shorten url & click on resolve.

https://www.test.com/en/testPage   ->  /content/mywebsite/en/testPage   //make sure you are passing complete path with domain name as per your etc mapping.

same value you can get in backend using

resourceResolver.resolve("https://www.test.com/en/testPage") //returns /content/mywebsite/en/testPage

 

 

To get relative/shorten url using absolute path: add complete path & click on map.

/content/mywebsite/en/testPage -> https://www.test.com/en/testPage

same value you can get in backend using

resourceResolver.map("/content/mywebsite/en/testPage") //returns https://www.test.com/en/testPage

 

Hope this helps!

 

 

Translate
Level 2
Dec 21, 2020 6:20 PM UTC

Hi,

 

i checked in jcrresolver and i am able to resolve my path and getting full content path.

for e.g."https://mydomain/en/test.html" is resolving into "/content/global/eb/test.html"

 

But from java i am not able to resolve URL properly(in servlet)

"request.getResourceResolver.resolve(https://mydomain/en/test.html).getPath()

" is giving output as "https://mydomain/en/test.html" instead of "/content/global/eb/test.html", Please let me know if i am missing anything. Thanks

 

Translate
BrianKasingli
New Participant
Dec 21, 2020 3:21 PM UTC

With a servlet, you can do something like this:

 

@3214626 private ResourceResolver resourceResolver; ... String mappedPath = resourceResolver.map("/content/mywebsite/en/testPage");

 


https://sling.apache.org/apidocs/sling8/org/apache/sling/api/resource/ResourceResolver.html

map(String resourcePath)
Returns a path mapped from the (resource) path applying the reverse mapping used by the resolve(String) such that when the path is given to the resolve(String) method the same resource is returned.
Translate
Level 2
Dec 21, 2020 6:30 PM UTC
Hi, i checked in jcrresolver and i am able to resolve my path and getting full content path. for e.g."https://mydomain/en/test.html" is resolving into "/content/global/eb/test.html" But from java i am not able to resolve URL properly(in servlet) "request.getResourceResolver.resolve(https://mydomain/en/test.html).getPath() " is giving output as "https://mydomain/en/test.html" instead of "/content/global/eb/test.html", Please let me know if i am missing anything. Thanks
Translate
Asutosh_Jena_
New Participant
Dec 21, 2020 11:00 AM UTC

@vinodprathipati You can use reverse mapping to get the actual content path.
resourceResolver.resolve() - This will give the resource with actual path and using the getPath() on this resource you can get the full path of the resource.

 

Resource resource = resourceResolver.resolve("shortpath");
String fullPath = resource.getPath();

Translate
Level 2
Dec 21, 2020 1:06 PM UTC

i tried it but fullPath is not coming up.

path = "/en/test.html";

String url = request.getParameter(PATH);
Resource resource = request.getResourceResolver().resolve(url);
String fullPath = resource.getPath();

 

i am getting fullPath as /en/test.html, let me know if i am missing something. etec mapping were already in place for publishers.

Translate
Kunal_Gaba_
Kunal_Gaba_Accepted solution
New Participant
Dec 21, 2020 9:37 AM UTC

You can get the reference to a resource from a shortened absolute path. The API method for that is resolve() which does a reverse mapping of path to resource - https://helpx.adobe.com/experience-manager/6-5/sites/developing/using/reference-materials/javadoc/org/apache/sling/api/resource/ResourceResolver.html#resolve-java.lang.String- 

After you get reference to a resource, you can call resource.getPath() to get the full path of the resource in repository. 

 

For examples see this thread-  https://stackoverflow.com/questions/21105300/cq5-how-to-programmatically-find-out-the-resource-given-a-url 

Translate
Level 2
Dec 21, 2020 1:08 PM UTC
Hi Kunal, i tried it but fullPath is not coming up. path = "/en/test.html"; String url = request.getParameter(PATH); Resource resource = request.getResourceResolver().resolve(url); String fullPath = resource.getPath(); i am getting fullPath as /en/test.html, let me know if i am missing something. etec mapping were already in place for publishers.
Translate