[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
This commit is contained in:
Maria Odea B. Ching 2008-07-13 14:05:28 +00:00
parent be270f9606
commit a5d144eec4
3 changed files with 130 additions and 36 deletions

View 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.common.utils.PathUtil;
import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration; 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.ArtifactReference;
import org.apache.maven.archiva.model.ProjectReference; import org.apache.maven.archiva.model.ProjectReference;
import org.apache.maven.archiva.model.VersionedReference; 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.layout.LayoutException;
import org.apache.maven.archiva.repository.metadata.MetadataTools; import org.apache.maven.archiva.repository.metadata.MetadataTools;
import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException; 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.repository.scanner.RepositoryContentConsumers;
import org.apache.maven.archiva.security.ArchivaXworkUser; import org.apache.maven.archiva.security.ArchivaXworkUser;
import org.apache.maven.archiva.security.ServletAuthenticator; 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.Model;
import org.apache.maven.model.Relocation; import org.apache.maven.model.Relocation;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader; 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.AuthenticationException;
import org.codehaus.plexus.redback.authentication.AuthenticationResult; import org.codehaus.plexus.redback.authentication.AuthenticationResult;
import org.codehaus.plexus.redback.authorization.AuthorizationException; import org.codehaus.plexus.redback.authorization.AuthorizationException;
@ -90,7 +98,7 @@ import com.opensymphony.xwork.ActionContext;
*/ */
public class ArchivaDavResourceFactory public class ArchivaDavResourceFactory
implements DavResourceFactory, Auditable implements DavResourceFactory, Auditable
{ {
private static final String PROXIED_SUFFIX = " (proxied)"; private static final String PROXIED_SUFFIX = " (proxied)";
private static final String HTTP_PUT_METHOD = "PUT"; private static final String HTTP_PUT_METHOD = "PUT";
@ -142,7 +150,6 @@ public class ArchivaDavResourceFactory
*/ */
private HttpAuthenticator httpAuth; private HttpAuthenticator httpAuth;
/** /**
* Lock Manager - use simple implementation from JackRabbit * Lock Manager - use simple implementation from JackRabbit
*/ */
@ -150,7 +157,9 @@ public class ArchivaDavResourceFactory
/** @plexus.requirement */ /** @plexus.requirement */
private RepositoryContentConsumers consumers; private RepositoryContentConsumers consumers;
private String defaultMergedMetadataLocation = "${appserver.base}/data/maven-metadata.xml";
public DavResource createResource( final DavResourceLocator locator, final DavServletRequest request, public DavResource createResource( final DavResourceLocator locator, final DavServletRequest request,
final DavServletResponse response ) final DavServletResponse response )
throws DavException throws DavException
@ -192,8 +201,9 @@ public class ArchivaDavResourceFactory
} }
List<DavResource> availableResources = new ArrayList<DavResource>(); List<DavResource> availableResources = new ArrayList<DavResource>();
List<String> resourcesInAbsolutePath = new ArrayList<String>();
DavException e = null; DavException e = null;
for ( String repositoryId : repositories ) for ( String repositoryId : repositories )
{ {
ManagedRepositoryContent managedRepository = null; ManagedRepositoryContent managedRepository = null;
@ -207,8 +217,9 @@ public class ArchivaDavResourceFactory
throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Invalid managed repository <" + throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Invalid managed repository <" +
repositoryId + ">" ); repositoryId + ">" );
} }
DavResource resource = null; DavResource resource = null;
if ( !locator.getResourcePath().startsWith( ArchivaDavResource.HIDDEN_PATH_PREFIX ) ) if ( !locator.getResourcePath().startsWith( ArchivaDavResource.HIDDEN_PATH_PREFIX ) )
{ {
if ( managedRepository != null ) if ( managedRepository != null )
@ -242,8 +253,11 @@ public class ArchivaDavResourceFactory
e = new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource does not exist" ); e = new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource does not exist" );
} }
else else
{ {
availableResources.add( resource ); availableResources.add( resource );
String logicalResource = RepositoryPathUtil.getLogicalResource( locator.getResourcePath() );
resourcesInAbsolutePath.add( managedRepository.getRepoRoot() + logicalResource );
} }
} }
else else
@ -251,22 +265,63 @@ public class ArchivaDavResourceFactory
e = new DavException( HttpServletResponse.SC_NOT_FOUND, "Repository does not exist" ); e = new DavException( HttpServletResponse.SC_NOT_FOUND, "Repository does not exist" );
} }
} }
} }
if (availableResources.isEmpty()) if ( availableResources.isEmpty() )
{ {
throw e; 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 // TODO MRM-872 : must merge all available metadatas
// use RepositoryMetadataMerge for the merging of the versions ArchivaRepositoryMetadata mergedMetadata = new ArchivaRepositoryMetadata();
// for ( String resourceAbsPath : resourcesInAbsolutePath )
// Deng: I'll continue this tomorrow, everything is getting blurry now {
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 ); setHeaders(response, locator, resource );
// compatibility with MRM-440 to ensure browsing the repository works ok // compatibility with MRM-440 to ensure browsing the repository works ok
@ -832,4 +887,32 @@ public class ArchivaDavResourceFactory
return allow; 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;
}
} }

View File

