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);
}
}
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;
}
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();
}
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);
}
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);
}
Aggregations