Guest User

RequestInterceptor

a guest
Feb 10th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. package br.com.ctbc.maestro.vantive.vraptor;
  2.  
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5.  
  6. import br.com.caelum.vraptor.InterceptionException;
  7. import br.com.caelum.vraptor.Intercepts;
  8. import br.com.caelum.vraptor.core.InterceptorStack;
  9. import br.com.caelum.vraptor.http.MutableRequest;
  10. import br.com.caelum.vraptor.interceptor.ExecuteMethodInterceptor;
  11. import br.com.caelum.vraptor.interceptor.Interceptor;
  12. import br.com.caelum.vraptor.interceptor.ParametersInstantiatorInterceptor;
  13. import br.com.caelum.vraptor.ioc.RequestScoped;
  14. import br.com.caelum.vraptor.resource.ResourceMethod;
  15.  
  16. import es.cenobit.const.UserAttributesConstants;
  17. import es.cenobit.util.AuditThreadLocal;
  18.  
  19. /**
  20.  * Intercepta as requisições (HttpRequest).
  21.  *
  22.  * @author Nycholas de Oliveira e Oliveira
  23.  */
  24. @RequestScoped
  25. @Intercepts(after = ParametersInstantiatorInterceptor.class, before = ExecuteMethodInterceptor.class)
  26. public class RequestInterceptor implements Interceptor {
  27.  
  28.     private static final Logger log = LoggerFactory.getLogger(RequestInterceptor.class);
  29.  
  30.     private final MutableRequest request;
  31.  
  32.     public RequestInterceptor(MutableRequest request) {
  33.         this.request = request;
  34.     }
  35.  
  36.     @Override
  37.     public void intercept(InterceptorStack stack, ResourceMethod method, Object resourceInstance)
  38.             throws InterceptionException {
  39.         this.injectUserAttributesInThreadLocal();
  40.         stack.next(method, resourceInstance);
  41.     }
  42.  
  43.     @SuppressWarnings("unused")
  44.     private void injectUserAttributesInThreadLocal() {
  45.         log.debug("Init RequestInterceptor::injectUserAttributesInThreadLocal()");
  46.         String userName = this.request.getHeader(UserAttributesConstants.USER_NAME);
  47.         AuditThreadLocal.set(userName);
  48.     }
  49.  
  50.     @Override
  51.     public boolean accepts(ResourceMethod method) {
  52.         return true;
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment