Skip to content

Commit

Permalink
Merge pull request #5131 from eclipse/jetty-9.4.x-5129-extraclasspath…
Browse files Browse the repository at this point in the history
…-glob

Issue #5129 - WebAppContext.setExtraClasspath(String) cleanup
  • Loading branch information
joakime committed Aug 12, 2020
2 parents e1e8e50 + d91cab0 commit e632d24
Show file tree
Hide file tree
Showing 8 changed files with 342 additions and 46 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,23 @@ 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);
}

// cleanup any lingering relative path nonsense (like "/./" and "/../")
this.path = absPath.normalize();

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 @@ -311,7 +301,6 @@ public static boolean isContainedIn(Resource r, Resource containingResource) thr
return r.isContainedIn(containingResource);
}


//@checkstyle-disable-check : NoFinalizer
@Override
protected void finalize()
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,13 +32,17 @@
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.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class ResourceTest
{
Expand Down Expand Up @@ -279,4 +285,16 @@ public void testResourceContent(Scenario data)
String c = IO.toString(in);
assertThat("Content: " + data.test, c, startsWith(data.content));
}

@Test
@DisabledOnOs(OS.WINDOWS) // this uses forbidden characters on some Windows Environments
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);
assertNotNull(globResource, "Should have produced a Resource");
}
}
Expand Up @@ -319,29 +319,30 @@ public void addJars(Resource lib)
{
if (lib.exists() && lib.isDirectory())
{
String[] files = lib.list();
if (files != null)
String[] entries = lib.list();
if (entries != null)
{
Arrays.sort(files);
}
for (int f = 0; files != null && f < files.length; f++)
{
try
Arrays.sort(entries);

for (String entry : entries)
{
Resource fn = lib.addPath(files[f]);
if (LOG.isDebugEnabled())
LOG.debug("addJar - {}", fn);
String fnlc = fn.getName().toLowerCase(Locale.ENGLISH);
// don't check if this is a directory (prevents use of symlinks), see Bug 353165
if (isFileSupported(fnlc))
try
{
String jar = URIUtil.encodeSpecific(fn.toString(), ",;");
addClassPath(jar);
Resource resource = lib.addPath(entry);
if (LOG.isDebugEnabled())
LOG.debug("addJar - {}", resource);
String fnlc = resource.getName().toLowerCase(Locale.ENGLISH);
// don't check if this is a directory (prevents use of symlinks), see Bug 353165
if (isFileSupported(fnlc))
{
String jar = URIUtil.encodeSpecific(resource.toString(), ",;");
addClassPath(jar);
}
}
catch (Exception ex)
{
LOG.warn(Log.EXCEPTION, ex);
}
}
catch (Exception ex)
{
LOG.warn(Log.EXCEPTION, ex);
}
}
}
Expand Down
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[] entries = dirResource.list();
if (entries != null)
{
Arrays.sort(entries);
for (String entry : entries)
{
try
{
Resource fileResource = dirResource.addPath(entry);
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,31 @@ 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();
// ignore glob references, they only refer to lists of jars/zips anyway
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 e632d24

Please sign in to comment.