From 4a38907a2c2ac3805d09008dbcd8af515fffcb32 Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Mon, 8 Jun 2009 07:54:24 +0000 Subject: [PATCH] Add JarFileResource.isContainedIn(Resource). git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@339 7e9141cc-0065-0410-87d8-b60c137991c4 --- .../jetty/util/resource/JarFileResource.java | 21 +++++++++++++++++++ .../jetty/util/resource/ResourceTest.java | 16 ++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/JarFileResource.java b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/JarFileResource.java index 3953bd02955..8dd5e59ecfc 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/JarFileResource.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/JarFileResource.java @@ -15,6 +15,7 @@ package org.eclipse.jetty.util.resource; import java.io.File; import java.io.IOException; import java.net.JarURLConnection; +import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; @@ -318,6 +319,26 @@ class JarFileResource extends JarResource return newResource; } + + /** + * Check if this jar:file: resource is contained in the + * named resource. Eg jar:file:///a/b/c/foo.jar!/x.html isContainedIn file:///a/b/c/foo.jar + * @param resource + * @return + * @throws MalformedURLException + */ + public boolean isContainedIn (Resource resource) + throws MalformedURLException + { + String string = _urlString; + int index = string.indexOf("!/"); + if (index > 0) + string = string.substring(0,index); + if (string.startsWith("jar:")) + string = string.substring(4); + URL url = new URL(string); + return url.sameFile(resource.getURL()); + } } diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceTest.java index 81087ba4e18..2a4daa3e620 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceTest.java @@ -255,6 +255,22 @@ public class ResourceTest extends junit.framework.TestCase assertNotNull(jin); } + + public void testJarFileIsContainedIn () + throws Exception + { + String s = "jar:"+__userURL+"TestData/test.zip!/subdir/"; + Resource r = Resource.newResource(s); + Resource container = Resource.newResource(__userURL+"TestData/test.zip"); + + assertTrue(r instanceof JarFileResource); + JarFileResource jarFileResource = (JarFileResource)r; + + assertTrue(jarFileResource.isContainedIn(container)); + + container = Resource.newResource(__userURL+"TestData"); + assertFalse(jarFileResource.isContainedIn(container)); + } /** * Test a class path resource for existence.