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
This commit is contained in:
Jan Bartel 2009-06-08 07:54:24 +00:00
parent 575bd42a7d
commit 4a38907a2c
2 changed files with 37 additions and 0 deletions

View File

@ -15,6 +15,7 @@ package org.eclipse.jetty.util.resource;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.JarURLConnection; import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
@ -318,6 +319,26 @@ class JarFileResource extends JarResource
return newResource; 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());
}
} }

View File

@ -255,6 +255,22 @@ public class ResourceTest extends junit.framework.TestCase
assertNotNull(jin); 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. * Test a class path resource for existence.