From a5d144eec42a9943e07125acf729856d8b169c80 Mon Sep 17 00:00:00 2001 From: "Maria Odea B. Ching" Date: Sun, 13 Jul 2008 14:05:28 +0000 Subject: [PATCH] [MRM-872] -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 --- .../webdav/ArchivaDavResourceFactory.java | 115 +++++++++++++++--- .../archiva/webdav/RepositoryServlet.java | 9 +- .../RepositoryServletRepositoryGroupTest.java | 42 ++++--- 3 files changed, 130 insertions(+), 36 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 544f62b3c..edce70ff0 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 @@ -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 availableResources = new ArrayList(); + List resourcesInAbsolutePath = new ArrayList(); 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; + } } 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 4cdffad27..cb46493c8 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 @@ -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 ); } } 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 f06c73cd1..60e1e24ae 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 @@ -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, "" + - "dummydummy-merged-metadata-resource" + - "1.01.01.0" + - "2.520080708095554", null ); + FileUtils.writeStringToFile( dummyInternalResourceFile, "\n" + + "\ndummy\ndummy-merged-metadata-resource\n" + + "\n1.0\n1.0\n\n1.0\n" + + "2.5\n\n20080708095554\n\n", null ); //second metadata file resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml"; @@ -239,22 +241,24 @@ public class RepositoryServletRepositoryGroupTest FileUtils.writeStringToFile( dummyInternalResourceFile, "" + "dummydummy-merged-metadata-resource" + "2.02.01.0" + - "1.52.020080709095554" + + "1.52.020080709095554" + "", 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 = "" + - "dummydummy-merged-metadata-resource" + - "2.51.0" + - "1.52.02.520080709095554" + - ""; - - //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 )