-
-
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(); | |
} | |
} | |
} |
o Guice não consegue fazer essa injeção muito bem...
troque o RoutesParser do construtor por PathAnnotationRoutesParser e veja se funciona
Abs
Funcionou :)
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)
vraptor 3.4.0 + guice
Exception in thread "main" java.lang.StackOverflowError
at sun.reflect.Reflection.getClassAccessFlags(Native Method)
at sun.reflect.Reflection.quickCheckMemberAccess(Reflection.java:51)
at java.lang.reflect.Method.invoke(Method.java:576)
at com.google.inject.internal.DelegatingInvocationHandler.invoke(DelegatingInvocationHandler.java:37)
at $Proxy64.rulesFor(Unknown Source)
at com.mytest.tenant.TenantRoutesParser.rulesFor(TenantRoutesParser.java:36)
at sun.reflect.GeneratedMethodAccessor23.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.inject.internal.DelegatingInvocationHandler.invoke(DelegatingInvocationHandler.java:37)
at $Proxy64.rulesFor(Unknown Source)
at com.mytest.tenant.TenantRoutesParser.rulesFor(TenantRoutesParser.java:36)
...