Reply To: Thoughts after migrating from Rewrite v1 to v2

Splash Forums Rewrite Users Thoughts after migrating from Rewrite v1 to v2 Reply To: Thoughts after migrating from Rewrite v1 to v2

#23998

fabmars
Participant

My code is just building the Rewrite config. It’s a quickly hacked copy/update from a bigger project

package plop;

import javax.servlet.ServletContext;

import org.ocpsoft.rewrite.config.Configuration;
import org.ocpsoft.rewrite.config.ConfigurationBuilder;
import org.ocpsoft.rewrite.context.EvaluationContext;
import org.ocpsoft.rewrite.el.El;
import org.ocpsoft.rewrite.event.Rewrite;
import org.ocpsoft.rewrite.param.Converter;
import org.ocpsoft.rewrite.servlet.config.HttpConfigurationProvider;
import org.ocpsoft.rewrite.servlet.config.rule.Join;

public class RewriteConfigProvider extends HttpConfigurationProvider {

  @Override
  public int priority() {
    return 10;
  }

  @Override
  public Configuration getConfiguration(ServletContext servletContext) {
    try {
      return buildPrettyConfiguration();
    }
    catch (Exception e) { //don't do that at home...
      e.printStackTrace();
      return null;
    }
  }

  /**
   * Builds the Rewrite config
   * @throws SecurityException 
   * @throws NoSuchFieldException 
   */
  private Configuration buildPrettyConfiguration() {
    return ConfigurationBuilder.begin()

        //then real short urls
        .addRule(Join.path("/prettylink/{id}/").to("/prettylink.jsf")).where("id").bindsTo(El.property("handler.id")).convertedBy(LongConverter.INSTANCE)
        .addRule(Join.path("/prettysafe/{id}/").to("/prettysafe.jsf")).where("id").bindsTo(El.property("handler.id")).convertedBy(LongConverter.INSTANCE)
        .addRule(Join.path("/outlink/{id}/").to("/outlink.jsf")).where("id").bindsTo(El.property("handler.id")).convertedBy(LongConverter.INSTANCE)
        .addRule(Join.path("/outpretty/{id}/").to("/outpretty.jsf")).where("id").bindsTo(El.property("handler.id")).convertedBy(LongConverter.INSTANCE);
  }
}

class LongConverter implements Converter<Long> {
  public final static LongConverter INSTANCE = new LongConverter();
  
  @Override
  public Long convert(final Rewrite event, final EvaluationContext context, final Object value) {
    if (value == null) {
      throw new IllegalArgumentException("Value to convert cannot be null.");
    }
    return Long.valueOf(value.toString());
  }
}

Yes I upgrade containers, but scarcely. I know from the JSF guys there’s no problem upgrading javax.faces.jar and I know the Weld guys don’t change the apis on 1.1 anymore (that would require the GF-weld integration bundle to be upgraded too and that’s where problems start). I’d never have sone that at the time of Weld 1.0.x.

  • This reply was modified 8 years, 7 months ago by  fabmars.
  • This reply was modified 8 years, 7 months ago by  fabmars.