Skip to content

Commit

Permalink
Hacking
Browse files Browse the repository at this point in the history
  • Loading branch information
philwebb committed May 9, 2024
1 parent 7708ec7 commit 0a1afaf
Showing 1 changed file with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
*/
public record NestedLocation(Path path, String nestedEntryName) {

private static final boolean HAS_JETTY = isClassPresent("org.eclipse.jetty.util.URIUtil");

private static final Map<String, NestedLocation> locationCache = new ConcurrentHashMap<>();

private static final Map<String, Path> pathCache = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -107,18 +109,36 @@ private static NestedLocation create(String location) {

private static Path asPath(String locationPath) {
return pathCache.computeIfAbsent(locationPath, (key) -> {
if (isWindows() && locationPath.length() > 2 && locationPath.charAt(2) == ':') {
// Use the same logic as Java's internal WindowsUriSupport class
return Path.of(locationPath.substring(1));
}
return Path.of(locationPath);
return Path.of((!isWindows()) ? locationPath : fixWindowsLocationPath(locationPath));
});
}

private static boolean isWindows() {
return File.separatorChar == '\\';
}

private static String fixWindowsLocationPath(String locationPath) {
// Same logic as Java's internal WindowsUriSupport class
if (locationPath.length() > 2 && locationPath.charAt(2) == ':') {
return locationPath.substring(1);
}
// Deal with Jetty's org.eclipse.jetty.util.URIUtil#correctURI(URI)
if (HAS_JETTY && locationPath.startsWith("///") && locationPath.charAt(4) == ':') {
return locationPath.substring(3);
}
return locationPath;
}

private static boolean isClassPresent(String className) {
try {
Class.forName(className, false, Thread.currentThread().getContextClassLoader());
return true;
}
catch (ClassNotFoundException ex) {
return false;
}
}

static void clearCache() {
locationCache.clear();
pathCache.clear();
Expand Down

0 comments on commit 0a1afaf

Please sign in to comment.