Search in sources :

Example 1 with HttpRequestHandler

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

the class ResourceHandlerRegistry method getHandlerMapping.

/**
	 * Return a handler mapping with the mapped resource handlers; or {@code null} in case
	 * of no registrations.
	 */
protected AbstractHandlerMapping getHandlerMapping() {
    if (this.registrations.isEmpty()) {
        return null;
    }
    Map<String, HttpRequestHandler> urlMap = new LinkedHashMap<>();
    for (ResourceHandlerRegistration registration : this.registrations) {
        for (String pathPattern : registration.getPathPatterns()) {
            ResourceHttpRequestHandler handler = registration.getRequestHandler();
            handler.setServletContext(this.servletContext);
            handler.setApplicationContext(this.applicationContext);
            handler.setContentNegotiationManager(this.contentNegotiationManager);
            try {
                handler.afterPropertiesSet();
            } catch (Throwable ex) {
                throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", ex);
            }
            urlMap.put(pathPattern, handler);
        }
    }
    SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
    handlerMapping.setOrder(order);
    handlerMapping.setUrlMap(urlMap);
    return handlerMapping;
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) HttpRequestHandler(org.springframework.web.HttpRequestHandler) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with HttpRequestHandler

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

the class DefaultServletHandlerConfigurer method getHandlerMapping.

/**
	 * Return a handler mapping instance ordered at {@link Integer#MAX_VALUE} containing the
	 * {@link DefaultServletHttpRequestHandler} instance mapped to {@code "/**"}; or {@code null} if
	 * default servlet handling was not been enabled.
	 */
protected AbstractHandlerMapping getHandlerMapping() {
    if (handler == null) {
        return null;
    }
    Map<String, HttpRequestHandler> urlMap = new HashMap<>();
    urlMap.put("/**", handler);
    SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
    handlerMapping.setOrder(Integer.MAX_VALUE);
    handlerMapping.setUrlMap(urlMap);
    return handlerMapping;
}
Also used : HashMap(java.util.HashMap) DefaultServletHttpRequestHandler(org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler) HttpRequestHandler(org.springframework.web.HttpRequestHandler) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)

Example 3 with HttpRequestHandler

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

the class HttpRequestHandlerTests method testHttpRequestHandlerServletPassThrough.

@Test
public void testHttpRequestHandlerServletPassThrough() throws Exception {
    MockServletContext servletContext = new MockServletContext();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.getBeanFactory().registerSingleton("myHandler", new HttpRequestHandler() {

        @Override
        public void handleRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
            assertSame(request, req);
            assertSame(response, res);
            String exception = request.getParameter("exception");
            if ("ServletException".equals(exception)) {
                throw new ServletException("test");
            }
            if ("IOException".equals(exception)) {
                throw new IOException("test");
            }
            res.getWriter().write("myResponse");
        }
    });
    wac.setServletContext(servletContext);
    wac.refresh();
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    Servlet servlet = new HttpRequestHandlerServlet();
    servlet.init(new MockServletConfig(servletContext, "myHandler"));
    servlet.service(request, response);
    assertEquals("myResponse", response.getContentAsString());
    try {
        request.setParameter("exception", "ServletException");
        servlet.service(request, response);
        fail("Should have thrown ServletException");
    } catch (ServletException ex) {
        assertEquals("test", ex.getMessage());
    }
    try {
        request.setParameter("exception", "IOException");
        servlet.service(request, response);
        fail("Should have thrown IOException");
    } catch (IOException ex) {
        assertEquals("test", ex.getMessage());
    }
}
Also used : HttpRequestHandler(org.springframework.web.HttpRequestHandler) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) IOException(java.io.IOException) MockServletContext(org.springframework.mock.web.test.MockServletContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletException(javax.servlet.ServletException) Servlet(javax.servlet.Servlet) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 4 with HttpRequestHandler

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

the class ServletWebSocketHandlerRegistry method getHandlerMapping.

/**
	 * Return a {@link HandlerMapping} with mapped {@link HttpRequestHandler}s.
	 */
public AbstractHandlerMapping getHandlerMapping() {
    Map<String, Object> urlMap = new LinkedHashMap<>();
    for (ServletWebSocketHandlerRegistration registration : this.registrations) {
        MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
        for (HttpRequestHandler httpHandler : mappings.keySet()) {
            for (String pattern : mappings.get(httpHandler)) {
                urlMap.put(pattern, httpHandler);
            }
        }
    }
    WebSocketHandlerMapping hm = new WebSocketHandlerMapping();
    hm.setUrlMap(urlMap);
    hm.setOrder(this.order);
    if (this.urlPathHelper != null) {
        hm.setUrlPathHelper(this.urlPathHelper);
    }
    return hm;
}
Also used : HttpRequestHandler(org.springframework.web.HttpRequestHandler) WebSocketHandlerMapping(org.springframework.web.socket.server.support.WebSocketHandlerMapping) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with HttpRequestHandler

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

the class WebMvcStompEndpointRegistry method getHandlerMapping.

/**
	 * Return a handler mapping with the mapped ViewControllers; or {@code null}
	 * in case of no registrations.
	 */
public AbstractHandlerMapping getHandlerMapping() {
    Map<String, Object> urlMap = new LinkedHashMap<>();
    for (WebMvcStompWebSocketEndpointRegistration registration : this.registrations) {
        MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
        for (HttpRequestHandler httpHandler : mappings.keySet()) {
            for (String pattern : mappings.get(httpHandler)) {
                urlMap.put(pattern, httpHandler);
            }
        }
    }
    WebSocketHandlerMapping hm = new WebSocketHandlerMapping();
    hm.setUrlMap(urlMap);
    hm.setOrder(this.order);
    if (this.urlPathHelper != null) {
        hm.setUrlPathHelper(this.urlPathHelper);
    }
    return hm;
}
Also used : HttpRequestHandler(org.springframework.web.HttpRequestHandler) WebSocketHandlerMapping(org.springframework.web.socket.server.support.WebSocketHandlerMapping) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

HttpRequestHandler (org.springframework.web.HttpRequestHandler)19 Test (org.junit.Test)14 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)11 SockJsHttpRequestHandler (org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler)11 DefaultSockJsService (org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService)5 List (java.util.List)4 Map (java.util.Map)4 MultiValueMap (org.springframework.util.MultiValueMap)4 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)4 LinkedHashMap (java.util.LinkedHashMap)3 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 DefaultHandshakeHandler (org.springframework.web.socket.server.support.DefaultHandshakeHandler)3 HttpSessionHandshakeInterceptor (org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor)3 WebSocketTransportHandler (org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler)3 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)2 MessageHandler (org.springframework.messaging.MessageHandler)2 SimpAnnotationMethodMessageHandler (org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler)2 SimpleBrokerMessageHandler (org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler)2