Skip to content

Commit

Permalink
Issue #5129 - WebAppContext.setExtraClasspath(String) cleanup
Browse files Browse the repository at this point in the history
+ More tests for both relative and absolute path references
+ More testing that will trigger quirks on Windows builds
  so that we can catch regressions faster
+ Reworked WebInfConfiguration to be glob aware in a way
  similar to how WebAppClassLoader behaves.
+ Reworked Resource.newResource(String) to delegate
  canonical path resolution to PathResource
+ Guarded PathResource's usage of Path.toAbsolutePath()
  to ignore valid conditions where the Path cannot be
  resolved to an absolute path (yet)
  • Loading branch information
joakime committed Aug 7, 2020
1 parent 1f14dfa commit 675c3cf
Show file tree
Hide file tree
Showing 6 changed files with 310 additions and 25 deletions.
Expand Up @@ -19,6 +19,7 @@
package org.eclipse.jetty.util.resource;

import java.io.File;
import java.io.IOError;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -184,7 +185,7 @@ public static boolean isSameName(Path pathA, Path pathB)
// different number of segments
return false;
}

// compare each segment of path, backwards
for (int i = bCount; i-- > 0; )
{
Expand All @@ -193,7 +194,7 @@ public static boolean isSameName(Path pathA, Path pathB)
return false;
}
}

return true;
}

Expand Down Expand Up @@ -226,7 +227,21 @@ public PathResource(File file)
*/
public PathResource(Path path)
{
this.path = path.toAbsolutePath();
Path absPath = path;
try
{
absPath = path.toAbsolutePath();
}
catch (IOError ioError)
{
// Not able to resolve absolute path from provided path
// This could be due to a glob reference, or a reference
// to a path that doesn't exist (yet)
if (LOG.isDebugEnabled())
LOG.debug("Unable to get absolute path for {}", path, ioError);
}
this.path = absPath;

assertValidPath(path);
this.uri = this.path.toUri();
this.alias = checkAliasPath();
Expand Down
Expand Up @@ -29,6 +29,7 @@
import java.net.URL;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Base64;
Expand Down Expand Up @@ -174,19 +175,8 @@ public static Resource newResource(String resource, boolean useCaches)
!resource.startsWith("file:") &&
!resource.startsWith("jar:"))
{
try
{
// It's a file.
if (resource.startsWith("./"))
resource = resource.substring(2);
File file = new File(resource).getCanonicalFile();
return new PathResource(file);
}
catch (IOException e2)
{
e2.addSuppressed(e);
throw e2;
}
// It's likely a file/path reference.
return new PathResource(Paths.get(resource));
}
else
{
Expand Down
Expand Up @@ -19,9 +19,11 @@
package org.eclipse.jetty.util.resource;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.stream.Stream;

Expand All @@ -30,6 +32,7 @@
import org.eclipse.jetty.util.IO;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand Down Expand Up @@ -279,4 +282,14 @@ public void testResourceContent(Scenario data)
String c = IO.toString(in);
assertThat("Content: " + data.test, c, startsWith(data.content));
}

@Test
public void testGlobPath() throws IOException
{
Path testDir = MavenTestingUtils.getTargetTestingPath("testGlobPath");
FS.ensureEmpty(testDir);

String globReference = testDir.toAbsolutePath().toString() + File.separator + '*';
Resource globResource = Resource.newResource(globReference);
}
}
Expand Up @@ -947,13 +947,47 @@ protected List<Resource> findExtraClasspathJars(WebAppContext context)
StringTokenizer tokenizer = new StringTokenizer(context.getExtraClasspath(), ",;");
while (tokenizer.hasMoreTokens())
{
Resource resource = context.newResource(tokenizer.nextToken().trim());
String fnlc = resource.getName().toLowerCase(Locale.ENGLISH);
int dot = fnlc.lastIndexOf('.');
String extension = (dot < 0 ? null : fnlc.substring(dot));
if (extension != null && (extension.equals(".jar") || extension.equals(".zip")))
String token = tokenizer.nextToken().trim();

// Is this a Glob Reference?
if (isGlobReference(token))
{
String dir = token.substring(0, token.length() - 2);
// Use directory
Resource dirResource = context.newResource(dir);
if (dirResource.exists() && dirResource.isDirectory())
{
// To obtain the list of files
String[] files = dirResource.list();
if (files != null)
{
Arrays.sort(files);
for (String file : files)
{
try
{
Resource fileResource = dirResource.addPath(file);
if (isFileSupported(fileResource))
{
jarResources.add(fileResource);
}
}
catch (Exception ex)
{
LOG.warn(Log.EXCEPTION, ex);
}
}
}
}
}
else
{
jarResources.add(resource);
// Simple reference, add as-is
Resource resource = context.newResource(token);
if (isFileSupported(resource))
{
jarResources.add(resource);
}
}
}

Expand Down Expand Up @@ -1003,11 +1037,28 @@ protected List<Resource> findExtraClasspathDirs(WebAppContext context)
StringTokenizer tokenizer = new StringTokenizer(context.getExtraClasspath(), ",;");
while (tokenizer.hasMoreTokens())
{
Resource resource = context.newResource(tokenizer.nextToken().trim());
if (resource.exists() && resource.isDirectory())
dirResources.add(resource);
String token = tokenizer.nextToken().trim();
if (!isGlobReference(token))
{
Resource resource = context.newResource(token);
if (resource.exists() && resource.isDirectory())
dirResources.add(resource);
}
}

return dirResources;
}

private boolean isGlobReference(String token)
{
return token.endsWith("/*") || token.endsWith("\\*");
}

private boolean isFileSupported(Resource resource)
{
String filenameLowercase = resource.getName().toLowerCase(Locale.ENGLISH);
int dot = filenameLowercase.lastIndexOf('.');
String extension = (dot < 0 ? null : filenameLowercase.substring(dot));
return (extension != null && (extension.equals(".jar") || extension.equals(".zip")));
}
}

0 comments on commit 675c3cf

Please sign in to comment.