Expand my Community achievements bar.

SOLVED

Obtaining shortened path

Avatar

Level 2

Hello community , 

I am trying to obtain shortened path in my sling model using resource resolver mapping . I also have etc/map in place that shortens it to domain specific urls

Eg: path="/content/mywebsite/wknd"

I injected resource resolver and when i try resourceResolver.map(path) i get result as http://localhost:4502/wknd
However i just want the shortened path without domain, i.e, /wknd , is there a way to achieve this without string manipulation ?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @Zendarkke ,

 

     String mappedUrl = resourceResolver.map(path);
        try {
            URI uri = new URI(mappedUrl);
            mappedUrl= uri.getPath();
        } catch (URISyntaxException e) {
            e.printStackTrace();
            mappedUrl="";// or you may assign null whatever required
        }

 

Thanks

 

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @Zendarkke ,

 

     String mappedUrl = resourceResolver.map(path);
        try {
            URI uri = new URI(mappedUrl);
            mappedUrl= uri.getPath();
        } catch (URISyntaxException e) {
            e.printStackTrace();
            mappedUrl="";// or you may assign null whatever required
        }

 

Thanks

 

Avatar

Community Advisor

Hi @Zendarkke 
Could you please check the page source? resourceResolver.map(path) methods seems correct to obtain the map path.

 

here is the explanation:

1. map(String resourcePath)

This method takes a simple resource path (a String) as its input and returns the mapped URL based on the Sling resource mapping configuration. It doesn't take into account the context of the current request or the full URL; it only considers the resource path.

Example:

Suppose you have a resource stored at the path /content/mywebsite/en/about-us and a mapping configuration that rewrites /content/mywebsite to /mywebsite.

  • Input: /content/mywebsite/en/about-us
  • Output: /mywebsite/en/about-us
ResourceResolver resolver = ...; // get the resource resolver
String resourcePath = "/content/mywebsite/en/about-us";
String mappedPath = resolver.map(resourcePath);
System.out.println(mappedPath); // Output: /mywebsite/en/about-us

 

In this case, the map(String resourcePath) method simply applies the configured mappings to the resource path.

2. map(HttpServletRequest request, String resourcePath)

This method is more context-aware. It takes both the current HttpServletRequest and the resource path as inputs. It considers the full context of the request, including the scheme (HTTP/HTTPS), hostname, and any other request-specific details when mapping the resource path to an external URL.

Example:

Given the same resource path /content/mywebsite/en/about-us, if you call this method with a request context, it will also consider the protocol, hostname, and other request information.

ResourceResolver resolver = ...; // get the resource resolver
HttpServletRequest request = ...; // get the current request
String resourcePath = "/content/mywebsite/en/about-us";
String mappedUrl = resolver.map(request, resourcePath);
System.out.println(mappedUrl); // Output: https://www.example.com/mywebsite/en/about-us

 

In this case, the map(HttpServletRequest request, String resourcePath) method generates a fully qualified URL that includes the scheme and domain, which is useful when you need to generate absolute URLs based on the current request context.



Arun Patidar