use of org.apache.archiva.repository.ManagedRepositoryContent in project archiva by apache.
the class DefaultRepositoriesService method removeProjectVersion.
@Override
public Boolean 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 (!isAuthorizedToDeleteArtifacts(repositoryId)) {
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 = repositorySessionFactory.createSession();
try {
ManagedRepositoryContent repository = getManagedRepositoryContent(repositoryId);
VersionedReference ref = new VersionedReference();
ref.setArtifactId(projectId);
ref.setGroupId(namespace);
ref.setVersion(version);
repository.deleteVersion(ref);
/*
ProjectReference projectReference = new ProjectReference();
projectReference.setGroupId( namespace );
projectReference.setArtifactId( projectId );
repository.getVersions( )
*/
ArtifactReference artifactReference = new ArtifactReference();
artifactReference.setGroupId(namespace);
artifactReference.setArtifactId(projectId);
artifactReference.setVersion(version);
MetadataRepository metadataRepository = repositorySession.getRepository();
Set<ArtifactReference> related = repository.getRelatedArtifacts(artifactReference);
log.debug("related: {}", related);
for (ArtifactReference artifactRef : related) {
repository.deleteArtifact(artifactRef);
}
Collection<ArtifactMetadata> artifacts = metadataRepository.getArtifacts(repositoryId, namespace, projectId, version);
for (ArtifactMetadata artifactMetadata : artifacts) {
metadataRepository.removeArtifact(artifactMetadata, version);
}
metadataRepository.removeProjectVersion(repositoryId, namespace, projectId, version);
} catch (MetadataRepositoryException e) {
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} catch (MetadataResolutionException e) {
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} catch (RepositoryException e) {
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} finally {
repositorySession.save();
repositorySession.close();
}
return Boolean.TRUE;
}
use of org.apache.archiva.repository.ManagedRepositoryContent in project archiva by apache.
the class DefaultRepositoriesService method deleteGroupId.
@Override
public Boolean deleteGroupId(String groupId, String repositoryId) throws ArchivaRestServiceException {
if (StringUtils.isEmpty(repositoryId)) {
throw new ArchivaRestServiceException("repositoryId cannot be null", 400, null);
}
if (!isAuthorizedToDeleteArtifacts(repositoryId)) {
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 = repositorySessionFactory.createSession();
try {
ManagedRepositoryContent repository = getManagedRepositoryContent(repositoryId);
repository.deleteGroupId(groupId);
MetadataRepository metadataRepository = repositorySession.getRepository();
metadataRepository.removeNamespace(repositoryId, groupId);
// just invalidate cache entry
String cacheKey = repositoryId + "-" + groupId;
namespacesCache.remove(cacheKey);
namespacesCache.remove(repositoryId);
metadataRepository.save();
} catch (MetadataRepositoryException 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);
} finally {
repositorySession.close();
}
return true;
}
use of org.apache.archiva.repository.ManagedRepositoryContent in project archiva by apache.
the class DefaultBrowseService method getArtifactContentEntries.
@Override
public List<ArtifactContentEntry> getArtifactContentEntries(String groupId, String artifactId, String version, String classifier, String type, String path, String repositoryId) throws ArchivaRestServiceException {
List<String> selectedRepos = getSelectedRepos(repositoryId);
try {
for (String repoId : selectedRepos) {
ManagedRepositoryContent managedRepositoryContent = getManagedRepositoryContent(repoId);
ArchivaArtifact archivaArtifact = new ArchivaArtifact(groupId, artifactId, version, classifier, StringUtils.isEmpty(type) ? "jar" : type, repoId);
Path file = managedRepositoryContent.toFile(archivaArtifact);
if (Files.exists(file)) {
return readFileEntries(file, path, repoId);
}
}
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
} catch (RepositoryNotFoundException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
} catch (RepositoryException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
}
return Collections.emptyList();
}
use of org.apache.archiva.repository.ManagedRepositoryContent in project archiva by apache.
the class DefaultBrowseService method getArtifactContentText.
@Override
public ArtifactContent getArtifactContentText(String groupId, String artifactId, String version, String classifier, String type, String path, String repositoryId) throws ArchivaRestServiceException {
List<String> selectedRepos = getSelectedRepos(repositoryId);
try {
for (String repoId : selectedRepos) {
ManagedRepositoryContent managedRepositoryContent = null;
try {
managedRepositoryContent = getManagedRepositoryContent(repoId);
} catch (RepositoryException e) {
log.error("No repository content found for " + repoId);
continue;
}
ArchivaArtifact archivaArtifact = new ArchivaArtifact(groupId, artifactId, version, classifier, StringUtils.isEmpty(type) ? "jar" : type, repoId);
Path file = managedRepositoryContent.toFile(archivaArtifact);
if (!Files.exists(file)) {
log.debug("file: {} not exists for repository: {} try next repository", file, repoId);
continue;
}
if (StringUtils.isNotBlank(path)) {
// zip entry of the path -> path must a real file entry of the archive
JarFile jarFile = new JarFile(file.toFile());
ZipEntry zipEntry = jarFile.getEntry(path);
try (InputStream inputStream = jarFile.getInputStream(zipEntry)) {
return new ArtifactContent(IOUtils.toString(inputStream, ARTIFACT_CONTENT_ENCODING), repoId);
} finally {
closeQuietly(jarFile);
}
}
return new ArtifactContent(new String(Files.readAllBytes(file), ARTIFACT_CONTENT_ENCODING), repoId);
}
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
}
log.debug("artifact: {}:{}:{}:{}:{} not found", groupId, artifactId, version, classifier, type);
// 404 ?
return new ArtifactContent();
}
use of org.apache.archiva.repository.ManagedRepositoryContent in project archiva by apache.
the class MetadataToolsTest method assertUpdatedSnapshotVersionMetadata.
private void assertUpdatedSnapshotVersionMetadata(String artifactId, String version, String expectedDate, String expectedTime, String expectedBuildNumber) throws Exception {
ManagedRepositoryContent testRepo = createTestRepoContent();
VersionedReference reference = new VersionedReference();
reference.setGroupId("org.apache.archiva.metadata.tests");
reference.setArtifactId(artifactId);
reference.setVersion(version);
prepTestRepo(testRepo, reference);
tools.updateMetadata(testRepo, reference);
StringBuilder buf = new StringBuilder();
buf.append("<metadata>\n");
buf.append(" <groupId>").append(reference.getGroupId()).append("</groupId>\n");
buf.append(" <artifactId>").append(reference.getArtifactId()).append("</artifactId>\n");
buf.append(" <version>").append(reference.getVersion()).append("</version>\n");
buf.append(" <versioning>\n");
buf.append(" <snapshot>\n");
buf.append(" <buildNumber>").append(expectedBuildNumber).append("</buildNumber>\n");
buf.append(" <timestamp>");
buf.append(expectedDate).append(".").append(expectedTime);
buf.append("</timestamp>\n");
buf.append(" </snapshot>\n");
buf.append(" <lastUpdated>").append(expectedDate).append(expectedTime).append("</lastUpdated>\n");
buf.append(" </versioning>\n");
buf.append("</metadata>");
assertMetadata(buf.toString(), testRepo, reference);
}
Aggregations