Create One Rule For All Supported Extensions

Splash Forums Rewrite Users Create One Rule For All Supported Extensions

This topic contains 2 replies, has 2 voices, and was last updated by  RB 39 minutes ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #27459

    RB
    Participant

    I want that all the pages of my website be accessible through .xhtml .jsf and .jsp (To be compatible with the old system). The code below is working but I have to create one rule for each extension as you can see. Is there any way to achieve the same with one rule? Something like: ” .to(“/Recommended.{xhtml|jsf|jsp}”) ”

    import javax.servlet.ServletContext;
    
    import org.ocpsoft.rewrite.annotation.RewriteConfiguration;
    import org.ocpsoft.rewrite.config.Configuration;
    import org.ocpsoft.rewrite.config.ConfigurationBuilder;
    import org.ocpsoft.rewrite.servlet.config.HttpConfigurationProvider;
    import org.ocpsoft.rewrite.servlet.config.rule.Join;
    
    @RewriteConfiguration
    public class ApplicationConfigurationProvider extends HttpConfigurationProvider
    {
       @Override
       public Configuration getConfiguration(ServletContext context)
       {
          return ConfigurationBuilder.begin()
        		  .addRule(Join.path("/recommended/id/{id}").to("/Recommended.xhtml")
        			.withInboundCorrection()).where("id").matches("[0-9]+")
        		  .addRule(Join.path("/recommended/id/{id}").to("/Recommended.jsf")
        			.withInboundCorrection()).where("id").matches("[0-9]+")
        		  .addRule(Join.path("/recommended/id/{id}").to("/Recommended.jsp")
        			.withInboundCorrection()).where("id").matches("[0-9]+");
       }
    
       @Override
       public int priority()
       {
          return 10;
       }
       
    }
    #27460

    You could simple create loop in your provider that adds these three rules.

    ConfigurationBuilder builder = ConfigurationBuilder.begin()
    for( String extension : Arrays.asList( "xhtml", "jsf", "jsp" ) {
      builder.addRule(Join.path("/recommended/id/{id}").to("/Recommended." + extension)
            .withInboundCorrection()).where("id").matches("[0-9]+");
    }
    

    That’s the nice thing about programmatic configuration. 😉

    #27461

    RB
    Participant

    Thanks again. It works.

    import java.util.Arrays;
    
    import javax.servlet.ServletContext;
    
    import org.ocpsoft.rewrite.annotation.RewriteConfiguration;
    import org.ocpsoft.rewrite.config.Configuration;
    import org.ocpsoft.rewrite.config.ConfigurationBuilder;
    import org.ocpsoft.rewrite.servlet.config.HttpConfigurationProvider;
    import org.ocpsoft.rewrite.servlet.config.rule.Join;
    
    @RewriteConfiguration
    public class ApplicationConfigurationProvider extends HttpConfigurationProvider
    {
       @Override
       public Configuration getConfiguration(ServletContext context) {
    	   ConfigurationBuilder builder = ConfigurationBuilder.begin();
    	   
    	   for(String extension : Arrays.asList("xhtml", "jsf", "jsp")) {
    		   builder.addRule(Join.path("/recommended/id/{id}").to("/Recommended."+extension)
    				   .withInboundCorrection()).where("id").matches("[0-9]+").withPriority(20);
    	   }
    	   
    	   for(String extension : Arrays.asList("xhtml", "jsf", "jsp")) {
    		   builder.addRule(Join.path("/recommended/{recd}").to("/Recommended."+extension)
    				   .withInboundCorrection()).where("recd").matches("[\\p{L}\\p{Punct}0-9\\s]+").withPriority(30);
    	   }
    	   
    	   return builder;
       }
    
       @Override
       public int priority()
       {
          return 10;
       }
       
    }
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

Comments are closed.