/** * Bind the given RequestAttributes to the current thread, * <i>not</i> exposing it as inheritable for child threads. * @param attributes the RequestAttributes to expose * @see #setRequestAttributes(RequestAttributes, boolean) */ public static void setRequestAttributes(@Nullable RequestAttributes attributes) { setRequestAttributes(attributes, false); }
private void resetContextHolders(HttpServletRequest request, @Nullable LocaleContext prevLocaleContext, @Nullable RequestAttributes previousAttributes) { LocaleContextHolder.setLocaleContext(prevLocaleContext, this.threadContextInheritable); RequestContextHolder.setRequestAttributes(previousAttributes, this.threadContextInheritable); }
private void initContextHolders(HttpServletRequest request, ServletRequestAttributes requestAttributes) { LocaleContextHolder.setLocale(request.getLocale(), this.threadContextInheritable); RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable); if (logger.isTraceEnabled()) { logger.trace("Bound request context to thread: " + request); } }
private void initContextHolders(HttpServletRequest request, @Nullable LocaleContext localeContext, @Nullable RequestAttributes requestAttributes) { if (localeContext != null) { LocaleContextHolder.setLocaleContext(localeContext, this.threadContextInheritable); } if (requestAttributes != null) { RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable); } }
@Override public void requestInitialized(ServletRequestEvent requestEvent) { if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) { throw new IllegalArgumentException( "Request is not an HttpServletRequest: " + requestEvent.getServletRequest()); } HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest(); ServletRequestAttributes attributes = new ServletRequestAttributes(request); request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes); LocaleContextHolder.setLocale(request.getLocale()); RequestContextHolder.setRequestAttributes(attributes); }
@After public void resetRequestAttributes() { RequestContextHolder.setRequestAttributes(null); }
@After public void tearDown() throws Exception { RequestContextHolder.setRequestAttributes(null); }
@After public void resetRequestAttributes() { RequestContextHolder.setRequestAttributes(null); }
@Before public void setup() { RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(this.request)); }
@Before public void setUp() throws Exception { adaptee = mock(WebArgumentResolver.class); adapter = new TestWebArgumentResolverAdapter(adaptee); parameter = new MethodParameter(getClass().getMethod("handle", Integer.TYPE), 0); webRequest = new ServletWebRequest(new MockHttpServletRequest()); // Expose request to the current thread (for SpEL expressions) RequestContextHolder.setRequestAttributes(webRequest); }
private void adaptRequestFromForwardedHeaders() throws Exception { MockFilterChain chain = new MockFilterChain(); new ForwardedHeaderFilter().doFilter(this.request, new MockHttpServletResponse(), chain); HttpServletRequest adaptedRequest = (HttpServletRequest) chain.getRequest(); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(adaptedRequest)); }
@Before @SuppressWarnings("resource") public void setUp() throws Exception { GenericWebApplicationContext context = new GenericWebApplicationContext(); context.refresh(); resolver = new ExpressionValueMethodArgumentResolver(context.getBeanFactory()); Method method = getClass().getMethod("params", int.class, String.class, String.class); paramSystemProperty = new MethodParameter(method, 0); paramContextPath = new MethodParameter(method, 1); paramNotSupported = new MethodParameter(method, 2); webRequest = new ServletWebRequest(new MockHttpServletRequest(), new MockHttpServletResponse()); // Expose request to the current thread (for SpEL expressions) RequestContextHolder.setRequestAttributes(webRequest); }
@Before public void createViewResolver() { StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(new MockServletContext()); wac.refresh(); viewResolver = new ContentNegotiatingViewResolver(); viewResolver.setApplicationContext(wac); request = new MockHttpServletRequest("GET", "/test"); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); }
@Test public void fromCurrentRequest() { this.request.setRequestURI("/mvc-showcase/data/param"); this.request.setQueryString("foo=123"); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(this.request)); try { String result = ServletUriComponentsBuilder.fromCurrentRequest().build().toUriString(); assertEquals("http://localhost/mvc-showcase/data/param?foo=123", result); } finally { RequestContextHolder.resetRequestAttributes(); } }
@Test public void getFromFactoryBeanInScope() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); RequestAttributes requestAttributes = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); String name = "requestScopedFactoryBean"; assertNull(request.getAttribute(name)); TestBean bean = (TestBean) this.beanFactory.getBean(name); assertTrue(request.getAttribute(name) instanceof FactoryBean); assertSame(bean, this.beanFactory.getBean(name)); }
@Test public void circleLeadsToException() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); RequestAttributes requestAttributes = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); try { String name = "requestScopedObjectCircle1"; assertNull(request.getAttribute(name)); this.beanFactory.getBean(name); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { assertTrue(ex.contains(BeanCurrentlyInCreationException.class)); } }
@Before public void setUp() { given(wac.getServletContext()).willReturn(mockServletContext); given(testContext.getApplicationContext()).willReturn(wac); MockHttpServletRequest request = new MockHttpServletRequest(mockServletContext); MockHttpServletResponse response = new MockHttpServletResponse(); ServletWebRequest servletWebRequest = new ServletWebRequest(request, response); request.setAttribute(SET_UP_OUTSIDE_OF_STEL, "true"); RequestContextHolder.setRequestAttributes(servletWebRequest); assertSetUpOutsideOfStelAttributeExists(); }
@Test public void getFromScope() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.setContextPath("/path"); RequestAttributes requestAttributes = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); String name = "requestScopedObject"; assertNull(request.getAttribute(name)); TestBean bean = (TestBean) this.beanFactory.getBean(name); assertEquals("/path", bean.getName()); assertSame(bean, request.getAttribute(name)); assertSame(bean, this.beanFactory.getBean(name)); }
@Test public void destructionAtRequestCompletion() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); String name = "requestScopedDisposableObject"; assertNull(request.getAttribute(name)); DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name); assertSame(bean, request.getAttribute(name)); assertSame(bean, this.beanFactory.getBean(name)); requestAttributes.requestCompleted(); assertTrue(bean.wasDestroyed()); }
@Before public void setUp() { ServletContext servletContext = new MockServletContext(); MockHttpServletRequest mockRequest = new MockHttpServletRequest(servletContext); mockRequest.setAttribute(FROM_CUSTOM_MOCK, FROM_CUSTOM_MOCK); RequestContextHolder.setRequestAttributes(new ServletWebRequest(mockRequest, new MockHttpServletResponse())); this.wac.setServletContext(servletContext); new AnnotatedBeanDefinitionReader(this.wac).register(WebConfig.class); this.wac.refresh(); this.mockMvc = webAppContextSetup(this.wac) .defaultRequest(get("/").requestAttr(FROM_MVC_TEST_DEFAULT, FROM_MVC_TEST_DEFAULT)) .alwaysExpect(status().isOk()) .build(); }