Search in sources :

Example 6 with HandlerInterceptor

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

the class HandlerMappingTests method orderedInterceptors.

@Test
public void orderedInterceptors() throws Exception {
    MappedInterceptor firstMappedInterceptor = new MappedInterceptor(new String[] { "/**" }, Mockito.mock(HandlerInterceptor.class));
    HandlerInterceptor secondHandlerInterceptor = Mockito.mock(HandlerInterceptor.class);
    MappedInterceptor thirdMappedInterceptor = new MappedInterceptor(new String[] { "/**" }, Mockito.mock(HandlerInterceptor.class));
    HandlerInterceptor fourthHandlerInterceptor = Mockito.mock(HandlerInterceptor.class);
    this.handlerMapping.setInterceptors(new Object[] { firstMappedInterceptor, secondHandlerInterceptor, thirdMappedInterceptor, fourthHandlerInterceptor });
    this.handlerMapping.setApplicationContext(this.context);
    HandlerExecutionChain chain = this.handlerMapping.getHandlerExecutionChain(new SimpleHandler(), this.request);
    Assert.assertThat(chain.getInterceptors(), Matchers.arrayContaining(firstMappedInterceptor.getInterceptor(), secondHandlerInterceptor, thirdMappedInterceptor.getInterceptor(), fourthHandlerInterceptor));
}
Also used : HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) Test(org.junit.Test)

Example 7 with HandlerInterceptor

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

the class PathMatchingUrlHandlerMappingTests method getHandler.

private HandlerExecutionChain getHandler(MockHttpServletRequest req) throws Exception {
    HandlerExecutionChain hec = hm.getHandler(req);
    HandlerInterceptor[] interceptors = hec.getInterceptors();
    if (interceptors != null) {
        for (HandlerInterceptor interceptor : interceptors) {
            interceptor.preHandle(req, null, hec.getHandler());
        }
    }
    return hec;
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor)

Example 8 with HandlerInterceptor

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

the class SimpleUrlHandlerMappingTests method getHandler.

private HandlerExecutionChain getHandler(HandlerMapping hm, MockHttpServletRequest req) throws Exception {
    HandlerExecutionChain hec = hm.getHandler(req);
    HandlerInterceptor[] interceptors = hec.getInterceptors();
    if (interceptors != null) {
        for (HandlerInterceptor interceptor : interceptors) {
            interceptor.preHandle(req, null, hec.getHandler());
        }
    }
    return hec;
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor)

Example 9 with HandlerInterceptor

use of org.springframework.web.servlet.HandlerInterceptor in project spring-integration by spring-projects.

the class CrossOriginTests method getCorsConfiguration.

private CorsConfiguration getCorsConfiguration(HandlerExecutionChain chain, boolean isPreFlightRequest) {
    if (isPreFlightRequest) {
        Object handler = chain.getHandler();
        assertTrue(handler.getClass().getSimpleName().equals("PreFlightHandler"));
        return TestUtils.getPropertyValue(handler, "config", CorsConfiguration.class);
    } else {
        HandlerInterceptor[] interceptors = chain.getInterceptors();
        if (interceptors != null) {
            for (HandlerInterceptor interceptor : interceptors) {
                if (interceptor.getClass().getSimpleName().equals("CorsInterceptor")) {
                    return TestUtils.getPropertyValue(interceptor, "config", CorsConfiguration.class);
                }
            }
        }
    }
    return null;
}
Also used : HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor)

Example 10 with HandlerInterceptor

use of org.springframework.web.servlet.HandlerInterceptor in project spring-integration by spring-projects.

the class IntegrationGraphControllerTests method testIntegrationGraphControllerParser.

@Test
public void testIntegrationGraphControllerParser() throws Exception {
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("IntegrationGraphControllerParserTests-context.xml", getClass());
    HandlerMapping handlerMapping = context.getBean(RequestMappingHandlerMapping.class.getName(), HandlerMapping.class);
    HandlerAdapter handlerAdapter = context.getBean(RequestMappingHandlerAdapter.class);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.setRequestURI("/foo");
    request.addHeader(HttpHeaders.ORIGIN, "http://foo.bar.com");
    MockHttpServletResponse response = new MockHttpServletResponse();
    HandlerExecutionChain executionChain = handlerMapping.getHandler(request);
    assertNotNull(executionChain);
    Object handler = executionChain.getHandler();
    for (HandlerInterceptor handlerInterceptor : executionChain.getInterceptors()) {
        // Assert the CORS config
        assertTrue(handlerInterceptor.preHandle(request, response, handler));
    }
    handlerAdapter.handle(request, response, handler);
    assertEquals(HttpStatus.OK.value(), response.getStatus());
    assertThat(response.getContentAsString(), containsString("\"name\":\"nullChannel\","));
    assertThat(response.getContentAsString(), not(containsString("\"name\":\"myChannel\",")));
    context.getBeanFactory().registerSingleton("myChannel", new DirectChannel());
    request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.setRequestURI("/foo/refresh");
    response = new MockHttpServletResponse();
    executionChain = handlerMapping.getHandler(request);
    assertNotNull(executionChain);
    handler = executionChain.getHandler();
    for (HandlerInterceptor handlerInterceptor : executionChain.getInterceptors()) {
        // Assert the CORS config
        assertTrue(handlerInterceptor.preHandle(request, response, handler));
    }
    handlerAdapter.handle(request, response, handler);
    assertEquals(HttpStatus.OK.value(), response.getStatus());
    assertThat(response.getContentAsString(), containsString("\"name\":\"myChannel\","));
    context.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) HandlerAdapter(org.springframework.web.servlet.HandlerAdapter) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) HandlerMapping(org.springframework.web.servlet.HandlerMapping) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) DirectChannel(org.springframework.integration.channel.DirectChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

HandlerInterceptor (org.springframework.web.servlet.HandlerInterceptor)30 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)14 Test (org.junit.jupiter.api.Test)10 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)8 Test (org.junit.Test)6 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)6 CorsConfiguration (org.springframework.web.cors.CorsConfiguration)5 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)4 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)4 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)3 ArrayList (java.util.ArrayList)3 Nullable (org.springframework.lang.Nullable)3 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)3 MappedInterceptor (org.springframework.web.servlet.handler.MappedInterceptor)3 RequestMappingHandlerAdapter (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter)3 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2