get encoded url in filter

Splash Forums Rewrite Users get encoded url in filter

This topic contains 2 replies, has 2 voices, and was last updated by  Anonymous 3 years, 4 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #19765

    Anonymous

    I am successfully using rewrite in my project, one thing I fail to do is to retrieve a rewritten url inside a filter.

    I want to redirect users accessing the login page to their user area if already logged in.

    The method httpResponse.encodeRedirectURL should give me the rewritten url but it returns the url unchanged, outside the filter it works. Is there a way to achieve my requirement?

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
    {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;

    if (httpRequest.getUserPrincipal() != null)
    {
    String targetURL = null;

    if (httpRequest.isUserInRole(Role.Enum.ADMIN.name()))
    targetURL = httpResponse.encodeRedirectURL("/pages/secured/admin/overview.jsf");

    else if (httpRequest.isUserInRole(Role.Enum.CUSTOMER.name()))
    targetURL = httpResponse.encodeRedirectURL("/pages/secured/customer/overview.jsf");

    httpResponse.sendRedirect(contextPath + targetURL);

    }

    // pass the request along the filter chain
    chain.doFilter(request, response);
    }

    #23486

    I guess your filter is configured to execute before the RewiteFilter. Rewrite wraps the HttpServletResponse to be able to modify the behavior of encodeRedirectURL(). But this won’t work if your filter is the first that is executed.

    I see two option here:

    1. Change the configuration of your filter so that it get executed after the RewriteFilter. Please note that you will have to set the dispatch type to FORWARD in this case. The problem with this solution is that HttpServletRequest.getRequestURI() will return the modified URL. But this may be ok depending on your exact requirements.

    2. You could implement the security constraints directly using Rewrite rules. There are some nice examples for similar rules on the examples page:

    http://ocpsoft.org/rewrite/examples/

    I hope this helps. :)

    Christian

    #23487

    Anonymous

    Thanks for the response

    I resolved it by creating a HttpRewriteWrappedResponse manually with the following lines:

    HttpRewriteWrappedResponse rewrite = HttpRewriteWrappedResponse.getInstance(httpRequest);

    String targetURL = rewrite.encodeRedirectURL(“/pages/secured/admin/overview.jsf”);

    Regards, Max

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

Comments are closed.