diff --git a/spring-core/src/main/java/org/springframework/core/io/UrlResource.java b/spring-core/src/main/java/org/springframework/core/io/UrlResource.java index 5c37b0b448e..626f8f70adc 100644 --- a/spring-core/src/main/java/org/springframework/core/io/UrlResource.java +++ b/spring-core/src/main/java/org/springframework/core/io/UrlResource.java @@ -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"); * 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.MalformedURLException; import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; @@ -40,6 +41,11 @@ import org.springframework.util.StringUtils; */ 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. */ @@ -50,14 +56,21 @@ public class UrlResource extends AbstractFileResolvingResource { */ 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 */ public UrlResource(URL url) { @@ -68,27 +81,56 @@ public class UrlResource extends AbstractFileResolvingResource { } /** - * Create a new UrlResource. - * @param uri a URI - * @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. + * Create a new UrlResource based on a URL path. + *

Note: The given path needs to be pre-encoded if necessary. * @param path a URL path * @throws MalformedURLException if the given URL path is not valid + * @see java.net.URL#URL(String) */ public UrlResource(String path) throws MalformedURLException { Assert.notNull(path, "Path must not be null"); + this.uri = null; this.url = new URL(path); this.cleanedUrl = getCleanedUrl(this.url, path); - this.uri = null; + } + + /** + * Create a new UrlResource based on a URI specification. + *

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. + *

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; + } } /** diff --git a/spring-web/src/main/java/org/springframework/web/context/support/ServletContextResourcePatternResolver.java b/spring-web/src/main/java/org/springframework/web/context/support/ServletContextResourcePatternResolver.java index 8ce1a653920..378bd8e6e50 100644 --- a/spring-web/src/main/java/org/springframework/web/context/support/ServletContextResourcePatternResolver.java +++ b/spring-web/src/main/java/org/springframework/web/context/support/ServletContextResourcePatternResolver.java @@ -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"); * 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 result) throws IOException { - Set candidates = servletContext.getResourcePaths(dir); + Set candidates = servletContext.getResourcePaths(dir); if (candidates != null) { boolean dirDepthNotFixed = fullPattern.contains("**"); int jarFileSep = fullPattern.indexOf(ResourceUtils.JAR_URL_SEPARATOR); @@ -119,8 +119,7 @@ public class ServletContextResourcePatternResolver extends PathMatchingResourceP jarFilePath = fullPattern.substring(0, jarFileSep); pathInJarFile = fullPattern.substring(jarFileSep + ResourceUtils.JAR_URL_SEPARATOR.length()); } - for (Object candidate : candidates) { - String currPath = (String) candidate; + for (String currPath : candidates) { if (!currPath.startsWith(dir)) { // Returned resource path does not start with relative directory: // 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 entryPattern the pattern for jar entries to match * @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 result) { if (logger.isDebugEnabled()) { @@ -166,9 +164,9 @@ public class ServletContextResourcePatternResolver extends PathMatchingResourceP JarEntry entry = entries.nextElement(); String entryPath = entry.getName(); if (getPathMatcher().match(entryPattern, entryPath)) { - result.add(new UrlResource(ResourceUtils.URL_PROTOCOL_JAR + ":" + - ResourceUtils.URL_PROTOCOL_FILE + ":" + jarFilePath + - ResourceUtils.JAR_URL_SEPARATOR + entryPath)); + result.add(new UrlResource( + ResourceUtils.URL_PROTOCOL_JAR, + ResourceUtils.FILE_URL_PREFIX + jarFilePath + ResourceUtils.JAR_URL_SEPARATOR + entryPath)); } } }