Last active
November 4, 2015 13:56
-
-
Save FagnerMartinsBrack/d4a03c6c86e7cf593b47 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.megafone.web.assets; | |
import javax.annotation.ManagedBean; | |
import javax.ejb.EJB; | |
import javax.servlet.ServletContext; | |
import org.megafone.core.service.city.CityReader; | |
import org.megafone.core.service.user.UserReader; | |
import org.ocpsoft.logging.Logger.Level; | |
import org.ocpsoft.rewrite.annotation.RewriteConfiguration; | |
import org.ocpsoft.rewrite.config.ConditionBuilder; | |
import org.ocpsoft.rewrite.config.Configuration; | |
import org.ocpsoft.rewrite.config.ConfigurationBuilder; | |
import org.ocpsoft.rewrite.config.Log; | |
import org.ocpsoft.rewrite.config.OperationBuilder; | |
import org.ocpsoft.rewrite.context.EvaluationContext; | |
import org.ocpsoft.rewrite.event.Rewrite; | |
import org.ocpsoft.rewrite.param.Constraint; | |
import org.ocpsoft.rewrite.servlet.config.DispatchType; | |
import org.ocpsoft.rewrite.servlet.config.Forward; | |
import org.ocpsoft.rewrite.servlet.config.HttpConfigurationProvider; | |
import org.ocpsoft.rewrite.servlet.config.Path; | |
import org.ocpsoft.rewrite.servlet.config.Redirect; | |
@RewriteConfiguration | |
@ManagedBean | |
public class WebConfiguratorProvider extends HttpConfigurationProvider { | |
@EJB | |
CityReader cityReader; | |
@EJB | |
UserReader userReader; | |
private final Constraint<String> VALID_SITE = new Constraint<String>() { | |
@Override | |
public boolean isSatisfiedBy( Rewrite event, EvaluationContext context, String sitePath ) { | |
return cityReader.isRegistered( sitePath ); | |
} | |
}; | |
private final Constraint<String> VALID_USER = new Constraint<String>() { | |
@Override | |
public boolean isSatisfiedBy( Rewrite event, EvaluationContext context, String username ) { | |
return userReader.isRegistered( username ); | |
} | |
}; | |
private final Constraint<String> SITE_CONTEXT = new Constraint<String>() { | |
@Override | |
public boolean isSatisfiedBy( Rewrite event, EvaluationContext context, String paths ) { | |
boolean validUser = false; | |
try { | |
String username = paths.split( "/" )[ 0 ]; | |
validUser = VALID_USER.isSatisfiedBy( event, context, username ); | |
} catch ( ArrayIndexOutOfBoundsException e ) {} | |
return !validUser; | |
} | |
}; | |
private final Constraint<String> ROOT_CONTEXT = new Constraint<String>() { | |
@Override | |
public boolean isSatisfiedBy( Rewrite event, EvaluationContext context, String paths ) { | |
boolean validSite = false; | |
boolean validUser = false; | |
try { | |
String cityPath = paths.split( "/" )[ 1 ]; | |
validSite = VALID_SITE.isSatisfiedBy( event, context, cityPath ); | |
} catch ( ArrayIndexOutOfBoundsException e ) {} | |
try { | |
String username = paths.split( "/" )[ 2 ]; | |
validUser = VALID_USER.isSatisfiedBy( event, context, username ); | |
} catch ( ArrayIndexOutOfBoundsException e ) {} | |
return !validSite && !validUser; | |
} | |
}; | |
@Override | |
public Configuration getConfiguration( ServletContext context ) { | |
return ConfigurationBuilder.begin() | |
// Redirect | |
.addRule() | |
.when( matches( "/{site}" ) ) | |
.perform( Redirect.temporary( "/{site}/" ) ) | |
.where( "site" ).constrainedBy( VALID_SITE ) | |
.addRule() | |
.when( matches( "/{site}/{user}" ) ) | |
.perform( Redirect.temporary( "/{site}/{user}/" ) ) | |
.where( "site" ).constrainedBy( VALID_SITE ) | |
.where( "user" ).constrainedBy( VALID_USER ) | |
.addRule() | |
.when( matches( "{page}" ) ) | |
.perform( forward( "{page}" , 0 ) ) | |
.where( "page" ).constrainedBy( ROOT_CONTEXT ) | |
.where( "page" ).matches( ".*" ) | |
.addRule() | |
.when( matches( "/{site}/{page}" ) ) | |
.perform( forward( "/site/{page}?site={site}" , 1 ) ) | |
.where( "site" ).constrainedBy( VALID_SITE ) | |
.where( "page" ).constrainedBy( SITE_CONTEXT ) | |
.where( "page" ).matches( ".*" ) | |
.addRule() | |
.when( matches( "/{site}/{user}/{page}" ) ) | |
.perform( forward( "/site/user/{page}?site={site}&user={user}", 2 ) ) | |
.where( "site" ).constrainedBy( VALID_SITE ) | |
.where( "user" ).constrainedBy( VALID_USER ) | |
.where( "page" ).matches( ".*" ); | |
} | |
@Override | |
public int priority() { | |
return 0; | |
} | |
private ConditionBuilder matches( String pathPattern ) { | |
return DispatchType.isRequest().and( Path.matches( pathPattern ) ); | |
} | |
private OperationBuilder forward( String location, int id ) { | |
return Log.message( Level.DEBUG, location + "::" + id ).and( Forward.to( location ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment