From 2c73c5a89336f29d30c819e1fcda512061b20307 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 9 Sep 2015 13:06:03 +0200 Subject: [PATCH] Polishing --- .../test/util/XpathExpectationsHelper.java | 26 +++++++------------ .../resource/CachingResourceResolver.java | 4 +-- .../resource/PathResourceResolver.java | 7 ++++- .../servlet/resource/ResourceUrlProvider.java | 3 +-- .../resource/VersionResourceResolver.java | 3 ++- .../web/servlet/tags/UrlTag.java | 5 ++-- .../view/ContentNegotiatingViewResolver.java | 17 ++++++------ 7 files changed, 32 insertions(+), 33 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/util/XpathExpectationsHelper.java b/spring-test/src/main/java/org/springframework/test/util/XpathExpectationsHelper.java index f7fac1a17d..0e5ba81e7e 100644 --- a/spring-test/src/main/java/org/springframework/test/util/XpathExpectationsHelper.java +++ b/spring-test/src/main/java/org/springframework/test/util/XpathExpectationsHelper.java @@ -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 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}. + * Parse the content, evaluate the XPath expression as a {@link Node}, + * and assert it with the given {@code Matcher}. */ - public void assertNode(String content, final Matcher matcher) throws Exception { + public void assertNode(String content, Matcher 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 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 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 matcher) throws Exception { @@ -222,4 +216,4 @@ public class XpathExpectationsHelper { assertEquals("XPath " + this.expression, expectedValue, Boolean.parseBoolean(actual)); } -} \ No newline at end of file +} diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/CachingResourceResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/CachingResourceResolver.java index e3d788f41f..669e5a2fdf 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/CachingResourceResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/CachingResourceResolver.java @@ -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"); } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java index 0b3f777e12..fc964907c7 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java @@ -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; } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlProvider.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlProvider.java index 47c0eb9c8f..dcd2927567 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlProvider.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlProvider.java @@ -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 acceptableMediaTypes = this.contentNegotiationManager.resolveMediaTypes(webRequest); - acceptableMediaTypes = acceptableMediaTypes.isEmpty() ? - Collections.singletonList(MediaType.ALL) : acceptableMediaTypes; + acceptableMediaTypes = (!acceptableMediaTypes.isEmpty() ? acceptableMediaTypes : + Collections.singletonList(MediaType.ALL)); List producibleMediaTypes = getProducibleMediaTypes(request); Set compatibleMediaTypes = new LinkedHashSet(); @@ -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 getCandidateViews(String viewName, Locale locale, List 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;