Java Code Examples for org.springframework.web.context.request.RequestContextHolder#setRequestAttributes()

The following examples show how to use org.springframework.web.context.request.RequestContextHolder#setRequestAttributes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testRequestScopeWithProxiedTargetClass() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	ApplicationContext context = createContext(ScopedProxyMode.TARGET_CLASS);
	IScopedTestBean bean = (IScopedTestBean) context.getBean("request");

	// should be a class-based proxy
	assertTrue(AopUtils.isCglibProxy(bean));
	assertTrue(bean instanceof RequestScopedTestBean);

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributes);
	// this is a proxy so it should be reset to default
	assertEquals(DEFAULT_NAME, bean.getName());

	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	assertEquals(MODIFIED_NAME, bean.getName());
}
 
Example 2
Source File: ClassPathBeanDefinitionScannerScopeIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void singletonScopeWithNoProxy() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	ApplicationContext context = createContext(NO);
	ScopedTestBean bean = (ScopedTestBean) context.getBean("singleton");

	// should not be a proxy
	assertFalse(AopUtils.isAopProxy(bean));

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributes);
	// not a proxy so this should not have changed
	assertEquals(MODIFIED_NAME, bean.getName());

	// singleton bean, so name should be modified even after lookup
	ScopedTestBean bean2 = (ScopedTestBean) context.getBean("singleton");
	assertEquals(MODIFIED_NAME, bean2.getName());
}
 
Example 3
Source File: AdditionalCertificateValidationsAspectShould.java    From mutual-tls-ssl with Apache License 2.0 6 votes vote down vote up
@Test
public void validateCertificateForNotAllowedCommonNameReturnsBadRequestHttpStatus() throws Throwable {
    var proceedingJoinPoint = mock(ProceedingJoinPoint.class);
    var additionalCertificateValidations = createAdditionalCertificateValidations();

    X509Certificate x509Certificate = mock(X509Certificate.class);
    X509Certificate[] x509Certificates = new X509Certificate[]{x509Certificate};
    X500Principal x500Principal = mock(X500Principal.class);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setAttribute("javax.servlet.request.X509Certificate", x509Certificates);
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));

    when(x509Certificate.getSubjectX500Principal()).thenReturn(x500Principal);
    when(x500Principal.getName()).thenReturn("CN=white-tower,OU=Altindag,O=Altindag,C=NL");

    Object response = victim.validate(proceedingJoinPoint, additionalCertificateValidations);

    verify(proceedingJoinPoint, times(0)).proceed();
    assertThat(response).isInstanceOf(ResponseEntity.class);

    ResponseEntity responseEntity = (ResponseEntity) response;
    assertThat(responseEntity.getStatusCode().value()).isEqualTo(400);
    assertThat(responseEntity.getBody()).isEqualTo("This certificate is not a valid one");
}
 
Example 4
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testRequestScopeWithProxiedInterfaces() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	ApplicationContext context = createContext(ScopedProxyMode.INTERFACES);
	IScopedTestBean bean = (IScopedTestBean) context.getBean("request");

	// should be dynamic proxy, implementing both interfaces
	assertTrue(AopUtils.isJdkDynamicProxy(bean));
	assertTrue(bean instanceof AnotherScopeTestInterface);

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributes);
	// this is a proxy so it should be reset to default
	assertEquals(DEFAULT_NAME, bean.getName());

	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	assertEquals(MODIFIED_NAME, bean.getName());
}
 
Example 5
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testRequestScopeWithNoProxy() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	ApplicationContext context = createContext(ScopedProxyMode.NO);
	ScopedTestBean bean = (ScopedTestBean) context.getBean("request");

	// should not be a proxy
	assertFalse(AopUtils.isAopProxy(bean));

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributes);
	// not a proxy so this should not have changed
	assertEquals(MODIFIED_NAME, bean.getName());

	// but a newly retrieved bean should have the default name
	ScopedTestBean bean2 = (ScopedTestBean) context.getBean("request");
	assertEquals(DEFAULT_NAME, bean2.getName());
}
 
Example 6
Source File: RequestContextDecorator.java    From Milkomeda with MIT License 6 votes vote down vote up
@Override
public Runnable decorate(Runnable runnable) {
    RequestAttributes context;
    try {
        context = RequestContextHolder.currentRequestAttributes();
    } catch (Exception e) {
        return runnable;
    }
    RequestAttributes finalContext = context;
    return () -> {
        try {
            RequestContextHolder.setRequestAttributes(finalContext);
            runnable.run();
        } finally {
            RequestContextHolder.resetRequestAttributes();
        }
    };
}
 
