Search in sources :

Example 1 with NativeWebRequest

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);
        }
    });
}
Also used : WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) DeferredResultProcessingInterceptorAdapter(org.springframework.web.context.request.async.DeferredResultProcessingInterceptorAdapter) CallableProcessingInterceptorAdapter(org.springframework.web.context.request.async.CallableProcessingInterceptorAdapter) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 2 with NativeWebRequest

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());
}
Also used : NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) AsyncEvent(javax.servlet.AsyncEvent) Test(org.junit.Test)

Example 3 with NativeWebRequest

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());
}
Also used : NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) AsyncEvent(javax.servlet.AsyncEvent) Test(org.junit.Test)

Example 4 with NativeWebRequest

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());
}
Also used : ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) Test(org.junit.Test)

Example 5 with NativeWebRequest

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());
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) ViewResolverComposite(org.springframework.web.servlet.view.ViewResolverComposite) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) ScriptTemplateViewResolver(org.springframework.web.servlet.view.script.ScriptTemplateViewResolver) FreeMarkerViewResolver(org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver) ViewResolver(org.springframework.web.servlet.ViewResolver) BeanNameViewResolver(org.springframework.web.servlet.view.BeanNameViewResolver) GroovyMarkupViewResolver(org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) TilesViewResolver(org.springframework.web.servlet.view.tiles3.TilesViewResolver) Test(org.junit.Test)

Aggregations

NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)12 Test (org.junit.Test)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)3 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)3 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)3 CallableProcessingInterceptorAdapter (org.springframework.web.context.request.async.CallableProcessingInterceptorAdapter)3 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)3 AsyncEvent (javax.servlet.AsyncEvent)2 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1 ConversionService (org.springframework.core.convert.ConversionService)1 FormattingConversionServiceFactoryBean (org.springframework.format.support.FormattingConversionServiceFactoryBean)1 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)1 AbstractJackson2HttpMessageConverter (org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter)1 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)1