Search in sources :

Example 31 with ManagedRepositoryContent

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;
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) Artifact(org.apache.archiva.maven2.model.Artifact) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) VersionedReference(org.apache.archiva.model.VersionedReference) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ArtifactReference(org.apache.archiva.model.ArtifactReference) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata)

Example 32 with ManagedRepositoryContent

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;
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession)

Example 33 with ManagedRepositoryContent

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();
}
Also used : Path(java.nio.file.Path) ArchivaArtifact(org.apache.archiva.model.ArchivaArtifact) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) IOException(java.io.IOException) RepositoryNotFoundException(org.apache.archiva.repository.RepositoryNotFoundException)

Example 34 with ManagedRepositoryContent

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();
}
Also used : Path(java.nio.file.Path) ArtifactContent(org.apache.archiva.rest.api.model.ArtifactContent) ArchivaArtifact(org.apache.archiva.model.ArchivaArtifact) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) IOException(java.io.IOException) JarFile(java.util.jar.JarFile)

Example 35 with ManagedRepositoryContent

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);
}
Also used : VersionedReference(org.apache.archiva.model.VersionedReference) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent)

Aggregations

ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)41 Path (java.nio.file.Path)16 Test (org.junit.Test)13 RepositoryException (org.apache.archiva.repository.RepositoryException)12 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)11 DavException (org.apache.jackrabbit.webdav.DavException)10 ArrayList (java.util.ArrayList)9 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)9 IOException (java.io.IOException)8 ArtifactReference (org.apache.archiva.model.ArtifactReference)8 VersionedReference (org.apache.archiva.model.VersionedReference)7 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)6 LayoutException (org.apache.archiva.repository.LayoutException)6 RepositoryContentProvider (org.apache.archiva.repository.RepositoryContentProvider)6 DavResourceLocator (org.apache.jackrabbit.webdav.DavResourceLocator)6 ManagedRepository (org.apache.archiva.admin.model.beans.ManagedRepository)5 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)5 ArchivaRepositoryMetadata (org.apache.archiva.model.ArchivaRepositoryMetadata)5 ManagedRepository (org.apache.archiva.repository.ManagedRepository)5 MavenManagedRepository (org.apache.archiva.repository.maven2.MavenManagedRepository)5