diff --git a/README.md b/README.md index 18d4083..25883f9 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,8 @@ This project provides a means to locate assets within WebJars. [![.github/workflows/test.yml](https://github.com/webjars/webjars-locator-core/actions/workflows/test.yml/badge.svg)](https://github.com/webjars/webjars-locator-core/actions/workflows/test.yml) [![Latest Release](https://img.shields.io/maven-central/v/org.webjars/webjars-locator-core.svg)](https://mvnrepository.com/artifact/org.webjars/webjars-locator-core) +[![CodeQL](https://github.com/webjars/webjars-locator-core/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/webjars/webjars-locator-core/actions/workflows/codeql-analysis.yml) + Obtain the full path of an asset -------------------------------- diff --git a/pom.xml b/pom.xml index 73a268b..e487fd0 100644 --- a/pom.xml +++ b/pom.xml @@ -334,7 +334,7 @@ org.sonatype.plugins nexus-staging-maven-plugin - 1.6.11 + 1.6.8 true sonatype-nexus-staging diff --git a/src/main/java/org/webjars/WebJarAssetLocator.java b/src/main/java/org/webjars/WebJarAssetLocator.java index 5ba1717..3d841c3 100644 --- a/src/main/java/org/webjars/WebJarAssetLocator.java +++ b/src/main/java/org/webjars/WebJarAssetLocator.java @@ -47,16 +47,39 @@ static class WebJarInfo { final URI uri; final List contents; - public WebJarInfo(final String version, final String groupId, final URI uri, final List contents) { + WebJarInfo(@Nullable final String version, @Nullable final String groupId, final URI uri, @Nonnull final List contents) { this.version = version; this.groupId = groupId; this.uri = uri; this.contents = contents; } + + @Nullable + String getVersion() { + return version; + } + + @Nullable + String getGroupId() { + return groupId; + } + + URI getUri() { + return uri; + } + + @Nonnull + List getContents() { + return contents; + } } protected final Map allWebJars; + public Map getAllWebJars() { + return allWebJars; + } + @Nonnull protected static ResourceList webJarResources(@Nonnull final String webJarName, @Nonnull final ResourceList resources) { if (isEmpty(webJarName)) { @@ -118,11 +141,11 @@ private static String groupId(@Nullable final URI classpathElementURI) { protected static Map findWebJars(@Nonnull ScanResult scanResult) { requireNonNull(scanResult, "Scan result must not be null"); ResourceList allResources = scanResult.getAllResources(); - Map allWebJars = new HashMap<>(allResources.size()); + Map webJars = new HashMap<>(allResources.size()); for (Resource resource : allResources) { final String noPrefix = resource.getPath().substring(WEBJARS_PATH_PREFIX.length() + 1); final String webJarName = noPrefix.substring(0, noPrefix.indexOf('/')); - if (!allWebJars.containsKey(webJarName)) { + if (!webJars.containsKey(webJarName)) { final ResourceList webJarResources = webJarResources(webJarName, allResources); final String maybeWebJarVersion = webJarVersion(webJarName, webJarResources); final String maybeGroupId = groupId(resource.getClasspathElementURI()); @@ -131,10 +154,10 @@ protected static Map findWebJars(@Nonnull ScanResult scanRes // // this removes duplicates final List paths = new ArrayList<>(new HashSet<>(webJarResources.getPaths())); - allWebJars.put(webJarName, new WebJarInfo(maybeWebJarVersion, maybeGroupId, resource.getClasspathElementURI(), paths)); + webJars.put(webJarName, new WebJarInfo(maybeWebJarVersion, maybeGroupId, resource.getClasspathElementURI(), paths)); } } - return allWebJars; + return webJars; } /** @@ -199,7 +222,7 @@ public String getFullPath(@Nonnull final String partialPath) { throw new IllegalArgumentException("Partial path must not be null or empty"); } - List paths = new ArrayList<>(); + List paths = new ArrayList<>(allWebJars.size()); for (String webJarName : allWebJars.keySet()) { try { @@ -239,7 +262,7 @@ public String getFullPath(@Nonnull final String webjar, @Nonnull final String pa if (allWebJars.containsKey(webjar)) { - List paths = allWebJars.get(webjar).contents.stream().filter(path -> path.endsWith(partialPath)).collect(Collectors.toList()); + List paths = allWebJars.get(webjar).getContents().stream().filter(path -> path.endsWith(partialPath)).collect(Collectors.toList()); if (paths.isEmpty()) { throwNotFoundException(partialPath); @@ -259,8 +282,8 @@ public String getFullPath(@Nonnull final String webjar, @Nonnull final String pa /** * Returns the full path of an asset within a specific WebJar * - * @param webJarName The id of the WebJar to search - * @param exactPath The exact path of the file within the WebJar + * @param webJarName The id of the WebJar to search (must not be {@code null} + * @param exactPath The exact path of the file within the WebJar (may be {@code null} for legacy reasons) * @return a fully qualified path to the resource of {@code null} if WebJar not found */ @Nullable @@ -270,17 +293,25 @@ public String getFullPathExact(@Nonnull final String webJarName, @Nullable final throw new IllegalArgumentException("WebJar ID must not be null or empty"); } - final String maybeVersion = getWebJars().get(webJarName); + if (isEmpty(exactPath)) { + return null; + } + + WebJarInfo webJarInfo = allWebJars.get(webJarName); + if (webJarInfo == null || webJarInfo.getContents().isEmpty()) { + return null; + } + + String version = webJarInfo.getVersion(); String fullPath; - if (maybeVersion == null) { + if (isEmpty(version)) { fullPath = String.format("%s/%s/%s", WEBJARS_PATH_PREFIX, webJarName, exactPath); } else { - fullPath = String.format("%s/%s/%s/%s", WEBJARS_PATH_PREFIX, webJarName, maybeVersion, exactPath); + fullPath = String.format("%s/%s/%s/%s", WEBJARS_PATH_PREFIX, webJarName, version, exactPath); } - WebJarInfo webJarInfo = allWebJars.get(webJarName); - if (webJarInfo != null && webJarInfo.contents.contains(fullPath)) { + if (webJarInfo.getContents().contains(fullPath)) { return fullPath; } @@ -304,7 +335,7 @@ public Set listAssets(@Nonnull final String folderPath) { final String prefix = String.format("%s%s%s", WEBJARS_PATH_PREFIX, folderPath.startsWith("/") ? "" : "/", folderPath); return allWebJars.values() .stream() - .flatMap(webJarInfo -> webJarInfo.contents.stream()) + .flatMap(webJarInfo -> webJarInfo.getContents().stream()) .filter(path -> path.startsWith(folderPath) || path.startsWith(prefix)) .collect(Collectors.toSet()); } @@ -316,7 +347,7 @@ public Set listAssets(@Nonnull final String folderPath) { public Map getWebJars() { Map webJars = new HashMap<>(allWebJars.size()); for (Entry entry : allWebJars.entrySet()) { - webJars.put(entry.getKey(), entry.getValue().version); + webJars.put(entry.getKey(), entry.getValue().getVersion()); } return webJars; } @@ -334,9 +365,9 @@ public String groupId(@Nullable final String fullPath) { } return allWebJars.values() .stream() - .filter(webJarInfo -> webJarInfo.contents.contains(fullPath)) + .filter(webJarInfo -> webJarInfo.getContents().contains(fullPath)) .findFirst() - .map(webJarInfo -> webJarInfo.groupId) + .map(WebJarInfo::getGroupId) .orElse(null); } diff --git a/src/main/java/org/webjars/WebJarExtractor.java b/src/main/java/org/webjars/WebJarExtractor.java index c2e9211..6e59389 100644 --- a/src/main/java/org/webjars/WebJarExtractor.java +++ b/src/main/java/org/webjars/WebJarExtractor.java @@ -130,7 +130,7 @@ private void extractResourcesTo(@Nonnull final String webJarName, @Nonnull final } private static void extractResource(@Nonnull String webJarName, @Nonnull WebJarInfo webJarInfo, @Nullable File to, @Nonnull String webJarId, @Nonnull Resource resource, @Nonnull InputStream inputStream) { - final String prefix = String.format("%s%s%s%s%s", WEBJARS_PATH_PREFIX, File.separator, webJarName, File.separator, webJarInfo.version == null ? "" : String.format("%s%s", webJarInfo.version, File.separator)); + final String prefix = String.format("%s%s%s%s%s", WEBJARS_PATH_PREFIX, File.separator, webJarName, File.separator, webJarInfo.getVersion() == null ? "" : String.format("%s%s", webJarInfo.getVersion(), File.separator)); if (resource.getPath().startsWith(prefix)) { final String newPath = resource.getPath().substring(prefix.length()); final String relativeName = String.format("%s%s%s", webJarId, File.separator, newPath); @@ -173,7 +173,7 @@ private void extractWebJarsTo(@Nullable final String name, @Nullable final Strin for (final String webJarName : allWebJars.keySet()) { final String moduleFilePath = webJarAssetLocator.getFullPathExact(webJarName, moduleNameFile); - final WebJarAssetLocator.WebJarInfo webJarInfo = webJarAssetLocator.allWebJars.get(webJarName); + final WebJarAssetLocator.WebJarInfo webJarInfo = webJarAssetLocator.getAllWebJars().get(webJarName); extractResourcesTo(webJarName, webJarInfo, moduleFilePath, WebJarAssetLocator.webJarResources(webJarName, scanResult.getAllResources()), to); } } diff --git a/src/test/java/org/webjars/WebJarExtractorTestUtils.java b/src/test/java/org/webjars/WebJarExtractorTestUtils.java index d8c9a74..4ac7c67 100644 --- a/src/test/java/org/webjars/WebJarExtractorTestUtils.java +++ b/src/test/java/org/webjars/WebJarExtractorTestUtils.java @@ -15,7 +15,7 @@ public static URLClassLoader createClassLoader() throws Exception { List webJarUrls = new ArrayList<>(); for (WebJarAssetLocator.WebJarInfo webJarInfo : webJarAssetLocator.allWebJars.values()) { - webJarUrls.add(webJarInfo.uri.toURL()); + webJarUrls.add(webJarInfo.getUri().toURL()); } return new URLClassLoader(webJarUrls.toArray(new URL[0]), null);