Hey all,
I am creating a SlingServlet that will work by both using a selector and a
suffix. The resource is for example /content/dam/image.jpg and the actual url
will be
/content/dam/nice-image.cdn.jpg/modification-date/20160815/nice-image.jpg
What is the most easy way to get the actual resource path again from the
SlingHttpServletRequest? Currently I am doing the following but I find it a bit
cumbersome:
private String getResourcePath(SlingHttpServletRequest request) {
String requestUrl = request.getRequestPathInfo().getResourcePath();
int endIndex =
requestUrl.lastIndexOf(request.getRequestPathInfo().getSuffix());
String resourcePathWithSelector = requestUrl.substring(0, endIndex);
endIndex =
resourcePathWithSelector.lastIndexOf(request.getRequestPathInfo().getSelectorString()
+ "." + request.getRequestPathInfo().getExtension());
return resourcePathWithSelector.substring(0, endIndex) +
request.getRequestPathInfo().getExtension();
}
Is there an easier way or is parsing it like this the only way?
Also after I got the actual resourcePath, I tried doing the following, but this
doesn’t seem to work, any clue on why?
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse
response) throws ServletException, IOException {
RequestDispatcherOptions opts = new RequestDispatcherOptions();
opts.setReplaceSelectors("");
String resourcePath = getResourcePath(request);
RequestDispatcher dispatcher = request.getRequestDispatcher(resourcePath,
opts);
if (dispatcher != null) {
dispatcher.forward(request, response);
}
}
I would expect that the previous would actually just forward it to the actual
image being fetched from the getResourcePath but it just gives me a 404 not
found (I checked the getResourcePath and it does return
/content/dam/nice-image.jpg)
Thanks!
Roy