</exclusion>
</exclusions>
</dependency>
- <!--
- <dependency>
- <groupId>commons-collections</groupId>
- <artifactId>commons-collections</artifactId>
- <version>3.2.2</version>
- </dependency>
- -->
+
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
</dependency>
- <!--
- <dependency>
- <groupId>commons-beanutils</groupId>
- <artifactId>commons-beanutils</artifactId>
- </dependency>
- -->
<!-- Test scope -->
-
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
</dependency>
+ <dependency>
+ <groupId>javax.annotation</groupId>
+ <artifactId>javax.annotation-api</artifactId>
+ </dependency>
<dependency>
<groupId>org.apache.archiva.components.registry</groupId>
<artifactId>archiva-components-spring-registry-api</artifactId>
import org.apache.archiva.event.EventSource;
import org.apache.archiva.indexer.ArchivaIndexManager;
import org.apache.archiva.indexer.IndexUpdateFailedException;
+import org.apache.archiva.repository.metadata.MetadataReader;
+import org.apache.archiva.repository.storage.StorageAsset;
import java.util.Collection;
*/
public interface RepositoryRegistry extends EventSource
{
+ /**
+ * Set the configuration for the registry
+ * @param archivaConfiguration
+ */
void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration );
+ /**
+ * Return the index manager for the given repository type
+ * @param type the repository type
+ * @return the index manager, if it exists
+ */
ArchivaIndexManager getIndexManager( RepositoryType type );
+ /**
+ * Returns the metadatareader for the given repository type
+ * @param type the repository type
+ * @return the metadata reader instance
+ */
+ MetadataReader getMetadataReader(RepositoryType type) throws UnsupportedRepositoryTypeException;
+
+ /**
+ * Returns all registered repositories
+ * @return the list of repositories
+ */
Collection<Repository> getRepositories( );
+ /**
+ * Returns all managed repositories
+ * @return the list of managed repositories
+ */
Collection<ManagedRepository> getManagedRepositories( );
Collection<RemoteRepository> getRemoteRepositories( );
<T extends Repository> Repository clone( T repo, String newId ) throws RepositoryException;
RemoteRepository clone( RemoteRepository repo, String newId ) throws RepositoryException;
+
+ /**
+ * Return the repository that stores the given asset.
+ * @param asset the asset
+ * @return the repository or <code>null</code> if no matching repository is found
+ */
+ Repository getRepositoryOfAsset( StorageAsset asset );
}
--- /dev/null
+package org.apache.archiva.repository.metadata;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.model.ArchivaRepositoryMetadata;
+import org.apache.archiva.repository.RepositoryType;
+import org.apache.archiva.repository.storage.StorageAsset;
+
+/**
+ * Interface for reading metadata from a given file
+ */
+public interface MetadataReader
+{
+ /**
+ * Reads the given metadata file and returns the corresponding metadata object.
+ * @param asset The asset where the metadata should be read from
+ * @return The parsed metadata
+ * @throws RepositoryMetadataException if the metadata could not be read
+ */
+ ArchivaRepositoryMetadata read( StorageAsset asset ) throws RepositoryMetadataException;
+
+ /**
+ * Returns <code>true</code>, if the given path is a valid path for a metadata file, otherwise <code>false</code>
+ * The implementation should not access the file directly, just use the path for validation.
+ * @param path the path to the metadata file / asset
+ * @return <code>true</code>, if the path is valid for a metadata file otherwise <code>false</code>
+ */
+ boolean isValidMetadataPath(String path);
+
+ /**
+ * Returns <code>true</code>, if this metadata reader instance can be used to read metadata for the
+ * given repository type, otherwise <code>false</code>.
+ *
+ * @param repositoryType the repository type to check for
+ * @return <code>true</code>, if this is a implementation for the given type, otherwise <code>false</code>
+ */
+ boolean isValidForType(RepositoryType repositoryType);
+}
<version>${project.version}</version>
<scope>test</scope>
</dependency>
+ <!--
<dependency>
<groupId>org.apache.archiva</groupId>
<artifactId>metadata-model</artifactId>
</dependency>
+ -->
+ <dependency>
+ <groupId>org.apache.archiva.components.registry</groupId>
+ <artifactId>archiva-components-spring-registry-api</artifactId>
+ </dependency>
<dependency>
<groupId>com.cronutils</groupId>
<artifactId>cron-utils</artifactId>
<artifactId>javax.inject</artifactId>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>javax.annotation</groupId>
+ <artifactId>javax.annotation-api</artifactId>
+ </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<!-- Test scope -->
+ <!--
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<scope>test</scope>
</dependency>
+ -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<scope>test</scope>
</dependency>
+ <!--
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<scope>test</scope>
</dependency>
+ -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
import org.apache.archiva.repository.RepositoryProvider;
import org.apache.archiva.repository.RepositoryRegistry;
import org.apache.archiva.repository.RepositoryType;
+import org.apache.archiva.repository.UnsupportedRepositoryTypeException;
import org.apache.archiva.repository.event.*;
import org.apache.archiva.event.EventHandler;
import org.apache.archiva.repository.features.IndexCreationFeature;
import org.apache.archiva.repository.features.StagingRepositoryFeature;
+import org.apache.archiva.repository.metadata.MetadataReader;
+import org.apache.archiva.repository.storage.StorageAsset;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Inject
ArchivaConfiguration archivaConfiguration;
+ @Inject
+ List<MetadataReader> metadataReaderList;
+
@Inject
@Named("repositoryContentFactory#default")
RepositoryContentFactory repositoryContentFactory;
return indexManagerFactory.getIndexManager(type);
}
+ @Override
+ public MetadataReader getMetadataReader( final RepositoryType type ) throws UnsupportedRepositoryTypeException
+ {
+ if (metadataReaderList!=null) {
+ return metadataReaderList.stream( ).filter( mr -> mr.isValidForType( type ) ).findFirst( ).orElseThrow( ( ) -> new UnsupportedRepositoryTypeException( type ) );
+ } else {
+ throw new UnsupportedRepositoryTypeException( type );
+ }
+ }
+
private void createIndexingContext( EditableRepository editableRepo) throws RepositoryException {
if (editableRepo.supportsFeature(IndexCreationFeature.class)) {
ArchivaIndexManager idxManager = getIndexManager(editableRepo.getType());
return cloned;
}
+ @Override
+ public Repository getRepositoryOfAsset( StorageAsset asset )
+ {
+ return getManagedRepositories( ).stream( ).filter( r -> r.getAsset( "" )
+ .getStorage( ).equals( asset.getStorage( ) ) ).findFirst( ).orElse( null );
+ }
+
@Override
public void configurationEvent(ConfigurationEvent event) {
import org.apache.archiva.configuration.ConfigurationNames;
import org.apache.archiva.configuration.FileTypes;
import org.apache.archiva.configuration.ProxyConnectorConfiguration;
-import org.apache.archiva.maven2.metadata.MavenMetadataReader;
+// import org.apache.archiva.maven2.metadata.MavenMetadataReader;
import org.apache.archiva.model.ArchivaRepositoryMetadata;
import org.apache.archiva.model.ArtifactReference;
import org.apache.archiva.model.Plugin;
import org.apache.archiva.repository.LayoutException;
import org.apache.archiva.repository.ManagedRepositoryContent;
import org.apache.archiva.repository.RemoteRepositoryContent;
+import org.apache.archiva.repository.Repository;
+import org.apache.archiva.repository.RepositoryRegistry;
+import org.apache.archiva.repository.RepositoryType;
+import org.apache.archiva.repository.metadata.MetadataReader;
import org.apache.archiva.repository.metadata.RepositoryMetadataException;
import org.apache.archiva.repository.storage.StorageAsset;
import org.apache.archiva.xml.XMLException;
public class MetadataTools
implements RegistryListener, ConfigurationListener
{
- private Logger log = LoggerFactory.getLogger( getClass() );
+ private static final Logger log = LoggerFactory.getLogger( MetadataTools.class );
public static final String MAVEN_METADATA = "maven-metadata.xml";
private static final char GROUP_SEPARATOR = '.';
+ @Inject
+ private RepositoryRegistry repositoryRegistry;
+
/**
*
*/
public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
ProjectReference reference, String proxyId )
{
+ MetadataReader reader = getMetadataReader( managedRepository );
+
String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) );
StorageAsset metadataFile = managedRepository.getRepository().getAsset( metadataPath );
- if ( !metadataFile.exists() || metadataFile.isContainer())
- {
- // Nothing to do. return null.
- return null;
- }
-
- try
- {
- return MavenMetadataReader.read( metadataFile );
- }
- catch (XMLException | IOException e )
- {
- // TODO: [monitor] consider a monitor for this event.
- // TODO: consider a read-redo on monitor return code?
- log.warn( "Unable to read metadata: {}", metadataFile.getPath(), e );
- return null;
- }
+ return readMetadataFile( managedRepository, metadataFile );
}
public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
{
String metadataPath = getRepositorySpecificName( proxyId, logicalResource );
StorageAsset metadataFile = managedRepository.getRepository().getAsset( metadataPath );
-
- if ( !metadataFile.exists() || metadataFile.isContainer())
- {
- // Nothing to do. return null.
- return null;
- }
-
- try
- {
- return MavenMetadataReader.read( metadataFile );
- }
- catch (XMLException | IOException e )
- {
- // TODO: [monitor] consider a monitor for this event.
- // TODO: consider a read-redo on monitor return code?
- log.warn( "Unable to read metadata: {}", metadataFile.getPath(), e );
- return null;
- }
+ return readMetadataFile( managedRepository, metadataFile );
}
public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
{
String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) );
StorageAsset metadataFile = managedRepository.getRepository().getAsset( metadataPath );
-
- if ( !metadataFile.exists() || metadataFile.isContainer())
- {
- // Nothing to do. return null.
- return null;
- }
-
- try
- {
- return MavenMetadataReader.read( metadataFile );
- }
- catch (XMLException | IOException e )
- {
- // TODO: [monitor] consider a monitor for this event.
- // TODO: consider a read-redo on monitor return code?
- log.warn( "Unable to read metadata: {}", metadataFile.getPath(), e );
- return null;
- }
+ return readMetadataFile( managedRepository, metadataFile );
}
public void updateMetadata( ManagedRepositoryContent managedRepository, String logicalResource )
if ( file.exists() )
{
- try
+ ArchivaRepositoryMetadata existingMetadata = readMetadataFile( managedRepository, file );
+ if ( existingMetadata != null )
{
- ArchivaRepositoryMetadata existingMetadata = MavenMetadataReader.read( file );
- if ( existingMetadata != null )
- {
- metadatas.add( existingMetadata );
- }
- }
- catch (XMLException | IOException e )
- {
- log.debug( "Could not read metadata at {}. Metadata will be removed.", file.getPath() );
- try {
- file.getStorage().removeAsset(file);
- } catch (IOException ex) {
- log.error("Could not remove asset {}", file.getPath());
- }
+ metadatas.add( existingMetadata );
}
}
{
StorageAsset metadataFile = managedRepository.getRepository().getAsset( toPath( reference ) );
+ ArchivaRepositoryMetadata existingMetadata = readMetadataFile( managedRepository, metadataFile );
- long lastUpdated = getExistingLastUpdated( metadataFile );
+ long lastUpdated = getExistingLastUpdated( existingMetadata );
ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
metadata.setGroupId( reference.getGroupId() );
// TODO: do we know this information instead?
// Set<Plugin> allPlugins = managedRepository.getPlugins( reference );
Set<Plugin> allPlugins;
- if ( metadataFile.exists())
+ if ( existingMetadata!=null)
{
- try
- {
- allPlugins = new LinkedHashSet<Plugin>( MavenMetadataReader.read( metadataFile ).getPlugins() );
- }
- catch ( XMLException e )
- {
- throw new RepositoryMetadataException( e.getMessage(), e );
- }
+ allPlugins = new LinkedHashSet<Plugin>( existingMetadata.getPlugins() );
}
else
{
checksum.fixChecksums( algorithms );
}
+ public MetadataReader getMetadataReader( ManagedRepositoryContent managedRepository )
+ {
+ if (managedRepository!=null)
+ {
+ return repositoryRegistry.getMetadataReader( managedRepository.getRepository( ).getType( ) );
+ } else {
+ return repositoryRegistry.getMetadataReader( RepositoryType.MAVEN );
+ }
+ }
+
private void updateMetadataVersions( Collection<String> allVersions, ArchivaRepositoryMetadata metadata )
{
// Sort the versions
}
}
- private long getExistingLastUpdated( StorageAsset metadataFile )
- {
- if ( !metadataFile.exists() )
+ ArchivaRepositoryMetadata readMetadataFile( ManagedRepositoryContent repository, StorageAsset asset) {
+ MetadataReader reader = getMetadataReader( repository );
+ try
{
- // Doesn't exist.
- return 0;
+ if (asset.exists() && !asset.isContainer())
+ {
+ return reader.read( asset );
+ } else {
+ log.error( "Trying to read metadata from container: {}", asset.getPath( ) );
+ return null;
+ }
}
-
- try
+ catch ( RepositoryMetadataException e )
{
- ArchivaRepositoryMetadata metadata = MavenMetadataReader.read( metadataFile );
-
- return getLastUpdated( metadata );
+ log.error( "Could not read metadata file {}", asset, e );
+ return null;
}
- catch (XMLException | IOException e )
+ }
+
+ private long getExistingLastUpdated( ArchivaRepositoryMetadata metadata )
+ {
+ if ( metadata==null )
{
- // Error.
+ // Doesn't exist.
return 0;
}
+
+ return getLastUpdated( metadata );
}
/**
throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException
{
StorageAsset metadataFile = managedRepository.getRepository().getAsset( toPath( reference ) );
+ ArchivaRepositoryMetadata existingMetadata = readMetadataFile(managedRepository, metadataFile );
- long lastUpdated = getExistingLastUpdated( metadataFile );
+ long lastUpdated = getExistingLastUpdated( existingMetadata );
ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
metadata.setGroupId( reference.getGroupId() );
{
log.debug( "Configuration event {}", event );
}
+
+
}
--- /dev/null
+package org.apache.archiva.repository.mock;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.model.ArchivaRepositoryMetadata;
+import org.apache.archiva.repository.RepositoryType;
+import org.apache.archiva.repository.metadata.MetadataReader;
+import org.apache.archiva.repository.metadata.RepositoryMetadataException;
+import org.apache.archiva.repository.storage.StorageAsset;
+import org.springframework.stereotype.Service;
+
+@Service("metadataReader#test")
+public class MetadataReaderMock implements MetadataReader
+{
+ @Override
+ public ArchivaRepositoryMetadata read( StorageAsset asset ) throws RepositoryMetadataException
+ {
+ return new ArchivaRepositoryMetadata();
+ }
+
+ @Override
+ public boolean isValidMetadataPath( String path )
+ {
+ return true;
+ }
+
+ @Override
+ public boolean isValidForType( RepositoryType repositoryType )
+ {
+ return true;
+ }
+}
<groupId>org.apache.archiva</groupId>
<artifactId>archiva-common</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.archiva</groupId>
+ <artifactId>archiva-storage-api</artifactId>
+ </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
* under the License.
*/
+import org.apache.archiva.repository.storage.StorageAsset;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.xpath.*;
import java.io.*;
import java.net.MalformedURLException;
+import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
public XMLReader( String type, Path file )
throws XMLException
{
+ initWithFile( type, file );
+ }
+
+ private void initWithFile( String type, Path file) throws XMLException {
if ( !Files.exists(file) )
{
throw new XMLException( "file does not exist: " + file.toAbsolutePath() );
try
{
- init( type, file.toUri().toURL() );
+ initWithUrl( type, file.toUri().toURL() );
}
catch ( MalformedURLException e )
{
}
}
+ public XMLReader( String type, StorageAsset asset) throws XMLException
+ {
+ if (asset.isFileBased()) {
+ initWithFile( type, asset.getFilePath( ) );
+ } else {
+ URI uri = asset.getStorage( ).getLocation( ).resolve( asset.getPath( ) );
+ try(InputStream in = asset.getReadStream()) {
+ initWithStream( type, uri.toURL( ), in );
+ }
+ catch ( IOException e )
+ {
+ throw new XMLException( "Could not open asset stream of " + uri + ": " + e.getMessage( ), e );
+ }
+ }
+
+
+ }
+
+
public XMLReader( String type, URL url )
throws XMLException
{
- init( type, url );
+ initWithUrl( type, url );
+ }
+
+
+ private void initWithUrl( String type, URL url ) throws XMLException {
+ try(InputStream in = url.openStream()) {
+ initWithStream( type, url, in );
+ }
+ catch ( IOException e )
+ {
+ throw new XMLException( "Could not open url " + url + ": " + e.getMessage( ), e );
+ }
}
- private void init( String type, URL url )
+ private void initWithStream( String type, URL url, InputStream in )
throws XMLException
{
this.documentType = type;
this.xmlUrl = url;
-
// SAXReader reader = new SAXReader();
- try (InputStream in = url.openStream(); Reader reader = new LatinEntityResolutionReader(new BufferedReader(new InputStreamReader(in, "UTF-8"))))
+ try (Reader reader = new LatinEntityResolutionReader(new BufferedReader(new InputStreamReader(in, "UTF-8"))))
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
<groupId>org.apache.archiva</groupId>
<artifactId>archiva-storage-api</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.archiva</groupId>
+ <artifactId>archiva-repository-api</artifactId>
+ </dependency>
<dependency>
<groupId>org.apache.archiva</groupId>
<artifactId>archiva-xml-tools</artifactId>
import org.apache.archiva.model.ArchivaRepositoryMetadata;
import org.apache.archiva.model.Plugin;
import org.apache.archiva.model.SnapshotVersion;
+import org.apache.archiva.repository.RepositoryType;
+import org.apache.archiva.repository.metadata.MetadataReader;
+import org.apache.archiva.repository.metadata.RepositoryMetadataException;
import org.apache.archiva.repository.storage.StorageAsset;
import org.apache.archiva.xml.XMLException;
import org.apache.archiva.xml.XMLReader;
import org.apache.archiva.xml.XmlUtil;
+import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Service;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.time.Instant;
import java.util.Date;
/**
* @author Olivier Lamy
* @since 1.4-M3
*/
-public class MavenMetadataReader
+@Service("metadataReader#maven")
+public class MavenMetadataReader implements MetadataReader
{
+ public static final String MAVEN_METADATA = "maven-metadata.xml";
+
+
/*
<?xml version="1.0" encoding="UTF-8"?>
<metadata modelVersion="1.1.0">
private static final Logger log = LoggerFactory.getLogger( MavenMetadataReader.class );
- public static ArchivaRepositoryMetadata read(StorageAsset metadataFile) throws XMLException, IOException {
- if (metadataFile.isFileBased()) {
- return read(metadataFile.getFilePath());
- } else {
- throw new IOException("StorageAsset is not file based");
- }
- }
/**
* Read and return the {@link org.apache.archiva.model.ArchivaRepositoryMetadata} object from the provided xml file.
*
* @param metadataFile the maven-metadata.xml file to read.
* @return the archiva repository metadata object that represents the provided file contents.
- * @throws XMLException
+ * @throws RepositoryMetadataException if the file cannot be read
*/
- public static ArchivaRepositoryMetadata read( Path metadataFile )
- throws XMLException {
+ public ArchivaRepositoryMetadata read( StorageAsset metadataFile )
+ throws RepositoryMetadataException {
- XMLReader xml = new XMLReader( "metadata", metadataFile );
- // invoke this to remove namespaces, see MRM-1136
- xml.removeNamespaces();
+ XMLReader xml;
+ try
+ {
+ xml = new XMLReader( "metadata", metadataFile );
+ }
+ catch ( XMLException e )
+ {
+ throw new RepositoryMetadataException( "Could not open XML metadata file " + metadataFile, e );
+ }
+ return read( xml, metadataFile.getModificationTime(), metadataFile.getSize() );
- ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
+ }
- metadata.setGroupId( xml.getElementText( "//metadata/groupId" ) );
- metadata.setArtifactId( xml.getElementText( "//metadata/artifactId" ) );
- metadata.setVersion( xml.getElementText( "//metadata/version" ) );
- Date modTime;
- try {
- modTime = new Date(Files.getLastModifiedTime(metadataFile).toMillis());
- } catch (IOException e) {
- modTime = new Date();
- log.error("Could not read modification time of {}", metadataFile);
+ public ArchivaRepositoryMetadata read( Path metadataFile )
+ throws RepositoryMetadataException {
+
+ XMLReader xml;
+ try
+ {
+ xml = new XMLReader( "metadata", metadataFile );
}
- metadata.setFileLastModified( modTime );
- try {
- metadata.setFileSize(Files.size(metadataFile));
- } catch (IOException e) {
- metadata.setFileSize( 0 );
- log.error("Could not read file size of {}", metadataFile);
+ catch ( XMLException e )
+ {
+ log.error( "XML error while reading metadata file {}: {}", metadataFile, e.getMessage(), e );
+ throw new RepositoryMetadataException( "Could not open XML metadata file " + metadataFile, e );
+ }
+ try
+ {
+ return read( xml, Files.getLastModifiedTime( metadataFile ).toInstant(), Files.size( metadataFile ) );
}
- metadata.setLastUpdated( xml.getElementText( "//metadata/versioning/lastUpdated" ) );
- metadata.setLatestVersion( xml.getElementText( "//metadata/versioning/latest" ) );
- metadata.setReleasedVersion( xml.getElementText( "//metadata/versioning/release" ) );
- metadata.setAvailableVersions( xml.getElementListText( "//metadata/versioning/versions/version" ) );
+ catch ( IOException e )
+ {
+ log.error( "IO Error while reading metadata file {}: {}", metadataFile, e.getMessage(), e );
+ throw new RepositoryMetadataException( "Could not open XML metadata file " + metadataFile, e );
+ }
+
+ }
- Element snapshotElem = xml.getElement( "//metadata/versioning/snapshot" );
- if ( snapshotElem != null )
+ private ArchivaRepositoryMetadata read( XMLReader xml, Instant modTime, long fileSize) throws RepositoryMetadataException
+ {
+ // invoke this to remove namespaces, see MRM-1136
+ xml.removeNamespaces();
+
+ ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
+
+ try
{
- SnapshotVersion snapshot = new SnapshotVersion();
- snapshot.setTimestamp(XmlUtil.getChildText(snapshotElem, "timestamp"));
- String buildNumber = XmlUtil.getChildText(snapshotElem, "buildNumber");
- if ( NumberUtils.isCreatable( buildNumber ) )
+ metadata.setGroupId( xml.getElementText( "//metadata/groupId" ) );
+ metadata.setArtifactId( xml.getElementText( "//metadata/artifactId" ) );
+ metadata.setVersion( xml.getElementText( "//metadata/version" ) );
+ metadata.setFileLastModified( Date.from(modTime) );
+ metadata.setFileSize( fileSize );
+ metadata.setLastUpdated( xml.getElementText( "//metadata/versioning/lastUpdated" ) );
+ metadata.setLatestVersion( xml.getElementText( "//metadata/versioning/latest" ) );
+ metadata.setReleasedVersion( xml.getElementText( "//metadata/versioning/release" ) );
+ metadata.setAvailableVersions( xml.getElementListText( "//metadata/versioning/versions/version" ) );
+
+ Element snapshotElem = xml.getElement( "//metadata/versioning/snapshot" );
+ if ( snapshotElem != null )
{
- snapshot.setBuildNumber( NumberUtils.toInt( buildNumber ) );
+ SnapshotVersion snapshot = new SnapshotVersion( );
+ snapshot.setTimestamp( XmlUtil.getChildText( snapshotElem, "timestamp" ) );
+ String buildNumber = XmlUtil.getChildText( snapshotElem, "buildNumber" );
+ if ( NumberUtils.isCreatable( buildNumber ) )
+ {
+ snapshot.setBuildNumber( NumberUtils.toInt( buildNumber ) );
+ }
+ metadata.setSnapshotVersion( snapshot );
}
- metadata.setSnapshotVersion( snapshot );
- }
- for ( Node node : xml.getElementList( "//metadata/plugins/plugin" ) )
- {
- if (node instanceof Element) {
- Element plugin = (Element) node;
- Plugin p = new Plugin();
- String prefix = plugin.getElementsByTagName("prefix").item(0).getTextContent().trim();
- p.setPrefix(prefix);
- String artifactId = plugin.getElementsByTagName("artifactId").item(0).getTextContent().trim();
- p.setArtifactId(artifactId);
- String name = plugin.getElementsByTagName("name").item(0).getTextContent().trim();
- p.setName(name);
- metadata.addPlugin(p);
+ for ( Node node : xml.getElementList( "//metadata/plugins/plugin" ) )
+ {
+ if ( node instanceof Element )
+ {
+ Element plugin = (Element) node;
+ Plugin p = new Plugin( );
+ String prefix = plugin.getElementsByTagName( "prefix" ).item( 0 ).getTextContent( ).trim( );
+ p.setPrefix( prefix );
+ String artifactId = plugin.getElementsByTagName( "artifactId" ).item( 0 ).getTextContent( ).trim( );
+ p.setArtifactId( artifactId );
+ String name = plugin.getElementsByTagName( "name" ).item( 0 ).getTextContent( ).trim( );
+ p.setName( name );
+ metadata.addPlugin( p );
+ }
}
+ } catch ( XMLException e) {
+ throw new RepositoryMetadataException( "XML Error while reading metadata file : " + e.getMessage( ), e );
}
-
return metadata;
+ }
+
+ @Override
+ public boolean isValidMetadataPath( String path )
+ {
+ if ( StringUtils.isNotEmpty( path ) ) {
+ return path.endsWith( MAVEN_METADATA );
+ } else {
+ return false;
+ }
+ }
+ @Override
+ public boolean isValidForType( RepositoryType repositoryType )
+ {
+ return RepositoryType.MAVEN.equals( repositoryType );
}
}
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements. See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership. The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied. See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ -->
+
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:context="http://www.springframework.org/schema/context"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans
+ http://www.springframework.org/schema/beans/spring-beans.xsd
+ http://www.springframework.org/schema/context
+ http://www.springframework.org/schema/context/spring-context.xsd"
+ default-lazy-init="true">
+
+ <context:annotation-config/>
+ <context:component-scan base-package="org.apache.archiva.maven2.metadata"/>
+
+</beans>
\ No newline at end of file
</dependency>
<!-- Test scope -->
+ <dependency>
+ <groupId>org.apache.archiva.maven</groupId>
+ <artifactId>archiva-maven-metadata</artifactId>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
StringWriter actualContents = new StringWriter();
FilesystemStorage fsStorage = new FilesystemStorage(actualFile.getParent(), new DefaultFileLockManager());
StorageAsset actualFileAsset = fsStorage.getAsset(actualFile.getFileName().toString());
- ArchivaRepositoryMetadata metadata = MavenMetadataReader.read( actualFileAsset );
+ ArchivaRepositoryMetadata metadata = metadataTools.getMetadataReader( null ).read( actualFileAsset );
RepositoryMetadataWriter.write( metadata, actualContents );
Diff detailedDiff = DiffBuilder.compare( expectedMetadataXml).withTest( actualContents.toString() ).checkForSimilar().build();
import org.apache.archiva.repository.RemoteRepository;
import org.apache.archiva.repository.RepositoryRegistry;
import org.apache.archiva.repository.maven2.MavenSystemManager;
+import org.apache.archiva.repository.metadata.RepositoryMetadataException;
import org.apache.archiva.repository.metadata.base.MetadataTools;
import org.apache.archiva.repository.storage.StorageAsset;
-import org.apache.archiva.xml.XMLException;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.bridge.MavenRepositorySystem;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
-import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Named( "repositoryPathTranslator#maven2" )
private RepositoryPathTranslator pathTranslator;
+ @Inject
+ @Named("metadataReader#maven")
+ private MavenMetadataReader metadataReader;
+
@Inject
private ProxyConnectorAdmin proxyConnectorAdmin;
{
try
{
- ArchivaRepositoryMetadata archivaRepositoryMetadata = MavenMetadataReader.read( metadataFile);
+ ArchivaRepositoryMetadata archivaRepositoryMetadata = metadataReader.read( metadataFile);
int buildNumber = archivaRepositoryMetadata.getSnapshotVersion().getBuildNumber();
String timeStamp = archivaRepositoryMetadata.getSnapshotVersion().getTimestamp();
// rebuild file name with timestamped version and build number
return managedRepo;
}
}
- catch (XMLException | IOException e )
+ catch ( RepositoryMetadataException e )
{
log.warn( "skip fail to find timestamped snapshot pom: {}", e.getMessage() );
}
import org.apache.archiva.repository.*;
import org.apache.archiva.repository.content.PathParser;
import org.apache.archiva.repository.maven2.MavenSystemManager;
+import org.apache.archiva.repository.metadata.RepositoryMetadataException;
import org.apache.archiva.repository.storage.StorageAsset;
import org.apache.archiva.xml.XMLException;
import org.apache.commons.lang3.ArrayUtils;
public class Maven2RepositoryStorage
implements RepositoryStorage {
- private static final Logger LOGGER = LoggerFactory.getLogger(Maven2RepositoryStorage.class);
+ private static final Logger log = LoggerFactory.getLogger(Maven2RepositoryStorage.class);
private ModelBuilder builder;
@Inject
RepositoryRegistry repositoryRegistry;
+ @Inject
+ @Named( "metadataReader#maven" )
+ MavenMetadataReader metadataReader;
+
@Inject
@Named("repositoryPathTranslator#maven2")
private RepositoryPathTranslator pathTranslator;
readMetadataRequest.getProjectId(), artifactVersion,
METADATA_FILENAME);
try {
- ArchivaRepositoryMetadata metadata = MavenMetadataReader.read(metadataFile);
+ ArchivaRepositoryMetadata metadata = metadataReader.read(metadataFile);
// re-adjust to timestamp if present, otherwise retain the original -SNAPSHOT filename
SnapshotVersion snapshotVersion = metadata.getSnapshotVersion();
artifactVersion =
artifactVersion + snapshotVersion.getTimestamp() + "-" + snapshotVersion.getBuildNumber();
}
- } catch (XMLException | IOException e) {
+ } catch ( RepositoryMetadataException e) {
// unable to parse metadata - LOGGER it, and continue with the version as the original SNAPSHOT version
- LOGGER.warn("Invalid metadata: {} - {}", metadataFile, e.getMessage());
+ log.warn("Invalid metadata: {} - {}", metadataFile, e.getMessage());
}
}
// MRM-1411
req.setModelResolver(
new RepositoryModelResolver(managedRepository, pathTranslator, wagonFactory, remoteRepositories,
- networkProxies, managedRepository, mavenSystemManager));
+ networkProxies, managedRepository, mavenSystemManager, metadataReader));
Model model;
try {
|| problem.getException() instanceof NoSuchFileException
) && e.getModelId() != null &&
!e.getModelId().equals(problem.getModelId()))) {
- LOGGER.warn("The artifact's parent POM file '{}' cannot be resolved. "
+ log.warn("The artifact's parent POM file '{}' cannot be resolved. "
+ "Using defaults for project version metadata..", file);
ProjectVersionMetadata metadata = new ProjectVersionMetadata();
readMetadataRequest.getProjectId(), readMetadataRequest.getProjectVersion(),
path));
} catch (Exception e) {
- LOGGER.debug("Could not create metadata for {}: {}", path, e.getMessage(), e);
+ log.debug("Could not create metadata for {}: {}", path, e.getMessage(), e);
return Try.<ArtifactMetadata>failure(e);
}
}
).collect(Collectors.groupingBy(Try::isSuccess));
if (result.containsKey(Boolean.FALSE) && result.get(Boolean.FALSE).size() > 0 && (!result.containsKey(Boolean.TRUE) || result.get(Boolean.TRUE).size() == 0)) {
- LOGGER.error("Could not get artifact metadata. Directory: {}. Number of errors {}.", dir, result.get(Boolean.FALSE).size());
+ log.error("Could not get artifact metadata. Directory: {}. Number of errors {}.", dir, result.get(Boolean.FALSE).size());
Try<ArtifactMetadata> failure = result.get(Boolean.FALSE).get(0);
- LOGGER.error("Sample exception {}", failure.getError().getMessage(), failure.getError());
+ log.error("Sample exception {}", failure.getError().getMessage(), failure.getError());
throw new RepositoryStorageRuntimeException(readMetadataRequest.getRepositoryId(), "Could not retrieve metadata of the files");
} else {
if (!result.containsKey(Boolean.TRUE) || result.get(Boolean.TRUE) == null) {
@Override
public String getFilePathWithVersion(final String requestPath, ManagedRepositoryContent managedRepositoryContent)
- throws RelocationException, XMLException, IOException {
+ throws RelocationException
+ {
if (StringUtils.endsWith(requestPath, METADATA_FILENAME)) {
return getFilePath(requestPath, managedRepositoryContent.getRepository());
if (!metadataFile.exists()) {
return filePath;
}
- ArchivaRepositoryMetadata archivaRepositoryMetadata = MavenMetadataReader.read(metadataFile);
+ ArchivaRepositoryMetadata archivaRepositoryMetadata = null;
+ try
+ {
+ archivaRepositoryMetadata = metadataReader.read(metadataFile);
+ }
+ catch ( RepositoryMetadataException e )
+ {
+ log.error( "Could not read metadata {}", e.getMessage( ), e );
+ return filePath;
+ }
int buildNumber = archivaRepositoryMetadata.getSnapshotVersion().getBuildNumber();
String timestamp = archivaRepositoryMetadata.getSnapshotVersion().getTimestamp();
try {
metadata.setMd5(checksummedFile.calculateChecksum(ChecksumAlgorithm.MD5));
} catch (IOException e) {
- LOGGER.error("Unable to checksum file {}: {},MD5", file, e.getMessage());
+ log.error("Unable to checksum file {}: {},MD5", file, e.getMessage());
}
try {
metadata.setSha1(checksummedFile.calculateChecksum(ChecksumAlgorithm.SHA1));
} catch (IOException e) {
- LOGGER.error("Unable to checksum file {}: {},SHA1", file, e.getMessage());
+ log.error("Unable to checksum file {}: {},SHA1", file, e.getMessage());
}
metadata.setSize(file.getSize());
}
StorageAsset metadataFile = directory.resolve(METADATA_FILENAME);
if (metadataFile.exists()) {
try {
- metadata = MavenMetadataReader.read(metadataFile);
- } catch (XMLException | IOException e) {
- // ignore missing or invalid metadata
+ metadata = metadataReader.read(metadataFile);
+ } catch ( RepositoryMetadataException e )
+ {
+ // Ignore missing or invalid metadata
}
}
return metadata;
import org.apache.archiva.repository.RemoteRepository;
import org.apache.archiva.repository.RepositoryCredentials;
import org.apache.archiva.repository.maven2.MavenSystemManager;
+import org.apache.archiva.repository.metadata.RepositoryMetadataException;
import org.apache.archiva.repository.storage.StorageAsset;
-import org.apache.archiva.xml.XMLException;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.maven.model.Dependency;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
-import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
private MavenSystemManager mavenSystemManager;
+ private MavenMetadataReader metadataReader;
+
private ManagedRepository managedRepository;
- public RepositoryModelResolver(StorageAsset basedir, RepositoryPathTranslator pathTranslator )
+ public RepositoryModelResolver(StorageAsset basedir, RepositoryPathTranslator pathTranslator)
{
this.basedir = basedir;
this.pathTranslator = pathTranslator;
+
}
public RepositoryModelResolver(ManagedRepository managedRepository, RepositoryPathTranslator pathTranslator,
WagonFactory wagonFactory, List<RemoteRepository> remoteRepositories,
Map<String, NetworkProxy> networkProxiesMap, ManagedRepository targetRepository,
- MavenSystemManager mavenSystemManager)
+ MavenSystemManager mavenSystemManager, MavenMetadataReader metadataReader)
{
this( managedRepository.getAsset(""), pathTranslator );
this.versionRangeResolver = mavenSystemManager.getLocator().getService(VersionRangeResolver.class);
this.mavenSystemManager = mavenSystemManager;
+ this.metadataReader = metadataReader;
}
{
try
{
- ArchivaRepositoryMetadata archivaRepositoryMetadata = MavenMetadataReader.read( mavenMetadata);
+ ArchivaRepositoryMetadata archivaRepositoryMetadata = metadataReader.read( mavenMetadata );
SnapshotVersion snapshotVersion = archivaRepositoryMetadata.getSnapshotVersion();
if ( snapshotVersion != null )
{
}
}
}
- catch (XMLException e )
+ catch ( RepositoryMetadataException e )
{
log.warn( "fail to read {}, {}", mavenMetadata.toAbsolutePath(), e.getCause() );
}
public ModelResolver newCopy()
{
return new RepositoryModelResolver( managedRepository, pathTranslator, wagonFactory, remoteRepositories,
- networkProxyMap, targetRepository, mavenSystemManager);
+ networkProxyMap, targetRepository, mavenSystemManager, metadataReader);
}
// FIXME: we need to do some refactoring, we cannot re-use the proxy components of archiva-proxy in maven2-repository
private boolean getModelFromProxy( RemoteRepository remoteRepository, String groupId, String artifactId,
String version, String filename )
throws AuthorizationException, TransferFailedException, ResourceDoesNotExistException, WagonFactoryException,
- XMLException, IOException
+ IOException, RepositoryMetadataException
{
boolean success = false;
Path tmpMd5 = null;
log.debug( "Successfully downloaded metadata." );
- ArchivaRepositoryMetadata metadata = MavenMetadataReader.read( tmpMetadataResource );
+ ArchivaRepositoryMetadata metadata = metadataReader.read( tmpMetadataResource );
// re-adjust to timestamp if present, otherwise retain the original -SNAPSHOT filename
SnapshotVersion snapshotVersion = metadata.getSnapshotVersion();
import org.apache.archiva.repository.storage.FilesystemAsset;
import org.apache.archiva.repository.storage.FilesystemStorage;
import org.apache.archiva.repository.storage.StorageAsset;
-import org.apache.archiva.xml.XMLException;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
implements RepositoryMerger
{
- private Logger log = LoggerFactory.getLogger( getClass() );
+ @Inject
+ @Named("metadataReader#maven")
+ private MavenMetadataReader metadataReader;
+
+ private static final Logger log = LoggerFactory.getLogger( Maven2RepositoryMerger.class );
private static final Comparator<ArtifactMetadata> META_COMPARATOR = Comparator.comparing(ArtifactMetadata::getNamespace)
.thenComparing(ArtifactMetadata::getProject)
ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
if ( Files.exists(metadataFile) )
{
- try
- {
- metadata = MavenMetadataReader.read( metadataFile );
- }
- catch (XMLException e )
- {
- throw new RepositoryMetadataException( e.getMessage(), e );
- }
+ metadata = metadataReader.read( metadataFile );
}
return metadata;
}
{
private Configuration configuration = new Configuration();
+ StubConfiguration() {
+ configuration.setRepositoryScanning( new RepositoryScanningConfiguration() );
+ }
+
@Override
public Configuration getConfiguration()
{
import org.apache.archiva.maven2.metadata.MavenMetadataReader;
import org.apache.archiva.model.ArchivaRepositoryMetadata;
import org.apache.archiva.model.Plugin;
+import org.apache.archiva.repository.metadata.RepositoryMetadataException;
import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
-import org.apache.archiva.xml.XMLException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
@Test
public void testGroupMetadata()
- throws XMLException, IOException {
+ throws RepositoryMetadataException
+ {
Path metadataFile = defaultRepoDir.resolve( "org/apache/maven/plugins/maven-metadata.xml" );
- ArchivaRepositoryMetadata metadata = MavenMetadataReader.read( metadataFile );
+ MavenMetadataReader metadataReader = new MavenMetadataReader( );
+ ArchivaRepositoryMetadata metadata = metadataReader.read( metadataFile );
assertNotNull( metadata );
assertEquals( "org.apache.maven.plugins", metadata.getGroupId() );
@Test
public void testProjectMetadata()
- throws XMLException, IOException {
+ throws RepositoryMetadataException
+ {
Path metadataFile = defaultRepoDir.resolve( "org/apache/maven/shared/maven-downloader/maven-metadata.xml" );
- ArchivaRepositoryMetadata metadata = MavenMetadataReader.read( metadataFile);
+ MavenMetadataReader metadataReader = new MavenMetadataReader( );
+ ArchivaRepositoryMetadata metadata = metadataReader.read( metadataFile );
assertNotNull( metadata );
assertEquals( "org.apache.maven.shared", metadata.getGroupId() );
@Test
public void testProjectVersionMetadata()
- throws XMLException, IOException {
+ throws RepositoryMetadataException
+ {
Path metadataFile = defaultRepoDir.resolve( "org/apache/apache/5-SNAPSHOT/maven-metadata.xml" );
- ArchivaRepositoryMetadata metadata = MavenMetadataReader.read( metadataFile );
+ MavenMetadataReader metadataReader = new MavenMetadataReader( );
+ ArchivaRepositoryMetadata metadata = metadataReader.read(metadataFile );
assertNotNull( metadata );
assertEquals( "org.apache", metadata.getGroupId() );
import org.apache.archiva.maven2.metadata.MavenMetadataReader;
import org.apache.archiva.model.ArchivaRepositoryMetadata;
import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
-import org.apache.archiva.xml.XMLException;
import org.junit.Test;
import org.junit.runner.RunWith;
-import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
{
@Test
public void testLoadSimple()
- throws XMLException, IOException {
+ throws RepositoryMetadataException
+ {
Path defaultRepoDir = Paths.get( "src/test/repositories/default-repository" );
Path metadataFile = defaultRepoDir.resolve( "org/apache/maven/shared/maven-downloader/maven-metadata.xml" );
- ArchivaRepositoryMetadata metadata = MavenMetadataReader.read( metadataFile);
+ MavenMetadataReader metadataReader = new MavenMetadataReader( );
+
+ ArchivaRepositoryMetadata metadata = metadataReader.read( metadataFile );
assertNotNull( metadata );
assertEquals( "Group Id", "org.apache.maven.shared", metadata.getGroupId() );
@Test
public void testLoadComplex()
- throws XMLException, IOException {
+ throws RepositoryMetadataException
+ {
Path defaultRepoDir = Paths.get( "src/test/repositories/default-repository" );
Path metadataFile = defaultRepoDir.resolve( "org/apache/maven/samplejar/maven-metadata.xml" );
+ MavenMetadataReader metadataReader = new MavenMetadataReader( );
- ArchivaRepositoryMetadata metadata = MavenMetadataReader.read( metadataFile );
+ ArchivaRepositoryMetadata metadata = metadataReader.read( metadataFile );
assertNotNull( metadata );
assertEquals( "Group Id", "org.apache.maven", metadata.getGroupId() );
import org.apache.archiva.repository.ReleaseScheme;
import org.apache.archiva.repository.RepositoryException;
import org.apache.archiva.repository.RepositoryNotFoundException;
+import org.apache.archiva.repository.RepositoryRegistry;
+import org.apache.archiva.repository.metadata.MetadataReader;
import org.apache.archiva.repository.metadata.base.MetadataTools;
import org.apache.archiva.repository.storage.StorageAsset;
import org.apache.archiva.repository.storage.StorageUtil;
@Inject
ProxyRegistry proxyRegistry;
+ @Inject
+ RepositoryRegistry repositoryRegistry;
+
@Inject
@Named( value = "browse#versionMetadata" )
private Cache<String, ProjectVersionMetadata> versionMetadataCache;
StorageAsset metadataFile = file.getStorage().getAsset(file.getParent().getPath()+"/"+MetadataTools.MAVEN_METADATA );
if ( metadataFile.exists() )
{
- try
- {
- ArchivaRepositoryMetadata archivaRepositoryMetadata =
- MavenMetadataReader.read( metadataFile );
- int buildNumber = archivaRepositoryMetadata.getSnapshotVersion().getBuildNumber();
- String timeStamp = archivaRepositoryMetadata.getSnapshotVersion().getTimestamp();
- // rebuild file name with timestamped version and build number
- String timeStampFileName = new StringBuilder( artifactId ).append( '-' ) //
- .append( StringUtils.remove( version, "-" + VersionUtil.SNAPSHOT ) ) //
- .append( '-' ).append( timeStamp ) //
- .append( '-' ).append( Integer.toString( buildNumber ) ) //
- .append( ( StringUtils.isEmpty( classifier ) ? "" : "-" + classifier ) ) //
- .append( ".jar" ).toString();
-
- StorageAsset timeStampFile = file.getStorage().getAsset(file.getParent().getPath() + "/" + timeStampFileName );
- log.debug( "try to find timestamped snapshot version file: {}", timeStampFile.getPath() );
- if ( timeStampFile.exists() )
- {
- return true;
- }
- }
- catch (XMLException | IOException e )
+ MetadataReader metadataReader = repositoryRegistry.getMetadataReader( managedRepositoryContent.getRepository( ).getType( ) );
+ ArchivaRepositoryMetadata archivaRepositoryMetadata =
+ metadataReader.read( metadataFile );
+ int buildNumber = archivaRepositoryMetadata.getSnapshotVersion().getBuildNumber();
+ String timeStamp = archivaRepositoryMetadata.getSnapshotVersion().getTimestamp();
+ // rebuild file name with timestamped version and build number
+ String timeStampFileName = new StringBuilder( artifactId ).append( '-' ) //
+ .append( StringUtils.remove( version, "-" + VersionUtil.SNAPSHOT ) ) //
+ .append( '-' ).append( timeStamp ) //
+ .append( '-' ).append( Integer.toString( buildNumber ) ) //
+ .append( ( StringUtils.isEmpty( classifier ) ? "" : "-" + classifier ) ) //
+ .append( ".jar" ).toString();
+
+ StorageAsset timeStampFile = file.getStorage().getAsset(file.getParent().getPath() + "/" + timeStampFileName );
+ log.debug( "try to find timestamped snapshot version file: {}", timeStampFile.getPath() );
+ if ( timeStampFile.exists() )
{
- log.warn( "skip fail to find timestamped snapshot file: {}", e.getMessage() );
+ return true;
}
}
}
import org.apache.archiva.repository.RepositoryException;
import org.apache.archiva.repository.RepositoryNotFoundException;
import org.apache.archiva.repository.RepositoryRegistry;
+import org.apache.archiva.repository.RepositoryType;
import org.apache.archiva.repository.storage.RepositoryStorage;
import org.apache.archiva.repository.storage.StorageAsset;
import org.apache.archiva.repository.storage.StorageUtil;
String timestamp = null;
StorageAsset versionMetadataFile = target.getAsset(path + "/" + MetadataTools.MAVEN_METADATA );
- /* unused */ getMetadata( versionMetadataFile );
+ /* unused */ getMetadata( targetRepository.getRepository().getType(), versionMetadataFile );
if ( !targetDir.exists() )
{
// explicitly update only if metadata-updater consumer is not enabled!
if ( !archivaAdministration.getKnownContentConsumers().contains( "metadata-updater" ) )
{
- updateProjectMetadata( target, targetDir, lastUpdatedTimestamp, timestamp, newBuildNumber,
+ updateProjectMetadata( target.getType(), target, targetDir, lastUpdatedTimestamp, timestamp, newBuildNumber,
fixChecksums, artifactTransferRequest );
}
}
- private ArchivaRepositoryMetadata getMetadata( StorageAsset metadataFile )
+ private ArchivaRepositoryMetadata getMetadata( RepositoryType repositoryType, StorageAsset metadataFile )
throws RepositoryMetadataException
{
ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
if ( metadataFile.exists() )
{
- try
- {
- metadata = MavenMetadataReader.read( metadataFile.getFilePath() );
- }
- catch (XMLException e )
- {
- throw new RepositoryMetadataException( e.getMessage(), e );
- }
+ metadata = repositoryRegistry.getMetadataReader( repositoryType ).read( metadataFile );
}
return metadata;
}
}
}
- private void updateProjectMetadata( RepositoryStorage storage, StorageAsset targetPath, Date lastUpdatedTimestamp, String timestamp, int buildNumber,
+ private void updateProjectMetadata( RepositoryType repositoryType, RepositoryStorage storage, StorageAsset targetPath, Date lastUpdatedTimestamp, String timestamp, int buildNumber,
boolean fixChecksums, ArtifactTransferRequest artifactTransferRequest )
throws RepositoryMetadataException
{
StorageAsset projectDir = targetPath.getParent();
StorageAsset projectMetadataFile = storage.getAsset( projectDir.getPath()+"/"+MetadataTools.MAVEN_METADATA );
- ArchivaRepositoryMetadata projectMetadata = getMetadata( projectMetadataFile );
+ ArchivaRepositoryMetadata projectMetadata = getMetadata( repositoryType, projectMetadataFile );
if ( projectMetadataFile.exists() )
{
repository.deleteArtifact( artifactRef );
}
StorageAsset metadataFile = getMetadata( repo, targetPath.getPath() );
- ArchivaRepositoryMetadata metadata = getMetadata( metadataFile );
+ ArchivaRepositoryMetadata metadata = getMetadata( repository.getRepository().getType(), metadataFile );
updateMetadata( metadata, metadataFile, lastUpdatedTimestamp, artifact );
}
import org.apache.archiva.model.ArtifactReference;
import org.apache.archiva.model.SnapshotVersion;
import org.apache.archiva.components.taskqueue.TaskQueueException;
+import org.apache.archiva.repository.Repository;
import org.apache.archiva.repository.RepositoryException;
import org.apache.archiva.repository.RepositoryNotFoundException;
+import org.apache.archiva.repository.RepositoryRegistry;
+import org.apache.archiva.repository.RepositoryType;
import org.apache.archiva.repository.content.base.ArtifactUtil;
+import org.apache.archiva.repository.metadata.MetadataReader;
import org.apache.archiva.repository.metadata.base.MetadataTools;
import org.apache.archiva.repository.metadata.RepositoryMetadataException;
import org.apache.archiva.repository.metadata.base.RepositoryMetadataWriter;
@Named(value = "archivaTaskScheduler#repository")
private ArchivaTaskScheduler<RepositoryTask> scheduler;
+ @Inject
+ private RepositoryRegistry repositoryRegistry;
+
private String getStringValue(MultipartBody multipartBody, String attachmentId)
throws IOException {
Attachment attachment = multipartBody.getAttachment(attachmentId);
throws RepositoryMetadataException {
ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
if (metadataFile.exists()) {
- try {
- metadata = MavenMetadataReader.read(metadataFile);
- } catch (XMLException | IOException e) {
- throw new RepositoryMetadataException(e.getMessage(), e);
- }
+ Repository repo = repositoryRegistry.getRepositoryOfAsset( metadataFile );
+ RepositoryType type = repo == null ? RepositoryType.MAVEN : repo.getType( );
+ MetadataReader metadataReader = repositoryRegistry.getMetadataReader( type );
+ metadata = metadataReader.read(metadataFile);
}
return metadata;
}
import org.apache.archiva.repository.RepositoryGroup;
import org.apache.archiva.repository.RepositoryRegistry;
import org.apache.archiva.repository.RepositoryRequestInfo;
+import org.apache.archiva.repository.storage.FilesystemAsset;
import org.apache.archiva.repository.storage.FilesystemStorage;
import org.apache.archiva.repository.storage.StorageAsset;
import org.apache.archiva.metadata.audit.AuditListener;
import javax.inject.Named;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
+import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
try
{
Path metadataFile = Paths.get( resourceAbsPath );
- ArchivaRepositoryMetadata repoMetadata = MavenMetadataReader.read( metadataFile );
+ FilesystemStorage storage = new FilesystemStorage( metadataFile.getParent( ), new DefaultFileLockManager( ) );
+ ArchivaRepositoryMetadata repoMetadata = repositoryRegistry.getMetadataReader( repoGroup.getType( ) ).read( storage.getAsset( metadataFile.getFileName().toString() ) );
mergedMetadata = RepositoryMetadataMerge.merge( mergedMetadata, repoMetadata );
}
- catch (XMLException e )
+ catch ( RepositoryMetadataException r )
{
throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
- "Error occurred while reading metadata file." );
+ "Error occurred while merging metadata file." );
}
- catch ( RepositoryMetadataException r )
+ catch ( IOException e )
{
throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
- "Error occurred while merging metadata file." );
+ "Error occurred while merging metadata file." );
}
}
Path returnedMetadata = getProjectBase().resolve( "target/test-classes/retrievedMetadataFile.xml" );
org.apache.archiva.common.utils.FileUtils.writeStringToFile( returnedMetadata, Charset.defaultCharset(), response.getContentAsString() );
- ArchivaRepositoryMetadata metadata = MavenMetadataReader.read( returnedMetadata );
+ MavenMetadataReader metadataReader = new MavenMetadataReader( );
+ ArchivaRepositoryMetadata metadata = metadataReader.read( returnedMetadata );
assertResponseOK( response );