-
-
Save lucascs/767021 to your computer and use it in GitHub Desktop.
@Component | |
@ApplicationScoped | |
public class TenantRoutesParser implements RoutesParser { | |
private final RoutesParser parser; | |
public TenantRoutesParser(RoutesParser parser) { | |
this.parser = parser; | |
} | |
@Override | |
public List<Route> rulesFor(ResourceClass resource) { | |
//for controllers that doesnt depend on tenants | |
if (resource.getType().isAnnotationPresent(NoTenant.class)) { | |
return parser.rulesFor(resource); | |
} | |
List<Route> rules = new ArrayList<Route>(); | |
for (Route route : parser.rulesFor(resource)) { | |
rules.add(new TenantRoute(route)); | |
} | |
return rules; | |
} | |
static class TenantRoute implements Route { | |
private final Route route; | |
public TenantRoute(Route route) { | |
this.route = route; | |
} | |
public ResourceMethod resourceMethod(MutableRequest request, String uri) { | |
request.setParameter("tenant.name", extractTenant(uri)); | |
return route.resourceMethod(request, removeTenant(uri)); | |
} | |
public boolean canHandle(String uri) { | |
return route.canHandle(removeTenant(uri)); | |
} | |
public EnumSet<HttpMethod> allowedMethods() { | |
return route.allowedMethods(); | |
} | |
public String urlFor(Class<?> type, Method m, Object... params) { | |
return tenantPrefix() + route.urlFor(type, m, params); | |
} | |
public boolean canHandle(Class<?> type, Method method) { | |
return route.canHandle(type, method); | |
} | |
public int getPriority() { | |
return route.getPriority(); | |
} | |
public String getOriginalUri() { | |
return route.getOriginalUri(); | |
} | |
private String tenantPrefix() { | |
//XXX unfortunately this is the only way without changing vraptor's api | |
MutableRequest request = VRaptorRequestHolder.currentRequest().getRequest(); | |
return "/" + request.getParameter("tenant.name"); | |
} | |
private String removeTenant(String uri) { | |
return uri.replaceFirst("/[^/]*(?=/)", ""); | |
} | |
private String extractTenant(String uri) { | |
return uri.replaceFirst("/([^/]*)(/.*|$)", "$1"); | |
} | |
@Override | |
public String toString() { | |
return route.toString(); | |
} | |
} | |
} |
Parece não funcionar com a nova versão do VRaptor. Ainda não analisei as soluções etc. mas o metodo getParameter(String s) não existe no MutableRequest.
Desculpa, erro meu. Não estava importanto o servlet-api.
Could not find a suitable constructor in br.com.caelum.vraptor.http.route.PathAnnotationRoutesParser. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
at br.com.caelum.vraptor.http.route.PathAnnotationRoutesParser.class(PathAnnotationRoutesParser.java:69)
while locating br.com.caelum.vraptor.http.route.PathAnnotationRoutesParser
for parameter 0 at br.com.codery.prescreva.TenantRoutesParser.(TenantRoutesParser.java:25)
at br.com.caelum.vraptor.ioc.guice.GuiceComponentRegistry.bindToConstructor(GuiceComponentRegistry.java:151)
Funcionou :)