use of org.apache.archiva.repository.content.ItemSelector in project archiva by apache.
the class MetadataTools method gatherSnapshotVersions.
/**
* Gather the set of snapshot versions found in a particular versioned reference.
*
* @return the Set of snapshot artifact versions found.
* @throws LayoutException
* @throws ContentNotFoundException
*/
public Set<String> gatherSnapshotVersions(ManagedRepositoryContent managedRepository, ItemSelector reference) throws LayoutException, IOException, ContentNotFoundException {
Set<String> foundVersions = null;
try {
ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace(reference.getNamespace()).withProjectId(reference.getArtifactId()).withArtifactId(reference.getArtifactId()).withVersion(reference.getVersion()).build();
try (Stream<? extends Artifact> stream = managedRepository.getLayout(BaseRepositoryContentLayout.class).newArtifactStream(selector)) {
foundVersions = stream.map(a -> a.getArtifactVersion()).filter(StringUtils::isNotEmpty).collect(Collectors.toSet());
}
} catch (ContentAccessException e) {
log.error("Error while accessing content {}", e.getMessage());
throw new IOException("Could not access repository content: " + e.getMessage());
}
// Next gather up the referenced 'latest' versions found in any proxied repositories
// maven-metadata-${proxyId}.xml files that may be present.
// Does this repository have a set of remote proxied repositories?
Set<String> proxiedRepoIds = this.proxies.get(managedRepository.getId());
if (CollectionUtils.isNotEmpty(proxiedRepoIds)) {
String baseVersion = VersionUtil.getBaseVersion(reference.getVersion());
baseVersion = baseVersion.substring(0, baseVersion.indexOf(VersionUtil.SNAPSHOT) - 1);
// Add in the proxied repo version ids too.
Iterator<String> it = proxiedRepoIds.iterator();
while (it.hasNext()) {
String proxyId = it.next();
ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata(managedRepository, reference, proxyId);
if (proxyMetadata == null) {
// There is no proxy metadata, skip it.
continue;
}
// Is there some snapshot info?
SnapshotVersion snapshot = proxyMetadata.getSnapshotVersion();
if (snapshot != null) {
String timestamp = snapshot.getTimestamp();
int buildNumber = snapshot.getBuildNumber();
// Only interested in the timestamp + buildnumber.
if (StringUtils.isNotBlank(timestamp) && (buildNumber > 0)) {
foundVersions.add(baseVersion + "-" + timestamp + "-" + buildNumber);
}
}
}
}
return foundVersions;
}
use of org.apache.archiva.repository.content.ItemSelector in project archiva by apache.
the class MetadataTransferTest method assertRepoGroupMetadataContents.
private void assertRepoGroupMetadataContents(String proxiedRepoId, String requestedResource, String[] expectedPlugins) throws Exception {
String proxiedFile = metadataTools.getRepositorySpecificName(proxiedRepoId, requestedResource);
Path actualFile = managedDefaultDir.resolve(proxiedFile);
assertTrue("Repo Specific Group Metadata should exist: " + requestedResource, Files.exists(actualFile));
ItemSelector actualMetadata = createGroupSelector(requestedResource);
assertGroupMetadata(actualFile, actualMetadata, expectedPlugins);
}
use of org.apache.archiva.repository.content.ItemSelector 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));
ItemSelector actualMetadata = createVersionedSelector(requestedResource);
assertSnapshotMetadata(actualFile, actualMetadata, expectedDate, expectedTime, expectedBuildnumber);
}
use of org.apache.archiva.repository.content.ItemSelector in project archiva by apache.
the class MetadataTransferTest method assertRepoProjectMetadata.
/**
* Ensures that the repository specific maven metadata file exists, and contains the appropriate
* list of expected versions within.
*
* @param proxiedRepoId
* @param requestedResource
* @param expectedProxyVersions
*/
private void assertRepoProjectMetadata(String proxiedRepoId, String requestedResource, String[] expectedProxyVersions) throws Exception {
String proxiedFile = metadataTools.getRepositorySpecificName(proxiedRepoId, requestedResource);
Path actualFile = managedDefaultDir.resolve(proxiedFile);
assertTrue(Files.exists(actualFile));
ItemSelector metadata = createProjectSelector(requestedResource);
// Build expected metadata XML
StringWriter expectedMetadataXml = new StringWriter();
ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
m.setGroupId(metadata.getNamespace());
m.setArtifactId(metadata.getArtifactId());
if (expectedProxyVersions != null) {
m.getAvailableVersions().addAll(Arrays.asList(expectedProxyVersions));
}
RepositoryMetadataWriter.write(m, expectedMetadataXml);
// Compare the file to the actual contents.
assertMetadataEquals(expectedMetadataXml.toString(), actualFile);
}
use of org.apache.archiva.repository.content.ItemSelector in project archiva by apache.
the class Maven2RepositoryStorage method applyServerSideRelocation.
@Override
public ItemSelector applyServerSideRelocation(ManagedRepository managedRepository, ItemSelector artifactSelector) throws ProxyDownloadException {
if ("pom".equals(artifactSelector.getType())) {
return artifactSelector;
}
// Build the artifact POM reference
BaseRepositoryContentLayout layout;
try {
layout = managedRepository.getContent().getLayout(BaseRepositoryContentLayout.class);
} catch (LayoutException e) {
throw new ProxyDownloadException("Could not set layout " + e.getMessage(), new HashMap<>());
}
RepositoryType repositoryType = managedRepository.getType();
if (!proxyRegistry.hasHandler(repositoryType)) {
throw new ProxyDownloadException("No proxy handler found for repository type " + repositoryType, new HashMap<>());
}
ItemSelector selector = ArchivaItemSelector.builder().withNamespace(artifactSelector.getNamespace()).withProjectId(artifactSelector.getArtifactId()).withArtifactId(artifactSelector.getArtifactId()).withVersion(artifactSelector.getVersion()).withArtifactVersion(artifactSelector.getVersion()).withType("pom").build();
Artifact pom = layout.getArtifact(selector);
RepositoryProxyHandler proxyHandler = proxyRegistry.getHandler(repositoryType).get(0);
// Get the artifact POM from proxied repositories if needed
proxyHandler.fetchFromProxies(managedRepository, pom);
if (!pom.exists()) {
return artifactSelector;
}
try {
// MavenXpp3Reader leaves the file open, so we need to close it ourselves.
Model model;
try (Reader reader = Channels.newReader(pom.getAsset().getReadChannel(), Charset.defaultCharset().name())) {
model = MAVEN_XPP_3_READER.read(reader);
}
DistributionManagement dist = model.getDistributionManagement();
if (dist != null) {
Relocation relocation = dist.getRelocation();
if (relocation != null) {
ArchivaItemSelector.Builder relocatedBuilder = ArchivaItemSelector.builder();
// artifact is relocated : update the repositoryPath
if (relocation.getGroupId() != null) {
relocatedBuilder.withNamespace(relocation.getGroupId());
} else {
relocatedBuilder.withNamespace(artifactSelector.getNamespace());
}
if (relocation.getArtifactId() != null) {
relocatedBuilder.withArtifactId(relocation.getArtifactId());
} else {
relocatedBuilder.withArtifactId(artifactSelector.getArtifactId());
}
if (relocation.getVersion() != null) {
relocatedBuilder.withVersion(relocation.getVersion());
} else {
relocatedBuilder.withVersion(artifactSelector.getVersion());
}
return relocatedBuilder.withArtifactVersion(artifactSelector.getArtifactVersion()).withClassifier(artifactSelector.getClassifier()).withType(artifactSelector.getType()).withProjectId(artifactSelector.getProjectId()).withExtension(artifactSelector.getExtension()).build();
}
}
} catch (IOException e) {
// Unable to read POM : ignore.
} catch (XmlPullParserException e) {
// Invalid POM : ignore
}
return artifactSelector;
}
Aggregations