Skip to content

Commit

Permalink
Merge pull request #83 from ouaibsky/master
Browse files Browse the repository at this point in the history
feat: Make sure we can have acces to maven info if available.
  • Loading branch information
jamesward authored Jun 28, 2022
2 parents b2a378e + 784b794 commit 05a9a71
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 38 deletions.
26 changes: 26 additions & 0 deletions src/main/java/org/webjars/MavenProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.webjars;

class MavenProperties {
final String groupId;
final String artifactId;
final String version;

MavenProperties(String groupId, String artifactId, String version) {
this.groupId = groupId;
this.artifactId = artifactId;
this.version = version;
}

public String getArtifactId() {
return artifactId;
}

public String getGroupId() {
return groupId;
}

// For future usage ?
public String getVersion() {
return version;
}
}
51 changes: 28 additions & 23 deletions src/main/java/org/webjars/WebJarAssetLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,9 @@
import io.github.classgraph.ScanResult;
import java.io.IOException;
import java.net.URI;
import java.util.*;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -44,32 +38,42 @@ public static class WebJarInfo {

final String version;
final String groupId;
final String artifactId;
final URI uri;
final List<String> contents;
final Collection<String> contents;

WebJarInfo(@Nullable final String version, @Nullable final String groupId, final URI uri, @Nonnull final List<String> contents) {
WebJarInfo(@Nullable final String version, Optional<MavenProperties> mavenProperties, final URI uri, @Nonnull final Collection<String> contents) {
this.version = version;
this.groupId = groupId;
this.groupId = mavenProperties.map(MavenProperties::getGroupId).orElse(null);
this.artifactId = mavenProperties.map(MavenProperties::getArtifactId).orElse(null);
this.uri = uri;
this.contents = contents;
}

@Nullable
String getVersion() {
public String getVersion() {
return version;
}

@Nullable
String getGroupId() {
public String getGroupId() {
return groupId;
}

URI getUri() {
@Nullable
public String getArtifactId() {
return artifactId;
}

public URI getUri() {
return uri;
}

/**
* @return an immutable list of resources.
*/
@Nonnull
List<String> getContents() {
public Collection<String> getContents() {
return contents;
}
}
Expand Down Expand Up @@ -117,23 +121,24 @@ protected static String webJarVersion(@Nullable final String webJarName, @Nonnul
return null;
}

@Nullable
private static String groupId(@Nullable final URI classpathElementURI) {
private static Optional<MavenProperties> findMavenInfo(@Nullable final URI classpathElementURI) {
final ClassGraph classGraph = new ClassGraph().overrideClasspath(classpathElementURI).ignoreParentClassLoaders().acceptPaths("META-INF/maven");
try (ScanResult scanResult = classGraph.scan()) {
final ResourceList maybePomProperties = scanResult.getResourcesWithLeafName("pom.properties");

if (maybePomProperties.size() == 1) {
try {
final Properties properties = new Properties();
properties.load(maybePomProperties.get(0).open());
maybePomProperties.get(0).close();
return properties.getProperty("groupId");
return Optional.of(new MavenProperties(properties.getProperty("groupId"),
properties.getProperty("artifactId"),
properties.getProperty("version")
));
} catch (IOException e) {
// ignored
}
}
return null;
return Optional.empty();
}
}

Expand All @@ -148,13 +153,13 @@ protected static Map<String, WebJarInfo> findWebJars(@Nonnull ScanResult scanRes
if (!webJars.containsKey(webJarName)) {
final ResourceList webJarResources = webJarResources(webJarName, allResources);
final String maybeWebJarVersion = webJarVersion(webJarName, webJarResources);
final String maybeGroupId = groupId(resource.getClasspathElementURI());
final Optional<MavenProperties> mavenProperties = findMavenInfo(resource.getClasspathElementURI());
// todo: this doesn't preserve the different URIs for the resources so if for some reason the actual duplicates are different,
// then things can get strange because on resource lookup, it can resolve to a difference classpath resource
//
// this removes duplicates
final List<String> paths = new ArrayList<>(new HashSet<>(webJarResources.getPaths()));
webJars.put(webJarName, new WebJarInfo(maybeWebJarVersion, maybeGroupId, resource.getClasspathElementURI(), paths));
// this removes duplicates.
final Collection<String> paths = Collections.unmodifiableCollection(new HashSet<>(webJarResources.getPaths()));
webJars.put(webJarName, new WebJarInfo(maybeWebJarVersion, mavenProperties, resource.getClasspathElementURI(), paths));
}
}
return webJars;
Expand Down
38 changes: 23 additions & 15 deletions src/test/java/org/webjars/WebJarAssetLocatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,17 @@
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.webresources.StandardRoot;
Expand All @@ -34,7 +27,7 @@ public class WebJarAssetLocatorTest {

private HashMap<String, WebJarAssetLocator.WebJarInfo> withList(List<String> paths) throws URISyntaxException {
HashMap<String, WebJarAssetLocator.WebJarInfo> webJars = new HashMap<>();
WebJarAssetLocator.WebJarInfo webJarInfo = new WebJarAssetLocator.WebJarInfo("1.0.0", "foo", new URI("asdf"), paths);
WebJarAssetLocator.WebJarInfo webJarInfo = new WebJarAssetLocator.WebJarInfo("1.0.0", Optional.of(new MavenProperties("foo", "fooId", "x.y.z")), new URI("asdf"), paths);
webJars.put("foo", webJarInfo);
return webJars;
}
Expand Down Expand Up @@ -393,15 +386,30 @@ public void issue_35() {
@Test
public void should_not_npe_in_getFullPath() {
Map<String, WebJarInfo> allWebJars = new HashMap<>();
allWebJars.put("webjar", new WebJarInfo("version", "groupId", null, emptyList()));
allWebJars.put("webjar", new WebJarInfo("version", Optional.of(new MavenProperties("groupId", "artifactId", "version")), null, emptyList()));
WebJarAssetLocator webJarAssetLocator = new WebJarAssetLocator(allWebJars);
String partialPath = "partialPath";

try {
webJarAssetLocator.getFullPath("webjar", "partialPath");
}
catch (NotFoundException e) {
} catch (NotFoundException e) {
assertEquals(partialPath + " could not be found. Make sure you've added the corresponding WebJar and please check for typos.", e.getMessage());
}
}

@Test
public void should_get_maven_info() {
Map<String, WebJarInfo> allWebJars = new HashMap<>();
allWebJars.put("webjar", new WebJarInfo("version", Optional.of(new MavenProperties("foo", "bar", "1.2.3")), null, emptyList()));
WebJarAssetLocator webJarAssetLocator = new WebJarAssetLocator(allWebJars);

Optional<WebJarInfo> info = webJarAssetLocator.getAllWebJars().entrySet().stream().filter(e -> e.getKey().equals("webjar")).map(Map.Entry::getValue).findFirst();
if (!info.isPresent()) {
fail("Could not retrieve web jar info for 'webjar'");
} else {
org.hamcrest.MatcherAssert.assertThat(info.get().groupId, is("foo"));
org.hamcrest.MatcherAssert.assertThat(info.get().artifactId, is("bar"));
org.hamcrest.MatcherAssert.assertThat(info.get().version, is("version"));
}
}
}

0 comments on commit 05a9a71

Please sign in to comment.