import org.apache.maven.archiva.discovery.ArtifactDiscoverer;
import org.apache.maven.archiva.discovery.DiscovererException;
import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
+import org.apache.maven.model.DistributionManagement;
+import org.apache.maven.model.Model;
+import org.apache.maven.model.Relocation;
+import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.wagon.ConnectionException;
import org.apache.maven.wagon.ResourceDoesNotExistException;
import org.apache.maven.wagon.TransferFailedException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
+import java.io.Reader;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.Iterator;
extends AbstractLogEnabled
implements ProxyRequestHandler
{
+ /**
+ * @plexus.requirement
+ */
+ private ArtifactFactory factory;
+
/**
* @plexus.requirement role-hint="default"
* @todo use a map, and have priorities in them
{
File target = new File( managedRepository.getBasedir(), path );
- for ( Iterator i = proxiedRepositories.iterator(); i.hasNext(); )
+ if ( path.endsWith( "maven-metadata.xml" ) )
{
- ProxiedArtifactRepository repository = (ProxiedArtifactRepository) i.next();
+ // Request for managed repository metadatas
+ getMetadata( path, target, proxiedRepositories, managedRepository, wagonProxy, force );
+ }
+ else
+ {
+ boolean checksum = false;
+ String checksumExtension = null;
+ String artifactPath = path;
+ if ( path.endsWith( ".md5" ) || path.endsWith( ".sha1" ) )
+ {
+ int index = path.lastIndexOf( '.' );
+ checksumExtension = path.substring( index + 1 );
+ checksum = true;
+ artifactPath = path.substring( 0, index );
+ }
- if ( !force && repository.isCachedFailure( path ) )
+ // Request for artifact: parse the requested path to build an Artifact.
+ Artifact artifact = null;
+ try
{
- processCachedRepositoryFailure( repository, "Cached failure found for: " + path );
+ artifact = defaultArtifactDiscoverer.buildArtifact( artifactPath );
}
- else
+ catch ( DiscovererException e )
+ {
+ getLogger().debug( "Failed to build artifact using default layout with message: " + e.getMessage() );
+ }
+
+ if ( artifact == null )
+ {
+ try
+ {
+ artifact = legacyArtifactDiscoverer.buildArtifact( artifactPath );
+ }
+ catch ( DiscovererException e )
+ {
+ getLogger().debug( "Failed to build artifact using legacy layout with message: " + e.getMessage() );
+ }
+ }
+
+ if ( artifact != null )
+ {
+ applyRelocation( managedRepository, artifact, proxiedRepositories, wagonProxy, force );
+
+ if ( !checksum )
+ {
+ // Build the target file name
+ target = new File( managedRepository.getBasedir(), managedRepository.pathOf( artifact ) );
+
+ // Get the requested artifact from proxiedRepositories
+ getArtifactFromRepository( managedRepository, target, artifact, proxiedRepositories, wagonProxy,
+ force );
+ }
+ else
+ {
+ // Just adjust the filename for relocation, don't actualy get it
+ target = new File( managedRepository.getBasedir(),
+ managedRepository.pathOf( artifact ) + "." + checksumExtension );
+ }
+ }
+ else if ( !checksum )
{
- target = get( path, target, repository, managedRepository, wagonProxy, force );
+ // Some other unknown file in the repository, proxy as is, unless it was a checksum
+ if ( force || !target.exists() )
+ {
+ getFileFromRepository( managedRepository, target, path, proxiedRepositories, wagonProxy, force );
+ }
}
}
return target;
}
- /**
- * @return the target File may not be same as the target argument, if a
- * maven1 to maven2 path convertion occured.
- */
- private File get( String path, File target, ProxiedArtifactRepository repository,
- ArtifactRepository managedRepository, ProxyInfo wagonProxy, boolean force )
- throws ProxyException
+ private void getFileFromRepository( ArtifactRepository managedRepository, File target, String path,
+ List proxiedRepositories, ProxyInfo wagonProxy, boolean force )
+ throws ProxyException, ResourceDoesNotExistException
{
- ArtifactRepositoryPolicy policy;
-
- if ( path.endsWith( ".md5" ) || path.endsWith( ".sha1" ) )
+ for ( Iterator i = proxiedRepositories.iterator(); i.hasNext(); )
{
- // always read from the managed repository, no need to make remote request
+ ProxiedArtifactRepository repository = (ProxiedArtifactRepository) i.next();
+
+ if ( !force && repository.isCachedFailure( path ) )
+ {
+ processCachedRepositoryFailure( repository, "Cached failure found for: " + path );
+ }
+ else
+ {
+ ArtifactRepositoryPolicy policy = repository.getRepository().getReleases();
+ getFileFromRepository( path, repository, managedRepository.getBasedir(), wagonProxy, target, policy,
+ force );
+ }
}
- else if ( path.endsWith( "maven-metadata.xml" ) )
- {
- File metadataFile = new File( target.getParentFile(), ".metadata-" + repository.getRepository().getId() );
+ }
- policy = repository.getRepository().getReleases();
+ private void getArtifactFromRepository( ArtifactRepository managedRepository, File target, Artifact artifact,
+ List proxiedRepositories, ProxyInfo wagonProxy, boolean force )
+ throws ProxyException, ResourceDoesNotExistException
+ {
+ for ( Iterator i = proxiedRepositories.iterator(); i.hasNext(); )
+ {
+ ProxiedArtifactRepository repository = (ProxiedArtifactRepository) i.next();
+ String path = repository.getRepository().getLayout().pathOf( artifact );
- // if it is snapshot metadata, use a different policy
- if ( path.endsWith( "-SNAPSHOT/maven-metadata.xml" ) )
+ if ( !force && repository.isCachedFailure( path ) )
{
- policy = repository.getRepository().getSnapshots();
+ processCachedRepositoryFailure( repository, "Cached failure found for: " + path );
}
-
- if ( force || !metadataFile.exists() || isOutOfDate( policy, metadataFile ) )
+ else
{
- getFileFromRepository( path, repository, managedRepository.getBasedir(), wagonProxy, metadataFile,
- policy, force );
-
- mergeMetadataFiles( target, metadataFile );
+ get( artifact, target, repository, managedRepository, wagonProxy, force );
}
}
- else
+ }
+
+ private void applyRelocation( ArtifactRepository managedRepository, Artifact artifact, List proxiedRepositories,
+ ProxyInfo wagonProxy, boolean force )
+ {
+ Artifact pomArtifact =
+ factory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() );
+
+ File pomFile = new File( managedRepository.getBasedir(), managedRepository.pathOf( pomArtifact ) );
+ try
{
- Artifact artifact = null;
+ getArtifactFromRepository( managedRepository, pomFile, pomArtifact, proxiedRepositories, wagonProxy,
+ force );
+ }
+ catch ( ProxyException e )
+ {
+ getLogger().warn( "Error getting POM for artifact - not relocating: " + e.getMessage() );
+ getLogger().debug( "Cause", e );
+ }
+ catch ( ResourceDoesNotExistException e )
+ {
+ getLogger().debug( "Remote POM not found for artifact - not relocating" );
+ }
+
+ if ( pomFile.exists() )
+ {
+ Model model = null;
try
{
- artifact = defaultArtifactDiscoverer.buildArtifact( path );
+ // Parse the pom and look at relocation metadata
+ Reader reader = new FileReader( pomFile );
+ model = new MavenXpp3Reader().read( reader );
}
- catch ( DiscovererException e )
+ catch ( IOException e )
{
- getLogger().debug( "Failed to build artifact using default layout with message: " + e.getMessage() );
+ getLogger().warn( "Error reading POM for artifact - not relocating: " + e.getMessage() );
+ getLogger().debug( "Cause", e );
}
-
- if ( artifact == null )
+ catch ( XmlPullParserException e )
{
- try
- {
- artifact = legacyArtifactDiscoverer.buildArtifact( path );
- }
- catch ( DiscovererException e )
- {
- getLogger().debug( "Failed to build artifact using legacy layout with message: " + e.getMessage() );
- }
+ getLogger().warn( "Error parsing POM for artifact - not relocating: " + e.getMessage() );
+ getLogger().debug( "Cause", e );
}
- if ( artifact != null )
+ if ( model != null )
{
- target = new File( managedRepository.getBasedir(), managedRepository.pathOf( artifact ) );
+ DistributionManagement dist;
+ dist = model.getDistributionManagement();
- ArtifactRepository artifactRepository = repository.getRepository();
+ if ( dist != null )
+ {
+ Relocation relocation = dist.getRelocation();
+ if ( relocation != null )
+ {
+ String requestedId =
+ artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion();
- // we use the release policy for tracking failures, but only check for updates on snapshots
- // also, we don't look for updates on timestamp snapshot files, only non-unique-version ones
- policy = artifact.isSnapshot() ? artifactRepository.getSnapshots() : artifactRepository.getReleases();
+ // artifact is relocated : update the artifact
+ if ( relocation.getGroupId() != null )
+ {
+ artifact.setGroupId( relocation.getGroupId() );
+ }
+ if ( relocation.getArtifactId() != null )
+ {
+ artifact.setArtifactId( relocation.getArtifactId() );
+ }
+ if ( relocation.getVersion() != null )
+ {
+ artifact.setVersion( relocation.getVersion() );
+ }
- boolean needsUpdate = false;
- if ( artifact.getVersion().endsWith( "-SNAPSHOT" ) && isOutOfDate( policy, target ) )
- {
- needsUpdate = true;
- }
+ String relocatedId =
+ artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion();
- if ( needsUpdate || force || !target.exists() )
- {
- getFileFromRepository( artifactRepository.pathOf( artifact ), repository,
- managedRepository.getBasedir(), wagonProxy, target, policy, force );
+ getLogger().debug( "Artifact " + requestedId + " has been relocated to " + relocatedId +
+ ( relocation.getMessage() != null ? ": " + relocation.getMessage() : "" ) );
+
+ applyRelocation( managedRepository, artifact, proxiedRepositories, wagonProxy, force );
+ }
}
}
- else
+ }
+ }
+
+ private void getMetadata( String path, File target, List proxiedRepositories, ArtifactRepository managedRepository,
+ ProxyInfo wagonProxy, boolean force )
+ throws ProxyException
+ {
+ for ( Iterator i = proxiedRepositories.iterator(); i.hasNext(); )
+ {
+ ProxiedArtifactRepository repository = (ProxiedArtifactRepository) i.next();
+ File metadataFile = new File( target.getParentFile(), ".metadata-" + repository.getRepository().getId() );
+
+ ArtifactRepositoryPolicy policy = repository.getRepository().getReleases();
+
+ // if it is snapshot metadata, use a different policy
+ if ( path.endsWith( "-SNAPSHOT/maven-metadata.xml" ) )
{
- // Some other unknown file in the repository, proxy as is
- if ( force || !target.exists() )
- {
- policy = repository.getRepository().getReleases();
- getFileFromRepository( path, repository, managedRepository.getBasedir(), wagonProxy, target, policy,
- force );
- }
+ policy = repository.getRepository().getSnapshots();
+ }
+
+ if ( force || !metadataFile.exists() || isOutOfDate( policy, metadataFile ) )
+ {
+ getFileFromRepository( path, repository, managedRepository.getBasedir(), wagonProxy, metadataFile,
+ policy, force );
+
+ mergeMetadataFiles( target, metadataFile );
}
}
+ }
- if ( target.exists() )
+ private void get( Artifact artifact, File target, ProxiedArtifactRepository repository,
+ ArtifactRepository managedRepository, ProxyInfo wagonProxy, boolean force )
+ throws ProxyException
+ {
+ ArtifactRepository artifactRepository = repository.getRepository();
+
+ // we use the release policy for tracking failures, but only check for updates on snapshots
+ // also, we don't look for updates on timestamp snapshot files, only non-unique-version ones
+ ArtifactRepositoryPolicy policy =
+ artifact.isSnapshot() ? artifactRepository.getSnapshots() : artifactRepository.getReleases();
+
+ boolean needsUpdate = false;
+ if ( artifact.getVersion().endsWith( "-SNAPSHOT" ) && isOutOfDate( policy, target ) )
{
- // in case it previously failed and we've since found it
- repository.clearFailure( path );
+ needsUpdate = true;
+ }
+
+ if ( needsUpdate || force || !target.exists() )
+ {
+ getFileFromRepository( artifactRepository.pathOf( artifact ), repository, managedRepository.getBasedir(),
+ wagonProxy, target, policy, force );
}
- return target;
}
private void mergeMetadataFiles( File target, File metadataFile )
checksumExt.toUpperCase(),
path.substring( path.lastIndexOf( '/' ) ) );
- String actualChecksum = checksum.getActualChecksum().toUpperCase();
+ String actualChecksum = checksum.getActualChecksum();
+
remoteChecksum = remoteChecksum.toUpperCase();
- if ( remoteChecksum.equals( actualChecksum ) )
+ if ( actualChecksum != null && remoteChecksum.equals( actualChecksum.toUpperCase() ) )
{
moveTempToTarget( tempChecksumFile, checksumFile );
assertEquals( "Check file matches", expectedFile, file );
}
+ public void testRelocateMaven1Request()
+ throws IOException, ResourceDoesNotExistException, ProxyException
+ {
+ String path = "org.apache.maven.test/jars/get-relocated-artefact-1.0.jar";
+ String relocatedPath =
+ "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
+ File expectedFile = new File( defaultManagedRepository.getBasedir(), relocatedPath );
+
+ assertTrue( expectedFile.exists() );
+
+ File file = requestHandler.get( path, proxiedRepositories, defaultManagedRepository );
+
+ assertEquals( "Check file matches", expectedFile, file );
+ }
+
+ public void testDoublyRelocateMaven1Request()
+ throws IOException, ResourceDoesNotExistException, ProxyException
+ {
+ String path = "org.apache.maven.test/jars/get-doubly-relocated-artefact-1.0.jar";
+ String relocatedPath =
+ "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
+ File expectedFile = new File( defaultManagedRepository.getBasedir(), relocatedPath );
+
+ assertTrue( expectedFile.exists() );
+
+ File file = requestHandler.get( path, proxiedRepositories, defaultManagedRepository );
+
+ assertEquals( "Check file matches", expectedFile, file );
+ }
+
+ public void testRelocateMaven1PomRequest()
+ throws IOException, ResourceDoesNotExistException, ProxyException
+ {
+ String path = "org.apache.maven.test/poms/get-relocated-artefact-with-pom-1.0.pom";
+ String relocatedPath =
+ "org/apache/maven/test/get-default-layout-present-with-pom/1.0/get-default-layout-present-with-pom-1.0.pom";
+ File expectedFile = new File( defaultManagedRepository.getBasedir(), relocatedPath );
+
+ assertTrue( expectedFile.exists() );
+
+ File file = requestHandler.get( path, proxiedRepositories, defaultManagedRepository );
+
+ assertEquals( "Check file matches", expectedFile, file );
+
+ assertTrue( expectedFile.exists() );
+ }
+
+ public void testRelocateMaven1PomRequestMissingTarget()
+ throws IOException, ResourceDoesNotExistException, ProxyException
+ {
+ String path = "org.apache.maven.test/poms/get-relocated-artefact-1.0.pom";
+ String relocatedPath =
+ "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.pom";
+ File expectedFile = new File( defaultManagedRepository.getBasedir(), relocatedPath );
+
+ assertFalse( expectedFile.exists() );
+
+ try
+ {
+ requestHandler.get( path, proxiedRepositories, defaultManagedRepository );
+ fail( "Should have failed to find target POM" );
+ }
+ catch ( ResourceDoesNotExistException e )
+ {
+ assertTrue( true );
+ }
+ }
+
+ public void testRelocateMaven1ChecksumRequest()
+ throws IOException, ResourceDoesNotExistException, ProxyException
+ {
+ String path = "org.apache.maven.test/jars/get-relocated-artefact-1.0.jar.md5";
+ String relocatedPath =
+ "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar.md5";
+ File expectedFile = new File( defaultManagedRepository.getBasedir(), relocatedPath );
+
+ assertTrue( expectedFile.exists() );
+
+ File file = requestHandler.get( path, proxiedRepositories, defaultManagedRepository );
+
+ assertEquals( "Check file matches", expectedFile, file );
+
+ assertTrue( expectedFile.exists() );
+
+ path = "org.apache.maven.test/jars/get-relocated-artefact-1.0.jar.sha1";
+ relocatedPath = "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar.sha1";
+ expectedFile = new File( defaultManagedRepository.getBasedir(), relocatedPath );
+
+ assertFalse( expectedFile.exists() );
+
+ try
+ {
+ requestHandler.get( path, proxiedRepositories, defaultManagedRepository );
+ fail( "Checksum was not present, should not be found" );
+ }
+ catch ( ResourceDoesNotExistException e )
+ {
+ assertTrue( true );
+ }
+ }
+
+ public void testRelocateMaven2Request()
+ throws IOException, ResourceDoesNotExistException, ProxyException
+ {
+ String path = "org/apache/maven/test/get-relocated-artefact/1.0/get-relocated-artefact-1.0.jar";
+ String relocatedPath =
+ "org/apache/maven/test/get-default-layout-present/1.0/get-default-layout-present-1.0.jar";
+ File expectedFile = new File( defaultManagedRepository.getBasedir(), relocatedPath );
+
+ assertTrue( expectedFile.exists() );
+
+ File file = requestHandler.get( path, proxiedRepositories, defaultManagedRepository );
+
+ assertEquals( "Check file matches", expectedFile, file );
+ }
+
+ public void testRelocateMaven2RequestInLegacyManagedRepo()
+ throws IOException, ResourceDoesNotExistException, ProxyException
+ {
+ String path = "org/apache/maven/test/get-relocated-artefact/1.0/get-relocated-artefact-1.0.jar";
+ String relocatedPath = "org.apache.maven.test/jars/get-default-layout-present-1.0.jar";
+ File expectedFile = new File( legacyManagedRepository.getBasedir(), relocatedPath );
+
+ assertTrue( expectedFile.exists() );
+
+ File file = requestHandler.get( path, proxiedRepositories, legacyManagedRepository );
+
+ assertEquals( "Check file matches", expectedFile, file );
+ }
+
private static Versioning getVersioning( List versions )
{
Versioning versioning = new Versioning();