Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the original request URI with PrettyFaces

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?

like image 261
ustun Avatar asked Oct 12 '11 10:10

ustun


2 Answers

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);
// ...
like image 78
BalusC Avatar answered Oct 18 '22 05:10

BalusC


Use this code to get the original request URL:

PrettyContext.getCurrentInstance().getRequestUrl().toURL()

like image 45
Rajat Gupta Avatar answered Oct 18 '22 03:10

Rajat Gupta