Polishing

This commit is contained in:
Juergen Hoeller
2015-09-09 13:06:03 +02:00
parent 987d93f2ce
commit 2c73c5a893
7 changed files with 32 additions and 33 deletions

View File

@@ -86,9 +86,9 @@ public class CachingResourceResolver extends AbstractResourceResolver {
protected String computeKey(HttpServletRequest request, String requestPath) {
StringBuilder key = new StringBuilder(RESOLVED_RESOURCE_CACHE_KEY_PREFIX);
key.append(requestPath);
if(request != null) {
if (request != null) {
String encoding = request.getHeader("Accept-Encoding");
if(encoding != null && encoding.contains("gzip")) {
if (encoding != null && encoding.contains("gzip")) {
key.append("+encoding=gzip");
}
}

View File

@@ -161,8 +161,10 @@ public class PathResourceResolver extends AbstractResourceResolver {
if (!resource.getClass().equals(location.getClass())) {
return false;
}
String resourcePath;
String locationPath;
if (resource instanceof UrlResource) {
resourcePath = resource.getURL().toExternalForm();
locationPath = StringUtils.cleanPath(location.getURL().toString());
@@ -179,13 +181,15 @@ public class PathResourceResolver extends AbstractResourceResolver {
resourcePath = resource.getURL().getPath();
locationPath = StringUtils.cleanPath(location.getURL().getPath());
}
if(locationPath.equals(resourcePath)) {
if (locationPath.equals(resourcePath)) {
return true;
}
locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/");
if (!resourcePath.startsWith(locationPath)) {
return false;
}
if (resourcePath.contains("%")) {
// Use URLDecoder (vs UriUtils) to preserve potentially decoded UTF-8 chars...
if (URLDecoder.decode(resourcePath, "UTF-8").contains("../")) {
@@ -195,6 +199,7 @@ public class PathResourceResolver extends AbstractResourceResolver {
return false;
}
}
return true;
}

View File

@@ -36,7 +36,6 @@ import org.springframework.util.PathMatcher;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.util.UrlPathHelper;
/**
* A central component to use to obtain the public URL path that clients should
* use to access a static resource.
@@ -130,7 +129,7 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
if (this.handlerMap.isEmpty() && logger.isDebugEnabled()) {
logger.debug("No resource handling mappings found");
}
if(!this.handlerMap.isEmpty()) {
if (!this.handlerMap.isEmpty()) {
this.autodetect = false;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.resource;
import java.util.ArrayList;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,7 +30,6 @@ import javax.servlet.jsp.PageContext;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.support.RequestDataValueProcessor;
import org.springframework.web.util.HtmlUtils;
import org.springframework.web.util.JavaScriptUtils;
import org.springframework.web.util.TagUtils;
import org.springframework.web.util.UriUtils;
@@ -311,10 +310,12 @@ public class UrlTag extends HtmlEscapingAwareTag implements ParamAware {
return uri;
}
/**
* Internal enum that classifies URLs by type.
*/
private enum UrlType {
CONTEXT_RELATIVE, RELATIVE, ABSOLUTE
}

View File

@@ -89,9 +89,8 @@ import org.springframework.web.servlet.ViewResolver;
* @see InternalResourceViewResolver
* @see BeanNameViewResolver
*/
public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport implements ViewResolver, Ordered, InitializingBean {
private static final Log logger = LogFactory.getLog(ContentNegotiatingViewResolver.class);
public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
implements ViewResolver, Ordered, InitializingBean {
private int order = Ordered.HIGHEST_PRECEDENCE;
@@ -267,7 +266,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
}
}
else {
for (int i=0; i < viewResolvers.size(); i++) {
for (int i = 0; i < viewResolvers.size(); i++) {
if (matchingBeans.contains(viewResolvers.get(i))) {
continue;
}
@@ -326,8 +325,8 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
ServletWebRequest webRequest = new ServletWebRequest(request);
List<MediaType> acceptableMediaTypes = this.contentNegotiationManager.resolveMediaTypes(webRequest);
acceptableMediaTypes = acceptableMediaTypes.isEmpty() ?
Collections.singletonList(MediaType.ALL) : acceptableMediaTypes;
acceptableMediaTypes = (!acceptableMediaTypes.isEmpty() ? acceptableMediaTypes :
Collections.singletonList(MediaType.ALL));
List<MediaType> producibleMediaTypes = getProducibleMediaTypes(request);
Set<MediaType> compatibleMediaTypes = new LinkedHashSet<MediaType>();
@@ -369,7 +368,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
*/
private MediaType getMostSpecificMediaType(MediaType acceptType, MediaType produceType) {
produceType = produceType.copyQualityValue(acceptType);
return MediaType.SPECIFICITY_COMPARATOR.compare(acceptType, produceType) < 0 ? acceptType : produceType;
return (MediaType.SPECIFICITY_COMPARATOR.compare(acceptType, produceType) < 0 ? acceptType : produceType);
}
private List<View> getCandidateViews(String viewName, Locale locale, List<MediaType> requestedMediaTypes)
@@ -416,8 +415,8 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
MediaType candidateContentType = MediaType.parseMediaType(candidateView.getContentType());
if (mediaType.isCompatibleWith(candidateContentType)) {
if (logger.isDebugEnabled()) {
logger.debug("Returning [" + candidateView + "] based on requested media type '"
+ mediaType + "'");
logger.debug("Returning [" + candidateView + "] based on requested media type '" +
mediaType + "'");
}
attrs.setAttribute(View.SELECTED_CONTENT_TYPE, mediaType, RequestAttributes.SCOPE_REQUEST);
return candidateView;