Skip to content

Commit

Permalink
Fix for #981
Browse files Browse the repository at this point in the history
  • Loading branch information
perwendel committed Mar 6, 2018
1 parent 99d7ddc commit ce9e115
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -9,7 +9,7 @@
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<packaging>bundle</packaging>
<version>3.0.0-SNAPSHOT</version>
<version>2.7.2-SNAPSHOT</version>
<name>Spark</name>
<description>A Sinatra inspired java web framework</description>
<url>http://www.sparkjava.com</url>
Expand Down
27 changes: 15 additions & 12 deletions src/main/java/spark/resource/ClassPathResource.java
Expand Up @@ -74,14 +74,22 @@ public ClassPathResource(String path) {
*/
public ClassPathResource(String path, ClassLoader classLoader) {
Assert.notNull(path, "Path must not be null");
Assert.state(doesNotContainFileColon(path), "Path must not contain 'file:'");

String pathToUse = StringUtils.cleanPath(path);

if (pathToUse.startsWith("/")) {
pathToUse = pathToUse.substring(1);
}

this.path = pathToUse;
this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
}

private static boolean doesNotContainFileColon(String path) {
return !path.contains("file:");
}

/**
* Create a new ClassPathResource with optional ClassLoader and Class.
* Only for internal usage.
Expand All @@ -108,10 +116,9 @@ public final String getPath() {
/**
* This implementation checks for the resolution of a resource URL.
*
* @return if exists.
* @see java.lang.ClassLoader#getResource(String)
* @see java.lang.Class#getResource(String)
*
* @return if exists.
*/
@Override
public boolean exists() {
Expand All @@ -127,10 +134,9 @@ public boolean exists() {
/**
* This implementation opens an InputStream for the given class path resource.
*
* @return the input stream.
* @see java.lang.ClassLoader#getResourceAsStream(String)
* @see java.lang.Class#getResourceAsStream(String)
*
* @return the input stream.
*/
@Override
public InputStream getInputStream() throws IOException {
Expand All @@ -149,10 +155,9 @@ public InputStream getInputStream() throws IOException {
/**
* This implementation returns a URL for the underlying class path resource.
*
* @return the url.
* @see java.lang.ClassLoader#getResource(String)
* @see java.lang.Class#getResource(String)
*
* @return the url.
*/
@Override
public URL getURL() throws IOException {
Expand All @@ -172,9 +177,8 @@ public URL getURL() throws IOException {
* This implementation creates a ClassPathResource, applying the given path
* relative to the path of the underlying resource of this descriptor.
*
* @see spark.utils.StringUtils#applyRelativePath(String, String)
*
* @return the resource.
* @see spark.utils.StringUtils#applyRelativePath(String, String)
*/
@Override
public Resource createRelative(String relativePath) {
Expand All @@ -186,9 +190,8 @@ public Resource createRelative(String relativePath) {
* This implementation returns the name of the file that this class path
* resource refers to.
*
* @see spark.utils.StringUtils#getFilename(String)
*
* @return the file name.
* @see spark.utils.StringUtils#getFilename(String)
*/
@Override
public String getFilename() {
Expand Down Expand Up @@ -233,8 +236,8 @@ public boolean equals(Object obj) {
ClassLoader otherLoader = otherRes.classLoader;

return (this.path.equals(otherRes.path) &&
thisLoader.equals(otherLoader) &&
this.clazz.equals(otherRes.clazz));
thisLoader.equals(otherLoader) &&
this.clazz.equals(otherRes.clazz));
}
return false;
}
Expand Down

0 comments on commit ce9e115

Please sign in to comment.