Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.lang.reflect.Method;
- import javax.servlet.ServletContext;
- import javax.servlet.http.HttpServletResponse;
- import br.com.caelum.vraptor.Get;
- import br.com.caelum.vraptor.http.MutableRequest;
- import br.com.caelum.vraptor.http.TypeCreator;
- import br.com.caelum.vraptor.http.route.Router;
- import br.com.caelum.vraptor.ioc.Component;
- import br.com.caelum.vraptor.ioc.Container;
- import br.com.caelum.vraptor.proxy.MethodInvocation;
- import br.com.caelum.vraptor.proxy.Proxifier;
- import br.com.caelum.vraptor.proxy.ProxyInvocationException;
- import br.com.caelum.vraptor.proxy.SuperMethod;
- import br.com.caelum.vraptor.resource.DefaultResourceMethod;
- import br.com.caelum.vraptor.resource.HttpMethod;
- import br.com.caelum.vraptor.view.DefaultLogicResult;
- import br.com.caelum.vraptor.view.PathResolver;
- @Component
- public class OC4JLogicResult extends DefaultLogicResult {
- private final Proxifier proxifier;
- private final Router router;
- private final MutableRequest request;
- private final HttpServletResponse response;
- private final TypeCreator creator;
- public OC4JLogicResult(Proxifier proxifier, Router router,
- ServletContext context, MutableRequest request,
- HttpServletResponse response, TypeCreator creator,
- Container container, PathResolver resolver) {
- super(proxifier, router, context, request, response, creator,
- container, resolver);
- this.proxifier = proxifier;
- this.response = response;
- this.request = request;
- this.router = router;
- this.creator = creator;
- }
- public <T> T redirectTo(final Class<T> type) {
- return proxifier.proxify(type, new MethodInvocation<T>() {
- public Object intercept(T proxy, Method method, Object[] args,
- SuperMethod superMethod) {
- if (!acceptsHttpGet(method)) {
- throw new IllegalArgumentException(
- "Your logic method must accept HTTP GET method if you want to redirect to it");
- }
- try {
- String path = request.getContextPath();
- String url = router.urlFor(type, method, args);
- includeParametersInFlash(type, method, args);
- response.sendRedirect(path + url);
- return null;
- } catch (IOException e) {
- throw new ProxyInvocationException(e);
- }
- }
- });
- }
- private <T> void includeParametersInFlash(final Class<T> type,
- Method method, Object[] args) {
- Object params = creator.instanceWithParameters(DefaultResourceMethod
- .instanceFor(type, method), args);
- request.getSession().setAttribute(FLASH_PARAMETERS, params);
- }
- private boolean acceptsHttpGet(Method method) {
- if (method.isAnnotationPresent(Get.class)) {
- return true;
- }
- for (HttpMethod httpMethod : HttpMethod.values()) {
- if (method.isAnnotationPresent(httpMethod.getAnnotation())) {
- return false;
- }
- }
- return true;
- }
- }
Add Comment
Please, Sign In to add comment