Example 7
Source File: AdditionalCertificateValidationsAspectShould.java    From mutual-tls-ssl with Apache License 2.0 6 votes vote down vote up
@Test
public void validateCertificateForAllowedCommonNameContinuesTheFlowWithProceedingJoinPoint() throws Throwable {
    var proceedingJoinPoint = mock(ProceedingJoinPoint.class);
    var additionalCertificateValidations = createAdditionalCertificateValidations();

    X509Certificate x509Certificate = mock(X509Certificate.class);
    X509Certificate[] x509Certificates = new X509Certificate[]{x509Certificate};
    X500Principal x500Principal = mock(X500Principal.class);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setAttribute("javax.servlet.request.X509Certificate", x509Certificates);
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));

    when(x509Certificate.getSubjectX500Principal()).thenReturn(x500Principal);
    when(x500Principal.getName()).thenReturn("CN=black-hole,OU=Altindag,O=Altindag,C=NL");

    victim.validate(proceedingJoinPoint, additionalCertificateValidations);

    verify(proceedingJoinPoint, times(1)).proceed();
}
 
Example 8
Source File: ExpressionValueMethodArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
}
 
Example 9
Source File: AdditionalCertificateValidationsAspectShould.java    From mutual-tls-ssl with Apache License 2.0 6 votes vote down vote up
@Test
public void validateCertificateForUnrecognizedCommonNameReturnsBadRequestHttpStatus() throws Throwable {
    var proceedingJoinPoint = mock(ProceedingJoinPoint.class);
    var additionalCertificateValidations = createAdditionalCertificateValidations();

    X509Certificate x509Certificate = mock(X509Certificate.class);
    X509Certificate[] x509Certificates = new X509Certificate[]{x509Certificate};
    X500Principal x500Principal = mock(X500Principal.class);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setAttribute("javax.servlet.request.X509Certificate", x509Certificates);
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));

    when(x509Certificate.getSubjectX500Principal()).thenReturn(x500Principal);
    when(x500Principal.getName()).thenReturn("qwertyuiop");

    Object response = victim.validate(proceedingJoinPoint, additionalCertificateValidations);

    verify(proceedingJoinPoint, times(0)).proceed();
    assertThat(response).isInstanceOf(ResponseEntity.class);

    ResponseEntity responseEntity = (ResponseEntity) response;
    assertThat(responseEntity.getStatusCode().value()).isEqualTo(400);
    assertThat(responseEntity.getBody()).isEqualTo("This certificate is not a valid one");
}
 
Example 10
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testSingletonScopeIgnoresProxyTargetClass() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	ApplicationContext context = createContext(ScopedProxyMode.TARGET_CLASS);
	ScopedTestBean bean = (ScopedTestBean) context.getBean("singleton");

	// should not be a proxy
	assertFalse(AopUtils.isAopProxy(bean));

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributes);
	// not a proxy so this should not have changed
	assertEquals(MODIFIED_NAME, bean.getName());

	// singleton bean, so name should be modified even after lookup
	ScopedTestBean bean2 = (ScopedTestBean) context.getBean("singleton");
	assertEquals(MODIFIED_NAME, bean2.getName());
}
 
Example 11
Source File: ClassPathBeanDefinitionScannerScopeIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void singletonScopeWithNoProxy() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributes);
	ApplicationContext context = createContext(NO);
	ScopedTestBean bean = (ScopedTestBean) context.getBean("singleton");

	// should not be a proxy
	assertFalse(AopUtils.isAopProxy(bean));

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributes);
	// not a proxy so this should not have changed
	assertEquals(MODIFIED_NAME, bean.getName());

	// singleton bean, so name should be modified even after lookup
	ScopedTestBean bean2 = (ScopedTestBean) context.getBean("singleton");
	assertEquals(MODIFIED_NAME, bean2.getName());
}
 
Example 12
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testSessionScopeWithProxiedInterfaces() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession);
	ApplicationContext context = createContext(ScopedProxyMode.INTERFACES);
	IScopedTestBean bean = (IScopedTestBean) context.getBean("session");

	// should be dynamic proxy, implementing both interfaces
	assertTrue(AopUtils.isJdkDynamicProxy(bean));
	assertTrue(bean instanceof AnotherScopeTestInterface);

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributesWithSession);
	// this is a proxy so it should be reset to default
	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	IScopedTestBean bean2 = (IScopedTestBean) context.getBean("session");
	assertEquals(MODIFIED_NAME, bean2.getName());
	bean2.setName(DEFAULT_NAME);
	assertEquals(DEFAULT_NAME, bean.getName());

	RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession);
	assertEquals(MODIFIED_NAME, bean.getName());
}
 
Example 13
Source File: ClassPathBeanDefinitionScannerScopeIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void sessionScopeWithProxiedTargetClass() {
	RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession);
	ApplicationContext context = createContext(TARGET_CLASS);
	IScopedTestBean bean = (IScopedTestBean) context.getBean("session");

	// should be a class-based proxy
	assertTrue(AopUtils.isCglibProxy(bean));
	assertTrue(bean instanceof ScopedTestBean);
	assertTrue(bean instanceof SessionScopedTestBean);

	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	RequestContextHolder.setRequestAttributes(newRequestAttributesWithSession);
	// this is a proxy so it should be reset to default
	assertEquals(DEFAULT_NAME, bean.getName());
	bean.setName(MODIFIED_NAME);

	IScopedTestBean bean2 = (IScopedTestBean) context.getBean("session");
	assertEquals(MODIFIED_NAME, bean2.getName());
	bean2.setName(DEFAULT_NAME);
	assertEquals(DEFAULT_NAME, bean.getName());

	RequestContextHolder.setRequestAttributes(oldRequestAttributesWithSession);
	assertEquals(MODIFIED_NAME, bean.getName());
}
 
