Forums de discussion

Liferay URL redirect

Traolly Xiong, modifié il y a 9 années.

Liferay URL redirect

Regular Member Publications: 195 Date d'inscription: 30/12/11 Publications récentes
Hello Community,
I can't find any solution via search, but is recommended way of redirecting end users to a new URL in a Liferay portlet jsp file?

I'm currently using "reqeust.sendRedirect("http://www.nba.com");" and it's not working.

Any ideas?

Thanks.

Traolly Xiong
thumbnail
Olaf Kock, modifié il y a 9 années.

RE: Liferay URL redirect

Liferay Legend Publications: 6396 Date d'inscription: 23/09/08 Publications récentes
if you're using this in a JSP, request will be the HttpServletRequest - just because the Servlet/JSP spec specifies that it must be there. (I'm assuming that you're referring to response.sendRedirect..., but still, HttpServletResponse). But you shouldn't use this at all when you render a portlet: While you're rendering your portlet, you can't assume that you're actually rendering the full page: Your portlet might be rendered as a result of an Ajax call, or the response might already be committed.

You can do this in an action handler, e.g. during processAction() on an ActionResponse - it has a legitimate sendRedirect method that you can call to link to a specific external URL.

Another way to achieve the same means, though it's also a hack, is to generate javascript that does the redirection. Why is this a hack? A portlet action can be handled by exactly one portlet, which, incidently, is also the number of redirections that make sense on a single request. If every portlet you have on a page would decide that it should send you somewhere else through javascript, which one would you expect to win?
Traolly Xiong, modifié il y a 9 années.

RE: Liferay URL redirect

Regular Member Publications: 195 Date d'inscription: 30/12/11 Publications récentes
Interesting. The goal is to perform a redirect when detecting a unique string in the URL on the back-end side before
the render phase (JSP). There is really no action to be performed before rendering the JSP so the action URL phase
might not work. It sounds like maybe I may need to override the portlet's render method? Any examples would be great.

Thanks.
thumbnail
Olaf Kock, modifié il y a 9 années.

RE: Liferay URL redirect

Liferay Legend Publications: 6396 Date d'inscription: 23/09/08 Publications récentes
ServletFilter?
Traolly Xiong, modifié il y a 9 années.

RE: Liferay URL redirect

Regular Member Publications: 195 Date d'inscription: 30/12/11 Publications récentes
I don't think the URL pattern mapping in the "web.xml" will pick up on the browser URL, but only the URL paths for folders / files (i.e. css, js, etc.).
I will attempt to try this and see if it's possible.

ex) web.xml
...
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
...

- "/" only triggers filter class when the request is the plugin root
- "/*" triggers for all requests under the plugin dir


Doesn't trigger the filter class base on the browser URL but currently only a servlet or static content path.

Thanks.
thumbnail
Olaf Kock, modifié il y a 9 années.

RE: Liferay URL redirect

Liferay Legend Publications: 6396 Date d'inscription: 23/09/08 Publications récentes
You can introduce new servlet filters as a hook by adding them to liferay-hook.xml. I'm doing exactly that in my HSTS plugin (without the redirect though). Source is contained in the plugin
Traolly Xiong, modifié il y a 9 années.

RE: Liferay URL redirect

Regular Member Publications: 195 Date d'inscription: 30/12/11 Publications récentes
Dealing with hooks for this sole purpose of detecting a unique URL with some query parameters, doing a redirect and setting session variables may not be enough to go that route. The idea is slick though. I'll use what I've implemented for now and will consider your suggestion if ever need be.

Thanks.
Traolly Xiong, modifié il y a 9 années.

RE: Liferay URL redirect

Regular Member Publications: 195 Date d'inscription: 30/12/11 Publications récentes
Hello,
Using this servlet, I can't seem to use the api to determine if the user is logged in or not.

ex)
- ThemeDisplay api
- request.getRemoteUser()
- User class

They all seem to just return null logged in or not. I'm not sure as to why yet.

Any tips on this issue?

Thanks.
thumbnail
Nagendra Kumar Busam, modifié il y a 9 années.

RE: Liferay URL redirect

Liferay Master Publications: 678 Date d'inscription: 07/07/09 Publications récentes
I am not sure whether you tried what was written in this link

http://stackoverflow.com/questions/19266690/how-to-get-liferay-remote-user-from-filter-class
Traolly Xiong, modifié il y a 9 années.

RE: Liferay URL redirect

Regular Member Publications: 195 Date d'inscription: 30/12/11 Publications récentes
Yes I've tried that and it always returns null. Have you tried it before and if yes, have you tried it locally as well?
thumbnail
Olaf Kock, modifié il y a 9 années.

RE: Liferay URL redirect

Liferay Legend Publications: 6396 Date d'inscription: 23/09/08 Publications récentes
In liferay-hook.xml you can specify where your filter should be inserted into the existing filters. Check what I did in HSTS-hook:
<servlet-filter-mapping>
  <servlet-filter-name>HSTS-Filter</servlet-filter-name>
  <before-filter>SSO Open SSO Filter</before-filter>
  <url-pattern>/*</url-pattern>
  <dispatcher>REQUEST</dispatcher>
  <dispatcher>FORWARD</dispatcher>
</servlet-filter-mapping>

e.g. the before-filter line. As I'm using request.getAttribute("USER") and it returns the valid user, you might insert your filter at a different place in the chain, when this is not yet initialized.
Traolly Xiong, modifié il y a 9 années.

RE: Liferay URL redirect

Regular Member Publications: 195 Date d'inscription: 30/12/11 Publications récentes
Interesting I must try this out.

Thanks.
thumbnail
Enrique Valdes Lacasa, modifié il y a 9 années.

RE: Liferay URL redirect

Junior Member Publications: 92 Date d'inscription: 29/07/14 Publications récentes
Interesting discussion...I haven't tried this one out yet but it seems like something cool to do with hooks.

I am in the middle of implementing a functionality, and maybe someone who used this had a quick answer:

I want authenticated users to be redirected to the home page (at the /home url) when they try to visit the login page...rather than to land at the login page. My login page url in portal-ext.properties is: auth.login.url=/web/guest/login.

So, after reading this...I think that adding a servlet filter hook with some url mapping to match /web/guest/login would be appropriate. Then I would have to process the redirection somehow...which is what is causing me some doubts. I thought of asking first to see if anyone could give some recommendation.

Thank you so much! And have a good weekend.
thumbnail
Enrique Valdes Lacasa, modifié il y a 9 années.

RE: Liferay URL redirect

Junior Member Publications: 92 Date d'inscription: 29/07/14 Publications récentes
Replying to myself...I just found a post where a member asked EXACTLY what I asked here on Friday, so I will continue with the discussion there:
https://www.liferay.com/community/forums/-/message_boards/message/21314522
It could be an interesting post for anyone that wants to learn more about Filter hooks like me. I think I was in the right path for a solution...a member replied with an interesting code snippet there that I am about to try.
Traolly Xiong, modifié il y a 9 années.

RE: Liferay URL redirect

Regular Member Publications: 195 Date d'inscription: 30/12/11 Publications récentes
With some minor tweaks I got the redirect to work as expected.

Thanks for the tips.