Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downgrade Nexus Staging Plugin / Another Refactoring #53

Merged
merged 2 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------------------------

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.11</version>
<version>1.6.8</version>
<extensions>true</extensions>
<configuration>
<serverId>sonatype-nexus-staging</serverId>
Expand Down
67 changes: 49 additions & 18 deletions src/main/java/org/webjars/WebJarAssetLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,39 @@ static class WebJarInfo {
final URI uri;
final List<String> contents;

public WebJarInfo(final String version, final String groupId, final URI uri, final List<String> contents) {
WebJarInfo(@Nullable final String version, @Nullable final String groupId, final URI uri, @Nonnull final List<String> 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<String> getContents() {
return contents;
}
}

protected final Map<String, WebJarInfo> allWebJars;

public Map<String, WebJarInfo> getAllWebJars() {
return allWebJars;
}

@Nonnull
protected static ResourceList webJarResources(@Nonnull final String webJarName, @Nonnull final ResourceList resources) {
if (isEmpty(webJarName)) {
Expand Down Expand Up @@ -118,11 +141,11 @@ private static String groupId(@Nullable final URI classpathElementURI) {
protected static Map<String, WebJarInfo> findWebJars(@Nonnull ScanResult scanResult) {
requireNonNull(scanResult, "Scan result must not be null");
ResourceList allResources = scanResult.getAllResources();
Map<String, WebJarInfo> allWebJars = new HashMap<>(allResources.size());
Map<String, WebJarInfo> 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());
Expand All @@ -131,10 +154,10 @@ protected static Map<String, WebJarInfo> findWebJars(@Nonnull ScanResult scanRes
//
// this removes duplicates
final List<String> 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;
}

/**
Expand Down Expand Up @@ -199,7 +222,7 @@ public String getFullPath(@Nonnull final String partialPath) {
throw new IllegalArgumentException("Partial path must not be null or empty");
}

List<String> paths = new ArrayList<>();
List<String> paths = new ArrayList<>(allWebJars.size());

for (String webJarName : allWebJars.keySet()) {
try {
Expand Down Expand Up @@ -239,7 +262,7 @@ public String getFullPath(@Nonnull final String webjar, @Nonnull final String pa

if (allWebJars.containsKey(webjar)) {

List<String> paths = allWebJars.get(webjar).contents.stream().filter(path -> path.endsWith(partialPath)).collect(Collectors.toList());
List<String> paths = allWebJars.get(webjar).getContents().stream().filter(path -> path.endsWith(partialPath)).collect(Collectors.toList());

if (paths.isEmpty()) {
throwNotFoundException(partialPath);
Expand All @@ -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
Expand All @@ -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;
}

Expand All @@ -304,7 +335,7 @@ public Set<String> 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());
}
Expand All @@ -316,7 +347,7 @@ public Set<String> listAssets(@Nonnull final String folderPath) {
public Map<String, String> getWebJars() {
Map<String, String> webJars = new HashMap<>(allWebJars.size());
for (Entry<String, WebJarInfo> entry : allWebJars.entrySet()) {
webJars.put(entry.getKey(), entry.getValue().version);
webJars.put(entry.getKey(), entry.getValue().getVersion());
}
return webJars;
}
Expand All @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/webjars/WebJarExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/webjars/WebJarExtractorTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static URLClassLoader createClassLoader() throws Exception {
List<URL> 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);
Expand Down