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;
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;
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;
*/
public class ArchivaDavResourceFactory
implements DavResourceFactory, Auditable
-{
+{
private static final String PROXIED_SUFFIX = " (proxied)";
private static final String HTTP_PUT_METHOD = "PUT";
*/
private HttpAuthenticator httpAuth;
-
/**
* Lock Manager - use simple implementation from JackRabbit
*/
/** @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
}
List<DavResource> availableResources = new ArrayList<DavResource>();
+ List<String> resourcesInAbsolutePath = new ArrayList<String>();
DavException e = null;
-
+
for ( String repositoryId : repositories )
{
ManagedRepositoryContent managedRepository = null;
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 )
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
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
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;
+ }
}
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;
WebResponse response = sc.getResponse( request );
assertResponseNotFound( response );
- }
+ }
/*
* Test Case 3.a
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() );
// 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";
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 )