use of org.apache.archiva.model.ArchivaRepositoryMetadata in project archiva by apache.
the class MavenMetadataReader method read.
/**
* 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
*/
public static ArchivaRepositoryMetadata read(Path metadataFile) throws XMLException {
XMLReader xml = new XMLReader("metadata", metadataFile);
// invoke this to remove namespaces, see MRM-1136
xml.removeNamespaces();
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);
}
metadata.setFileLastModified(modTime);
try {
metadata.setFileSize(Files.size(metadataFile));
} catch (IOException e) {
metadata.setFileSize(0);
log.error("Could not read file size of {}", 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"));
Element snapshotElem = xml.getElement("//metadata/versioning/snapshot");
if (snapshotElem != null) {
SnapshotVersion snapshot = new SnapshotVersion();
snapshot.setTimestamp(snapshotElem.elementTextTrim("timestamp"));
String tmp = snapshotElem.elementTextTrim("buildNumber");
if (NumberUtils.isNumber(tmp)) {
snapshot.setBuildNumber(NumberUtils.toInt(tmp));
}
metadata.setSnapshotVersion(snapshot);
}
for (Element plugin : xml.getElementList("//metadata/plugins/plugin")) {
Plugin p = new Plugin();
p.setPrefix(plugin.elementTextTrim("prefix"));
p.setArtifactId(plugin.elementTextTrim("artifactId"));
p.setName(plugin.elementTextTrim("name"));
metadata.addPlugin(p);
}
return metadata;
}
use of org.apache.archiva.model.ArchivaRepositoryMetadata in project archiva by apache.
the class Maven2RepositoryStorage method readMetadata.
private ArchivaRepositoryMetadata readMetadata(Path directory) {
ArchivaRepositoryMetadata metadata = null;
Path metadataFile = directory.resolve(METADATA_FILENAME);
if (Files.exists(metadataFile)) {
try {
metadata = MavenMetadataReader.read(metadataFile);
} catch (XMLException e) {
// ignore missing or invalid metadata
}
}
return metadata;
}
use of org.apache.archiva.model.ArchivaRepositoryMetadata in project archiva by apache.
the class Maven2RepositoryStorage method getFilePathWithVersion.
@Override
public String getFilePathWithVersion(final String requestPath, ManagedRepositoryContent managedRepositoryContent) throws XMLException, RelocationException {
if (StringUtils.endsWith(requestPath, METADATA_FILENAME)) {
return getFilePath(requestPath, managedRepositoryContent.getRepository());
}
String filePath = getFilePath(requestPath, managedRepositoryContent.getRepository());
ArtifactReference artifactReference = null;
try {
artifactReference = pathParser.toArtifactReference(filePath);
} catch (LayoutException e) {
return filePath;
}
if (StringUtils.endsWith(artifactReference.getVersion(), VersionUtil.SNAPSHOT)) {
// read maven metadata to get last timestamp
Path metadataDir = Paths.get(managedRepositoryContent.getRepoRoot(), filePath).getParent();
if (!Files.exists(metadataDir)) {
return filePath;
}
Path metadataFile = metadataDir.resolve(METADATA_FILENAME);
if (!Files.exists(metadataFile)) {
return filePath;
}
ArchivaRepositoryMetadata archivaRepositoryMetadata = MavenMetadataReader.read(metadataFile);
int buildNumber = archivaRepositoryMetadata.getSnapshotVersion().getBuildNumber();
String timestamp = archivaRepositoryMetadata.getSnapshotVersion().getTimestamp();
// MRM-1846
if (buildNumber < 1 && timestamp == null) {
return filePath;
}
// org/apache/archiva/archiva-checksum/1.4-M4-SNAPSHOT/archiva-checksum-1.4-M4-SNAPSHOT.jar
// -> archiva-checksum-1.4-M4-20130425.081822-1.jar
filePath = //
StringUtils.replace(//
filePath, //
artifactReference.getArtifactId() + "-" + //
artifactReference.getVersion(), //
artifactReference.getArtifactId() + "-" + StringUtils.remove(artifactReference.getVersion(), //
"-" + VersionUtil.SNAPSHOT) + "-" + //
timestamp + "-" + buildNumber);
throw new RelocationException("/repository/" + managedRepositoryContent.getRepository().getId() + (StringUtils.startsWith(filePath, "/") ? "" : "/") + filePath, RelocationException.RelocationType.TEMPORARY);
}
return filePath;
}
use of org.apache.archiva.model.ArchivaRepositoryMetadata in project archiva by apache.
the class Maven2RepositoryStorage method isProjectVersion.
private boolean isProjectVersion(Path dir) {
final String artifactId = dir.getParent().getFileName().toString();
final String projectVersion = dir.getFileName().toString();
// check if there is a POM artifact file to ensure it is a version directory
Predicate<Path> filter;
if (VersionUtil.isSnapshot(projectVersion)) {
filter = new PomFilenameFilter(artifactId, projectVersion);
} else {
final String pomFile = artifactId + "-" + projectVersion + ".pom";
filter = new PomFileFilter(pomFile);
}
try (Stream<Path> stream = Files.list(dir)) {
if (stream.filter(Files::isRegularFile).anyMatch(filter)) {
return true;
}
} catch (IOException e) {
LOGGER.error("Could not list directory {}: {}", dir, e.getMessage(), e);
}
// if a metadata file is present, check if this is the "version" directory, marking it as a project version
ArchivaRepositoryMetadata metadata = readMetadata(dir);
if (metadata != null && projectVersion.equals(metadata.getVersion())) {
return true;
}
return false;
}
use of org.apache.archiva.model.ArchivaRepositoryMetadata in project archiva by apache.
the class RepositoryServletRepositoryGroupTest method testGetMergedMetadata.
// MRM-872
@Test
public void testGetMergedMetadata() throws Exception {
// first metadata file
String resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";
Path dummyInternalResourceFile = repoRootFirst.resolve(resourceName);
Files.createDirectories(dummyInternalResourceFile.getParent());
org.apache.archiva.common.utils.FileUtils.writeStringToFile(dummyInternalResourceFile, Charset.defaultCharset(), "<?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>");
// second metadata file
resourceName = "dummy/dummy-merged-metadata-resource/maven-metadata.xml";
dummyInternalResourceFile = repoRootLast.resolve(resourceName);
Files.createDirectories(dummyInternalResourceFile.getParent());
org.apache.archiva.common.utils.FileUtils.writeStringToFile(dummyInternalResourceFile, Charset.defaultCharset(), "<?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></versions><lastUpdated>20080709095554</lastUpdated>" + "</versioning></metadata>");
WebRequest request = new GetMethodWebRequest("http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/" + "dummy-merged-metadata-resource/maven-metadata.xml");
WebResponse response = getServletUnitClient().getResource(request);
Path returnedMetadata = Paths.get("target/test-classes/retrievedMetadataFile.xml");
org.apache.archiva.common.utils.FileUtils.writeStringToFile(returnedMetadata, Charset.defaultCharset(), response.getContentAsString());
ArchivaRepositoryMetadata metadata = MavenMetadataReader.read(returnedMetadata);
assertResponseOK(response);
assertThat(metadata.getAvailableVersions()).isNotNull().hasSize(4).contains("1.0", "1.5", "2.0", "2.5");
// check if the checksum files were generated
Path checksumFileSha1 = repoRootFirst.resolve(resourceName + ".sha1");
Files.createDirectories(checksumFileSha1.getParent());
org.apache.archiva.common.utils.FileUtils.writeStringToFile(checksumFileSha1, Charset.defaultCharset(), "3290853214d3687134");
Path checksumFileMd5 = repoRootFirst.resolve(resourceName + ".md5");
Files.createDirectories(checksumFileMd5.getParent());
org.apache.archiva.common.utils.FileUtils.writeStringToFile(checksumFileMd5, Charset.defaultCharset(), "98745897234eda12836423");
// request the sha1 checksum of the metadata
request = new GetMethodWebRequest("http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/" + "dummy-merged-metadata-resource/maven-metadata.xml.sha1");
response = getServletUnitClient().getResource(request);
assertResponseOK(response);
assertThat(response.getContentAsString()).isEqualTo("add113b0d7f8c6adb92a5015a7a3701081edf998 maven-metadata-group-with-valid-repos.xml");
// request the md5 checksum of the metadata
request = new GetMethodWebRequest("http://machine.com/repository/" + REPO_GROUP_WITH_VALID_REPOS + "/dummy/" + "dummy-merged-metadata-resource/maven-metadata.xml.md5");
response = getServletUnitClient().getResource(request);
assertResponseOK(response);
assertThat(response.getContentAsString()).isEqualTo("5b85ea4aa5f52bb76760041a52f98de8 maven-metadata-group-with-valid-repos.xml");
}
Aggregations