Example 14
Source File: ContextCopyingDecorator.java    From staffjoy with MIT License 5 votes vote down vote up
@Override
public Runnable decorate(Runnable runnable) {
    RequestAttributes context = RequestContextHolder.currentRequestAttributes();
    return () -> {
        try {
            RequestContextHolder.setRequestAttributes(context);
            runnable.run();
        } finally {
            RequestContextHolder.resetRequestAttributes();
        }
    };
}
 
Example 15
Source File: AopNamespaceHandlerScopeIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testSessionScoping() throws Exception {
	MockHttpSession oldSession = new MockHttpSession();
	MockHttpSession newSession = new MockHttpSession();

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setSession(oldSession);
	RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));

	ITestBean scoped = (ITestBean) this.context.getBean("sessionScoped");
	assertTrue("Should be AOP proxy", AopUtils.isAopProxy(scoped));
	assertFalse("Should not be target class proxy", scoped instanceof TestBean);

	ITestBean scopedAlias = (ITestBean) this.context.getBean("sessionScopedAlias");
	assertSame(scoped, scopedAlias);

	ITestBean testBean = (ITestBean) this.context.getBean("testBean");
	assertTrue("Should be AOP proxy", AopUtils.isAopProxy(testBean));
	assertFalse("Regular bean should be JDK proxy", testBean instanceof TestBean);

	String rob = "Rob Harrop";
	String bram = "Bram Smeets";

	assertEquals(rob, scoped.getName());
	scoped.setName(bram);
	request.setSession(newSession);
	assertEquals(rob, scoped.getName());
	request.setSession(oldSession);
	assertEquals(bram, scoped.getName());

	assertTrue("Should have advisors", ((Advised) scoped).getAdvisors().length > 0);
}
 
Example 16
Source File: FrameworkServlet.java    From java-technology-stack with MIT License 5 votes vote down vote up
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);
	}
}
 
Example 17
Source File: AopNamespaceHandlerScopeIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testRequestScoping() throws Exception {
	MockHttpServletRequest oldRequest = new MockHttpServletRequest();
	MockHttpServletRequest newRequest = new MockHttpServletRequest();

	RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(oldRequest));

	ITestBean scoped = (ITestBean) this.context.getBean("requestScoped");
	assertTrue("Should be AOP proxy", AopUtils.isAopProxy(scoped));
	assertTrue("Should be target class proxy", scoped instanceof TestBean);

	ITestBean testBean = (ITestBean) this.context.getBean("testBean");
	assertTrue("Should be AOP proxy", AopUtils.isAopProxy(testBean));
	assertFalse("Regular bean should be JDK proxy", testBean instanceof TestBean);

	String rob = "Rob Harrop";
	String bram = "Bram Smeets";

	assertEquals(rob, scoped.getName());
	scoped.setName(bram);
	RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(newRequest));
	assertEquals(rob, scoped.getName());
	RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(oldRequest));
	assertEquals(bram, scoped.getName());

	assertTrue("Should have advisors", ((Advised) scoped).getAdvisors().length > 0);
}
 
Example 18
Source File: AopNamespaceHandlerScopeIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testSessionScoping() throws Exception {
	MockHttpSession oldSession = new MockHttpSession();
	MockHttpSession newSession = new MockHttpSession();

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setSession(oldSession);
	RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));

	ITestBean scoped = (ITestBean) this.context.getBean("sessionScoped");
	assertTrue("Should be AOP proxy", AopUtils.isAopProxy(scoped));
	assertFalse("Should not be target class proxy", scoped instanceof TestBean);

	ITestBean scopedAlias = (ITestBean) this.context.getBean("sessionScopedAlias");
	assertSame(scoped, scopedAlias);

	ITestBean testBean = (ITestBean) this.context.getBean("testBean");
	assertTrue("Should be AOP proxy", AopUtils.isAopProxy(testBean));
	assertFalse("Regular bean should be JDK proxy", testBean instanceof TestBean);

	String rob = "Rob Harrop";
	String bram = "Bram Smeets";

	assertEquals(rob, scoped.getName());
	scoped.setName(bram);
	request.setSession(newSession);
	assertEquals(rob, scoped.getName());
	request.setSession(oldSession);
	assertEquals(bram, scoped.getName());

	assertTrue("Should have advisors", ((Advised) scoped).getAdvisors().length > 0);
}
 
Example 19
Source File: FrameworkServlet.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private void resetContextHolders(HttpServletRequest request,
		@Nullable LocaleContext prevLocaleContext, @Nullable RequestAttributes previousAttributes) {

	LocaleContextHolder.setLocaleContext(prevLocaleContext, this.threadContextInheritable);
	RequestContextHolder.setRequestAttributes(previousAttributes, this.threadContextInheritable);
}
 
Example 20
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@After
public void tearDown() throws Exception {
	RequestContextHolder.setRequestAttributes(null);
}