1

Currently I´m trying the following JSF - Lib:

https://www.ocpsoft.org/rewrite/examples/

I have the following issue:

I have a page: /page.jsf

In my page I have more then only one parameter. E.g. I have: - parameter1 - parameter2

String parameter1 = FacesContext.getCurrentInstance().getExternalContext()
                    .getRequestParameterMap().get("parameter1");

            String parameter2 = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                    .get("parameter2");

Currently I understood I can add this in my UrlConfigProvider class:

.addRule(Join.path("/page/{parameter1}").to("/portal/mypage.jsf") .withInboundCorrection())

This is working for one parameter.

But how can I do this for multiple parameter, so the URL is then: /page/{parameter1}/{parameter2} ....

Any ideas?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
vished2000
  • 164
  • 1
  • 11
  • This might help you: https://www.ocpsoft.org/opensource/how-to-safely-add-modify-servlet-request-parameter-values/ – fuggerjaki61 Jun 03 '20 at 19:06
  • thank you, but should be the URL rewrite rule? – vished2000 Jun 08 '20 at 20:10
  • just to make it clear: you want to rewrite `/page/p1/p2/p3/ ...` to page `/portal/mypage.jsf?p1=?&p2=?&p3=?& ...` with a dynamic number of parameters? – fuggerjaki61 Jun 09 '20 at 16:30
  • yes, correct.... How do I have to adjust the rule then? .addRule(Join.path("/page/{parameter1}").to("/portal/mypage.jsf") .withInboundCorrection()) – vished2000 Jun 09 '20 at 17:02

1 Answers1

0

The rewrite API doesn't bring a native solution for this problem.


Kick-Off Example

.addRule()
.when(/* your condition */)
.perform(new HttpOperation() {
    @Override
    public void performHttp(HttpServletRewrite httpServletRewrite, EvaluationContext evaluationContext) {
        // this is the default wrapper
        HttpRewriteWrappedRequest request = ((HttpRewriteWrappedRequest) httpServletRewrite.getRequest());

        // get the uri (example: '/index/p1/p2')
        String uri = httpServletRewrite.getRequest().getRequestURI();

        // split by slash
        String[] split = uri.split("/");

        // this is example specific
        // split value 0 is empty and split value 1 is the page (e.g. 'index')
        // for every folder increment the index
        // for '/pages/index' the start index should 3
        for (int i = 2; i < split.length; i++) {
            String s = split[i];

            // the request parameter is by default an immutable map
            // but this returns a modifiable
            request.getModifiableParameters().put("prefix" + (i - 1), new String[]{s});
        }
    }
});

Explanation

The only important piece is the HttpOperation. By default the ServletRequest is wrapped in a HttpRewriteWrappedRequest.

The default HttpServletRequest doesn't allow to change the parameters once they were initialized. The method getParameterMap() returns an immutable map.

getParameterMap() of the HttpRewriteWrappedRequest also returns an immutable map. But getModifiableMap() returns obviously a modifiable map.

The rest should be self-explanatory.


See Also

Ocpsoft: How to modify parameters

Modify request parameter with servlet filter

fuggerjaki61
  • 822
  • 1
  • 11
  • 24