]> source.dussan.org Git - archiva.git/commitdiff
[MRM-872]
authorMaria Odea B. Ching <oching@apache.org>
Sun, 13 Jul 2008 14:05:28 +0000 (14:05 +0000)
committerMaria Odea B. Ching <oching@apache.org>
Sun, 13 Jul 2008 14:05:28 +0000 (14:05 +0000)
-merge metadatas of the repositories under a virtual repo

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@676322 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

index 544f62b3c2c56854cfb3e2c051fc778883227275..edce70ff08563fe4705285197ad91b37c787c12d 100644 (file)
@@ -43,6 +43,7 @@ import org.apache.jackrabbit.webdav.lock.SimpleLockManager;
 import org.apache.maven.archiva.common.utils.PathUtil;
 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
+import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
 import org.apache.maven.archiva.model.ArtifactReference;
 import org.apache.maven.archiva.model.ProjectReference;
 import org.apache.maven.archiva.model.VersionedReference;
@@ -59,6 +60,9 @@ import org.apache.maven.archiva.repository.content.RepositoryRequest;
 import org.apache.maven.archiva.repository.layout.LayoutException;
 import org.apache.maven.archiva.repository.metadata.MetadataTools;
 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException;
+import org.apache.maven.archiva.repository.metadata.RepositoryMetadataMerge;
+import org.apache.maven.archiva.repository.metadata.RepositoryMetadataReader;
+import org.apache.maven.archiva.repository.metadata.RepositoryMetadataWriter;
 import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers;
 import org.apache.maven.archiva.security.ArchivaXworkUser;
 import org.apache.maven.archiva.security.ServletAuthenticator;
@@ -69,6 +73,10 @@ 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.redback.authentication.AuthenticationException;
 import org.codehaus.plexus.redback.authentication.AuthenticationResult;
 import org.codehaus.plexus.redback.authorization.AuthorizationException;
@@ -90,7 +98,7 @@ import com.opensymphony.xwork.ActionContext;
  */
 public class ArchivaDavResourceFactory
     implements DavResourceFactory, Auditable
-{
+{   
     private static final String PROXIED_SUFFIX = " (proxied)";
 
     private static final String HTTP_PUT_METHOD = "PUT";
@@ -142,7 +150,6 @@ public class ArchivaDavResourceFactory
      */
     private HttpAuthenticator httpAuth;
 
-
     /**
      * Lock Manager - use simple implementation from JackRabbit
      */
@@ -150,7 +157,9 @@ public class ArchivaDavResourceFactory
 
     /** @plexus.requirement */
     private RepositoryContentConsumers consumers;
-
+        
+    private String defaultMergedMetadataLocation = "${appserver.base}/data/maven-metadata.xml";
+    
     public DavResource createResource( final DavResourceLocator locator, final DavServletRequest request,
                                        final DavServletResponse response )
         throws DavException
@@ -192,8 +201,9 @@ public class ArchivaDavResourceFactory
         }
 
         List<DavResource> availableResources = new ArrayList<DavResource>();
+        List<String> resourcesInAbsolutePath = new ArrayList<String>();
         DavException e = null;
-
+        
         for ( String repositoryId : repositories )
         {
             ManagedRepositoryContent managedRepository = null;
@@ -207,8 +217,9 @@ public class ArchivaDavResourceFactory
                 throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Invalid managed repository <" +
                     repositoryId + ">" );
             }
-
+            
             DavResource resource = null;
+            
             if ( !locator.getResourcePath().startsWith( ArchivaDavResource.HIDDEN_PATH_PREFIX ) )
             {
                 if ( managedRepository != null )
@@ -242,8 +253,11 @@ public class ArchivaDavResourceFactory
                         e = new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource does not exist" );
                     }
                     else
-                    {
+                    {   
                         availableResources.add( resource );
+
+                        String logicalResource = RepositoryPathUtil.getLogicalResource( locator.getResourcePath() );
+                        resourcesInAbsolutePath.add( managedRepository.getRepoRoot() + logicalResource );
                     }
                 }
                 else
@@ -251,22 +265,63 @@ public class ArchivaDavResourceFactory
                     e = new DavException( HttpServletResponse.SC_NOT_FOUND, "Repository does not exist" );
                 }
             }
-        }
-
-        if (availableResources.isEmpty())
+        }        
+        
+        if ( availableResources.isEmpty() )
         {
             throw e;
         }
 
-        if ( request.getRequestURI().endsWith( "metadata.xml" ) )
-        {
+        // merge metadata only when requested via the repo group
+        if ( request.getRequestURI().endsWith( "metadata.xml" ) && repoGroupConfig != null )
+        {   
             // TODO MRM-872 : must merge all available metadatas
-            // use RepositoryMetadataMerge for the merging of the versions 
-            // 
-            // Deng: I'll continue this tomorrow, everything is getting blurry now
+            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 );   
+                
+                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." );
+            }
         }
-
-        DavResource resource = availableResources.get( 0 );
+        
+        DavResource resource = availableResources.get( 0 );               
         setHeaders(response, locator, resource );
 
         // compatibility with MRM-440 to ensure browsing the repository works ok
@@ -832,4 +887,32 @@ public class ArchivaDavResourceFactory
         return allow;
     }
 
