use of org.apache.archiva.repository.content.ContentAccessException in project archiva by apache.
the class MetadataTools method updateProjectMetadata.
/**
* Update the metadata to represent the all versions/plugins of
* the provided groupId:artifactId project or group reference,
* based off of information present in the repository,
* the maven-metadata.xml files, and the proxy/repository specific
* metadata file contents.
* <p>
* We must treat this as a group or a project metadata file as there is no way to know in advance
*
* @param managedRepository the managed repository where the metadata is kept.
* @param reference the reference to update.
* @throws LayoutException
* @throws RepositoryMetadataException
* @throws IOException
* @throws ContentNotFoundException
* @deprecated
*/
public void updateProjectMetadata(ManagedRepositoryContent managedRepository, ItemSelector reference) throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException {
StorageAsset metadataFile = managedRepository.getRepository().getAsset(toPath(reference));
ArchivaRepositoryMetadata existingMetadata = readMetadataFile(managedRepository, metadataFile);
BaseRepositoryContentLayout layout = managedRepository.getLayout(BaseRepositoryContentLayout.class);
long lastUpdated = getExistingLastUpdated(existingMetadata);
ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
metadata.setGroupId(reference.getNamespace());
metadata.setArtifactId(reference.getProjectId());
// Gather up all versions found in the managed repository.
ItemSelector selector = ArchivaItemSelector.builder().withNamespace(reference.getNamespace()).withProjectId(reference.getProjectId()).build();
Set<String> allVersions = null;
try {
Project project = layout.getProject(selector);
allVersions = layout.getVersions(project).stream().map(v -> v.getId()).collect(Collectors.toSet());
} catch (ContentAccessException e) {
log.error("Error while accessing repository: {}", e.getMessage(), e);
throw new RepositoryMetadataException("Error while accessing repository " + e.getMessage(), e);
}
// Gather up all plugins found in the managed repository.
// TODO: do we know this information instead?
// Set<Plugin> allPlugins = managedRepository.getPlugins( reference );
Set<Plugin> allPlugins;
if (existingMetadata != null) {
allPlugins = new LinkedHashSet<Plugin>(existingMetadata.getPlugins());
} else {
allPlugins = new LinkedHashSet<Plugin>();
}
// Does this repository have a set of remote proxied repositories?
Set<String> proxiedRepoIds = this.proxies.get(managedRepository.getId());
if (CollectionUtils.isNotEmpty(proxiedRepoIds)) {
// 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) {
allVersions.addAll(proxyMetadata.getAvailableVersions());
allPlugins.addAll(proxyMetadata.getPlugins());
long proxyLastUpdated = getLastUpdated(proxyMetadata);
lastUpdated = Math.max(lastUpdated, proxyLastUpdated);
}
}
}
if (!allVersions.isEmpty()) {
updateMetadataVersions(allVersions, metadata);
} else {
// Add the plugins to the metadata model.
metadata.setPlugins(new ArrayList<>(allPlugins));
// artifact ID was actually the last part of the group
metadata.setGroupId(metadata.getGroupId() + "." + metadata.getArtifactId());
metadata.setArtifactId(null);
}
if (lastUpdated > 0) {
metadata.setLastUpdatedTimestamp(toLastUpdatedDate(lastUpdated));
}
// Save the metadata model to disk.
RepositoryMetadataWriter.write(metadata, metadataFile);
ChecksummedFile checksum = new ChecksummedFile(metadataFile.getFilePath());
checksum.fixChecksums(algorithms);
}
use of org.apache.archiva.repository.content.ContentAccessException 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;
}
Aggregations