1 package org.apache.archiva.metadata.maven;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
20 import org.apache.archiva.model.ArchivaRepositoryMetadata;
21 import org.apache.archiva.model.Plugin;
22 import org.apache.archiva.model.SnapshotVersion;
23 import org.apache.archiva.repository.RepositoryType;
24 import org.apache.archiva.repository.metadata.MetadataReader;
25 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
26 import org.apache.archiva.repository.storage.StorageAsset;
27 import org.apache.archiva.xml.XMLException;
28 import org.apache.archiva.xml.XMLReader;
29 import org.apache.archiva.xml.XmlUtil;
30 import org.apache.commons.lang3.StringUtils;
31 import org.apache.commons.lang3.math.NumberUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.stereotype.Service;
35 import org.w3c.dom.Element;
36 import org.w3c.dom.Node;
38 import java.io.IOException;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.time.Instant;
42 import java.util.Date;
45 * @author Olivier Lamy
48 @Service("metadataReader#maven")
49 public class MavenMetadataReader implements MetadataReader
51 public static final String MAVEN_METADATA = "maven-metadata.xml";
55 <?xml version="1.0" encoding="UTF-8"?>
56 <metadata modelVersion="1.1.0">
57 <groupId>org.apache.archiva</groupId>
58 <artifactId>archiva</artifactId>
59 <version>1.4-M3-SNAPSHOT</version>
62 <timestamp>20120310.230917</timestamp>
63 <buildNumber>2</buildNumber>
65 <lastUpdated>20120310230917</lastUpdated>
68 <extension>pom</extension>
69 <value>1.4-M3-20120310.230917-2</value>
70 <updated>20120310230917</updated>
77 private static final Logger log = LoggerFactory.getLogger( MavenMetadataReader.class );
81 * Read and return the {@link org.apache.archiva.model.ArchivaRepositoryMetadata} object from the provided xml file.
83 * @param metadataFile the maven-metadata.xml file to read.
84 * @return the archiva repository metadata object that represents the provided file contents.
85 * @throws RepositoryMetadataException if the file cannot be read
87 public ArchivaRepositoryMetadata read( StorageAsset metadataFile )
88 throws RepositoryMetadataException {
93 xml = new XMLReader( "metadata", metadataFile );
95 catch ( XMLException e )
97 throw new RepositoryMetadataException( "Could not open XML metadata file " + metadataFile, e );
99 return read( xml, metadataFile.getModificationTime(), metadataFile.getSize() );
103 public ArchivaRepositoryMetadata read( Path metadataFile )
104 throws RepositoryMetadataException {
109 xml = new XMLReader( "metadata", metadataFile );
111 catch ( XMLException e )
113 log.error( "XML error while reading metadata file {}: {}", metadataFile, e.getMessage(), e );
114 throw new RepositoryMetadataException( "Could not open XML metadata file " + metadataFile, e );
118 return read( xml, Files.getLastModifiedTime( metadataFile ).toInstant(), Files.size( metadataFile ) );
120 catch ( IOException e )
122 log.error( "IO Error while reading metadata file {}: {}", metadataFile, e.getMessage(), e );
123 throw new RepositoryMetadataException( "Could not open XML metadata file " + metadataFile, e );
128 private ArchivaRepositoryMetadata read( XMLReader xml, Instant modTime, long fileSize) throws RepositoryMetadataException
130 // invoke this to remove namespaces, see MRM-1136
131 xml.removeNamespaces();
133 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
137 metadata.setGroupId( xml.getElementText( "//metadata/groupId" ) );
138 metadata.setArtifactId( xml.getElementText( "//metadata/artifactId" ) );
139 metadata.setVersion( xml.getElementText( "//metadata/version" ) );
140 metadata.setFileLastModified( Date.from(modTime) );
141 metadata.setFileSize( fileSize );
142 metadata.setLastUpdated( xml.getElementText( "//metadata/versioning/lastUpdated" ) );
143 metadata.setLatestVersion( xml.getElementText( "//metadata/versioning/latest" ) );
144 metadata.setReleasedVersion( xml.getElementText( "//metadata/versioning/release" ) );
145 metadata.setAvailableVersions( xml.getElementListText( "//metadata/versioning/versions/version" ) );
147 Element snapshotElem = xml.getElement( "//metadata/versioning/snapshot" );
148 if ( snapshotElem != null )
150 SnapshotVersion snapshot = new SnapshotVersion( );
151 snapshot.setTimestamp( XmlUtil.getChildText( snapshotElem, "timestamp" ) );
152 String buildNumber = XmlUtil.getChildText( snapshotElem, "buildNumber" );
153 if ( NumberUtils.isCreatable( buildNumber ) )
155 snapshot.setBuildNumber( NumberUtils.toInt( buildNumber ) );
157 metadata.setSnapshotVersion( snapshot );
160 for ( Node node : xml.getElementList( "//metadata/plugins/plugin" ) )
162 if ( node instanceof Element )
164 Element plugin = (Element) node;
165 Plugin p = new Plugin( );
166 String prefix = plugin.getElementsByTagName( "prefix" ).item( 0 ).getTextContent( ).trim( );
167 p.setPrefix( prefix );
168 String artifactId = plugin.getElementsByTagName( "artifactId" ).item( 0 ).getTextContent( ).trim( );
169 p.setArtifactId( artifactId );
170 String name = plugin.getElementsByTagName( "name" ).item( 0 ).getTextContent( ).trim( );
172 metadata.addPlugin( p );
175 } catch ( XMLException e) {
176 throw new RepositoryMetadataException( "XML Error while reading metadata file : " + e.getMessage( ), e );
182 public boolean isValidMetadataPath( String path )
184 if ( StringUtils.isNotEmpty( path ) ) {
185 return path.endsWith( MAVEN_METADATA );
192 public boolean isValidForType( RepositoryType repositoryType )
194 return RepositoryType.MAVEN.equals( repositoryType );