I am using PrettyFaces in my JSF application. The site requires authentication to access some pages, so I'm using a listener (prerender view) that checks whether the user is logged in. So, if the user tries to access /foo (/foo.jsf before PrettyFaces), I redirect to /login.
However, I want to redirect them to their initial destination, so I want to attach a request parameter "next" so that I redirect the user to /login?next=/foo instead. Unfortunately, I can't get the original requestURI from the request object, the uri string in the following code is /appname/foo.jsf instead of /appname/foo
ctx = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request = (HttpServletRequest) ctx.getRequest();
String uri = request.getRequestURI();
Is there a way to retrieve the original URI path?
PrettyFaces uses under the covers RequestDispatcher#forward()
to forward a pretty URL to the real URL. Using this technique, the original request URI is available as request attribute with the key RequestDispatcher#FORWARD_REQUEST_URI
.
So, this should do:
String originalURI = (String) externalContext.getRequestMap().get(RequestDispatcher.FORWARD_REQUEST_URI);
// ...
Use this code to get the original request URL:
PrettyContext.getCurrentInstance().getRequestUrl().toURL()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With