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

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 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.
@@ -56,8 +56,7 @@ public class XpathExpectationsHelper {
/**
* Class constructor.
*
* XpathExpectationsHelper constructor.
* @param expression the XPath expression
* @param namespaces XML namespaces referenced in the XPath expression, or {@code null}
* @param args arguments to parameterize the XPath expression with using the
@@ -72,6 +71,7 @@ public class XpathExpectationsHelper {
this.hasNamespaces = !CollectionUtils.isEmpty(namespaces);
}
private XPathExpression compileXpathExpression(String expression, Map<String, String> namespaces)
throws XPathExpressionException {
@@ -83,17 +83,17 @@ public class XpathExpectationsHelper {
}
/**
* @return the compiled XPath expression.
* Return the compiled XPath expression.
*/
protected XPathExpression getXpathExpression() {
return this.xpathExpression;
}
/**
* Parse the content, evaluate the XPath expression as a {@link Node}, and
* assert it with the given {@code Matcher<Node>}.
* Parse the content, evaluate the XPath expression as a {@link Node},
* and assert it with the given {@code Matcher<Node>}.
*/
public void assertNode(String content, final Matcher<? super Node> matcher) throws Exception {
public void assertNode(String content, Matcher<? super Node> matcher) throws Exception {
Document document = parseXmlString(content);
Node node = evaluateXpath(document, XPathConstants.NODE, Node.class);
assertThat("XPath " + this.expression, node, matcher);
@@ -101,18 +101,15 @@ public class XpathExpectationsHelper {
/**
* Parse the given XML content to a {@link Document}.
*
* @param xml the content to parse
* @return the parsed document
* @throws Exception in case of errors
*/
protected Document parseXmlString(String xml) throws Exception {
protected Document parseXmlString(String xml) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(this.hasNamespaces);
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
InputSource inputSource = new InputSource(new StringReader(xml));
Document document = documentBuilder.parse(inputSource);
return document;
return documentBuilder.parse(inputSource);
}
/**
@@ -149,7 +146,6 @@ public class XpathExpectationsHelper {
/**
* Apply the XPath expression and assert the resulting content with the
* given Hamcrest matcher.
*
* @throws Exception if content parsing or expression evaluation fails
*/
public void assertNodeCount(String content, Matcher<Integer> matcher) throws Exception {
@@ -171,7 +167,6 @@ public class XpathExpectationsHelper {
/**
* Apply the XPath expression and assert the resulting content with the
* given Hamcrest matcher.
*
* @throws Exception if content parsing or expression evaluation fails
*/
public void assertString(String content, Matcher<? super String> matcher) throws Exception {
@@ -193,7 +188,6 @@ public class XpathExpectationsHelper {
/**
* Apply the XPath expression and assert the resulting content with the
* given Hamcrest matcher.
*
* @throws Exception if content parsing or expression evaluation fails
*/
public void assertNumber(String content, Matcher<? super Double> matcher) throws Exception {
@@ -222,4 +216,4 @@ public class XpathExpectationsHelper {
assertEquals("XPath " + this.expression, expectedValue, Boolean.parseBoolean(actual));
}
}
}

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;