Search in sources :

Example 31 with HandlerExecutionChain

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

the class CloudFoundryEndpointHandlerMappingTests method registersCloudFoundryDiscoveryEndpoint.

@Test
public void registersCloudFoundryDiscoveryEndpoint() throws Exception {
    StaticApplicationContext context = new StaticApplicationContext();
    CloudFoundryEndpointHandlerMapping handlerMapping = new CloudFoundryEndpointHandlerMapping(Collections.<NamedMvcEndpoint>emptySet(), null, null);
    handlerMapping.setPrefix("/test");
    handlerMapping.setApplicationContext(context);
    handlerMapping.afterPropertiesSet();
    HandlerExecutionChain handler = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/test"));
    HandlerMethod handlerMethod = (HandlerMethod) handler.getHandler();
    assertThat(handlerMethod.getBean()).isInstanceOf(CloudFoundryDiscoveryMvcEndpoint.class);
}
Also used : StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 32 with HandlerExecutionChain

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

the class SimpleUrlHandlerMappingTests method testNewlineInRequest.

@Test
public void testNewlineInRequest() throws Exception {
    SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
    handlerMapping.setUrlDecode(false);
    Object controller = new Object();
    Map<String, Object> urlMap = new LinkedHashMap<>();
    urlMap.put("/*/baz", controller);
    handlerMapping.setUrlMap(urlMap);
    handlerMapping.setApplicationContext(new StaticApplicationContext());
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo%0a%0dbar/baz");
    HandlerExecutionChain hec = handlerMapping.getHandler(request);
    assertNotNull(hec);
    assertSame(controller, hec.getHandler());
}
Also used : StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 33 with HandlerExecutionChain

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

the class SimpleUrlHandlerMappingTests method checkMappings.

private void checkMappings(String beanName) throws Exception {
    MockServletContext sc = new MockServletContext("");
    XmlWebApplicationContext wac = new XmlWebApplicationContext();
    wac.setServletContext(sc);
    wac.setConfigLocations(new String[] { "/org/springframework/web/servlet/handler/map2.xml" });
    wac.refresh();
    Object bean = wac.getBean("mainController");
    Object otherBean = wac.getBean("otherController");
    Object defaultBean = wac.getBean("starController");
    HandlerMapping hm = (HandlerMapping) wac.getBean(beanName);
    MockHttpServletRequest req = new MockHttpServletRequest("GET", "/welcome.html");
    HandlerExecutionChain hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    assertEquals("/welcome.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    req = new MockHttpServletRequest("GET", "/welcome.x");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == otherBean);
    assertEquals("welcome.x", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    req = new MockHttpServletRequest("GET", "/welcome/");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == otherBean);
    assertEquals("welcome", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    req = new MockHttpServletRequest("GET", "/");
    req.setServletPath("/welcome.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/welcome.html");
    req.setContextPath("/app");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/show.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/bookseats.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/original-welcome.html");
    req.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/welcome.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/original-show.html");
    req.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/show.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/original-bookseats.html");
    req.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/bookseats.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    assertEquals("/", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    req = new MockHttpServletRequest("GET", "/somePath");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    assertEquals("/somePath", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
}
Also used : XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) HandlerMapping(org.springframework.web.servlet.HandlerMapping) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext)

Example 34 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)

Example 35 with HandlerExecutionChain

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

the class AbstractHandlerMapping method getHandlerExecutionChain.

/**
	 * Build a {@link HandlerExecutionChain} for the given handler, including
	 * applicable interceptors.
	 * <p>The default implementation builds a standard {@link HandlerExecutionChain}
	 * with the given handler, the handler mapping's common interceptors, and any
	 * {@link MappedInterceptor}s matching to the current request URL. Interceptors
	 * are added in the order they were registered. Subclasses may override this
	 * in order to extend/rearrange the list of interceptors.
	 * <p><b>NOTE:</b> The passed-in handler object may be a raw handler or a
	 * pre-built {@link HandlerExecutionChain}. This method should handle those
	 * two cases explicitly, either building a new {@link HandlerExecutionChain}
	 * or extending the existing chain.
	 * <p>For simply adding an interceptor in a custom subclass, consider calling
	 * {@code super.getHandlerExecutionChain(handler, request)} and invoking
	 * {@link HandlerExecutionChain#addInterceptor} on the returned chain object.
	 * @param handler the resolved handler instance (never {@code null})
	 * @param request current HTTP request
	 * @return the HandlerExecutionChain (never {@code null})
	 * @see #getAdaptedInterceptors()
	 */
protected HandlerExecutionChain getHandlerExecutionChain(Object handler, HttpServletRequest request) {
    HandlerExecutionChain chain = (handler instanceof HandlerExecutionChain ? (HandlerExecutionChain) handler : new HandlerExecutionChain(handler));
    String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
    for (HandlerInterceptor interceptor : this.adaptedInterceptors) {
        if (interceptor instanceof MappedInterceptor) {
            MappedInterceptor mappedInterceptor = (MappedInterceptor) interceptor;
            if (mappedInterceptor.matches(lookupPath, this.pathMatcher)) {
                chain.addInterceptor(mappedInterceptor.getInterceptor());
            }
        } else {
            chain.addInterceptor(interceptor);
        }
    }
    return chain;
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor)

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