]> source.dussan.org Git - archiva.git/commitdiff
[MRM-872]
authorMaria Odea B. Ching <oching@apache.org>
Mon, 14 Jul 2008 09:04:34 +0000 (09:04 +0000)
committerMaria Odea B. Ching <oching@apache.org>
Mon, 14 Jul 2008 09:04:34 +0000 (09:04 +0000)
-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

archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/maven/archiva/webdav/ArchivaDavResourceFactory.java
archiva-modules/archiva-web/archiva-webdav/src/main/java/org/apache/maven/archiva/webdav/RepositoryServlet.java
archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/maven/archiva/webdav/RepositoryServletRepositoryGroupTest.java
archiva-modules/archiva-web/archiva-webdav/src/test/resources/org/apache/maven/archiva/webdav/RepositoryServletTest.xml

index edce70ff08563fe4705285197ad91b37c787c12d..679c3daf3e559816aa0026001aa0e7eb4660ec5d 100644 (file)
@@ -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;
-    }    
 }
index cb46493c883fd7ad820d35c76827bb4916d52836..5fa2d3bd7d61bb9ad7d7c9bc3073f4c46177aa77 100644 (file)
@@ -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 );
         }
     }
index 60e1e24aed5b6192f7bf06311fff053a940e5801..e3c3303bdbefdb625114291441f27712ff1d6d5a 100644 (file)
@@ -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 )
index f40d694f2e26a3e8e4423011e725e3d1780204f3..83fd2a4e6b11f99d7fe379a4eed37dd8fa6d70c0 100644 (file)
           <role>org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers</role>
           <role-hint>default</role-hint>
         </requirement>
+        <requirement>
+          <role>org.codehaus.plexus.digest.ChecksumFile</role>
+          <field-name>checksum</field-name>
+        </requirement>
+        <requirement>
+          <role>org.codehaus.plexus.digest.Digester</role>
+          <role-hint>sha1</role-hint>
+          <field-name>digestSha1</field-name>
+        </requirement>
+        <requirement>
+          <role>org.codehaus.plexus.digest.Digester</role>
+          <role-hint>md5</role-hint>
+          <field-name>digestMd5</field-name>
+        </requirement>        
       </requirements>
     </component>
   </components>