Repository: tapestry-5 Updated Branches: refs/heads/master 6ba6bd809 -> dcef62eeb
TAP5-2448: prevent false negatives when checking if a Resource references a directory inside a JAR file Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/dcef62ee Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/dcef62ee Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/dcef62ee Branch: refs/heads/master Commit: dcef62eebf3bbf98ea459a8edf0179373eed5cb7 Parents: 6ba6bd8 Author: Jochen Kemnade <[email protected]> Authored: Thu Apr 9 16:15:53 2015 +0200 Committer: Jochen Kemnade <[email protected]> Committed: Thu Apr 9 16:15:53 2015 +0200 ---------------------------------------------------------------------- .../ioc/internal/util/AbstractResource.java | 31 ++++++++++++++++++-- .../ioc/specs/ClasspathResourceSpec.groovy | 18 ++++++++++-- 2 files changed, 43 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/dcef62ee/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/AbstractResource.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/AbstractResource.java b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/AbstractResource.java index 5f3d0fa..a05b2bc 100644 --- a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/AbstractResource.java +++ b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/AbstractResource.java @@ -12,6 +12,7 @@ package org.apache.tapestry5.ioc.internal.util; +import org.apache.commons.io.IOUtils; import org.apache.tapestry5.ioc.Resource; import org.apache.tapestry5.ioc.util.LocalizedNameGenerator; @@ -21,6 +22,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.URISyntaxException; import java.net.URL; +import java.net.URLClassLoader; import java.util.List; import java.util.Locale; @@ -281,10 +283,33 @@ public abstract class AbstractResource extends LockSupport implements Resource return null; } if ("jar".equals(url.getProtocol())){ - // TAP5-2448: make sure that the URL does not reference a directory - if (new URL(url.toString() + "/") != null) + + URLClassLoader classLoaderWithJar = null; + + try + { + + // TAP5-2448: make sure that the URL does not reference a directory + String urlAsString = url.toString(); + + int indexOfExclamationMark = url.toString().indexOf('!'); + + String jarFile = urlAsString.substring(4, indexOfExclamationMark); + String resourceInJar = urlAsString.substring(indexOfExclamationMark + 2); + + classLoaderWithJar = new URLClassLoader(new URL[]{ new URL(jarFile) }); + + boolean isDirectory = classLoaderWithJar.getResource(resourceInJar + "/") != null; + + classLoaderWithJar.close(); + + if (isDirectory) + { + throw new IOException("Cannot open a steam for a resource that references a directory inside a JAR file (" + url + ")."); + } + } finally { - throw new IOException("Cannot open a steam for a resource that references a directory inside a JAR file (" + url + ")."); + IOUtils.closeQuietly(classLoaderWithJar); } } http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/dcef62ee/tapestry-ioc/src/test/groovy/ioc/specs/ClasspathResourceSpec.groovy ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/test/groovy/ioc/specs/ClasspathResourceSpec.groovy b/tapestry-ioc/src/test/groovy/ioc/specs/ClasspathResourceSpec.groovy index e3e4310..003789a 100644 --- a/tapestry-ioc/src/test/groovy/ioc/specs/ClasspathResourceSpec.groovy +++ b/tapestry-ioc/src/test/groovy/ioc/specs/ClasspathResourceSpec.groovy @@ -253,18 +253,30 @@ class ClasspathResourceSpec extends Specification { r.forFile("../foo/bar").toString() == "classpath:foo/bar" } - + @Issue('TAP5-2448') def "Cannot open a stream for a directory resource within a JAR file"() { setup: ClasspathResource r = new ClasspathResource('org/slf4j/spi') - + when: r.openStream() - + then: IOException e = thrown() e.message.contains 'Cannot open a steam for a resource that references a directory inside a JAR file' } + @Issue('TAP5-2448') + def "Can open a stream for a file resource within a JAR file"() { + setup: + ClasspathResource r = new ClasspathResource('org/slf4j/helpers/Util.class') + + when: + r.openStream() + + then: + notThrown(IOException) + } + }
