jsfprettyfacesocpsoft-rewrite

JSF - ignore case in URLs (OCPSoft)


Is there a way to define rewrite rules in OCPSoft rewrite independent of case instead of defining multiple rules, since any character could be lower or upper case?

cb.addRule(Join.path('/Test').to("/test.xhtml"));
cb.addRule(Join.path('/test').to("/test.xhtml"));

Something like:

boolean ignoreCase = true;
cb.addRule(Join.path('/Test', ignoreCase).to("/test.xhtml"));

Background:

Logging our 404 errors i encounter a lot of requests just with lower case URLs.


Solution

  • Yes! Absolutely.

    The simplest way to do this is probably use a parameter with a regex .matches() constraint:

    cb.addRule(
      Join.path('/{path}').to("/test.xhtml"))
          .where("path")
          .matches("(?i)test"); // <-- your path regex pattern goes here
    

    You could also use the Resource or Filesystem condition with a custom transform to make this a more global rule:

    public class Lowercase implements Transposition<String>
    {
       @Override
       public String transpose(Rewrite event, EvaluationContext context, String value)
       {
          return value.toLowerCase();
       }
    }
    

    Then...

    .addRule(Join.path("/{path}").to("/{path}.xhtml"))
    .when(Resource.exists("{path}.xhtml"))
    .where("path")
    .matches("[a-zA-Z]+")
    .transposedBy(new Lowercase())
    

    This of course assumes that your xhtml files all use a lowercase naming scheme.

    You could create your own custom .when() conditions to locate the most likely matches, then substitute the 'found / correct' value of {path} in the transposition, if you wanted to go even farther.