use of org.apache.archiva.metadata.repository.MetadataSessionException 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.metadata.repository.MetadataSessionException in project archiva by apache.
the class DefaultManagedRepositoryAdmin method deleteManagedRepository.
private Boolean deleteManagedRepository(ManagedRepositoryConfiguration repository, boolean deleteContent, Configuration config, boolean stagedOne) throws RepositoryAdminException {
if (!stagedOne) {
boolean success = false;
try (RepositorySession repositorySession = getRepositorySessionFactory().createSession()) {
MetadataRepository metadataRepository = repositorySession.getRepository();
metadataRepository.removeRepository(repositorySession, repository.getId());
// invalidate cache
namespacesCache.remove(repository.getId());
repositorySession.save();
success = true;
} catch (MetadataRepositoryException e) {
// throw new RepositoryAdminException( e.getMessage(), e );
log.warn("skip error during removing repository from MetadataRepository:{}", e.getMessage(), e);
success = false;
} catch (MetadataSessionException e) {
log.warn("skip error during removing repository from MetadataRepository:{}", e.getMessage(), e);
success = false;
}
if (success) {
log.debug("call repositoryStatisticsManager.deleteStatistics");
try {
getRepositoryStatisticsManager().deleteStatistics(repository.getId());
} catch (MetadataRepositoryException e) {
e.printStackTrace();
}
}
}
// olamy: copy list for reading as a unit test in webapp fail with ConcurrentModificationException
List<ProxyConnectorConfiguration> proxyConnectors = new ArrayList<>(config.getProxyConnectors());
for (ProxyConnectorConfiguration proxyConnector : proxyConnectors) {
if (StringUtils.equals(proxyConnector.getSourceRepoId(), repository.getId())) {
config.removeProxyConnector(proxyConnector);
}
}
try {
removeRepositoryRoles(repository);
} catch (RoleManagerException e) {
throw new RepositoryAdminException("fail to remove repository roles for repository " + repository.getId() + " : " + e.getMessage(), e);
}
try {
org.apache.archiva.repository.ManagedRepository repo = repositoryRegistry.getManagedRepository(repository.getId());
if (repo != null) {
repositoryRegistry.removeRepository(repo, config);
if (deleteContent) {
// TODO could be async ? as directory can be huge
Path dir = Paths.get(repository.getLocation());
org.apache.archiva.common.utils.FileUtils.deleteQuietly(dir);
}
}
} catch (RepositoryException e) {
throw new RepositoryAdminException("Removal of repository " + repository.getId() + " failed: " + e.getMessage());
}
saveConfiguration(config);
return Boolean.TRUE;
}
use of org.apache.archiva.metadata.repository.MetadataSessionException 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.metadata.repository.MetadataSessionException in project archiva by apache.
the class DefaultRepositoriesService method deleteGroupId.
@Override
public ActionStatus deleteGroupId(String groupId, 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);
}
RepositorySession repositorySession = null;
try {
repositorySession = repositorySessionFactory.createSession();
} catch (MetadataRepositoryException e) {
e.printStackTrace();
}
try {
ManagedRepositoryContent repository = getManagedRepositoryContent(repositoryId);
ArchivaItemSelector itemselector = ArchivaItemSelector.builder().withNamespace(groupId).build();
ContentItem item = repository.getItem(itemselector);
repository.deleteItem(item);
MetadataRepository metadataRepository = repositorySession.getRepository();
metadataRepository.removeNamespace(repositorySession, repositoryId, groupId);
// just invalidate cache entry
String cacheKey = repositoryId + "-" + groupId;
namespacesCache.remove(cacheKey);
namespacesCache.remove(repositoryId);
repositorySession.save();
} catch (MetadataRepositoryException | MetadataSessionException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} 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);
} finally {
repositorySession.close();
}
return ActionStatus.SUCCESS;
}
use of org.apache.archiva.metadata.repository.MetadataSessionException in project archiva by apache.
the class DefaultRepositoriesService method removeProjectVersion.
@Override
public ActionStatus removeProjectVersion(String repositoryId, String namespace, String projectId, String version) throws ArchivaRestServiceException {
// if not a generic we can use the standard way to delete artifact
if (!VersionUtil.isGenericSnapshot(version)) {
Artifact artifact = new Artifact(namespace, projectId, version);
artifact.setRepositoryId(repositoryId);
artifact.setContext(repositoryId);
return deleteArtifact(artifact);
}
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(namespace)) {
throw new ArchivaRestServiceException("groupId cannot be null", 400, null);
}
if (StringUtils.isEmpty(projectId)) {
throw new ArchivaRestServiceException("artifactId cannot be null", 400, null);
}
if (StringUtils.isEmpty(version)) {
throw new ArchivaRestServiceException("version cannot be null", 400, null);
}
RepositorySession repositorySession = null;
try {
repositorySession = repositorySessionFactory.createSession();
} catch (MetadataRepositoryException e) {
e.printStackTrace();
}
try {
ManagedRepositoryContent repository = getManagedRepositoryContent(repositoryId);
BaseRepositoryContentLayout layout = repository.getLayout(BaseRepositoryContentLayout.class);
ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace(namespace).withProjectId(projectId).withVersion(version).build();
Version versionItem = layout.getVersion(selector);
if (versionItem != null && versionItem.exists()) {
repository.deleteItem(versionItem);
}
MetadataRepository metadataRepository = repositorySession.getRepository();
Collection<ArtifactMetadata> artifacts = metadataRepository.getArtifacts(repositorySession, repositoryId, namespace, projectId, version);
for (ArtifactMetadata artifactMetadata : artifacts) {
metadataRepository.removeTimestampedArtifact(repositorySession, artifactMetadata, version);
}
metadataRepository.removeProjectVersion(repositorySession, repositoryId, namespace, projectId, version);
} catch (MetadataRepositoryException | MetadataResolutionException | RepositoryException | ItemNotFoundException | LayoutException e) {
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} finally {
try {
repositorySession.save();
} catch (MetadataSessionException e) {
log.error("Session save failed {}", e.getMessage());
}
repositorySession.close();
}
return ActionStatus.SUCCESS;
}
Aggregations