Skip to content

Commit cb29888

Browse files
authored
Merge pull request #2505 from sswguo/directory_listing
Update PathMaskChecker for Directory List
2 parents 0f11a39 + 8cc13bd commit cb29888

File tree

2 files changed

+132
-5
lines changed

2 files changed

+132
-5
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package org.commonjava.indy.koji.util;
2+
3+
import org.commonjava.indy.core.content.PathMaskChecker;
4+
import org.commonjava.indy.model.core.HostedRepository;
5+
import org.commonjava.indy.model.core.RemoteRepository;
6+
import org.junit.Test;
7+
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Set;
11+
12+
import static org.junit.Assert.*;
13+
14+
public class PathMaskCheckerTest
15+
{
16+
17+
@Test
18+
public void extractGroupIdPath() throws Exception
19+
{
20+
List<Map.Entry<String, String>> metadataFiles = List.of(
21+
Map.entry("com/github/fge/msg-simple/maven-metadata.xml", "com/github/fge/"),
22+
Map.entry("org/jboss/eap/jboss-eap-parent/maven-metadata.xml", "org/jboss/eap/"),
23+
Map.entry("org/apache/lucene/lucene-core/maven-metadata.xml", "org/apache/lucene/"),
24+
Map.entry("org/wildfly/security/wildfly-elytron/maven-metadata.xml", "org/wildfly/security/"),
25+
Map.entry("io/dropwizard/metrics/metrics-core/maven-metadata.xml", "io/dropwizard/metrics/"),
26+
Map.entry("org/jboss/spec/javax/el/jboss-el-api_3.0_spec/maven-metadata.xml", "org/jboss/spec/javax/el/")
27+
);
28+
29+
for (var entry : metadataFiles)
30+
{
31+
assertEquals( entry.getValue(), PathMaskChecker.extractGroupIdPath(entry.getKey()) );
32+
}
33+
}
34+
35+
@Test
36+
public void checkListingMask() throws Exception
37+
{
38+
HostedRepository hostedRepository = new HostedRepository(
39+
"mvn-hosted"
40+
);
41+
42+
RemoteRepository mrrcRepo = new RemoteRepository(
43+
"mrrc-ga",
44+
"http://example.url"
45+
);
46+
mrrcRepo.setPathMaskPatterns(
47+
Set.of("r|.+[-.]redhat[-_]\\d+.*|")
48+
);
49+
50+
RemoteRepository repositoryMatched = new RemoteRepository(
51+
"koji-org.infinispan-infinispan-parent-9.4.2.Final_redhat_00001-2",
52+
"http://example.url"
53+
);
54+
repositoryMatched.setPathMaskPatterns(
55+
Set.of("r|org\\/infinispan\\/.+\\/9.4.2.Final-redhat-00001\\/.+|",
56+
"org/infinispan/infinispan-query-dsl/maven-metadata.xml",
57+
"r|org\\/infinispan\\/server\\/.+\\/9.4.2.Final-redhat-00001\\/.+|"));
58+
59+
RemoteRepository repositoryUnMatched = new RemoteRepository(
60+
"koji-org.jboss.eap-jboss-eap-parent-7.2.0.GA_redhat_00002-2",
61+
"http://example.url"
62+
);
63+
repositoryUnMatched.setPathMaskPatterns(
64+
Set.of("r|org\\/jboss\\/eap\\/.+\\/7.2.0.GA-redhat-00002\\/.+|",
65+
"org/jboss/eap/jboss-eap-parent/maven-metadata.xml"));
66+
67+
RemoteRepository repositoryWithMultipleGroupIds = new RemoteRepository(
68+
"koji-com.sun.mail-all-1.6.1.redhat_1-1",
69+
"http://example.url"
70+
);
71+
repositoryWithMultipleGroupIds.setPathMaskPatterns(
72+
Set.of("javax/mail/javax.mail-api/maven-metadata.xml",
73+
"com/sun/mail/javax.mail-api/maven-metadata.xml",
74+
"r|javax\\/mail\\/.+\\/1.6.1.redhat-1\\/.+|",
75+
"r|com\\/sun\\/mail\\/.+\\/1.6.1.redhat-1\\/.+|"));
76+
77+
assertTrue(PathMaskChecker.checkListingMask(repositoryMatched, "org/"));
78+
assertTrue(PathMaskChecker.checkListingMask(repositoryMatched, "org/infinispan/"));
79+
assertTrue(PathMaskChecker.checkListingMask(repositoryMatched, "org/infinispan/infinispan-component-annotations/"));
80+
assertTrue(PathMaskChecker.checkListingMask(repositoryWithMultipleGroupIds, "com/sun/mail/"));
81+
assertTrue(PathMaskChecker.checkListingMask(repositoryWithMultipleGroupIds, "javax/mail/"));
82+
assertFalse(PathMaskChecker.checkListingMask(repositoryUnMatched, "org/infinispan/"));
83+
84+
assertTrue(PathMaskChecker.checkListingMask(hostedRepository, "org/infinispan/"));
85+
assertTrue(PathMaskChecker.checkListingMask(mrrcRepo, "org/infinispan/"));
86+
}
87+
88+
}

