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 ?
Solved! Go to Solution.
Views
Replies
Total Likes
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
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
Hi @Zendarkke
Could you please check the page source? resourceResolver.map(path) methods seems correct to obtain the map path.
here is the explanation:
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
.
/content/mywebsite/en/about-us
/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.
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.
/content/mywebsite/en/about-us
within the context of a request to https://www.example.com
https://www.example.com/mywebsite/en/about-us
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.
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies