Skip to content

Commit

Permalink
[JBVFS-118]; fix canonical url cache lookup.
Browse files Browse the repository at this point in the history
  • Loading branch information
alesj committed Sep 16, 2009
1 parent a8c8180 commit 6041c48
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 26 deletions.
58 changes: 44 additions & 14 deletions src/main/java/org/jboss/virtual/VFSUtils.java
Expand Up @@ -48,6 +48,7 @@
import org.jboss.virtual.plugins.copy.TempCopyMechanism;
import org.jboss.virtual.plugins.copy.UnjarCopyMechanism;
import org.jboss.virtual.plugins.copy.UnpackCopyMechanism;
import org.jboss.virtual.plugins.vfs.helpers.PathTokenizer;
import org.jboss.virtual.spi.LinkInfo;
import org.jboss.virtual.spi.Options;
import org.jboss.virtual.spi.VFSContext;
Expand Down Expand Up @@ -1079,12 +1080,13 @@ public static String getRelativePath(VFSContext context, URI uri)
}

/**
* Strip protocol from url string.
* Get path.
* Apply Carlo's fix as well.
*
* @param uri the uri
* @return uri's path string
* @return uri's path
*/
public static String stripProtocol(URI uri)
protected static String getPath(URI uri)
{
String path = uri.getPath();
if(path == null)
Expand All @@ -1093,23 +1095,51 @@ public static String stripProtocol(URI uri)
if(s.startsWith("jar:file:"))
path = s.substring("jar:file:".length()).replaceFirst("!/", "/") + "/";
}
if (path != null && path.length() > 0)
{
StringBuilder sb = new StringBuilder(path);
return path;
}

if (sb.charAt(0) != '/')
sb.insert(0, '/');
if (sb.charAt(sb.length() - 1) != '/')
sb.append('/');
/**
* Strip protocol from url string, return tokens.
*
* @param uri the uri
* @return uri's path string tokens
*/
public static List<String> stripProtocolToTokens(URI uri)
{
String path = getPath(uri);
try
{
return PathTokenizer.applySpecialPathsToTokens(path);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}

path = sb.toString();
/**
* Strip protocol from url string.
*
* @param uri the uri
* @return uri's path string
*/
public static String stripProtocol(URI uri)
{
List<String> tokens = stripProtocolToTokens(uri);
if (tokens.isEmpty() == false)
{
StringBuilder builder = new StringBuilder();
for (String token : tokens)
{
builder.append("/").append(token);
}
builder.append("/");
return builder.toString();
}
else
{
path = "/";
return "/";
}

return path;
}

/**
Expand Down
Expand Up @@ -25,7 +25,6 @@
import java.util.List;

import org.jboss.virtual.VFSUtils;
import org.jboss.virtual.plugins.vfs.helpers.PathTokenizer;
import org.jboss.virtual.spi.VFSContext;

/**
Expand All @@ -43,8 +42,7 @@ public abstract class PathMatchingVFSCache extends AbstractVFSCache
*/
public VFSContext findContext(URI uri)
{
String uriString = VFSUtils.stripProtocol(uri);
List<String> tokens = PathTokenizer.getTokens(uriString);
List<String> tokens = VFSUtils.stripProtocolToTokens(uri);
StringBuilder sb = new StringBuilder("/");
readLock();
try
Expand Down
Expand Up @@ -221,18 +221,15 @@ public static String getRemainingPath(List<String> tokens, int i)
}

/**
* Apply any . or .. paths in the path param.
* Handle special tokens.
*
* @param tokens the tokens
* @param path the path
* @return simple path, containing no . or .. paths
* @throws IOException if reverse path goes over the top path
* @return index of last token
* @throws IOException for any error
*/
public static String applySpecialPaths(String path) throws IOException
protected static int applySpecialTokens(List<String> tokens, String path) throws IOException
{
List<String> tokens = getTokens(path);
if (tokens == null)
return null;

int i = 0;
for(int j = 0; j < tokens.size(); j++)
{
Expand All @@ -248,7 +245,41 @@ else if (isReverseToken(token))
if (i < 0)
throw new IOException("Using reverse path on top path: " + path);
}
return getRemainingPath(tokens, 0, i);
return i;
}

/**
* Apply any . or .. paths in the path param.
*
* @param path the path
* @return simple path, containing no . or .. paths
* @throws IOException if reverse path goes over the top path
*/
public static List<String> applySpecialPathsToTokens(String path) throws IOException
{
List<String> tokens = getTokens(path);
if (tokens == null)
return null;

int index = applySpecialTokens(tokens, path);
return tokens.subList(0, index);
}

/**
* Apply any . or .. paths in the path param.
*
* @param path the path
* @return simple path, containing no . or .. paths
* @throws IOException if reverse path goes over the top path
*/
public static String applySpecialPaths(String path) throws IOException
{
List<String> tokens = getTokens(path);
if (tokens == null)
return null;

int index = applySpecialTokens(tokens, path);
return getRemainingPath(tokens, 0, index);
}

/**
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/org/jboss/test/virtual/test/VFSCacheTest.java
Expand Up @@ -24,9 +24,11 @@
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.security.ProtectionDomain;
import java.util.Collections;
import java.util.Map;

import org.jboss.util.Strings;
import org.jboss.virtual.VFS;
import org.jboss.virtual.VirtualFile;
import org.jboss.virtual.plugins.context.jar.JarContextFactory;
Expand Down Expand Up @@ -246,6 +248,41 @@ public void testJarPath() throws Exception
}
}

public void testCanonicalPath() throws Exception
{
ProtectionDomain pd = getClass().getProtectionDomain();
URL url = pd.getCodeSource().getLocation();
String urlString = url.toExternalForm();
if (urlString.endsWith("/") == false)
urlString += "/";
String testDir = urlString + "vfs/../vfs/test/";
URL testURL = Strings.toURL(testDir);

VFSCache cache = createCache();
cache.start();
try
{
VFSCacheFactory.setInstance(cache);
try
{
configureCache(cache);

VirtualFile testVF = VFS.getRoot(testURL);
URL jar1URL = new URL(testDir + "jar1.jar/");
VirtualFile jar1VF = VFS.getRoot(jar1URL);
assertEquals(testVF, jar1VF.getParent()); // should find previous root
}
finally
{
VFSCacheFactory.setInstance(null);
}
}
finally
{
stopCache(cache);
}
}

private class WrapperVFSCache implements VFSCache
{
private VFSCache delegate;
Expand Down

0 comments on commit 6041c48

Please sign in to comment.