In CQ, ResourceResolver class can be used for resolving a Request into a Sling Resource. However, given a random URL served by a CQ instance, how do you figure out the corresponding Sling Resource of the given URL as a result of Resource Mapping definition? Let's assume we have some Resource Mapping rules (mapping definition) set up on our Author node via /etc/map so that a sample request to, URL: http://dictionary.mycompany.com/definition/visa-validity.html will be served by the resource at, Sling Resource: /content/public/dictionary/legal-terms/v/visa-validity.html You can have as many Resource Mapping rules (mapping definition) as you want. The above mapping is just an example. I want to use this example to demonstrate how did I programmatically figure out the corresponding resource from a given URL. ResourceResolverResourceResolver class was implemented to return Resource. Specifically, the resolve() functions exist for this type of resolution. However, even there are three overloaded resolve() functions, none of them takes in a URL String. Resource resolve(HttpServletRequest request) the thought processGiven ResourceResolver takes in HttpServletRequest as input, if I can transform (adapt) a given URL into a HttpServletRequest using HttpServletRequestWrapper, the problem will be solved. Therefore, I implemented a ResolverRequest class which extends HttpServletRequestWrapper. code samplepackage com.company.util.http; Once you have ResolverRequest class implemented and available to you, you can construct a ResolverRequest from an URL string, and pass the ResolverRequest into resolve() function of ResourceResolver to find the Resource. Problem solved: <?xml version="1.0" encoding="UTF-8"?> [1] Get the value of url parameter from the request (e.g., "http://localhost:4502/apps/company/tools/dispatcher/invalidate?url=http://dictionary.mycompany.com/definition/visa-validity.html")
2 Comments
|
Categories
All
Archives
May 2020
|