Skip to content

Commit fa79cb2

Browse files
committed
[MNG-7116] add support for mirrorOf external:http:*
1 parent e5f6634 commit fa79cb2

File tree

2 files changed

+89
-10
lines changed

2 files changed

+89
-10
lines changed

maven-compat/src/main/java/org/apache/maven/repository/DefaultMirrorSelector.java

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class DefaultMirrorSelector
4141

4242
private static final String EXTERNAL_WILDCARD = "external:*";
4343

44+
private static final String EXTERNAL_HTTP_WILDCARD = "external:http:*";
45+
4446
public Mirror getMirror( ArtifactRepository repository, List<Mirror> mirrors )
4547
{
4648
String repoId = repository.getId();
@@ -68,9 +70,14 @@ public Mirror getMirror( ArtifactRepository repository, List<Mirror> mirrors )
6870
}
6971

7072
/**
71-
* This method checks if the pattern matches the originalRepository. Valid patterns: * =
72-
* everything external:* = everything not on the localhost and not file based. repo,repo1 = repo
73-
* or repo1 *,!repo1 = everything except repo1
73+
* This method checks if the pattern matches the originalRepository. Valid patterns:
74+
* <ul>
75+
* <li>{@code *} = everything,</li>
76+
* <li>{@code external:*} = everything not on the localhost and not file based,</li>
77+
* <li>{@code external:http:*} = any repository not on the localhost using HTTP,</li>
78+
* <li>{@code repo,repo1} = {@code repo} or {@code repo1},</li>
79+
* <li>{@code *,!repo1} = everything except {@code repo1}.</li>
80+
* </ul>
7481
*
7582
* @param originalRepository to compare for a match.
7683
* @param pattern used for match. Currently only '*' is supported.
@@ -115,6 +122,12 @@ else if ( EXTERNAL_WILDCARD.equals( repo ) && isExternalRepo( originalRepository
115122
result = true;
116123
// don't stop processing in case a future segment explicitly excludes this repo
117124
}
125+
// check for external:http:*
126+
else if ( EXTERNAL_HTTP_WILDCARD.equals( repo ) && isExternalHttpRepo( originalRepository ) )
127+
{
128+
result = true;
129+
// don't stop processing in case a future segment explicitly excludes this repo
130+
}
118131
else if ( WILDCARD.equals( repo ) )
119132
{
120133
result = true;
@@ -136,8 +149,34 @@ static boolean isExternalRepo( ArtifactRepository originalRepository )
136149
try
137150
{
138151
URL url = new URL( originalRepository.getUrl() );
139-
return !( url.getHost().equals( "localhost" ) || url.getHost().equals( "127.0.0.1" )
140-
|| url.getProtocol().equals( "file" ) );
152+
return !( isLocal( url.getHost() ) || url.getProtocol().equals( "file" ) );
153+
}
154+
catch ( MalformedURLException e )
155+
{
156+
// bad url just skip it here. It should have been validated already, but the wagon lookup will deal with it
157+
return false;
158+
}
159+
}
160+
161+
private static boolean isLocal( String host )
162+
{
163+
return "localhost".equals( host ) || "127.0.0.1".equals( host );
164+
}
165+
166+
/**
167+
* Checks the URL to see if this repository refers to a non-localhost repository using HTTP.
168+
*
169+
* @param originalRepository
170+
* @return true if external.
171+
*/
172+
static boolean isExternalHttpRepo( ArtifactRepository originalRepository )
173+
{
174+
try
175+
{
176+
URL url = new URL( originalRepository.getUrl() );
177+
return ( "http".equalsIgnoreCase( url.getProtocol() ) || "dav".equalsIgnoreCase( url.getProtocol() )
178+
|| "dav:http".equalsIgnoreCase( url.getProtocol() )
179+
|| "dav+http".equalsIgnoreCase( url.getProtocol() ) ) && !isLocal( url.getHost() );
141180
}
142181
catch ( MalformedURLException e )
143182
{
@@ -146,7 +185,7 @@ static boolean isExternalRepo( ArtifactRepository originalRepository )
146185
}
147186
}
148187

