Hmm Sorry Jason,
I might have had to notice that I am extending SlingSafeMethodServlet but also
implementing the following servlet:
https://docs.adobe.com/docs/en/cq/5-6-1/javadoc/com/day/cq/commons/servlets/NonExistingResourceServlet.html
And as it states in the docs itself, it also says:
public boolean accepts(SlingHttpServletRequest request) {
// get non-existing path (incl. selectors and extension!)
String path = request.getResource().getPath();
// return true if this servlet can handle this path
return true;
}
So probably it’s not possible to do it through this interface in without
manually parseing :)
It also says in the docs: Please note: This is a temporary solution until Sling
provides a built-in mechanism for this use case. Not to be used by client
implementations!
Any clue if it is possible yet to do it with standard sling, the thing I try to
achieve is a servlet that also catches resources with a selector AND suffix (as
stated in my example of course) ?
Thanks!
Roy
> On 28 Jul 2016, at 18:35, Jason Bailey <[email protected]> wrote:
>
> I'm under the understanding that it is;
>
> request.getResource().getPath()
>
> -----Original Message-----
> From: Roy Teeuwen [mailto:[email protected]]
> Sent: Thursday, July 28, 2016 12:31 PM
> To: [email protected]
> Subject: Getting the actual resource from a request
>
> 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