use of org.springframework.web.context.request.NativeWebRequest in project spring-framework by spring-projects.
the class TestDispatcherServlet method registerAsyncResultInterceptors.
private void registerAsyncResultInterceptors(final HttpServletRequest request) {
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
asyncManager.registerCallableInterceptor(KEY, new CallableProcessingInterceptorAdapter() {
@Override
public <T> void postProcess(NativeWebRequest r, Callable<T> task, Object value) throws Exception {
getMvcResult(request).setAsyncResult(value);
}
});
asyncManager.registerDeferredResultInterceptor(KEY, new DeferredResultProcessingInterceptorAdapter() {
@Override
public <T> void postProcess(NativeWebRequest r, DeferredResult<T> result, Object value) throws Exception {
getMvcResult(request).setAsyncResult(value);
}
});
}
use of org.springframework.web.context.request.NativeWebRequest in project spring-framework by spring-projects.
the class WebAsyncManagerTimeoutTests method startDeferredResultProcessingAfterTimeoutException.
@Test
public void startDeferredResultProcessingAfterTimeoutException() throws Exception {
DeferredResult<Integer> deferredResult = new DeferredResult<>();
final Exception exception = new Exception();
DeferredResultProcessingInterceptor interceptor = new DeferredResultProcessingInterceptorAdapter() {
@Override
public <T> boolean handleTimeout(NativeWebRequest request, DeferredResult<T> result) throws Exception {
throw exception;
}
};
this.asyncManager.registerDeferredResultInterceptor("interceptor", interceptor);
this.asyncManager.startDeferredResultProcessing(deferredResult);
AsyncEvent event = null;
this.asyncWebRequest.onTimeout(event);
assertTrue(this.asyncManager.hasConcurrentResult());
assertEquals(exception, this.asyncManager.getConcurrentResult());
assertEquals("/test", ((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath());
}
use of org.springframework.web.context.request.NativeWebRequest in project spring-framework by spring-projects.
the class WebAsyncManagerTimeoutTests method startDeferredResultProcessingTimeoutAndResumeThroughInterceptor.
@Test
public void startDeferredResultProcessingTimeoutAndResumeThroughInterceptor() throws Exception {
DeferredResult<Integer> deferredResult = new DeferredResult<>();
DeferredResultProcessingInterceptor interceptor = new DeferredResultProcessingInterceptorAdapter() {
@Override
public <T> boolean handleTimeout(NativeWebRequest request, DeferredResult<T> result) throws Exception {
result.setErrorResult(23);
return true;
}
};
this.asyncManager.registerDeferredResultInterceptor("interceptor", interceptor);
this.asyncManager.startDeferredResultProcessing(deferredResult);
AsyncEvent event = null;
this.asyncWebRequest.onTimeout(event);
assertTrue(this.asyncManager.hasConcurrentResult());
assertEquals(23, this.asyncManager.getConcurrentResult());
assertEquals("/test", ((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath());
}
use of org.springframework.web.context.request.NativeWebRequest in project spring-framework by spring-projects.
the class WebMvcConfigurationSupportExtensionTests method contentNegotiation.
@Test
public void contentNegotiation() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.json");
NativeWebRequest webRequest = new ServletWebRequest(request);
RequestMappingHandlerMapping mapping = this.config.requestMappingHandlerMapping();
ContentNegotiationManager manager = mapping.getContentNegotiationManager();
assertEquals(Collections.singletonList(APPLICATION_JSON), manager.resolveMediaTypes(webRequest));
request.setRequestURI("/foo.xml");
assertEquals(Collections.singletonList(APPLICATION_XML), manager.resolveMediaTypes(webRequest));
request.setRequestURI("/foo.rss");
assertEquals(Collections.singletonList(MediaType.valueOf("application/rss+xml")), manager.resolveMediaTypes(webRequest));
request.setRequestURI("/foo.atom");
assertEquals(Collections.singletonList(APPLICATION_ATOM_XML), manager.resolveMediaTypes(webRequest));
request.setRequestURI("/foo");
request.setParameter("f", "json");
assertEquals(Collections.singletonList(APPLICATION_JSON), manager.resolveMediaTypes(webRequest));
request.setRequestURI("/resources/foo.gif");
SimpleUrlHandlerMapping handlerMapping = (SimpleUrlHandlerMapping) this.config.resourceHandlerMapping();
handlerMapping.setApplicationContext(this.context);
HandlerExecutionChain chain = handlerMapping.getHandler(request);
assertNotNull(chain);
ResourceHttpRequestHandler handler = (ResourceHttpRequestHandler) chain.getHandler();
assertNotNull(handler);
assertSame(manager, handler.getContentNegotiationManager());
}
use of org.springframework.web.context.request.NativeWebRequest in project spring-framework by spring-projects.
the class MvcNamespaceTests method testContentNegotiationManager.
@Test
public void testContentNegotiationManager() throws Exception {
loadBeanDefinitions("mvc-config-content-negotiation-manager.xml", 15);
RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
ContentNegotiationManager manager = mapping.getContentNegotiationManager();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.xml");
NativeWebRequest webRequest = new ServletWebRequest(request);
assertEquals(Collections.singletonList(MediaType.valueOf("application/rss+xml")), manager.resolveMediaTypes(webRequest));
ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
assertNotNull(compositeResolver);
assertEquals("Actual: " + compositeResolver.getViewResolvers(), 1, compositeResolver.getViewResolvers().size());
ViewResolver resolver = compositeResolver.getViewResolvers().get(0);
assertEquals(ContentNegotiatingViewResolver.class, resolver.getClass());
ContentNegotiatingViewResolver cnvr = (ContentNegotiatingViewResolver) resolver;
assertSame(manager, cnvr.getContentNegotiationManager());
}
Aggregations