|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2013 the original author or authors. |
|
|
|
* Copyright 2002-2012 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -17,7 +17,6 @@ |
|
|
|
package org.springframework.web.servlet.config.annotation; |
|
|
|
package org.springframework.web.servlet.config.annotation; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Arrays; |
|
|
|
|
|
|
|
import java.util.LinkedHashMap; |
|
|
|
import java.util.LinkedHashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
@ -32,8 +31,6 @@ import org.springframework.web.servlet.HandlerMapping; |
|
|
|
import org.springframework.web.servlet.handler.AbstractHandlerMapping; |
|
|
|
import org.springframework.web.servlet.handler.AbstractHandlerMapping; |
|
|
|
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; |
|
|
|
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; |
|
|
|
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; |
|
|
|
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; |
|
|
|
import org.springframework.web.servlet.resource.ResourceResolver; |
|
|
|
|
|
|
|
import org.springframework.web.servlet.resource.ResourceTransformer; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Stores registrations of resource handlers for serving static resources such as images, css files and others |
|
|
|
* Stores registrations of resource handlers for serving static resources such as images, css files and others |
|
|
|
@ -60,13 +57,8 @@ public class ResourceHandlerRegistry { |
|
|
|
|
|
|
|
|
|
|
|
private final List<ResourceHandlerRegistration> registrations = new ArrayList<ResourceHandlerRegistration>(); |
|
|
|
private final List<ResourceHandlerRegistration> registrations = new ArrayList<ResourceHandlerRegistration>(); |
|
|
|
|
|
|
|
|
|
|
|
private List<ResourceResolver> resourceResolvers; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private List<ResourceTransformer> resourceTransformers; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int order = Integer.MAX_VALUE -1; |
|
|
|
private int order = Integer.MAX_VALUE -1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ResourceHandlerRegistry(ApplicationContext applicationContext, ServletContext servletContext) { |
|
|
|
public ResourceHandlerRegistry(ApplicationContext applicationContext, ServletContext servletContext) { |
|
|
|
Assert.notNull(applicationContext, "ApplicationContext is required"); |
|
|
|
Assert.notNull(applicationContext, "ApplicationContext is required"); |
|
|
|
this.applicationContext = applicationContext; |
|
|
|
this.applicationContext = applicationContext; |
|
|
|
@ -84,18 +76,6 @@ public class ResourceHandlerRegistry { |
|
|
|
return registration; |
|
|
|
return registration; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Whether a resource handler has already been registered for the given pathPattern. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public boolean hasMappingForPattern(String pathPattern) { |
|
|
|
|
|
|
|
for (ResourceHandlerRegistration registration : registrations) { |
|
|
|
|
|
|
|
if (Arrays.asList(registration.getPathPatterns()).contains(pathPattern)) { |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Specify the order to use for resource handling relative to other {@link HandlerMapping}s configured in |
|
|
|
* Specify the order to use for resource handling relative to other {@link HandlerMapping}s configured in |
|
|
|
* the Spring MVC application context. The default value used is {@code Integer.MAX_VALUE-1}. |
|
|
|
* the Spring MVC application context. The default value used is {@code Integer.MAX_VALUE-1}. |
|
|
|
@ -105,22 +85,6 @@ public class ResourceHandlerRegistry { |
|
|
|
return this; |
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Configure the {@link ResourceResolver}s to use by default in resource handlers that |
|
|
|
|
|
|
|
* don't have them set. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public void setResourceResolvers(List<ResourceResolver> resourceResolvers) { |
|
|
|
|
|
|
|
this.resourceResolvers = resourceResolvers; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Configure the {@link ResourceTransformer}s to use by default in resource handlers |
|
|
|
|
|
|
|
* that don't have them set. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public void setResourceTransformers(List<ResourceTransformer> transformers) { |
|
|
|
|
|
|
|
this.resourceTransformers = transformers; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Return a handler mapping with the mapped resource handlers; or {@code null} in case of no registrations. |
|
|
|
* Return a handler mapping with the mapped resource handlers; or {@code null} in case of no registrations. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@ -132,22 +96,16 @@ public class ResourceHandlerRegistry { |
|
|
|
Map<String, HttpRequestHandler> urlMap = new LinkedHashMap<String, HttpRequestHandler>(); |
|
|
|
Map<String, HttpRequestHandler> urlMap = new LinkedHashMap<String, HttpRequestHandler>(); |
|
|
|
for (ResourceHandlerRegistration registration : registrations) { |
|
|
|
for (ResourceHandlerRegistration registration : registrations) { |
|
|
|
for (String pathPattern : registration.getPathPatterns()) { |
|
|
|
for (String pathPattern : registration.getPathPatterns()) { |
|
|
|
ResourceHttpRequestHandler handler = registration.getRequestHandler(); |
|
|
|
ResourceHttpRequestHandler requestHandler = registration.getRequestHandler(); |
|
|
|
handler.setServletContext(servletContext); |
|
|
|
requestHandler.setServletContext(servletContext); |
|
|
|
handler.setApplicationContext(applicationContext); |
|
|
|
requestHandler.setApplicationContext(applicationContext); |
|
|
|
if ((this.resourceResolvers != null) && (registration.getResourceResolvers() == null)) { |
|
|
|
|
|
|
|
handler.setResourceResolvers(this.resourceResolvers); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if ((this.resourceTransformers != null) && (registration.getResourceTransformers() == null)) { |
|
|
|
|
|
|
|
handler.setResourceTransformers(this.resourceTransformers); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
handler.afterPropertiesSet(); |
|
|
|
requestHandler.afterPropertiesSet(); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception e) { |
|
|
|
catch (Exception e) { |
|
|
|
throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", e); |
|
|
|
throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", e); |
|
|
|
} |
|
|
|
} |
|
|
|
urlMap.put(pathPattern, handler); |
|
|
|
urlMap.put(pathPattern, requestHandler); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|