mirror of https://github.com/apache/archiva.git
[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:
parent
be270f9606
commit
a5d144eec4
|
@ -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;
|
||||
|
@ -142,7 +150,6 @@ public class ArchivaDavResourceFactory
|
|||
*/
|
||||
private HttpAuthenticator httpAuth;
|
||||
|
||||
|
||||
/**
|
||||
* Lock Manager - use simple implementation from JackRabbit
|
||||
*/
|
||||
|
@ -151,6 +158,8 @@ 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,6 +201,7 @@ public class ArchivaDavResourceFactory
|
|||
}
|
||||
|
||||
List<DavResource> availableResources = new ArrayList<DavResource>();
|
||||
List<String> resourcesInAbsolutePath = new ArrayList<String>();
|
||||
DavException e = null;
|
||||
|
||||
for ( String repositoryId : repositories )
|
||||
|
@ -209,6 +219,7 @@ public class ArchivaDavResourceFactory
|
|||
}
|
||||
|
||||
DavResource resource = null;
|
||||
|
||||
if ( !locator.getResourcePath().startsWith( ArchivaDavResource.HIDDEN_PATH_PREFIX ) )
|
||||
{
|
||||
if ( managedRepository != null )
|
||||
|
@ -244,6 +255,9 @@ public class ArchivaDavResourceFactory
|
|||
else
|
||||
{
|
||||
availableResources.add( resource );
|
||||
|
||||
String logicalResource = RepositoryPathUtil.getLogicalResource( locator.getResourcePath() );
|
||||
resourcesInAbsolutePath.add( managedRepository.getRepoRoot() + logicalResource );
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -258,12 +272,53 @@ public class ArchivaDavResourceFactory
|
|||
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 );
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
@ -227,10 +229,10 @@ public class RepositoryServletRepositoryGroupTest
|
|||
|
||||
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,7 +241,7 @@ 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 =
|
||||
|
@ -247,14 +249,16 @@ public class RepositoryServletRepositoryGroupTest
|
|||
"dummy-merged-metadata-resource/maven-metadata.xml" );
|
||||
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>";
|
||||
File returnedMetadata = new File( getBasedir(), "/target/test-classes/retrievedMetadataFile.xml");
|
||||
FileUtils.writeStringToFile( returnedMetadata, response.getText() );
|
||||
ArchivaRepositoryMetadata metadata = RepositoryMetadataReader.read( returnedMetadata );
|
||||
|
||||
//assertResponseOK( response );
|
||||
//assertEquals( "Expected file contents", expectedString, response.getText() );
|
||||
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 )
|
||||
|
|
Loading…
Reference in New Issue