use of org.apache.archiva.model.VersionedReference in project archiva by apache.
the class MetadataTools method toVersionedReference.
/**
* Take a path to a maven-metadata.xml, and attempt to translate it to a VersionedReference.
*
* @param path
* @return
*/
public VersionedReference toVersionedReference(String path) throws RepositoryMetadataException {
if (!path.endsWith("/" + MAVEN_METADATA)) {
throw new RepositoryMetadataException("Cannot convert to versioned reference, not a metadata file. ");
}
VersionedReference reference = new VersionedReference();
String normalizedPath = StringUtils.replace(path, "\\", "/");
String[] pathParts = StringUtils.split(normalizedPath, '/');
int versionOffset = pathParts.length - 2;
int artifactIdOffset = versionOffset - 1;
int groupIdEnd = artifactIdOffset - 1;
reference.setVersion(pathParts[versionOffset]);
if (!hasNumberAnywhere(reference.getVersion())) {
// Scary check, but without it, all paths are version references;
throw new RepositoryMetadataException("Not a versioned reference, as version id on path has no number in it.");
}
reference.setArtifactId(pathParts[artifactIdOffset]);
StringBuilder gid = new StringBuilder();
for (int i = 0; i <= groupIdEnd; i++) {
if (i > 0) {
gid.append(".");
}
gid.append(pathParts[i]);
}
reference.setGroupId(gid.toString());
return reference;
}
use of org.apache.archiva.model.VersionedReference in project archiva by apache.
the class MetadataTools method getFirstArtifact.
/**
* Get the first Artifact found in the provided VersionedReference location.
*
* @param managedRepository the repository to search within.
* @param reference the reference to the versioned reference to search within
* @return the ArtifactReference to the first artifact located within the versioned reference. or null if
* no artifact was found within the versioned reference.
* @throws IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
* @throws LayoutException
*/
public ArtifactReference getFirstArtifact(ManagedRepositoryContent managedRepository, VersionedReference reference) throws LayoutException, IOException {
String path = toPath(reference);
int idx = path.lastIndexOf('/');
if (idx > 0) {
path = path.substring(0, idx);
}
Path repoDir = Paths.get(managedRepository.getRepoRoot(), path);
if (!Files.exists(repoDir)) {
throw new IOException("Unable to gather the list of snapshot versions on a non-existant directory: " + repoDir.toAbsolutePath());
}
if (!Files.isDirectory(repoDir)) {
throw new IOException("Unable to gather the list of snapshot versions on a non-directory: " + repoDir.toAbsolutePath());
}
try (Stream<Path> stream = Files.list(repoDir)) {
String result = stream.filter(Files::isRegularFile).map(path1 -> PathUtil.getRelative(managedRepository.getRepoRoot(), path1)).filter(filetypes::matchesArtifactPattern).findFirst().orElse(null);
if (result != null) {
return managedRepository.toArtifactReference(result);
}
}
// No artifact was found.
return null;
}
use of org.apache.archiva.model.VersionedReference in project archiva by apache.
the class MetadataTransferTest method assertReleaseMetadataContents.
/**
* Test for the existance of the requestedResource in the default managed repository, and if it exists,
* does it contain the expected release maven-metadata.xml contents?
*
* @param requestedResource the requested resource
* @throws Exception
*/
private void assertReleaseMetadataContents(String requestedResource) throws Exception {
Path actualFile = managedDefaultDir.resolve(requestedResource);
assertTrue("Release Metadata should exist: " + requestedResource, Files.exists(actualFile));
VersionedReference metadata = createVersionedReference(requestedResource);
// Build expected metadata XML
StringWriter expectedMetadataXml = new StringWriter();
ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
m.setGroupId(metadata.getGroupId());
m.setArtifactId(metadata.getArtifactId());
m.setVersion(metadata.getVersion());
RepositoryMetadataWriter.write(m, expectedMetadataXml);
// Compare the file to the actual contents.
assertMetadataEquals(expectedMetadataXml.toString(), actualFile);
}
use of org.apache.archiva.model.VersionedReference in project archiva by apache.
the class MetadataTransferTest method assertSnapshotMetadataContents.
/**
* Test for the existance of the snapshot metadata in the default managed repository, and if it exists,
* does it contain the expected release maven-metadata.xml contents?
*
* @param requestedResource the requested resource
* @param expectedDate the date in "yyyyMMdd" format
* @param expectedTime the time in "hhmmss" format
* @param expectedBuildnumber the build number
* @throws Exception
*/
private void assertSnapshotMetadataContents(String requestedResource, String expectedDate, String expectedTime, int expectedBuildnumber) throws Exception {
Path actualFile = managedDefaultDir.resolve(requestedResource);
assertTrue("Snapshot Metadata should exist: " + requestedResource, Files.exists(actualFile));
VersionedReference actualMetadata = createVersionedReference(requestedResource);
assertSnapshotMetadata(actualFile, actualMetadata, expectedDate, expectedTime, expectedBuildnumber);
}
use of org.apache.archiva.model.VersionedReference in project archiva by apache.
the class MetadataTransferTest method assertFetchVersionedFailed.
/**
* Transfer the metadata file, not expected to succeed.
*
* @param requestedResource the requested resource
* @throws Exception
*/
private void assertFetchVersionedFailed(String requestedResource) throws Exception {
Path expectedFile = managedDefaultDir.resolve(requestedResource);
VersionedReference metadata = createVersionedReference(requestedResource);
Path downloadedFile = proxyHandler.fetchMetadataFromProxies(managedDefaultRepository, managedDefaultRepository.toMetadataPath(metadata)).getFile();
assertNull(downloadedFile);
assertNoTempFiles(expectedFile);
}
Aggregations