Expand my Community achievements bar.

SOLVED

Get resource path from full url

Avatar

Level 4

Hi All,

I am implementing login component in which I want to support goto parameter for example

https://myDomain/login.html?goto=https://myDomain/securePage.html.

I am able to get the redirect path in my servlet from the query parameter but from query parameter how do i get the resource path? I was hoping resolve method might come handy but it does not seem to work :(. Anything more elegant that string manipulation to solve this problem?

final String gotoPath = xssApi.filterHTML(request.getParameter(gotoParam)); if (StringUtils.isNotEmpty(gotoPath)) { final Resource gotoPathResource = request.getResourceResolver().resolve(gotoPath); if (null != gotoPathResource && null != gotoPathResource.adaptTo(Page.class)) { response.sendRedirect(gotoPath); return; } }

Above code is giving resource as non existent

Thanks!

Shehjad

1 Accepted Solution

Avatar

Correct answer by
Level 4

I have changed code to below to make it work.

final String gotoPath = xssApi.filterHTML(request.getParameter(gotoParam)); final URI gotoUri = new URI(gotoPath, false); if (StringUtils.isNotEmpty(gotoPath)) { final Resource gotoPathResource = request.getResourceResolver().resolve(gotoUri.getPath()); if (null != gotoPathResource && null != gotoPathResource.adaptTo(Page.class)) { response.sendRedirect(gotoPath); return; } } redirect(request, response, redirectPath);

View solution in original post

3 Replies

Avatar

Level 8

I'm assuming that you have some rewriting going on to handle the URL without /content/<path>/, so, you could simply take your go to parameter, strip out the https://<domain> and use what is left over (minus the .html)

So, https://www.mydomain.com/mypage.html would become "mypage", you know the content path, so prepend it and the string becomes /content/<path>/mypage and then get the resource.

Avatar

Level 4

Asling Thanks for your reply, yes i understand i can do some string manipulation but I don't find that be a elegant way :)

Yes we have mapping to map short urls as you mentioned in your comment

Avatar

Correct answer by
Level 4

I have changed code to below to make it work.

final String gotoPath = xssApi.filterHTML(request.getParameter(gotoParam)); final URI gotoUri = new URI(gotoPath, false); if (StringUtils.isNotEmpty(gotoPath)) { final Resource gotoPathResource = request.getResourceResolver().resolve(gotoUri.getPath()); if (null != gotoPathResource && null != gotoPathResource.adaptTo(Page.class)) { response.sendRedirect(gotoPath); return; } } redirect(request, response, redirectPath);