org.springframework.web.context.request.RequestContextHolder#setRequestAttributes ( )源码实例Demo

下面列出了org.springframework.web.context.request.RequestContextHolder#setRequestAttributes ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@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");
}
 
@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());
}
 
源代码3 项目: Milkomeda   文件: RequestContextDecorator.java
@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();
        }
    };
}
 
@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();
}
 
@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);
}
 
@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");
}
 
@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());
}
 
@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());
}
 
@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());
}
 
@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());
}
 
@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());
}
 
@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());
}
 
源代码13 项目: staffjoy   文件: ContextCopyingDecorator.java
@Override
public Runnable decorate(Runnable runnable) {
    RequestAttributes context = RequestContextHolder.currentRequestAttributes();
    return () -> {
        try {
            RequestContextHolder.setRequestAttributes(context);
            runnable.run();
        } finally {
            RequestContextHolder.resetRequestAttributes();
        }
    };
}
 
@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);
}
 
@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);
}
 
源代码16 项目: java-technology-stack   文件: FrameworkServlet.java
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);
	}
}
 
@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);
}
 
@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());
}
 
@After
public void tearDown() throws Exception {
	RequestContextHolder.setRequestAttributes(null);
}
 
源代码20 项目: spring-analysis-note   文件: FrameworkServlet.java
private void resetContextHolders(HttpServletRequest request,
		@Nullable LocaleContext prevLocaleContext, @Nullable RequestAttributes previousAttributes) {

	LocaleContextHolder.setLocaleContext(prevLocaleContext, this.threadContextInheritable);
	RequestContextHolder.setRequestAttributes(previousAttributes, this.threadContextInheritable);
}