+    private File writeMergedMetadataToFile( ArchivaRepositoryMetadata mergedMetadata, String outputFilename )
+        throws EvaluatorException, RepositoryMetadataException
+    {   
+        File outputFile = new File( outputFilename );
+        outputFile.getParentFile().mkdirs();
+        RepositoryMetadataWriter.write( mergedMetadata, outputFile );
+        
+        return outputFile;
+    }
+    
+    private String evaluateExpressions( String outputFilename )
+        throws EvaluatorException
+    {
+        ExpressionEvaluator expressionEvaluator = new DefaultExpressionEvaluator();
+        expressionEvaluator.addExpressionSource( new SystemPropertyExpressionSource() );
+     
+        return expressionEvaluator.expand( outputFilename );
+    }
+
+    public String getDefaultMergedMetadataLocation()
+    {
+        return defaultMergedMetadataLocation;
+    }
+
+    public void setDefaultMergedMetadataLocation( String defaultMergedMetadataLocation )
+    {
+        this.defaultMergedMetadataLocation = defaultMergedMetadataLocation;
+    }    
 }
index 4cdffad275a793dbebec4d3d72ec3b446904ff7d..cb46493c883fd7ad820d35c76827bb4916d52836 100644 (file)
@@ -28,6 +28,7 @@ import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.commons.io.FileUtils;
 import org.apache.jackrabbit.webdav.DavException;
 import org.apache.jackrabbit.webdav.DavLocatorFactory;
 import org.apache.jackrabbit.webdav.DavMethods;
@@ -104,6 +105,7 @@ public class RepositoryServlet
             DavMethods.isDeltaVMethod( webdavRequest ) &&
                 !( DavMethods.DAV_VERSION_CONTROL == methodCode || DavMethods.DAV_REPORT == methodCode );
         WebdavResponse webdavResponse = new WebdavResponseImpl( response, noCache );
+        DavResource resource = null;
         
         try
         {   
@@ -114,7 +116,7 @@ public class RepositoryServlet
             }
 
             // check matching if=header for lock-token relevant operations
-            DavResource resource =
+            resource =
                 getResourceFactory().createResource( webdavRequest.getRequestLocator(), webdavRequest, webdavResponse );
             
             if ( !isPreconditionValid( webdavRequest, resource ) )
@@ -156,6 +158,11 @@ 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 f06c73cd18c8cbe9befa0f2e86618682b8cd41f2..60e1e24aed5b6192f7bf06311fff053a940e5801 100644 (file)
@@ -30,6 +30,8 @@ import org.apache.commons.io.FileUtils;
 import org.apache.maven.archiva.configuration.Configuration;
 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
+import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
+import org.apache.maven.archiva.repository.metadata.RepositoryMetadataReader;
 
 import com.meterware.httpunit.GetMethodWebRequest;
 import com.meterware.httpunit.PutMethodWebRequest;
@@ -176,7 +178,7 @@ public class RepositoryServletRepositoryGroupTest
         WebResponse response = sc.getResponse( request );
         
         assertResponseNotFound( response );
-    }
+    } 
     
     /*
      * Test Case 3.a
@@ -212,7 +214,7 @@ public class RepositoryServletRepositoryGroupTest
         throws Exception
     {
         WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS );
-        WebResponse response = sc.getResponse( request );
+        WebResponse response = sc.getResponse( request ); 
                 
         assertNotNull( "Should have received a response", response );
         assertEquals( "Should have been an 401 response code.", HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode() );
@@ -221,16 +223,16 @@ public class RepositoryServletRepositoryGroupTest
     // MRM-872
     public void testGetMergedMetadata()
         throws Exception
-    {
-        // first metadata file
-        String resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";
+    {   
+        // first metadata file        
+        String resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";  
         
         File dummyInternalResourceFile = new File( repoRootFirst, resourceName );
         dummyInternalResourceFile.getParentFile().mkdirs();
-        FileUtils.writeStringToFile( dummyInternalResourceFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
-                       "<metadata><groupId>dummy</groupId><artifactId>dummy-merged-metadata-resource</artifactId>" +
-                       "<versioning><latest>1.0</latest><release>1.0</release><versions><version>1.0</version>" +
-                       "<version>2.5</version><lastUpdated>20080708095554</lastUpdated></versioning></metadata>", null );
+        FileUtils.writeStringToFile( dummyInternalResourceFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
+                       "<metadata>\n<groupId>dummy</groupId>\n<artifactId>dummy-merged-metadata-resource</artifactId>\n" +
+                       "<versioning>\n<latest>1.0</latest>\n<release>1.0</release>\n<versions>\n<version>1.0</version>\n" +
+                       "<version>2.5</version>\n</versions>\n<lastUpdated>20080708095554</lastUpdated>\n</versioning>\n</metadata>", null );
         
         //second metadata file
         resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";        
@@ -239,22 +241,24 @@ public class RepositoryServletRepositoryGroupTest
         FileUtils.writeStringToFile( dummyInternalResourceFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                 "<metadata><groupId>dummy</groupId><artifactId>dummy-merged-metadata-resource</artifactId>" +
                 "<versioning><latest>2.0</latest><release>2.0</release><versions><version>1.0</version>" +
-                "<version>1.5</version><version>2.0</version><lastUpdated>20080709095554</lastUpdated>" +
+                "<version>1.5</version><version>2.0</version></versions><lastUpdated>20080709095554</lastUpdated>" +
                 "</versioning></metadata>", null );
         
         WebRequest request =
             new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/" +
                 "dummy-merged-metadata-resource/maven-metadata.xml" );
-        WebResponse response = sc.getResource( request );
+        WebResponse response = sc.getResource( request );              
         
-        String expectedString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
-            "<metadata><groupId>dummy</groupId><artifactId>dummy-merged-metadata-resource</artifactId>" +
-            "<versioning><latest>2.5</latest><versions><version>1.0</version>" +
-            "<version>1.5</version><version>2.0</version><version>2.5</version><lastUpdated>20080709095554</lastUpdated>" +
-            "</versioning></metadata>"; 
-                
-        //assertResponseOK( response );
-        //assertEquals( "Expected file contents", expectedString, response.getText() );
+        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" ) );
     }
         
     protected void assertResponseMethodNotAllowed( WebResponse response )