jsfprettyfaces

how can I use capturedWith method in prettyFaces


I want to force https url when http url was requested. I found this in prettyfaces forum. but this code give cannot find symbol error. how can I fix this ?

return ConfigurationBuilder.begin()
    .addRule()
    .when(URL.captureIn("url").and(Path.matches("/login")).and(Scheme.matches("http")))
    .perform(Redirect.permanent(URL.capturedWith("url").toScheme("https")));

Solution

  • Try using a parameter Transposition:

    return ConfigurationBuilder.begin()
        .addRule()
        .when(URL.captureIn("url").and(Path.matches("/login")).and(Scheme.matches("http")))
        .perform(Redirect.permanent("{url}"))
        .where("url").transposedBy(new Transposition() { ... convert to HTTPS HERE ... });
    

    https://github.com/ocpsoft/rewrite/blob/master/api/src/main/java/org/ocpsoft/rewrite/param/Transposition.java

    You can also achieve the same thing by doing something like this with a custom Operation:

    https://github.com/ocpsoft/rewrite/blob/master/config-servlet/src/test/java/org/ocpsoft/rewrite/servlet/config/SchemeChangeConfigurationProvider.java

    public class SchemeChangeConfigurationProvider extends HttpConfigurationProvider
    {
       @Override
       public int priority()
       {
          return 0;
       }
    
       @Override
       public Configuration getConfiguration(final ServletContext context)
       {
          Configuration config = ConfigurationBuilder.begin()
    
                   .addRule().when(Scheme.matches("http")).perform(new HttpOperation() {
    
                      @Override
                      public void performHttp(HttpServletRewrite event, EvaluationContext context)
                      {
                         String url = event.getRequest().getRequestURL().toString().replaceFirst("http", "https");
                         Redirect.temporary(url).perform(event, context);
                      }
                   });
          return config;
       }
    }