149-
static boolean matchesLayout( ArtifactRepository repository, Mirror mirror )
188+
static boolean matchesLayout( ArtifactRepository repository, Mirror mirror )
150189
{
151190
return matchesLayout( RepositoryUtils.getLayout( repository ), mirror.getMirrorOfLayouts() );
152191
}

maven-core/src/main/java/org/apache/maven/bridge/MavenRepositorySystem.java

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,8 @@ public ArtifactRepository createLocalRepository( MavenExecutionRequest request,
710710

711711
private static final String EXTERNAL_WILDCARD = "external:*";
712712

713+
private static final String EXTERNAL_HTTP_WILDCARD = "external:http:*";
714+
713715
public static Mirror getMirror( ArtifactRepository repository, List<Mirror> mirrors )
714716
{
715717
String repoId = repository.getId();
@@ -737,8 +739,14 @@ public static Mirror getMirror( ArtifactRepository repository, List<Mirror> mirr
737739
}
738740

739741
/**
740-
* This method checks if the pattern matches the originalRepository. Valid patterns: * = everything external:* =
741-
* everything not on the localhost and not file based. repo,repo1 = repo or repo1 *,!repo1 = everything except repo1
742+
* This method checks if the pattern matches the originalRepository. Valid patterns:
743+
* <ul>
744+
* <li>{@code *} = everything,</li>
745+
* <li>{@code external:*} = everything not on the localhost and not file based,</li>
746+
* <li>{@code external:http:*} = any repository not on the localhost using HTTP,</li>
747+
* <li>{@code repo,repo1} = {@code repo} or {@code repo1},</li>
748+
* <li>{@code *,!repo1} = everything except {@code repo1}.</li>
749+
* </ul>
742750
*
743751
* @param originalRepository to compare for a match.
744752
* @param pattern used for match. Currently only '*' is supported.
@@ -782,6 +790,12 @@ else if ( EXTERNAL_WILDCARD.equals( repo ) && isExternalRepo( originalRepository
782790
result = true;
783791
// don't stop processing in case a future segment explicitly excludes this repo
784792
}
793+
// check for external:http:*
794+
else if ( EXTERNAL_HTTP_WILDCARD.equals( repo ) && isExternalHttpRepo( originalRepository ) )
795+
{
796+
result = true;
797+
// don't stop processing in case a future segment explicitly excludes this repo
798+
}
785799
else if ( WILDCARD.equals( repo ) )
786800
{
787801
result = true;
@@ -803,8 +817,34 @@ static boolean isExternalRepo( ArtifactRepository originalRepository )
803817
try
804818
{
805819
URL url = new URL( originalRepository.getUrl() );
806-
return !( url.getHost().equals( "localhost" ) || url.getHost().equals( "127.0.0.1" )
807-
|| url.getProtocol().equals( "file" ) );
820+
return !( isLocal( url.getHost() ) || url.getProtocol().equals( "file" ) );
821+
}
822+
catch ( MalformedURLException e )
823+
{
824+
// bad url just skip it here. It should have been validated already, but the wagon lookup will deal with it
825+
return false;
826+
}
827+
}
828+
829+
private static boolean isLocal( String host )
830+
{
831+
return "localhost".equals( host ) || "127.0.0.1".equals( host );
832+
}
833+
834+
/**
835+
* Checks the URL to see if this repository refers to a non-localhost repository using HTTP.
836+
*
837+
* @param originalRepository
838+
* @return true if external.
839+
*/
840+
static boolean isExternalHttpRepo( ArtifactRepository originalRepository )
841+
{
842+
try
843+
{
844+
URL url = new URL( originalRepository.getUrl() );
845+
return ( "http".equalsIgnoreCase( url.getProtocol() ) || "dav".equalsIgnoreCase( url.getProtocol() )
846+
|| "dav:http".equalsIgnoreCase( url.getProtocol() )
847+
|| "dav+http".equalsIgnoreCase( url.getProtocol() ) ) && !isLocal( url.getHost() );
808848
}
809849
catch ( MalformedURLException e )
810850
{

0 commit comments

Comments
 (0)