Search in sources :

Example 26 with HandlerExecutionChain

use of org.springframework.web.servlet.HandlerExecutionChain in project gocd by gocd.

the class InterceptorInjectorTest method testShouldMergeInterceptors.

public void testShouldMergeInterceptors() throws Throwable {
    HandlerInterceptor interceptorOfFramework = new HandlerInterceptorSub();
    HandlerInterceptor interceptorOfTab = new HandlerInterceptorSub();
    HandlerInterceptor[] interceptorsOfFramework = new HandlerInterceptor[] { interceptorOfFramework };
    HandlerInterceptor[] interceptorsOfTab = new HandlerInterceptor[] { interceptorOfTab };
    Mock proceedingJoinPoint = mock(ProceedingJoinPoint.class);
    proceedingJoinPoint.expects(once()).method("proceed").will(returnValue(new HandlerExecutionChain(null, interceptorsOfTab)));
    InterceptorInjector injector = new InterceptorInjector();
    injector.setInterceptors(interceptorsOfFramework);
    HandlerExecutionChain handlers = injector.mergeInterceptorsToTabs((ProceedingJoinPoint) proceedingJoinPoint.proxy());
    assertEquals(2, handlers.getInterceptors().length);
    assertSame(interceptorOfFramework, handlers.getInterceptors()[0]);
    assertSame(interceptorOfTab, handlers.getInterceptors()[1]);
}
Also used : HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) Mock(org.jmock.Mock)

Example 27 with HandlerExecutionChain

use of org.springframework.web.servlet.HandlerExecutionChain in project gocd by gocd.

the class InterceptorInjectorTest method testShouldJustReturnInterceptorsOfFrameworkIfNoTabInterceptors.

public void testShouldJustReturnInterceptorsOfFrameworkIfNoTabInterceptors() throws Throwable {
    HandlerInterceptor interceptorOfFramework = new HandlerInterceptorSub();
    HandlerInterceptor[] interceptorsOfFramework = new HandlerInterceptor[] { interceptorOfFramework };
    Mock proceedingJoinPoint = mock(ProceedingJoinPoint.class);
    proceedingJoinPoint.expects(once()).method("proceed").will(returnValue(new HandlerExecutionChain(null, null)));
    InterceptorInjector injector = new InterceptorInjector();
    injector.setInterceptors(interceptorsOfFramework);
    HandlerExecutionChain handlers = injector.mergeInterceptorsToTabs((ProceedingJoinPoint) proceedingJoinPoint.proxy());
    assertEquals(1, handlers.getInterceptors().length);
    assertSame(interceptorOfFramework, handlers.getInterceptors()[0]);
}
Also used : HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) Mock(org.jmock.Mock)

Example 28 with HandlerExecutionChain

use of org.springframework.web.servlet.HandlerExecutionChain in project gocd by gocd.

the class InterceptorInjectorTest method testShouldNotChangeHandler.

public void testShouldNotChangeHandler() throws Throwable {
    SimpleUrlHandlerMapping handler = new SimpleUrlHandlerMapping();
    Mock proceedingJoinPoint = mock(ProceedingJoinPoint.class);
    proceedingJoinPoint.expects(once()).method("proceed").will(returnValue(new HandlerExecutionChain(handler, null)));
    InterceptorInjector injector = new InterceptorInjector();
    HandlerExecutionChain handlers = injector.mergeInterceptorsToTabs((ProceedingJoinPoint) proceedingJoinPoint.proxy());
    assertSame(handler, handlers.getHandler());
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) Mock(org.jmock.Mock)

Example 29 with HandlerExecutionChain

use of org.springframework.web.servlet.HandlerExecutionChain in project gocd by gocd.

the class InterceptorInjectorTest method testShouldReturnNullWhenNoHandlerFound.

public void testShouldReturnNullWhenNoHandlerFound() throws Throwable {
    Mock proceedingJoinPoint = mock(ProceedingJoinPoint.class);
    proceedingJoinPoint.expects(once()).method("proceed").will(returnValue(null));
    InterceptorInjector injector = new InterceptorInjector();
    HandlerExecutionChain handlers = injector.mergeInterceptorsToTabs((ProceedingJoinPoint) proceedingJoinPoint.proxy());
    assertNull(handlers);
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) Mock(org.jmock.Mock)

Example 30 with HandlerExecutionChain

use of org.springframework.web.servlet.HandlerExecutionChain in project spring-framework by spring-projects.

the class AbstractHandlerMapping method getCorsHandlerExecutionChain.

/**
	 * Update the HandlerExecutionChain for CORS-related handling.
	 * <p>For pre-flight requests, the default implementation replaces the selected
	 * handler with a simple HttpRequestHandler that invokes the configured
	 * {@link #setCorsProcessor}.
	 * <p>For actual requests, the default implementation inserts a
	 * HandlerInterceptor that makes CORS-related checks and adds CORS headers.
	 * @param request the current request
	 * @param chain the handler chain
	 * @param config the applicable CORS configuration, possibly {@code null}
	 * @since 4.2
	 */
protected HandlerExecutionChain getCorsHandlerExecutionChain(HttpServletRequest request, HandlerExecutionChain chain, CorsConfiguration config) {
    if (CorsUtils.isPreFlightRequest(request)) {
        HandlerInterceptor[] interceptors = chain.getInterceptors();
        chain = new HandlerExecutionChain(new PreFlightHandler(config), interceptors);
    } else {
        chain.addInterceptor(new CorsInterceptor(config));
    }
    return chain;
}
Also used : HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain)

Aggregations

HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)61 Test (org.junit.Test)47 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)28 CorsConfiguration (org.springframework.web.cors.CorsConfiguration)16 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)11 HandlerInterceptor (org.springframework.web.servlet.HandlerInterceptor)9 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)8 ConversionServiceExposingInterceptor (org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor)7 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)7 HandlerMethod (org.springframework.web.method.HandlerMethod)6 ModelAndView (org.springframework.web.servlet.ModelAndView)6 LocaleChangeInterceptor (org.springframework.web.servlet.i18n.LocaleChangeInterceptor)5 ThemeChangeInterceptor (org.springframework.web.servlet.theme.ThemeChangeInterceptor)5 Mock (org.jmock.Mock)4 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)3 NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)3 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)3 HttpRequestHandlerAdapter (org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ApplicationContext (org.springframework.context.ApplicationContext)2