ServletContextResourcePatternResolver uses encoded jar file location for UrlResource

Adding overloaded constructors for URI specification to UrlResource, as a convenience.

Issue: SPR-10471
This commit is contained in:
Juergen Hoeller 2013-05-10 22:11:40 +02:00
parent 0634555424
commit 1f0f46fb06
2 changed files with 68 additions and 28 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 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.
@ -22,6 +22,7 @@ import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
@ -40,6 +41,11 @@ import org.springframework.util.StringUtils;
*/ */
public class UrlResource extends AbstractFileResolvingResource { public class UrlResource extends AbstractFileResolvingResource {
/**
* Original URI, if available; used for URI and File access.
*/
private final URI uri;
/** /**
* Original URL, used for actual access. * Original URL, used for actual access.
*/ */
@ -50,14 +56,21 @@ public class UrlResource extends AbstractFileResolvingResource {
*/ */
private final URL cleanedUrl; private final URL cleanedUrl;
/** /**
* Original URI, if available; used for URI and File access. * Create a new UrlResource based on the given URI object.
* @param uri a URI
* @throws MalformedURLException if the given URL path is not valid
*/ */
private final URI uri; public UrlResource(URI uri) throws MalformedURLException {
Assert.notNull(uri, "URI must not be null");
this.uri = uri;
this.url = uri.toURL();
this.cleanedUrl = getCleanedUrl(this.url, uri.toString());
}
/** /**
* Create a new UrlResource. * Create a new UrlResource based on the given URL object.
* @param url a URL * @param url a URL
*/ */
public UrlResource(URL url) { public UrlResource(URL url) {
@ -68,27 +81,56 @@ public class UrlResource extends AbstractFileResolvingResource {
} }
/** /**
* Create a new UrlResource. * Create a new UrlResource based on a URL path.
* @param uri a URI * <p>Note: The given path needs to be pre-encoded if necessary.
* @throws MalformedURLException if the given URL path is not valid
*/
public UrlResource(URI uri) throws MalformedURLException {
Assert.notNull(uri, "URI must not be null");
this.url = uri.toURL();
this.cleanedUrl = getCleanedUrl(this.url, uri.toString());
this.uri = uri;
}
/**
* Create a new UrlResource.
* @param path a URL path * @param path a URL path
* @throws MalformedURLException if the given URL path is not valid * @throws MalformedURLException if the given URL path is not valid
* @see java.net.URL#URL(String)
*/ */
public UrlResource(String path) throws MalformedURLException { public UrlResource(String path) throws MalformedURLException {
Assert.notNull(path, "Path must not be null"); Assert.notNull(path, "Path must not be null");
this.uri = null;
this.url = new URL(path); this.url = new URL(path);
this.cleanedUrl = getCleanedUrl(this.url, path); this.cleanedUrl = getCleanedUrl(this.url, path);
this.uri = null; }
/**
* Create a new UrlResource based on a URI specification.
* <p>The given parts will automatically get encoded if necessary.
* @param protocol the URL protocol to use (e.g. "jar" or "file" - without colon);
* also known as "scheme"
* @param location the location (e.g. the file path within that protocol);
* also known as "scheme-specific part"
* @throws MalformedURLException if the given URL specification is not valid
* @see java.net.URI#URI(String, String, String)
*/
public UrlResource(String protocol, String location) throws MalformedURLException {
this(protocol, location, null);
}
/**
* Create a new UrlResource based on a URI specification.
* <p>The given parts will automatically get encoded if necessary.
* @param protocol the URL protocol to use (e.g. "jar" or "file" - without colon);
* also known as "scheme"
* @param location the location (e.g. the file path within that protocol);
* also known as "scheme-specific part"
* @param fragment the fragment within that location (e.g. anchor on an HTML page,
* as following after a "#" separator)
* @throws MalformedURLException if the given URL specification is not valid
* @see java.net.URI#URI(String, String, String)
*/
public UrlResource(String protocol, String location, String fragment) throws MalformedURLException {
try {
this.uri = new URI(protocol, location, fragment);
this.url = this.uri.toURL();
this.cleanedUrl = getCleanedUrl(this.url, this.uri.toString());
}
catch (URISyntaxException ex) {
MalformedURLException exToThrow = new MalformedURLException(ex.getMessage());
exToThrow.initCause(ex);
throw exToThrow;
}
} }
/** /**

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 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.
@ -109,7 +109,7 @@ public class ServletContextResourcePatternResolver extends PathMatchingResourceP
ServletContext servletContext, String fullPattern, String dir, Set<Resource> result) ServletContext servletContext, String fullPattern, String dir, Set<Resource> result)
throws IOException { throws IOException {
Set candidates = servletContext.getResourcePaths(dir); Set<String> candidates = servletContext.getResourcePaths(dir);
if (candidates != null) { if (candidates != null) {
boolean dirDepthNotFixed = fullPattern.contains("**"); boolean dirDepthNotFixed = fullPattern.contains("**");
int jarFileSep = fullPattern.indexOf(ResourceUtils.JAR_URL_SEPARATOR); int jarFileSep = fullPattern.indexOf(ResourceUtils.JAR_URL_SEPARATOR);
@ -119,8 +119,7 @@ public class ServletContextResourcePatternResolver extends PathMatchingResourceP
jarFilePath = fullPattern.substring(0, jarFileSep); jarFilePath = fullPattern.substring(0, jarFileSep);
pathInJarFile = fullPattern.substring(jarFileSep + ResourceUtils.JAR_URL_SEPARATOR.length()); pathInJarFile = fullPattern.substring(jarFileSep + ResourceUtils.JAR_URL_SEPARATOR.length());
} }
for (Object candidate : candidates) { for (String currPath : candidates) {
String currPath = (String) candidate;
if (!currPath.startsWith(dir)) { if (!currPath.startsWith(dir)) {
// Returned resource path does not start with relative directory: // Returned resource path does not start with relative directory:
// assuming absolute path returned -> strip absolute path. // assuming absolute path returned -> strip absolute path.
@ -150,11 +149,10 @@ public class ServletContextResourcePatternResolver extends PathMatchingResourceP
} }
/** /**
* Method extracts entries from the given jar by pattern. * Extract entries from the given jar by pattern.
* @param jarFilePath the path to the jar file * @param jarFilePath the path to the jar file
* @param entryPattern the pattern for jar entries to match * @param entryPattern the pattern for jar entries to match
* @param result the Set of matching Resources to add to * @param result the Set of matching Resources to add to
* @throws IOException if jar contents could not be retrieved
*/ */
private void doRetrieveMatchingJarEntries(String jarFilePath, String entryPattern, Set<Resource> result) { private void doRetrieveMatchingJarEntries(String jarFilePath, String entryPattern, Set<Resource> result) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
@ -166,9 +164,9 @@ public class ServletContextResourcePatternResolver extends PathMatchingResourceP
JarEntry entry = entries.nextElement(); JarEntry entry = entries.nextElement();
String entryPath = entry.getName(); String entryPath = entry.getName();
if (getPathMatcher().match(entryPattern, entryPath)) { if (getPathMatcher().match(entryPattern, entryPath)) {
result.add(new UrlResource(ResourceUtils.URL_PROTOCOL_JAR + ":" + result.add(new UrlResource(
ResourceUtils.URL_PROTOCOL_FILE + ":" + jarFilePath + ResourceUtils.URL_PROTOCOL_JAR,
ResourceUtils.JAR_URL_SEPARATOR + entryPath)); ResourceUtils.FILE_URL_PREFIX + jarFilePath + ResourceUtils.JAR_URL_SEPARATOR + entryPath));
} }
} }
} }