Skip to content

Commit

Permalink
[MNG-7116] add support for mirrorOf external:http:*
Browse files Browse the repository at this point in the history
  • Loading branch information
hboutemy committed Mar 19, 2021
1 parent e5f6634 commit fa79cb2
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class DefaultMirrorSelector

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

private static final String EXTERNAL_HTTP_WILDCARD = "external:http:*";

public Mirror getMirror( ArtifactRepository repository, List<Mirror> mirrors )
{
String repoId = repository.getId();
Expand Down Expand Up @@ -68,9 +70,14 @@ public Mirror getMirror( ArtifactRepository repository, List<Mirror> mirrors )
}

/**
* This method checks if the pattern matches the originalRepository. Valid patterns: * =
* everything external:* = everything not on the localhost and not file based. repo,repo1 = repo
* or repo1 *,!repo1 = everything except repo1
* This method checks if the pattern matches the originalRepository. Valid patterns:
* <ul>
* <li>{@code *} = everything,</li>
* <li>{@code external:*} = everything not on the localhost and not file based,</li>
* <li>{@code external:http:*} = any repository not on the localhost using HTTP,</li>
* <li>{@code repo,repo1} = {@code repo} or {@code repo1},</li>
* <li>{@code *,!repo1} = everything except {@code repo1}.</li>
* </ul>
*
* @param originalRepository to compare for a match.
* @param pattern used for match. Currently only '*' is supported.
Expand Down Expand Up @@ -115,6 +122,12 @@ else if ( EXTERNAL_WILDCARD.equals( repo ) && isExternalRepo( originalRepository
result = true;
// don't stop processing in case a future segment explicitly excludes this repo
}
// check for external:http:*
else if ( EXTERNAL_HTTP_WILDCARD.equals( repo ) && isExternalHttpRepo( originalRepository ) )
{
result = true;
// don't stop processing in case a future segment explicitly excludes this repo
}
else if ( WILDCARD.equals( repo ) )
{
result = true;
Expand All @@ -136,8 +149,34 @@ static boolean isExternalRepo( ArtifactRepository originalRepository )
try
{
URL url = new URL( originalRepository.getUrl() );
return !( url.getHost().equals( "localhost" ) || url.getHost().equals( "127.0.0.1" )
|| url.getProtocol().equals( "file" ) );
return !( isLocal( url.getHost() ) || url.getProtocol().equals( "file" ) );
}
catch ( MalformedURLException e )
{
// bad url just skip it here. It should have been validated already, but the wagon lookup will deal with it
return false;
}
}

private static boolean isLocal( String host )
{
return "localhost".equals( host ) || "127.0.0.1".equals( host );
}

/**
* Checks the URL to see if this repository refers to a non-localhost repository using HTTP.
*
* @param originalRepository
* @return true if external.
*/
static boolean isExternalHttpRepo( ArtifactRepository originalRepository )
{
try
{
URL url = new URL( originalRepository.getUrl() );
return ( "http".equalsIgnoreCase( url.getProtocol() ) || "dav".equalsIgnoreCase( url.getProtocol() )
|| "dav:http".equalsIgnoreCase( url.getProtocol() )
|| "dav+http".equalsIgnoreCase( url.getProtocol() ) ) && !isLocal( url.getHost() );
}
catch ( MalformedURLException e )
{
Expand All @@ -146,7 +185,7 @@ static boolean isExternalRepo( ArtifactRepository originalRepository )
}
}

static boolean matchesLayout( ArtifactRepository repository, Mirror mirror )
static boolean matchesLayout( ArtifactRepository repository, Mirror mirror )
{
return matchesLayout( RepositoryUtils.getLayout( repository ), mirror.getMirrorOfLayouts() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,8 @@ public ArtifactRepository createLocalRepository( MavenExecutionRequest request,

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

private static final String EXTERNAL_HTTP_WILDCARD = "external:http:*";

public static Mirror getMirror( ArtifactRepository repository, List<Mirror> mirrors )
{
String repoId = repository.getId();
Expand Down Expand Up @@ -737,8 +739,14 @@ public static Mirror getMirror( ArtifactRepository repository, List<Mirror> mirr
}

/**
* This method checks if the pattern matches the originalRepository. Valid patterns: * = everything external:* =
* everything not on the localhost and not file based. repo,repo1 = repo or repo1 *,!repo1 = everything except repo1
* This method checks if the pattern matches the originalRepository. Valid patterns:
* <ul>
* <li>{@code *} = everything,</li>
* <li>{@code external:*} = everything not on the localhost and not file based,</li>
* <li>{@code external:http:*} = any repository not on the localhost using HTTP,</li>
* <li>{@code repo,repo1} = {@code repo} or {@code repo1},</li>
* <li>{@code *,!repo1} = everything except {@code repo1}.</li>
* </ul>
*
* @param originalRepository to compare for a match.
* @param pattern used for match. Currently only '*' is supported.
Expand Down Expand Up @@ -782,6 +790,12 @@ else if ( EXTERNAL_WILDCARD.equals( repo ) && isExternalRepo( originalRepository
result = true;
// don't stop processing in case a future segment explicitly excludes this repo
}
// check for external:http:*
else if ( EXTERNAL_HTTP_WILDCARD.equals( repo ) && isExternalHttpRepo( originalRepository ) )
{
result = true;
// don't stop processing in case a future segment explicitly excludes this repo
}
else if ( WILDCARD.equals( repo ) )
{
result = true;
Expand All @@ -803,8 +817,34 @@ static boolean isExternalRepo( ArtifactRepository originalRepository )
try
{
URL url = new URL( originalRepository.getUrl() );
return !( url.getHost().equals( "localhost" ) || url.getHost().equals( "127.0.0.1" )
|| url.getProtocol().equals( "file" ) );
return !( isLocal( url.getHost() ) || url.getProtocol().equals( "file" ) );
}
catch ( MalformedURLException e )
{
// bad url just skip it here. It should have been validated already, but the wagon lookup will deal with it
return false;
}
}

private static boolean isLocal( String host )
{
return "localhost".equals( host ) || "127.0.0.1".equals( host );
}

/**
* Checks the URL to see if this repository refers to a non-localhost repository using HTTP.
*
* @param originalRepository
* @return true if external.
*/
static boolean isExternalHttpRepo( ArtifactRepository originalRepository )
{
try
{
URL url = new URL( originalRepository.getUrl() );
return ( "http".equalsIgnoreCase( url.getProtocol() ) || "dav".equalsIgnoreCase( url.getProtocol() )
|| "dav:http".equalsIgnoreCase( url.getProtocol() )
|| "dav+http".equalsIgnoreCase( url.getProtocol() ) ) && !isLocal( url.getHost() );
}
catch ( MalformedURLException e )
{
Expand Down

0 comments on commit fa79cb2

Please sign in to comment.