Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 20th, 2013  |  syntax: None  |  size: 3.96 KB  |  views: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. @Component
  2. @Scope(value = "request")
  3. public class ApplicationConfig
  4. {
  5.     private String userCode;
  6.  
  7.     public String getUserCode()
  8.     {
  9.         return this.userCode;
  10.     }
  11.  
  12.     public void setUserCode(String userCode)
  13.     {
  14.         this.userCode = userCode;
  15.     }
  16. }
  17.        
  18. @Component
  19. public class ApplicationConfigManager
  20. {
  21.     @Autowired
  22.     public ApplicationConfig applicationConfig;
  23.  
  24.     public ApplicationConfig getApplicationConfig()
  25.     {
  26.         return this.applicationConfig;
  27.     }
  28. }
  29.        
  30. @Component
  31. @Provider
  32. public class ApplicationConfigFilter implements ResourceFilter, ContainerRequestFilter
  33. {
  34.     @Autowired
  35.     private ApplicationConfigManager applicationConfigManager;
  36.  
  37.     @Override
  38.     public ContainerRequest filter(ContainerRequest request)
  39.     {
  40.         this.applicationConfigManager.getApplicationConfig().setUserCode(
  41.             request.getSecurityContext().getUserPrincipal().getName()
  42.         );
  43.         return request;
  44.     }
  45.  
  46.     @Override
  47.     public ContainerRequestFilter getRequestFilter()
  48.     {
  49.         return this;
  50.     }
  51.  
  52.     @Override
  53.     public ContainerResponseFilter getResponseFilter()
  54.     {
  55.         return null;
  56.     }
  57. }
  58.        
  59. @Component
  60. @Provider
  61. public class ResourceFilterFactory extends RolesAllowedResourceFilterFactory
  62. {
  63.     @Autowired
  64.     private ApplicationConfigFilter applicationConfigFilter;
  65.  
  66.     @Override
  67.     public List<ResourceFilter> create(AbstractMethod am)
  68.     {
  69.         // get filters from RolesAllowedResourceFilterFactory Factory!
  70.         List<ResourceFilter> rolesFilters = super.create(am);
  71.         if (null == rolesFilters) {
  72.             rolesFilters = new ArrayList<ResourceFilter>();
  73.         }
  74.  
  75.         // Convert into mutable List, so as to add more filters that we need
  76.         // (RolesAllowedResourceFilterFactory generates immutable list of filters)
  77.         List<ResourceFilter> filters = new ArrayList<ResourceFilter>(rolesFilters);
  78.  
  79.         filters.add(this.applicationConfigFilter);
  80.  
  81.         return filters;
  82.     }
  83. }
  84.        
  85. <servlet>
  86.     <servlet-name>Jersey REST Service</servlet-name>
  87.     <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
  88.     <init-param>
  89.         <param-name>com.sun.jersey.config.property.packages</param-name>
  90.         <param-value>com.mypackage</param-value>
  91.     </init-param>
  92.     <init-param>
  93.         <param-name>com.sun.jersey.spi.container.ResourceFilters</param-name>
  94.         <param-value>com.mypackage.ResourceFilterFactory</param-value>
  95.     </init-param>
  96.     <load-on-startup>1</load-on-startup>
  97. </servlet>
  98.        
  99. <listener>
  100.     <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  101. </listener>
  102. <listener>
  103.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  104. </listener>
  105.        
  106. <context:annotation-config/>
  107. <context:component-scan base-package="com.mypackage" />
  108.  
  109. <bean id="applicationConfig" class="com.mypackage.ApplicationConfig" scope="request"/>
  110.        
  111. .
  112. .
  113. Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually o
  114. perating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
  115.         at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) ~[spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
  116.         at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:40) ~[spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
  117.         at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:328) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
  118.         ... 57 common frames omitted
  119.        
  120. @Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)