Search in sources :

Example 6 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.

the class FrameworkServlet method processRequest.

/**
	 * Process this request, publishing an event regardless of the outcome.
	 * <p>The actual event handling is performed by the abstract
	 * {@link #doService} template method.
	 */
protected final void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    long startTime = System.currentTimeMillis();
    Throwable failureCause = null;
    LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
    LocaleContext localeContext = buildLocaleContext(request);
    RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
    ServletRequestAttributes requestAttributes = buildRequestAttributes(request, response, previousAttributes);
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), new RequestBindingInterceptor());
    initContextHolders(request, localeContext, requestAttributes);
    try {
        doService(request, response);
    } catch (ServletException ex) {
        failureCause = ex;
        throw ex;
    } catch (IOException ex) {
        failureCause = ex;
        throw ex;
    } catch (Throwable ex) {
        failureCause = ex;
        throw new NestedServletException("Request processing failed", ex);
    } finally {
        resetContextHolders(request, previousLocaleContext, previousAttributes);
        if (requestAttributes != null) {
            requestAttributes.requestCompleted();
        }
        if (logger.isDebugEnabled()) {
            if (failureCause != null) {
                this.logger.debug("Could not complete request", failureCause);
            } else {
                if (asyncManager.isConcurrentHandlingStarted()) {
                    logger.debug("Leaving response open for concurrent processing");
                } else {
                    this.logger.debug("Successfully completed request");
                }
            }
        }
        publishRequestHandledEvent(request, response, startTime, failureCause);
    }
}
Also used : WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) ServletException(javax.servlet.ServletException) NestedServletException(org.springframework.web.util.NestedServletException) SimpleLocaleContext(org.springframework.context.i18n.SimpleLocaleContext) LocaleContext(org.springframework.context.i18n.LocaleContext) NestedServletException(org.springframework.web.util.NestedServletException) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) IOException(java.io.IOException)

Example 7 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.

the class MvcUriComponentsBuilder method getWebApplicationContext.

private static WebApplicationContext getWebApplicationContext() {
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (requestAttributes == null) {
        logger.debug("No request bound to the current thread: not in a DispatcherServlet request?");
        return null;
    }
    HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
    WebApplicationContext wac = (WebApplicationContext) request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    if (wac == null) {
        logger.debug("No WebApplicationContext found: not in a DispatcherServlet request?");
        return null;
    }
    return wac;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 8 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.

the class ServletUriComponentsBuilder method getCurrentRequest.

/**
	 * Obtain current request through {@link RequestContextHolder}.
	 */
protected static HttpServletRequest getCurrentRequest() {
    RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
    Assert.state(attrs instanceof ServletRequestAttributes, "No current ServletRequestAttributes");
    return ((ServletRequestAttributes) attrs).getRequest();
}
Also used : ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 9 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.

the class AopNamespaceHandlerScopeIntegrationTests method testSessionScoping.

@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);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MockHttpSession(org.springframework.mock.web.MockHttpSession) Test(org.junit.Test)

Example 10 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.

the class ClassPathBeanDefinitionScannerScopeIntegrationTests method setUp.

@Before
public void setUp() {
    MockHttpServletRequest oldRequestWithSession = new MockHttpServletRequest();
    oldRequestWithSession.setSession(new MockHttpSession());
    this.oldRequestAttributesWithSession = new ServletRequestAttributes(oldRequestWithSession);
    MockHttpServletRequest newRequestWithSession = new MockHttpServletRequest();
    newRequestWithSession.setSession(new MockHttpSession());
    this.newRequestAttributesWithSession = new ServletRequestAttributes(newRequestWithSession);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MockHttpSession(org.springframework.mock.web.MockHttpSession) Before(org.junit.Before)

Aggregations

ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)30 RequestAttributes (org.springframework.web.context.request.RequestAttributes)13 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 Before (org.junit.Before)6 Test (org.junit.Test)3 MockHttpSession (org.springframework.mock.web.MockHttpSession)3 Date (java.util.Date)2 HttpSession (javax.servlet.http.HttpSession)2 BeforeClass (org.junit.BeforeClass)2 RecordNameEntity (org.orcid.persistence.jpa.entities.RecordNameEntity)2 ITestBean (org.springframework.tests.sample.beans.ITestBean)2 TestBean (org.springframework.tests.sample.beans.TestBean)2 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 Principal (java.security.Principal)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 PortletRequest (javax.portlet.PortletRequest)1 ServletException (javax.servlet.ServletException)1