core/src/main/java/org/commonjava/indy/core/content/PathMaskChecker.java

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
import org.slf4j.Logger;
2020
import org.slf4j.LoggerFactory;
2121

22+
import java.util.Arrays;
23+
import java.util.List;
24+
import java.util.Objects;
2225
import java.util.Set;
26+
import java.util.stream.Collectors;
2327

2428
public class PathMaskChecker
2529
{
@@ -71,13 +75,34 @@ public static boolean checkListingMask( final ArtifactStore store, final String
7175
return true;
7276
}
7377

74-
for ( String pattern : maskPatterns )
78+
// if the pattern contains the metadata path, let's try to extract the groupId to filter the repos.
79+
List<String> metadataPatterns = maskPatterns.stream()
80+
.filter(pattern -> pattern.endsWith("maven-metadata.xml"))
81+
.collect( Collectors.toList() );
82+
83+
if ( metadataPatterns.isEmpty() )
7584
{
76-
if ( isRegexPattern( pattern ) )
85+
for ( String pattern : maskPatterns )
86+
{
87+
if ( isRegexPattern( pattern ) )
88+
{
89+
// if there is a regexp pattern we cannot check presence of directory listing, because we would have to
90+
// check only the beginning of the regexp and that's impossible, so we have to assume that the path is
91+
// present
92+
return true;
93+
}
94+
}
95+
}
96+
else
97+
{
98+
boolean matches = metadataPatterns.stream()
99+
.map(pattern -> extractGroupIdPath(pattern))
100+
.filter( Objects::nonNull )
101+
.anyMatch(groupIdPath -> path.startsWith( groupIdPath ) || groupIdPath.startsWith( path ) );
102+
103+
if ( matches )
77104
{
78-
// if there is a regexp pattern we cannot check presence of directory listing, because we would have to
79-
// check only the beginning of the regexp and that's impossible, so we have to assume that the path is
80-
// present
105+
logger.trace( "Checking mask in: {}, pattern with groupId path. - MATCH.", store.getName() );
81106
return true;
82107
}
83108
}
@@ -96,6 +121,20 @@ public static boolean checkListingMask( final ArtifactStore store, final String
96121
return false;
97122
}
98123

124+
public static String extractGroupIdPath(String metadataPath)
125+
{
126+
if (metadataPath == null || !metadataPath.endsWith("maven-metadata.xml"))
127+
{
128+
return null;
129+
}
130+
131+
// Need at least groupId/artifactId/filename
132+
String[] parts = metadataPath.split("/");
133+
if (parts.length < 3) return null;
134+
135+
return String.join("/", Arrays.copyOf(parts, parts.length - 2)) + "/";
136+
}
137+
99138
public static boolean checkMavenMetadataMask( final ArtifactStore store, final String path )
100139
{
101140
Set<String> maskPatterns = store.getPathMaskPatterns();

0 commit comments

Comments
 (0)