Created
June 22, 2014 02:00
-
-
Save dtelaroli/1492af52f56bbb4ee1d4 to your computer and use it in GitHub Desktop.
VRaptor 4 com rotas com idioma
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 br.com.flexait.crud.component; | |
import java.lang.reflect.Method; | |
import java.util.ArrayList; | |
import java.util.EnumSet; | |
import java.util.List; | |
import javax.enterprise.context.ApplicationScoped; | |
import javax.enterprise.inject.Specializes; | |
import javax.inject.Inject; | |
import javax.servlet.http.HttpServletRequest; | |
import br.com.caelum.vraptor.Path; | |
import br.com.caelum.vraptor.controller.BeanClass; | |
import br.com.caelum.vraptor.controller.ControllerMethod; | |
import br.com.caelum.vraptor.controller.HttpMethod; | |
import br.com.caelum.vraptor.http.MutableRequest; | |
import br.com.caelum.vraptor.http.route.PathAnnotationRoutesParser; | |
import br.com.caelum.vraptor.http.route.Route; | |
import br.com.caelum.vraptor.http.route.Router; | |
@Specializes @ApplicationScoped | |
public class CustomRouteParser extends PathAnnotationRoutesParser { | |
private HttpServletRequest request; | |
/** | |
* @deprecated CDI eyes only | |
*/ | |
public CustomRouteParser() { | |
} | |
@Inject | |
public CustomRouteParser(Router router, HttpServletRequest request) { | |
super(router); | |
this.request = request; | |
} | |
@Override | |
protected String extractPrefix(Class<?> type) { | |
String prefix = super.extractPrefix(type); | |
if(!type.isAnnotationPresent(Path.class) && prefix.isEmpty()) { | |
return "/" + type.getSimpleName().toLowerCase().replace("controller", ""); | |
} | |
return prefix; | |
} | |
static class TenantRoute implements Route { | |
private Route route; | |
private HttpServletRequest request; | |
public TenantRoute(Route route, HttpServletRequest request) { | |
this.route = route; | |
this.request = request; | |
} | |
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() { | |
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(); | |
} | |
@Override | |
public ControllerMethod controllerMethod(MutableRequest request, | |
String uri) { | |
request.setParameter("tenant.name", extractTenant(uri)); | |
return route.controllerMethod(request, removeTenant(uri)); | |
} | |
@Override | |
public ControllerMethod getControllerMethod() { | |
return route.getControllerMethod(); | |
} | |
} | |
@Override | |
public List<Route> rulesFor(BeanClass controller) { | |
if (controller.getType().isAnnotationPresent(NoTenant.class)) { | |
return super.rulesFor(controller); | |
} | |
List<Route> rules = new ArrayList<Route>(); | |
for (Route route : super.rulesFor(controller)) { | |
rules.add(new TenantRoute(route, request)); | |
} | |
return rules; | |
} | |
} |
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 br.com.flexait.crud; | |
import java.util.Locale; | |
import javax.enterprise.context.RequestScoped; | |
import javax.inject.Inject; | |
import javax.servlet.http.Cookie; | |
import javax.servlet.http.HttpServletResponse; | |
import br.com.caelum.vraptor.BeforeCall; | |
import br.com.caelum.vraptor.Intercepts; | |
import br.com.caelum.vraptor.http.MutableRequest; | |
import com.google.common.base.Objects; | |
@Intercepts | |
@RequestScoped | |
public class LocaleInterceptor { | |
@Inject private MutableRequest request; | |
@Inject private HttpServletResponse response; | |
public LocaleInterceptor() { | |
} | |
@BeforeCall | |
public void setLocale() { | |
String parameter = request.getParameter("tenant.name"); | |
Locale locale = Locale.forLanguageTag(parameter); | |
Locale.setDefault(locale); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment