Appending query string in perform redirect

Splash Forums PrettyFaces Users Appending query string in perform redirect

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

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #25305

    maxmil
    Participant

    We are using an HttpConfigurationProvider to programatically define a rule that redirects a user to a page if they are not logged in.

    .defineRule()
        .when(Direction.isInbound()				
        .and(Path.matches("/ui/{tail}").where("tail").matches(".*").andNot(loggedIn)))
        .perform(Redirect.temporary(context.getContextPath() + "/login"));

    We would like to append parameters from the query string of the request to to redirect. Something like “/login?{queryString}”

    I haven’t been able to find any documentation on this, is it possible?

    Maybe org.ocpsoft.rewrite.servlet.config.QueryString#matches could be used to create a binable variable?

    Thanks

    #25306

    Hey! Absolutely.

    Here’s just a short example of how I’ve done something like this to capture the target URL for a user that is not logged in, but is trying to access restricted content:

    .addRule()
    .when(Path.matches("/account/{*}")
        .andNot(AuthenticationStatus.isLoggedIn(identity))
    )
    .perform(Redirect.temporary(context.getContextPath() + "/login?" +
        LoginController.RETURN_TO_PARAM + "={url}")
    )
    .where("url").bindsTo(requestedPath);
    

    Where requestedPath is:

     ValueBinding requestedPath = new ValueBinding() {
          @Override
          public Object retrieve(Rewrite event, EvaluationContext context)
          {
             HttpServletRewrite rewrite = (HttpServletRewrite) event;
             String url = rewrite.getAddress().getPathAndQuery();
             if (url.startsWith(rewrite.getContextPath()))
                url = url.substring(rewrite.getContextPath().length());
             return url;
          }
       };

    And ValueBinding is just a class I created to simplify the binding interface and make my configuration a bit smaller:

    public abstract class ValueBinding implements Binding
    {
       @Override
       public Object submit(Rewrite event, EvaluationContext context, Object value)
       {
          throw new UnsupportedOperationException(getClass().getName() + " does not support value submission.");
       }
    
       @Override
       public boolean supportsRetrieval()
       {
          return true;
       }
    
       @Override
       public boolean supportsSubmission()
       {
          return false;
       }
    }
    #25307

    That being said, this could be as simple as:

    .addRule()
     .when(Path.matches("/account/{*}")))
     .perform(Redirect.temporary(context.getContextPath() + "/login?param=value"));
    

    But if you want to access query parameter values in the redirect, I think this is a case where we probably could make this simpler:

    .addRule()
     .when(Query.matches("{*}foo={bar}{*}"))
     .perform(Redirect.temporary(context.getContextPath() + "/login?foo={bar}"));
    
    #25310

    maxmil
    Participant

    Thanks Lincoln, in my case the ValueBinding is just what i was looking for.

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

You must be logged in to reply to this topic.

Comments are closed.