use of org.apache.archiva.repository.content.ContentNotFoundException in project archiva by apache.
the class DefaultRepositoriesService method deleteProject.
@Override
public ActionStatus deleteProject(String groupId, String projectId, String repositoryId) throws ArchivaRestServiceException {
if (StringUtils.isEmpty(repositoryId)) {
throw new ArchivaRestServiceException("repositoryId cannot be null", 400, null);
}
if (!getPermissionStatus(repositoryId).isAuthorizedToDeleteArtifacts()) {
throw new ArchivaRestServiceException("not authorized to delete artifacts", 403, null);
}
if (StringUtils.isEmpty(groupId)) {
throw new ArchivaRestServiceException("groupId cannot be null", 400, null);
}
if (StringUtils.isEmpty(projectId)) {
throw new ArchivaRestServiceException("artifactId cannot be null", 400, null);
}
RepositorySession repositorySession = null;
try {
repositorySession = repositorySessionFactory.createSession();
} catch (MetadataRepositoryException e) {
e.printStackTrace();
}
try {
ManagedRepositoryContent repository = getManagedRepositoryContent(repositoryId);
ArchivaItemSelector itemSelector = ArchivaItemSelector.builder().withNamespace(groupId).withProjectId(projectId).build();
ContentItem item = repository.getItem(itemSelector);
repository.deleteItem(item);
} catch (ContentNotFoundException e) {
log.warn("skip ContentNotFoundException: {}", e.getMessage());
} catch (RepositoryException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} catch (ItemNotFoundException e) {
log.error("Item not found {}", e.getMessage(), e);
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
}
try {
MetadataRepository metadataRepository = repositorySession.getRepository();
metadataRepository.removeProject(repositorySession, repositoryId, groupId, projectId);
repositorySession.save();
} catch (MetadataRepositoryException | MetadataSessionException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} finally {
repositorySession.close();
}
return ActionStatus.SUCCESS;
}
use of org.apache.archiva.repository.content.ContentNotFoundException in project archiva by apache.
the class CleanupReleasedSnapshotsRepositoryPurge method updateMetadata.
/*
* TODO: Uses a deprecated API, but if we use the API with location string, it does not work as expected
* -> not sure what needs to be changed here.
*/
@SuppressWarnings("deprecation")
private void updateMetadata(Artifact artifact) {
ItemSelector versionRef = ArchivaItemSelector.builder().withNamespace(artifact.getNamespace().getId()).withProjectId(artifact.getId()).withVersion(artifact.getVersion().getId()).build();
ItemSelector projectRef = ArchivaItemSelector.builder().withNamespace(artifact.getNamespace().getId()).withProjectId(artifact.getId()).build();
try {
metadataTools.updateVersionMetadata(repository, versionRef);
} catch (ContentNotFoundException e) {
// Ignore. (Just means we have no snapshot versions left to reference).
} catch (RepositoryMetadataException | IOException | LayoutException e) {
// Ignore.
}
try {
metadataTools.updateProjectMetadata(repository, projectRef);
} catch (ContentNotFoundException | RepositoryMetadataException | IOException | LayoutException e) {
// Ignore. (Just means we have no snapshot versions left to reference).
}
}
use of org.apache.archiva.repository.content.ContentNotFoundException in project archiva by apache.
the class DefaultRepositoriesService method deleteArtifact.
@Override
public ActionStatus deleteArtifact(Artifact artifact) throws ArchivaRestServiceException {
String repositoryId = artifact.getContext();
// so try both!!
if (StringUtils.isEmpty(repositoryId)) {
repositoryId = artifact.getRepositoryId();
}
if (StringUtils.isEmpty(repositoryId)) {
throw new ArchivaRestServiceException("repositoryId cannot be null", 400, null);
}
if (!getPermissionStatus(repositoryId).isAuthorizedToDeleteArtifacts()) {
throw new ArchivaRestServiceException("not authorized to delete artifacts", 403, null);
}
if (artifact == null) {
throw new ArchivaRestServiceException("artifact cannot be null", 400, null);
}
if (StringUtils.isEmpty(artifact.getGroupId())) {
throw new ArchivaRestServiceException("artifact.groupId cannot be null", 400, null);
}
if (StringUtils.isEmpty(artifact.getArtifactId())) {
throw new ArchivaRestServiceException("artifact.artifactId cannot be null", 400, null);
}
// TODO more control on artifact fields
boolean snapshotVersion = VersionUtil.isSnapshot(artifact.getVersion()) | VersionUtil.isGenericSnapshot(artifact.getVersion());
String baseVersion = VersionUtil.getBaseVersion(artifact.getVersion());
RepositorySession repositorySession = null;
try {
repositorySession = repositorySessionFactory.createSession();
} catch (MetadataRepositoryException e) {
e.printStackTrace();
}
try {
Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
TimeZone timezone = TimeZone.getTimeZone("UTC");
DateFormat fmt = new SimpleDateFormat("yyyyMMdd.HHmmss");
fmt.setTimeZone(timezone);
ManagedRepository repo = repositoryRegistry.getManagedRepository(repositoryId);
ManagedRepositoryContent repository = getManagedRepositoryContent(repositoryId);
BaseRepositoryContentLayout layout = repository.getLayout(BaseRepositoryContentLayout.class);
ArchivaItemSelector versionSelector = ArchivaItemSelector.builder().withNamespace(artifact.getGroupId()).withProjectId(artifact.getArtifactId()).withVersion(baseVersion).build();
Version version1 = layout.getVersion(versionSelector);
String path = repository.toPath(version1);
ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace(artifact.getGroupId()).withProjectId(artifact.getArtifactId()).withVersion(baseVersion).withClassifier(artifact.getClassifier()).withArtifactId(artifact.getArtifactId()).withType(artifact.getType()).includeRelatedArtifacts().build();
MetadataRepository metadataRepository = repositorySession.getRepository();
if (StringUtils.isNotBlank(artifact.getClassifier())) {
if (StringUtils.isBlank(artifact.getPackaging())) {
throw new ArchivaRestServiceException("You must configure a type/packaging when using classifier", 400, null);
}
List<? extends org.apache.archiva.repository.content.Artifact> artifactItems = layout.getArtifacts(selector);
for (org.apache.archiva.repository.content.Artifact aRef : artifactItems) {
try {
repository.deleteItem(aRef);
} catch (ItemNotFoundException e) {
log.error("Could not delete item, seems to be deleted by other thread. {}, {} ", aRef, e.getMessage());
}
}
} else {
int index = path.lastIndexOf('/');
path = path.substring(0, index);
StorageAsset targetPath = repo.getAsset(path);
if (!targetPath.exists()) {
// throw new ContentNotFoundException(
// artifact.getNamespace() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() );
log.warn("targetPath {} not found skip file deletion", targetPath);
return ActionStatus.FAIL;
}
// delete from file system
if (!snapshotVersion && version1.exists()) {
try {
repository.deleteItem(version1);
} catch (ItemNotFoundException e) {
log.error("Could not delete version item {}", e.getMessage());
}
} else {
// We are deleting all version related artifacts for a snapshot version
for (org.apache.archiva.repository.content.Artifact delArtifact : layout.getArtifacts(selector)) {
try {
repository.deleteItem(delArtifact);
} catch (ItemNotFoundException e) {
log.warn("Artifact that should be deleted, was not found: {}", delArtifact);
}
}
StorageAsset metadataFile = getMetadata(repo, targetPath.getPath());
ArchivaRepositoryMetadata metadata = getMetadata(repository.getRepository().getType(), metadataFile);
updateMetadata(metadata, metadataFile, lastUpdatedTimestamp, artifact);
}
}
Collection<ArtifactMetadata> artifacts = Collections.emptyList();
if (snapshotVersion) {
artifacts = metadataRepository.getArtifacts(repositorySession, repositoryId, artifact.getGroupId(), artifact.getArtifactId(), baseVersion);
} else {
artifacts = metadataRepository.getArtifacts(repositorySession, repositoryId, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
}
log.debug("artifacts: {}", artifacts);
if (artifacts.isEmpty()) {
if (!snapshotVersion) {
// verify metata repository doesn't contains anymore the version
Collection<String> projectVersions = metadataRepository.getProjectVersions(repositorySession, repositoryId, artifact.getGroupId(), artifact.getArtifactId());
if (projectVersions.contains(artifact.getVersion())) {
log.warn("artifact not found when deleted but version still here ! so force cleanup");
metadataRepository.removeProjectVersion(repositorySession, repositoryId, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
}
}
}
for (ArtifactMetadata artifactMetadata : artifacts) {
// TODO: mismatch between artifact (snapshot) version and project (base) version here
if (artifactMetadata.getVersion().equals(artifact.getVersion())) {
if (StringUtils.isNotBlank(artifact.getClassifier())) {
if (StringUtils.isBlank(artifact.getPackaging())) {
throw new ArchivaRestServiceException("You must configure a type/packaging when using classifier", 400, null);
}
// cleanup facet which contains classifier information
MavenArtifactFacet mavenArtifactFacet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
if (StringUtils.equals(artifact.getClassifier(), mavenArtifactFacet.getClassifier())) {
artifactMetadata.removeFacet(MavenArtifactFacet.FACET_ID);
String groupId = artifact.getGroupId(), artifactId = artifact.getArtifactId(), version = artifact.getVersion();
MavenArtifactFacet mavenArtifactFacetToCompare = new MavenArtifactFacet();
mavenArtifactFacetToCompare.setClassifier(artifact.getClassifier());
metadataRepository.removeFacetFromArtifact(repositorySession, repositoryId, groupId, artifactId, version, mavenArtifactFacetToCompare);
repositorySession.save();
}
} else {
if (snapshotVersion) {
metadataRepository.removeTimestampedArtifact(repositorySession, artifactMetadata, VersionUtil.getBaseVersion(artifact.getVersion()));
} else {
metadataRepository.removeArtifact(repositorySession, artifactMetadata.getRepositoryId(), artifactMetadata.getNamespace(), artifactMetadata.getProject(), artifact.getVersion(), artifactMetadata.getId());
}
}
// repository metadata to an artifact
for (RepositoryListener listener : listeners) {
listener.deleteArtifact(metadataRepository, repository.getId(), artifactMetadata.getNamespace(), artifactMetadata.getProject(), artifactMetadata.getVersion(), artifactMetadata.getId());
}
triggerAuditEvent(repositoryId, path, AuditEvent.REMOVE_FILE);
}
}
} catch (ContentNotFoundException e) {
throw new ArchivaRestServiceException("Artifact does not exist: " + e.getMessage(), 400, e);
} catch (RepositoryNotFoundException e) {
throw new ArchivaRestServiceException("Target repository cannot be found: " + e.getMessage(), 400, e);
} catch (RepositoryException e) {
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} catch (MetadataResolutionException | MetadataSessionException | MetadataRepositoryException | LayoutException e) {
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} finally {
try {
repositorySession.save();
} catch (MetadataSessionException e) {
log.error("Could not save sesion {}", e.getMessage());
}
repositorySession.close();
}
return ActionStatus.SUCCESS;
}
use of org.apache.archiva.repository.content.ContentNotFoundException in project archiva by apache.
the class MetadataTools method updateVersionMetadata.
/**
* Update the metadata based on the following rules.
* <p>
* 1) If this is a SNAPSHOT reference, then utilize the proxy/repository specific
* metadata files to represent the current / latest SNAPSHOT available.
* 2) If this is a RELEASE reference, and the metadata file does not exist, then
* create the metadata file with contents required of the VersionedReference
*
* @param managedRepository the managed repository where the metadata is kept.
* @param reference the versioned reference to update
* @throws LayoutException
* @throws RepositoryMetadataException
* @throws IOException
* @throws ContentNotFoundException
* @deprecated
*/
public void updateVersionMetadata(ManagedRepositoryContent managedRepository, ItemSelector reference) throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException {
StorageAsset metadataFile = managedRepository.getRepository().getAsset(toPath(reference));
ArchivaRepositoryMetadata existingMetadata = readMetadataFile(managedRepository, metadataFile);
long lastUpdated = getExistingLastUpdated(existingMetadata);
ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
metadata.setGroupId(reference.getNamespace());
metadata.setArtifactId(reference.getArtifactId());
if (VersionUtil.isSnapshot(reference.getVersion())) {
// Do SNAPSHOT handling.
metadata.setVersion(VersionUtil.getBaseVersion(reference.getVersion()));
// Gather up all of the versions found in the reference dir, and any
// proxied maven-metadata.xml files.
Set<String> snapshotVersions = gatherSnapshotVersions(managedRepository, reference);
if (snapshotVersions.isEmpty()) {
throw new ContentNotFoundException("No snapshot versions found on reference [" + reference + "].");
}
// sort the list to determine to aide in determining the Latest version.
List<String> sortedVersions = new ArrayList<>();
sortedVersions.addAll(snapshotVersions);
Collections.sort(sortedVersions, new VersionComparator());
String latestVersion = sortedVersions.get(sortedVersions.size() - 1);
if (VersionUtil.isUniqueSnapshot(latestVersion)) {
// The latestVersion will contain the full version string "1.0-alpha-5-20070821.213044-8"
// This needs to be broken down into ${base}-${timestamp}-${build_number}
Matcher m = VersionUtil.UNIQUE_SNAPSHOT_PATTERN.matcher(latestVersion);
if (m.matches()) {
metadata.setSnapshotVersion(new SnapshotVersion());
int buildNumber = NumberUtils.toInt(m.group(3), -1);
metadata.getSnapshotVersion().setBuildNumber(buildNumber);
Matcher mtimestamp = VersionUtil.TIMESTAMP_PATTERN.matcher(m.group(2));
if (mtimestamp.matches()) {
String tsDate = mtimestamp.group(1);
String tsTime = mtimestamp.group(2);
long snapshotLastUpdated = toLastUpdatedLong(tsDate + tsTime);
lastUpdated = Math.max(lastUpdated, snapshotLastUpdated);
metadata.getSnapshotVersion().setTimestamp(m.group(2));
}
}
} else if (VersionUtil.isGenericSnapshot(latestVersion)) {
// The latestVersion ends with the generic version string.
// Example: 1.0-alpha-5-SNAPSHOT
metadata.setSnapshotVersion(new SnapshotVersion());
/* Disabled due to decision in [MRM-535].
* Do not set metadata.lastUpdated to file.lastModified.
*
* Should this be the last updated timestamp of the file, or in the case of an
* archive, the most recent timestamp in the archive?
*
ArtifactReference artifact = getFirstArtifact( managedRepository, reference );
if ( artifact == null )
{
throw new IOException( "Not snapshot artifact found to reference in " + reference );
}
File artifactFile = managedRepository.toFile( artifact );
if ( artifactFile.exists() )
{
Date lastModified = new Date( artifactFile.lastModified() );
metadata.setLastUpdatedTimestamp( lastModified );
}
*/
} else {
throw new RepositoryMetadataException("Unable to process snapshot version <" + latestVersion + "> reference <" + reference + ">");
}
} else {
// Do RELEASE handling.
metadata.setVersion(reference.getVersion());
}
// Set last updated
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.ContentNotFoundException 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