import javax.servlet.http.HttpServletResponse;
+import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.jackrabbit.webdav.DavException;
import org.apache.jackrabbit.webdav.DavResource;
import org.apache.maven.model.Model;
import org.apache.maven.model.Relocation;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
-import org.codehaus.plexus.evaluator.DefaultExpressionEvaluator;
-import org.codehaus.plexus.evaluator.EvaluatorException;
-import org.codehaus.plexus.evaluator.ExpressionEvaluator;
-import org.codehaus.plexus.evaluator.sources.SystemPropertyExpressionSource;
+import org.codehaus.plexus.digest.ChecksumFile;
+import org.codehaus.plexus.digest.Digester;
+import org.codehaus.plexus.digest.DigesterException;
import org.codehaus.plexus.redback.authentication.AuthenticationException;
import org.codehaus.plexus.redback.authentication.AuthenticationResult;
import org.codehaus.plexus.redback.authorization.AuthorizationException;
*/
private final LockManager lockManager = new SimpleLockManager();
- /** @plexus.requirement */
+ /**
+ * @plexus.requirement
+ */
private RepositoryContentConsumers consumers;
-
- private String defaultMergedMetadataLocation = "${appserver.base}/data/maven-metadata.xml";
+ /**
+ * @plexus.requirement
+ */
+ private ChecksumFile checksum;
+
+ /**
+ * @plexus.requirement role-hint="sha1"
+ */
+ private Digester digestSha1;
+
+ /**
+ * @plexus.requirement role-hint="md5";
+ */
+ private Digester digestMd5;
+
+ private static final String mergedMetadataFilename = "/merged-maven-metadata.xml";
+
public DavResource createResource( final DavResourceLocator locator, final DavServletRequest request,
final DavServletResponse response )
throws DavException
}
}
}
- catch ( DavException de )
+ catch ( DavException de )
{
e = de;
continue;
{
throw e;
}
-
- // merge metadata only when requested via the repo group
- if ( request.getRequestURI().endsWith( "metadata.xml" ) && repoGroupConfig != null )
+
+ String requestedResource = request.getRequestURI();
+
+ // MRM-872 : merge all available metadata
+ // merge metadata only when requested via the repo group
+ if ( ( repositoryRequest.isMetadata( requestedResource ) || ( requestedResource.endsWith( "metadata.xml.sha1" ) || requestedResource.endsWith( "metadata.xml.md5" ) ) ) &&
+ repoGroupConfig != null )
{
- // TODO MRM-872 : must merge all available metadatas
- ArchivaRepositoryMetadata mergedMetadata = new ArchivaRepositoryMetadata();
- for ( String resourceAbsPath : resourcesInAbsolutePath )
- {
- try
- {
- File metadataFile = new File( resourceAbsPath );
- ArchivaRepositoryMetadata repoMetadata = RepositoryMetadataReader.read( metadataFile );
- mergedMetadata = RepositoryMetadataMerge.merge( mergedMetadata, repoMetadata );
- }
- catch ( RepositoryMetadataException r )
- {
- throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
- "Error occurred while reading metadata file." );
- }
- }
-
- try
- {
- if( StringUtils.contains( defaultMergedMetadataLocation, "${" ) )
- {
- defaultMergedMetadataLocation =
- evaluateExpressions( defaultMergedMetadataLocation );
- }
- File resourceFile = writeMergedMetadataToFile( mergedMetadata, defaultMergedMetadataLocation );
+ // this should only be at the project level not version level!
+ if( isProjectReference( requestedResource ) )
+ {
+ String artifactId = StringUtils.substringBeforeLast( requestedResource.replace( '\\', '/' ), "/" );
+ artifactId = StringUtils.substringAfterLast( artifactId, "/" );
- LogicalResource logicalResource =
- new LogicalResource( RepositoryPathUtil.getLogicalResource( locator.getResourcePath() ) );
-
- ArchivaDavResource metadataResource =
- new ArchivaDavResource( resourceFile.getAbsolutePath(), logicalResource.getPath(), null,
- request.getRemoteAddr(), request.getDavSession(), archivaLocator, this,
- mimeTypes, auditListeners, consumers );
- availableResources.add( 0, metadataResource );
- }
- catch ( EvaluatorException ee )
- {
- throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ee.getMessage() );
- }
- catch ( RepositoryMetadataException r )
- {
- throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
- "Error occurred while writing metadata file." );
+ ArchivaDavResource res = ( ArchivaDavResource ) availableResources.get( 0 );
+ String filePath = StringUtils.substringBeforeLast( res.getLocalResource().getAbsolutePath().replace( '\\', '/' ), "/" );
+ filePath = filePath + mergedMetadataFilename;
+
+ // for MRM-872 handle checksums of the merged metadata files
+ if( repositoryRequest.isSupportFile( requestedResource ) )
+ {
+ File metadataChecksum = new File( filePath + "."
+ + StringUtils.substringAfterLast( requestedResource, "." ) );
+ if( metadataChecksum.exists() )
+ {
+ LogicalResource logicalResource =
+ new LogicalResource( RepositoryPathUtil.getLogicalResource( locator.getResourcePath() ) );
+
+ ArchivaDavResource metadataChecksumResource =
+ new ArchivaDavResource( metadataChecksum.getAbsolutePath(), logicalResource.getPath(), null,
+ request.getRemoteAddr(), request.getDavSession(), archivaLocator, this,
+ mimeTypes, auditListeners, consumers );
+ availableResources.add( 0, metadataChecksumResource );
+ }
+ }
+ else
+ { // merge the metadata of all repos under group
+ ArchivaRepositoryMetadata mergedMetadata = new ArchivaRepositoryMetadata();
+ for ( String resourceAbsPath : resourcesInAbsolutePath )
+ {
+ try
+ {
+ File metadataFile = new File( resourceAbsPath );
+ ArchivaRepositoryMetadata repoMetadata = RepositoryMetadataReader.read( metadataFile );
+ mergedMetadata = RepositoryMetadataMerge.merge( mergedMetadata, repoMetadata );
+ }
+ catch ( RepositoryMetadataException r )
+ {
+ throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
+ "Error occurred while reading metadata file." );
+ }
+ }
+
+ try
+ {
+ File resourceFile = writeMergedMetadataToFile( mergedMetadata, filePath );
+
+ LogicalResource logicalResource =
+ new LogicalResource( RepositoryPathUtil.getLogicalResource( locator.getResourcePath() ) );
+
+ ArchivaDavResource metadataResource =
+ new ArchivaDavResource( resourceFile.getAbsolutePath(), logicalResource.getPath(), null,
+ request.getRemoteAddr(), request.getDavSession(), archivaLocator, this,
+ mimeTypes, auditListeners, consumers );
+ availableResources.add( 0, metadataResource );
+ }
+ catch ( RepositoryMetadataException r )
+ {
+ throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
+ "Error occurred while writing metadata file." );
+ }
+ catch ( IOException ie )
+ {
+ throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
+ "Error occurred while generating checksum files." );
+ }
+ catch ( DigesterException de )
+ {
+ throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
+ "Error occurred while generating checksum files." );
+ }
+ }
}
}
-
+
DavResource resource = availableResources.get( 0 );
setHeaders(response, locator, resource );
}
private File writeMergedMetadataToFile( ArchivaRepositoryMetadata mergedMetadata, String outputFilename )
- throws EvaluatorException, RepositoryMetadataException
- {
- File outputFile = new File( outputFilename );
+ throws RepositoryMetadataException, DigesterException, IOException
+ {
+ File outputFile = new File( outputFilename );
+ if( outputFile.exists() )
+ {
+ FileUtils.deleteQuietly( outputFile );
+ }
+
outputFile.getParentFile().mkdirs();
RepositoryMetadataWriter.write( mergedMetadata, outputFile );
+ createChecksumFile( outputFilename, digestSha1 );
+ createChecksumFile( outputFilename, digestMd5 );
+
return outputFile;
}
- private String evaluateExpressions( String outputFilename )
- throws EvaluatorException
- {
- ExpressionEvaluator expressionEvaluator = new DefaultExpressionEvaluator();
- expressionEvaluator.addExpressionSource( new SystemPropertyExpressionSource() );
-
- return expressionEvaluator.expand( outputFilename );
+ private void createChecksumFile( String path, Digester digester )
+ throws DigesterException, IOException
+ {
+ File checksumFile = new File( path + digester.getFilenameExtension() );
+ if ( !checksumFile.exists() )
+ {
+ FileUtils.deleteQuietly( checksumFile );
+ checksum.createChecksum( new File( path ), digester );
+ }
+ else if ( !checksumFile.isFile() )
+ {
+ log.error( "Checksum file is not a file." );
+ }
}
-
- public String getDefaultMergedMetadataLocation()
- {
- return defaultMergedMetadataLocation;
+
+ private boolean isProjectReference( String requestedResource )
+ {
+ try
+ {
+ VersionedReference versionRef = metadataTools.toVersionedReference( requestedResource );
+ return false;
+ }
+ catch ( RepositoryMetadataException re )
+ {
+ return true;
+ }
}
-
- public void setDefaultMergedMetadataLocation( String defaultMergedMetadataLocation )
- {
- this.defaultMergedMetadataLocation = defaultMergedMetadataLocation;
- }
}
File returnedMetadata = new File( getBasedir(), "/target/test-classes/retrievedMetadataFile.xml");
FileUtils.writeStringToFile( returnedMetadata, response.getText() );
ArchivaRepositoryMetadata metadata = RepositoryMetadataReader.read( returnedMetadata );
-
+
assertResponseOK( response );
assertEquals( "Versions list size", 4, metadata.getAvailableVersions().size() );
assertTrue( "Versions list contains version 1.0", metadata.getAvailableVersions().contains( "1.0" ) );
assertTrue( "Versions list contains version 1.5", metadata.getAvailableVersions().contains( "1.5" ) );
assertTrue( "Versions list contains version 2.0", metadata.getAvailableVersions().contains( "2.0" ) );
assertTrue( "Versions list contains version 2.5", metadata.getAvailableVersions().contains( "2.5" ) );
+
+ //check if the checksum files were generated
+ File checksumFileSha1 = new File( repoRootFirst, resourceName + ".sha1" );
+ checksumFileSha1.getParentFile().mkdirs();
+ FileUtils.writeStringToFile( checksumFileSha1, "3290853214d3687134", null );
+
+ File checksumFileMd5 = new File( repoRootFirst, resourceName + ".md5" );
+ checksumFileMd5.getParentFile().mkdirs();
+ FileUtils.writeStringToFile( checksumFileMd5, "98745897234eda12836423", null );
+
+ // request the sha1 checksum of the metadata
+ request =
+ new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/" +
+ "dummy-merged-metadata-resource/maven-metadata.xml.sha1" );
+ response = sc.getResource( request );
+
+ assertResponseOK( response );
+ assertEquals( "d2321a573e0488bca571b624f891104009408dd8 merged-maven-metadata.xml", response.getText() );
+
+ // request the md5 checksum of the metadata
+ request =
+ new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/" +
+ "dummy-merged-metadata-resource/maven-metadata.xml.md5" );
+ response = sc.getResource( request );
+
+ assertResponseOK( response );
+ assertEquals( "79d271fbe8bd1d17b23273937750d407 merged-maven-metadata.xml", response.getText().trim() );
}
protected void assertResponseMethodNotAllowed( WebResponse response )