Narrowing in on possible bug in jar:file:// handling of Resource.getFileName()

This commit is contained in:
Joakim Erdfelt 2022-12-21 14:39:20 -06:00
parent ce52ffdd33
commit ff48442fa0
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
2 changed files with 43 additions and 0 deletions

View File

@ -16,6 +16,8 @@ package org.eclipse.jetty.http.content;
import java.io.IOException;
import org.eclipse.jetty.util.resource.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* An {@link HttpContent.Factory} implementation which takes a Resource and fakes this resource as
@ -24,6 +26,8 @@ import org.eclipse.jetty.util.resource.Resource;
*/
public class VirtualHttpContentFactory implements HttpContent.Factory
{
private static final Logger LOG = LoggerFactory.getLogger(VirtualHttpContentFactory.class);
private final HttpContent.Factory _factory;
private final Resource _resource;
private final String _contentType;
@ -35,6 +39,10 @@ public class VirtualHttpContentFactory implements HttpContent.Factory
_resource = resource;
_matchSuffix = "/" + _resource.getFileName();
_contentType = contentType;
if (LOG.isDebugEnabled())
{
LOG.debug("resource=({}) {}, resource.getFileName()={}", _resource.getClass().getName(), _resource, _resource.getFileName());
}
}
/**

View File

@ -23,6 +23,7 @@ import java.util.List;
import java.util.zip.ZipFile;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenPaths;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
@ -73,6 +74,10 @@ public class MountedPathResourceTest
List<String> entries = r.list().stream().map(Resource::getFileName).toList();
assertThat(entries, containsInAnyOrder("alphabet", "numbers", "subsubdir"));
Resource file = r.resolve("subsubdir/numbers");
assertTrue(Resources.isReadableFile(file));
assertThat(file.getFileName(), is("numbers"));
Path extract = workDir.getPathFile("extract");
FS.ensureEmpty(extract);
@ -101,6 +106,36 @@ public class MountedPathResourceTest
}
}
@Test
public void testZipFileName()
{
Path testZip = MavenTestingUtils.getTestResourcePathFile("TestData/test.zip");
String s = "jar:" + testZip.toUri().toASCIIString() + "!/subdir/numbers";
URI uri = URI.create(s);
try (ResourceFactory.Closeable resourceFactory = ResourceFactory.closeable())
{
Resource r = resourceFactory.newResource(uri);
assertTrue(Resources.isReadableFile(r));
assertThat(r.getFileName(), is("numbers"));
}
}
@Test
public void testJarFileName()
{
Path testZip = MavenPaths.findTestResourceFile("jar-file-resource.jar");
String s = "jar:" + testZip.toUri().toASCIIString() + "!/rez/deep/zzz";
URI uri = URI.create(s);
try (ResourceFactory.Closeable resourceFactory = ResourceFactory.closeable())
{
Resource r = resourceFactory.newResource(uri);
assertTrue(Resources.isReadableFile(r));
assertThat(r.getFileName(), is("zzz"));
}
}
@Test
public void testJarFileUnMounted() throws Exception
{