From 7f83fefd8d94efea0faff0b02fe483da7f778bef Mon Sep 17 00:00:00 2001 From: "Maria Odea B. Ching" Date: Mon, 14 Jul 2008 09:04:34 +0000 Subject: [PATCH] [MRM-872] -generate checksums for the merged metadata -additional fixes for handling the requests for the merged metadata and it's checksums git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@676516 13f79535-47bb-0310-9956-ffa450edef68 --- .../webdav/ArchivaDavResourceFactory.java | 215 ++++++++++++------ .../archiva/webdav/RepositoryServlet.java | 9 +- .../RepositoryServletRepositoryGroupTest.java | 29 ++- .../archiva/webdav/RepositoryServletTest.xml | 14 ++ 4 files changed, 186 insertions(+), 81 deletions(-) diff --git a/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/maven/archiva/webdav/ArchivaDavResourceFactory.java b/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/maven/archiva/webdav/ArchivaDavResourceFactory.java index edce70ff0..679c3daf3 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/maven/archiva/webdav/ArchivaDavResourceFactory.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/maven/archiva/webdav/ArchivaDavResourceFactory.java @@ -30,6 +30,7 @@ import java.util.Map; 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; @@ -73,10 +74,9 @@ 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.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; @@ -155,11 +155,28 @@ public class ArchivaDavResourceFactory */ 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 @@ -242,7 +259,7 @@ public class ArchivaDavResourceFactory } } } - catch ( DavException de ) + catch ( DavException de ) { e = de; continue; @@ -271,56 +288,91 @@ public class ArchivaDavResourceFactory { 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 ); @@ -888,31 +940,48 @@ public class ArchivaDavResourceFactory } 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; - } } diff --git a/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/maven/archiva/webdav/RepositoryServlet.java b/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/maven/archiva/webdav/RepositoryServlet.java index cb46493c8..5fa2d3bd7 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/maven/archiva/webdav/RepositoryServlet.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/maven/archiva/webdav/RepositoryServlet.java @@ -29,6 +29,7 @@ import javax.servlet.http.HttpServletRequest; 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.DavLocatorFactory; import org.apache.jackrabbit.webdav.DavMethods; @@ -45,7 +46,6 @@ import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.configuration.ConfigurationEvent; import org.apache.maven.archiva.configuration.ConfigurationListener; import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; -import org.apache.maven.archiva.repository.audit.AuditEvent; import org.apache.maven.archiva.security.ServletAuthenticator; import org.codehaus.plexus.redback.xwork.filter.authentication.HttpAuthenticator; import org.codehaus.plexus.spring.PlexusToSpringUtils; @@ -157,12 +157,7 @@ public class RepositoryServlet } } finally - { - if( resource != null && resource.getResourcePath().endsWith( "metadata.xml" ) ); - { - String tmpFile = ( (ArchivaDavResourceFactory) getResourceFactory() ).getDefaultMergedMetadataLocation(); - FileUtils.deleteQuietly( new File( tmpFile ) ); - } + { getDavSessionProvider().releaseSession( webdavRequest ); } } diff --git a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/maven/archiva/webdav/RepositoryServletRepositoryGroupTest.java b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/maven/archiva/webdav/RepositoryServletRepositoryGroupTest.java index 60e1e24ae..e3c3303bd 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/maven/archiva/webdav/RepositoryServletRepositoryGroupTest.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/maven/archiva/webdav/RepositoryServletRepositoryGroupTest.java @@ -252,13 +252,40 @@ public class RepositoryServletRepositoryGroupTest 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 ) diff --git a/archiva-modules/archiva-web/archiva-webdav/src/test/resources/org/apache/maven/archiva/webdav/RepositoryServletTest.xml b/archiva-modules/archiva-web/archiva-webdav/src/test/resources/org/apache/maven/archiva/webdav/RepositoryServletTest.xml index f40d694f2..83fd2a4e6 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/test/resources/org/apache/maven/archiva/webdav/RepositoryServletTest.xml +++ b/archiva-modules/archiva-web/archiva-webdav/src/test/resources/org/apache/maven/archiva/webdav/RepositoryServletTest.xml @@ -152,6 +152,20 @@ org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers default + + org.codehaus.plexus.digest.ChecksumFile + checksum + + + org.codehaus.plexus.digest.Digester + sha1 + digestSha1 + + + org.codehaus.plexus.digest.Digester + md5 + digestMd5 + -- 2.39.5