Guest User

Untitled

a guest
Apr 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.lang.reflect.Method;
  3.  
  4. import javax.servlet.ServletContext;
  5. import javax.servlet.http.HttpServletResponse;
  6.  
  7. import br.com.caelum.vraptor.Get;
  8. import br.com.caelum.vraptor.http.MutableRequest;
  9. import br.com.caelum.vraptor.http.TypeCreator;
  10. import br.com.caelum.vraptor.http.route.Router;
  11. import br.com.caelum.vraptor.ioc.Component;
  12. import br.com.caelum.vraptor.ioc.Container;
  13. import br.com.caelum.vraptor.proxy.MethodInvocation;
  14. import br.com.caelum.vraptor.proxy.Proxifier;
  15. import br.com.caelum.vraptor.proxy.ProxyInvocationException;
  16. import br.com.caelum.vraptor.proxy.SuperMethod;
  17. import br.com.caelum.vraptor.resource.DefaultResourceMethod;
  18. import br.com.caelum.vraptor.resource.HttpMethod;
  19. import br.com.caelum.vraptor.view.DefaultLogicResult;
  20. import br.com.caelum.vraptor.view.PathResolver;
  21.  
  22. @Component
  23. public class OC4JLogicResult extends DefaultLogicResult {
  24.  
  25. private final Proxifier proxifier;
  26. private final Router router;
  27. private final MutableRequest request;
  28. private final HttpServletResponse response;
  29. private final TypeCreator creator;
  30.  
  31. public OC4JLogicResult(Proxifier proxifier, Router router,
  32. ServletContext context, MutableRequest request,
  33. HttpServletResponse response, TypeCreator creator,
  34. Container container, PathResolver resolver) {
  35. super(proxifier, router, context, request, response, creator,
  36. container, resolver);
  37.  
  38. this.proxifier = proxifier;
  39. this.response = response;
  40. this.request = request;
  41. this.router = router;
  42. this.creator = creator;
  43. }
  44.  
  45. public <T> T redirectTo(final Class<T> type) {
  46. return proxifier.proxify(type, new MethodInvocation<T>() {
  47. public Object intercept(T proxy, Method method, Object[] args,
  48. SuperMethod superMethod) {
  49. if (!acceptsHttpGet(method)) {
  50. throw new IllegalArgumentException(
  51. "Your logic method must accept HTTP GET method if you want to redirect to it");
  52. }
  53. try {
  54. String path = request.getContextPath();
  55. String url = router.urlFor(type, method, args);
  56. includeParametersInFlash(type, method, args);
  57. response.sendRedirect(path + url);
  58. return null;
  59. } catch (IOException e) {
  60. throw new ProxyInvocationException(e);
  61. }
  62. }
  63.  
  64. });
  65. }
  66.  
  67. private <T> void includeParametersInFlash(final Class<T> type,
  68. Method method, Object[] args) {
  69. Object params = creator.instanceWithParameters(DefaultResourceMethod
  70. .instanceFor(type, method), args);
  71. request.getSession().setAttribute(FLASH_PARAMETERS, params);
  72. }
  73.  
  74. private boolean acceptsHttpGet(Method method) {
  75. if (method.isAnnotationPresent(Get.class)) {
  76. return true;
  77. }
  78. for (HttpMethod httpMethod : HttpMethod.values()) {
  79. if (method.isAnnotationPresent(httpMethod.getAnnotation())) {
  80. return false;
  81. }
  82. }
  83. return true;
  84. }
  85.  
  86. }
Add Comment
Please, Sign In to add comment