Is it possible to add/remove query parameters?

Splash Forums Rewrite Users Is it possible to add/remove query parameters?

This topic contains 6 replies, has 3 voices, and was last updated by  Christian Kaltepoth 2 years, 2 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #26274

    Jhs
    Participant

    Is it possible to remove some query parameters in outgoing links?

    For example I have a link /some/page.xhtml?param=123&other=321
    If I rewrite it to /some/page/123 it still generates links with the remaining query parameters. So, in this case /some/page/123?other=321.

    Is it possible to remove that “other” parameter from generated links?

    #26276

    Hey there. Yes, it is certainly possible to remove parameters from links. This should probably be done in an outbound rewrite rule, but I don’t think there’s a shorthand way of doing it. You’ll have to do it in plain old Java, something like this:

    .addRule()
    .when(Direction.isOutbound().and(QueryParameter.exists("other")))
    .perform(new HttpOperation() {
       public void perform(HttpServletRewrite event, EvalutationContext context)
       {
           Address address = event.getAddress();
           String newAddress = address.toString();
           newAddress = // Remove parameters from newAddress.
           event.setAddress(AddressBuilder.create(newAddress))
       }
    })

    Something like that. There are some Deprecated URLBuilder and QueryString builder types that may help you do this, but we will probably be replacing them with a betting API in the AddressBuilder API at some point in the future. This will work for now.

    #26278

    Jhs
    Participant

    Thanks for the quick reply!

    And is it possible to perform this for every rewrite rule (like a global rewrite rule?) or a batch of rules or does it have to be copied to every single one of them?

    P.S. PrettyFaces has quite good docs, it would be great if rewrite got better documentation. It seems like a good tool. 🙂

    #26279

    The rule Lincoln posted is already “global”. It is applied whenever there is a query parameter “other” in an outbound URL. See the when part of the rule:

    
    Direction.isOutbound().and(QueryParameter.exists("other"))
    
    #26281

    Jhs
    Participant

    Ok, thanks.

    And what about these?

    The method exists(String) is undefined for the type QueryParameter

    The method setAddress(Address) is undefined for the type HttpServletRewrite

    #26282

    Jhs
    Participant

    Ok, I guess QueryParameter must be changed to RequestParameter. That fixes the first error and I found tests using it this way in GitHub.

    2nd problem about setAddress() remains. Any hints?

    #26283

    I think you have to cast the event to HttpOutboundServletRewrite and then use setOutboundAddress if I remember correctly.

    And I think you have to use Query.parameterExists().

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

You must be logged in to reply to this topic.

Comments are closed.