@Override protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { ServletRequestAttributes attributes = new ServletRequestAttributes(request, response); initContextHolders(request, attributes); try { filterChain.doFilter(request, response); } finally { resetContextHolders(); if (logger.isTraceEnabled()) { logger.trace("Cleared thread-bound request context: " + request); } attributes.requestCompleted(); } }
@Override public void requestDestroyed(ServletRequestEvent requestEvent) { ServletRequestAttributes attributes = null; Object reqAttr = requestEvent.getServletRequest().getAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE); if (reqAttr instanceof ServletRequestAttributes) { attributes = (ServletRequestAttributes) reqAttr; } RequestAttributes threadAttributes = RequestContextHolder.getRequestAttributes(); if (threadAttributes != null) { // We're assumably within the original request thread... LocaleContextHolder.resetLocaleContext(); RequestContextHolder.resetRequestAttributes(); if (attributes == null && threadAttributes instanceof ServletRequestAttributes) { attributes = (ServletRequestAttributes) threadAttributes; } } if (attributes != null) { attributes.requestCompleted(); } }
@Override protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { ServletRequestAttributes attributes = new ServletRequestAttributes(request, response); initContextHolders(request, attributes); try { filterChain.doFilter(request, response); } finally { resetContextHolders(); if (logger.isTraceEnabled()) { logger.trace("Cleared thread-bound request context: " + request); } attributes.requestCompleted(); } }
@Override public void requestDestroyed(ServletRequestEvent requestEvent) { ServletRequestAttributes attributes = null; Object reqAttr = requestEvent.getServletRequest().getAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE); if (reqAttr instanceof ServletRequestAttributes) { attributes = (ServletRequestAttributes) reqAttr; } RequestAttributes threadAttributes = RequestContextHolder.getRequestAttributes(); if (threadAttributes != null) { // We're assumably within the original request thread... LocaleContextHolder.resetLocaleContext(); RequestContextHolder.resetRequestAttributes(); if (attributes == null && threadAttributes instanceof ServletRequestAttributes) { attributes = (ServletRequestAttributes) threadAttributes; } } if (attributes != null) { attributes.requestCompleted(); } }
resetContextHolders(request, previousLocaleContext, previousAttributes); if (requestAttributes != null) { requestAttributes.requestCompleted();
private void doSkipImmutableValue(Object immutableValue) { HttpServletRequest request = mock(HttpServletRequest.class); HttpSession session = mock(HttpSession.class); given(request.getSession(anyBoolean())).willReturn(session); given(session.getAttribute(KEY)).willReturn(immutableValue); ServletRequestAttributes attrs = new ServletRequestAttributes(request); attrs.getAttribute(KEY, RequestAttributes.SCOPE_SESSION); attrs.requestCompleted(); verify(session, times(2)).getAttribute(KEY); verifyNoMoreInteractions(session); }
@Test public void updateAccessedAttributes() throws Exception { HttpServletRequest request = mock(HttpServletRequest.class); HttpSession session = mock(HttpSession.class); given(request.getSession(anyBoolean())).willReturn(session); given(session.getAttribute(KEY)).willReturn(VALUE); ServletRequestAttributes attrs = new ServletRequestAttributes(request); assertSame(VALUE, attrs.getAttribute(KEY, RequestAttributes.SCOPE_SESSION)); attrs.requestCompleted(); verify(session, times(2)).getAttribute(KEY); verify(session).setAttribute(KEY, VALUE); verifyNoMoreInteractions(session); }
resetContextHolders(request, previousLocaleContext, previousAttributes); if (requestAttributes != null) { requestAttributes.requestCompleted();
@Test public void getFromScopeWithSingleAccess() throws Exception { AtomicInteger count = new AtomicInteger(); MockHttpSession session = new MockHttpSession() { @Override public void setAttribute(String name, Object value) { super.setAttribute(name, value); count.incrementAndGet(); } }; MockHttpServletRequest request = new MockHttpServletRequest(); request.setSession(session); ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); String name = "sessionScopedObject"; assertNull(session.getAttribute(name)); TestBean bean = (TestBean) this.beanFactory.getBean(name); assertEquals(1, count.intValue()); // should re-propagate updated attribute requestAttributes.requestCompleted(); assertEquals(session.getAttribute(name), bean); assertEquals(2, count.intValue()); }
@Test public void testRequestScope() { WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_REQUEST); MockHttpServletRequest request = new MockHttpServletRequest(); ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); try { assertNull(request.getAttribute(NAME)); DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class); assertSame(bean, request.getAttribute(NAME)); assertSame(bean, ac.getBean(NAME)); requestAttributes.requestCompleted(); assertTrue(bean.wasDestroyed()); } finally { RequestContextHolder.setRequestAttributes(null); } }
@Test public void getFromScope() throws Exception { AtomicInteger count = new AtomicInteger(); MockHttpSession session = new MockHttpSession() { @Override public void setAttribute(String name, Object value) { super.setAttribute(name, value); count.incrementAndGet(); } }; MockHttpServletRequest request = new MockHttpServletRequest(); request.setSession(session); ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); String name = "sessionScopedObject"; assertNull(session.getAttribute(name)); TestBean bean = (TestBean) this.beanFactory.getBean(name); assertEquals(1, count.intValue()); assertEquals(session.getAttribute(name), bean); assertSame(bean, this.beanFactory.getBean(name)); assertEquals(1, count.intValue()); // should re-propagate updated attribute requestAttributes.requestCompleted(); assertEquals(session.getAttribute(name), bean); assertEquals(2, count.intValue()); }
@Test public void setSessionScopedAttributeAfterCompletion() throws Exception { MockHttpSession session = new MockHttpSession(); session.setAttribute(KEY, VALUE); MockHttpServletRequest request = new MockHttpServletRequest(); request.setSession(session); ServletRequestAttributes attrs = new ServletRequestAttributes(request); assertSame(VALUE, attrs.getAttribute(KEY, RequestAttributes.SCOPE_SESSION)); attrs.requestCompleted(); request.close(); attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_SESSION); assertSame(VALUE, session.getAttribute(KEY)); }
@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()); }
@Test public void destructionAtSessionTermination() throws Exception { MockHttpSession session = new MockHttpSession(); MockHttpServletRequest request = new MockHttpServletRequest(); request.setSession(session); ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); String name = "sessionScopedDisposableObject"; assertNull(session.getAttribute(name)); DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name); assertEquals(session.getAttribute(name), bean); assertSame(bean, this.beanFactory.getBean(name)); requestAttributes.requestCompleted(); session.invalidate(); assertTrue(bean.wasDestroyed()); }
@Test public void requestScopedInnerBeanDestroyedWhileContainedBySingleton() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); String outerBeanName = "singletonOuterBean"; TestBean outer1 = (TestBean) this.beanFactory.getBean(outerBeanName); assertNull(request.getAttribute(outerBeanName)); TestBean inner1 = (TestBean) outer1.getSpouse(); TestBean outer2 = (TestBean) this.beanFactory.getBean(outerBeanName); assertSame(outer1, outer2); assertSame(inner1, outer2.getSpouse()); requestAttributes.requestCompleted(); assertTrue(inner1.wasDestroyed()); assertFalse(outer1.wasDestroyed()); }
@Test public void testDestructionAtRequestCompletion() throws Exception { String name = "requestScopedDisposableObject"; DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name); assertTrue(AopUtils.isCglibProxy(bean)); MockHttpServletRequest request = new MockHttpServletRequest(); ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); try { assertNull(request.getAttribute("scopedTarget." + name)); assertEquals("scoped", bean.getName()); assertNotNull(request.getAttribute("scopedTarget." + name)); assertEquals(DerivedTestBean.class, request.getAttribute("scopedTarget." + name).getClass()); assertEquals("scoped", ((TestBean) request.getAttribute("scopedTarget." + name)).getName()); assertSame(bean, this.beanFactory.getBean(name)); requestAttributes.requestCompleted(); assertTrue(((TestBean) request.getAttribute("scopedTarget." + name)).wasDestroyed()); } finally { RequestContextHolder.setRequestAttributes(null); } }
private void cleanupSpringWebEnvironment(HttpServletRequest request) { RequestAttributes attributes = RequestContextHolder.getRequestAttributes(); RequestContextHolder.resetRequestAttributes(); LocaleContextHolder.resetLocaleContext(); if (attributes instanceof ServletRequestAttributes) { ((ServletRequestAttributes) attributes).requestCompleted(); } getLogger().debug("Cleared thread-bound request context: {}", request); }
private void cleanupSpringWebEnvironment(HttpServletRequest request) { RequestAttributes attributes = RequestContextHolder.getRequestAttributes(); RequestContextHolder.resetRequestAttributes(); LocaleContextHolder.resetLocaleContext(); if (attributes instanceof ServletRequestAttributes) { ((ServletRequestAttributes) attributes).requestCompleted(); } getLogger().debug("Cleared thread-bound request context: {}", request); }
assertSame(bean, this.beanFactory.getBean(name)); requestAttributes.requestCompleted(); serializedState = session.serializeState(); assertFalse(bean.wasDestroyed()); assertSame(bean, this.beanFactory.getBean(name)); requestAttributes.requestCompleted(); session.invalidate(); assertTrue(bean.wasDestroyed());
@Test public void innerBeanInheritsContainingBeanScopeByDefault() { MockHttpServletRequest request = new MockHttpServletRequest(); ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); String outerBeanName = "requestScopedOuterBean"; assertNull(request.getAttribute(outerBeanName)); TestBean outer1 = (TestBean) this.beanFactory.getBean(outerBeanName); assertNotNull(request.getAttribute(outerBeanName)); TestBean inner1 = (TestBean) outer1.getSpouse(); assertSame(outer1, this.beanFactory.getBean(outerBeanName)); requestAttributes.requestCompleted(); assertTrue(outer1.wasDestroyed()); assertTrue(inner1.wasDestroyed()); request = new MockHttpServletRequest(); requestAttributes = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); TestBean outer2 = (TestBean) this.beanFactory.getBean(outerBeanName); assertNotSame(outer1, outer2); assertNotSame(inner1, outer2.getSpouse()); }