@ -28,6 +28,7 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.jackrabbit.webdav.DavException; import org.apache.jackrabbit.webdav.DavException;
import org.apache.jackrabbit.webdav.DavLocatorFactory; import org.apache.jackrabbit.webdav.DavLocatorFactory;
import org.apache.jackrabbit.webdav.DavMethods; import org.apache.jackrabbit.webdav.DavMethods;
@ -104,6 +105,7 @@ public class RepositoryServlet
DavMethods.isDeltaVMethod( webdavRequest ) && DavMethods.isDeltaVMethod( webdavRequest ) &&
!( DavMethods.DAV_VERSION_CONTROL == methodCode || DavMethods.DAV_REPORT == methodCode ); !( DavMethods.DAV_VERSION_CONTROL == methodCode || DavMethods.DAV_REPORT == methodCode );
WebdavResponse webdavResponse = new WebdavResponseImpl( response, noCache ); WebdavResponse webdavResponse = new WebdavResponseImpl( response, noCache );
DavResource resource = null;
try try
{ {
@ -114,7 +116,7 @@ public class RepositoryServlet
} }
// check matching if=header for lock-token relevant operations // check matching if=header for lock-token relevant operations
DavResource resource = resource =
getResourceFactory().createResource( webdavRequest.getRequestLocator(), webdavRequest, webdavResponse ); getResourceFactory().createResource( webdavRequest.getRequestLocator(), webdavRequest, webdavResponse );
if ( !isPreconditionValid( webdavRequest, resource ) ) if ( !isPreconditionValid( webdavRequest, resource ) )
@ -156,6 +158,11 @@ public class RepositoryServlet
} }
finally finally
{ {
if( resource != null && resource.getResourcePath().endsWith( "metadata.xml" ) );
{
String tmpFile = ( (ArchivaDavResourceFactory) getResourceFactory() ).getDefaultMergedMetadataLocation();
FileUtils.deleteQuietly( new File( tmpFile ) );
}
getDavSessionProvider().releaseSession( webdavRequest ); getDavSessionProvider().releaseSession( webdavRequest );
} }
} }

View File

@ -30,6 +30,8 @@ import org.apache.commons.io.FileUtils;
import org.apache.maven.archiva.configuration.Configuration; import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration; 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.GetMethodWebRequest;
import com.meterware.httpunit.PutMethodWebRequest; import com.meterware.httpunit.PutMethodWebRequest;
@ -176,7 +178,7 @@ public class RepositoryServletRepositoryGroupTest
WebResponse response = sc.getResponse( request ); WebResponse response = sc.getResponse( request );
assertResponseNotFound( response ); assertResponseNotFound( response );
} }
/* /*
* Test Case 3.a * Test Case 3.a
@ -212,7 +214,7 @@ public class RepositoryServletRepositoryGroupTest
throws Exception throws Exception
{ {
WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS ); 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 ); assertNotNull( "Should have received a response", response );
assertEquals( "Should have been an 401 response code.", HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode() ); assertEquals( "Should have been an 401 response code.", HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode() );
@ -221,16 +223,16 @@ public class RepositoryServletRepositoryGroupTest
// MRM-872 // MRM-872
public void testGetMergedMetadata() public void testGetMergedMetadata()
throws Exception throws Exception
{ {
// first metadata file // first metadata file
String resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml"; String resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";
File dummyInternalResourceFile = new File( repoRootFirst, resourceName ); File dummyInternalResourceFile = new File( repoRootFirst, resourceName );
dummyInternalResourceFile.getParentFile().mkdirs(); dummyInternalResourceFile.getParentFile().mkdirs();
FileUtils.writeStringToFile( dummyInternalResourceFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + FileUtils.writeStringToFile( dummyInternalResourceFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<metadata><groupId>dummy</groupId><artifactId>dummy-merged-metadata-resource</artifactId>" + "<metadata>\n<groupId>dummy</groupId>\n<artifactId>dummy-merged-metadata-resource</artifactId>\n" +
"<versioning><latest>1.0</latest><release>1.0</release><versions><version>1.0</version>" + "<versioning>\n<latest>1.0</latest>\n<release>1.0</release>\n<versions>\n<version>1.0</version>\n" +
"<version>2.5</version><lastUpdated>20080708095554</lastUpdated></versioning></metadata>", null ); "<version>2.5</version>\n</versions>\n<lastUpdated>20080708095554</lastUpdated>\n</versioning>\n</metadata>", null );
//second metadata file //second metadata file
resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml"; 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\"?>" + FileUtils.writeStringToFile( dummyInternalResourceFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<metadata><groupId>dummy</groupId><artifactId>dummy-merged-metadata-resource</artifactId>" + "<metadata><groupId>dummy</groupId><artifactId>dummy-merged-metadata-resource</artifactId>" +
"<versioning><latest>2.0</latest><release>2.0</release><versions><version>1.0</version>" + "<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 ); "</versioning></metadata>", null );
WebRequest request = WebRequest request =
new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/" + new GetMethodWebRequest( "http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/" +
"dummy-merged-metadata-resource/maven-metadata.xml" ); "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\"?>" + File returnedMetadata = new File( getBasedir(), "/target/test-classes/retrievedMetadataFile.xml");
"<metadata><groupId>dummy</groupId><artifactId>dummy-merged-metadata-resource</artifactId>" + FileUtils.writeStringToFile( returnedMetadata, response.getText() );
"<versioning><latest>2.5</latest><versions><version>1.0</version>" + ArchivaRepositoryMetadata metadata = RepositoryMetadataReader.read( returnedMetadata );
"<version>1.5</version><version>2.0</version><version>2.5</version><lastUpdated>20080709095554</lastUpdated>" +
"</versioning></metadata>"; assertResponseOK( response );
assertEquals( "Versions list size", 4, metadata.getAvailableVersions().size() );
//assertResponseOK( response ); assertTrue( "Versions list contains version 1.0", metadata.getAvailableVersions().contains( "1.0" ) );
//assertEquals( "Expected file contents", expectedString, response.getText() ); 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 ) protected void assertResponseMethodNotAllowed( WebResponse response )