Get Absolute content path from short URL

Avatar

Avatar

vinodprathipati

Avatar

vinodprathipati

vinodprathipati

21-12-2020

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!

mappings

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

kunal23

MVP

Avatar

kunal23

MVP

kunal23
MVP

21-12-2020

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/or... 

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... 

Answers (3)

Answers (3)

Avatar

Avatar

asutosh_jena

Avatar

asutosh_jena

asutosh_jena

21-12-2020

@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();

Avatar

Avatar

Manjunath_K

MVP

Avatar

Manjunath_K

MVP

Manjunath_K
MVP

21-12-2020

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!

 

 

Avatar

Avatar

BrianKasingli

MVP

Avatar

BrianKasingli

MVP

BrianKasingli
MVP

21-12-2020

With a servlet, you can do something like this:

 

@reference
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.