Search in sources :

Example 1 with SimpleUrlHandlerMapping

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

the class WebMvcAutoConfigurationTests method getMappingLocations.

@SuppressWarnings("unchecked")
protected Map<String, List<Resource>> getMappingLocations(HandlerMapping mapping) throws IllegalAccessException {
    Map<String, List<Resource>> mappingLocations = new LinkedHashMap<>();
    if (mapping instanceof SimpleUrlHandlerMapping) {
        Field locationsField = ReflectionUtils.findField(ResourceHttpRequestHandler.class, "locations");
        locationsField.setAccessible(true);
        for (Map.Entry<String, Object> entry : ((SimpleUrlHandlerMapping) mapping).getHandlerMap().entrySet()) {
            ResourceHttpRequestHandler handler = (ResourceHttpRequestHandler) entry.getValue();
            mappingLocations.put(entry.getKey(), (List<Resource>) locationsField.get(handler));
        }
    }
    return mappingLocations;
}
Also used : Field(java.lang.reflect.Field) ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with SimpleUrlHandlerMapping

use of org.springframework.web.servlet.handler.SimpleUrlHandlerMapping 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 3 with SimpleUrlHandlerMapping

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

the class ViewControllerRegistry method getHandlerMapping.

/**
	 * Return the {@code HandlerMapping} that contains the registered view
	 * controller mappings, or {@code null} for no registrations.
	 */
protected AbstractHandlerMapping getHandlerMapping() {
    if (this.registrations.isEmpty() && this.redirectRegistrations.isEmpty()) {
        return null;
    }
    Map<String, Object> urlMap = new LinkedHashMap<>();
    for (ViewControllerRegistration registration : this.registrations) {
        urlMap.put(registration.getUrlPath(), registration.getViewController());
    }
    for (RedirectViewControllerRegistration registration : this.redirectRegistrations) {
        urlMap.put(registration.getUrlPath(), registration.getViewController());
    }
    SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
    handlerMapping.setOrder(this.order);
    handlerMapping.setUrlMap(urlMap);
    return handlerMapping;
}
Also used : SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with SimpleUrlHandlerMapping

use of org.springframework.web.servlet.handler.SimpleUrlHandlerMapping 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 5 with SimpleUrlHandlerMapping

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

the class ResourceUrlProvider method detectResourceHandlers.

protected void detectResourceHandlers(ApplicationContext appContext) {
    logger.debug("Looking for resource handler mappings");
    Map<String, SimpleUrlHandlerMapping> map = appContext.getBeansOfType(SimpleUrlHandlerMapping.class);
    List<SimpleUrlHandlerMapping> handlerMappings = new ArrayList<>(map.values());
    AnnotationAwareOrderComparator.sort(handlerMappings);
    for (SimpleUrlHandlerMapping hm : handlerMappings) {
        for (String pattern : hm.getHandlerMap().keySet()) {
            Object handler = hm.getHandlerMap().get(pattern);
            if (handler instanceof ResourceHttpRequestHandler) {
                ResourceHttpRequestHandler resourceHandler = (ResourceHttpRequestHandler) handler;
                if (logger.isDebugEnabled()) {
                    logger.debug("Found resource handler mapping: URL pattern=\"" + pattern + "\", " + "locations=" + resourceHandler.getLocations() + ", " + "resolvers=" + resourceHandler.getResourceResolvers());
                }
                this.handlerMap.put(pattern, resourceHandler);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)

Aggregations

SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)36 Test (org.junit.Test)25 ResourceHttpRequestHandler (org.springframework.web.servlet.resource.ResourceHttpRequestHandler)9 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)8 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)7 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)6 Map (java.util.Map)5 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)5 HandlerMapping (org.springframework.web.servlet.HandlerMapping)5 ModelAndView (org.springframework.web.servlet.ModelAndView)5 DefaultServletHttpRequestHandler (org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler)5 HandshakeInterceptor (org.springframework.web.socket.server.HandshakeInterceptor)5 OriginHandshakeInterceptor (org.springframework.web.socket.server.support.OriginHandshakeInterceptor)5 SockJsHttpRequestHandler (org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler)5 LinkedHashMap (java.util.LinkedHashMap)3 HttpRequestHandler (org.springframework.web.HttpRequestHandler)3 ParameterizableViewController (org.springframework.web.servlet.mvc.ParameterizableViewController)3 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2