Search in sources :

Example 6 with ArchivaItemSelector

use of org.apache.archiva.repository.content.base.ArchivaItemSelector in project archiva by apache.

the class ManagedDefaultRepositoryContentTest method testAddArtifact.

@Test
public void testAddArtifact() throws IOException, URISyntaxException, LayoutException {
    ManagedRepository repo = createManagedRepoWithContent("delete-repository");
    ManagedRepositoryContent myRepoContent = repo.getContent();
    BaseRepositoryContentLayout layout = myRepoContent.getLayout(BaseRepositoryContentLayout.class);
    Path repoRoot = repo.getRoot().getFilePath();
    Path tmpFile = Files.createTempFile("archiva-mvn-repotest", "jar");
    try (OutputStream outputStream = Files.newOutputStream(tmpFile)) {
        for (int i = 0; i < 255; i++) {
            outputStream.write("test.test.test\n".getBytes(Charset.forName("UTF-8")));
        }
    }
    Path file = repoRoot.resolve("org/apache/maven/samplejar/2.0/samplejar-2.0.jar");
    FileTime lmt = Files.getLastModifiedTime(file);
    ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace("org.apache.maven").withProjectId("samplejar").withVersion("2.0").withArtifactId("samplejar").withArtifactVersion("2.0").withExtension("jar").build();
    Artifact artifact = layout.getArtifact(selector);
    layout.addArtifact(tmpFile, artifact);
    FileTime lmtAfter = Files.getLastModifiedTime(file);
    assertNotEquals(lmtAfter, lmt);
    Reader ln = Files.newBufferedReader(file, Charset.forName("UTF-8"));
    char[] content = new char[50];
    ln.read(content);
    assertTrue(new String(content).startsWith("test.test.test"));
    tmpFile = Files.createTempFile("archiva-mvn-repotest", "jar");
    try (OutputStream outputStream = Files.newOutputStream(tmpFile)) {
        for (int i = 0; i < 255; i++) {
            outputStream.write("test.test.test\n".getBytes(Charset.forName("UTF-8")));
        }
    }
    file = repoRoot.resolve("org/apache/maven/samplejar/2.0/samplejar-2.0.test");
    assertFalse(Files.exists(file));
    assertTrue(Files.exists(tmpFile));
    selector = ArchivaItemSelector.builder().withNamespace("org.apache.maven").withProjectId("samplejar").withVersion("2.0").withArtifactId("samplejar").withArtifactVersion("2.0").withExtension("test").build();
    artifact = layout.getArtifact(selector);
    layout.addArtifact(tmpFile, artifact);
    ln = Files.newBufferedReader(file, Charset.forName("UTF-8"));
    ln.read(content);
    assertTrue(new String(content).startsWith("test.test.test"));
}
Also used : Path(java.nio.file.Path) ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) MavenManagedRepository(org.apache.archiva.maven.repository.MavenManagedRepository) ManagedRepository(org.apache.archiva.repository.ManagedRepository) EditableManagedRepository(org.apache.archiva.repository.EditableManagedRepository) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) OutputStream(java.io.OutputStream) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) MavenMetadataReader(org.apache.archiva.maven.metadata.MavenMetadataReader) Reader(java.io.Reader) FileTime(java.nio.file.attribute.FileTime) Artifact(org.apache.archiva.repository.content.Artifact) Test(org.junit.Test)

Example 7 with ArchivaItemSelector

use of org.apache.archiva.repository.content.base.ArchivaItemSelector in project archiva by apache.

the class ManagedDefaultRepositoryContentTest method deleteNamespaceItem.

@Test
public void deleteNamespaceItem() throws IOException, URISyntaxException, ItemNotFoundException {
    ManagedRepository repo = createManagedRepoWithContent("delete-repository");
    ManagedRepositoryContent myRepoContent = repo.getContent();
    Path repoRoot = repo.getRoot().getFilePath();
    assertTrue(Files.exists(repoRoot.resolve("org/apache/maven")));
    ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace("org.apache.maven").build();
    ContentItem item = myRepoContent.getItem(selector);
    assertTrue(item instanceof Namespace);
    myRepoContent.deleteItem(item);
    assertFalse(Files.exists(repoRoot.resolve("org/apache/maven")));
    assertTrue(Files.exists(repoRoot.resolve("org/apache")));
    // Sub namespaces are deleted too
    assertTrue(Files.exists(repoRoot.resolve("org/apache/test/samplejar")));
    assertTrue(Files.exists(repoRoot.resolve("org/apache/test/sub/samplejar")));
    selector = ArchivaItemSelector.builder().withNamespace("org.apache.test").build();
    item = myRepoContent.getItem(selector);
    assertTrue(item instanceof Namespace);
    myRepoContent.deleteItem(item);
    assertFalse(Files.exists(repoRoot.resolve("org/apache/test/samplejar")));
    assertFalse(Files.exists(repoRoot.resolve("org/apache/test/sub/samplejar")));
}
Also used : Path(java.nio.file.Path) ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) MavenManagedRepository(org.apache.archiva.maven.repository.MavenManagedRepository) ManagedRepository(org.apache.archiva.repository.ManagedRepository) EditableManagedRepository(org.apache.archiva.repository.EditableManagedRepository) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ContentItem(org.apache.archiva.repository.content.ContentItem) ArchivaContentItem(org.apache.archiva.repository.content.base.ArchivaContentItem) Namespace(org.apache.archiva.repository.content.Namespace) Test(org.junit.Test)

Example 8 with ArchivaItemSelector

use of org.apache.archiva.repository.content.base.ArchivaItemSelector 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;
            }
            ArchivaItemSelector itemSelector = ArchivaItemSelector.builder().withNamespace(groupId).withProjectId(artifactId).withVersion(version).withClassifier(classifier).withType(StringUtils.isEmpty(type) ? "jar" : type).withArtifactId(artifactId).build();
            org.apache.archiva.repository.content.Artifact archivaArtifact = managedRepositoryContent.getItem(itemSelector).adapt(org.apache.archiva.repository.content.Artifact.class);
            StorageAsset file = archivaArtifact.getAsset();
            if (!file.exists()) {
                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
                FsStorageUtil.PathInformation pathInfo = FsStorageUtil.getAssetDataAsPath(file);
                JarFile jarFile = new JarFile(pathInfo.getPath().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);
                    if (pathInfo.isTmpFile()) {
                        Files.deleteIfExists(pathInfo.getPath());
                    }
                }
            }
            try (InputStream readStream = file.getReadStream()) {
                return new ArtifactContent(IOUtils.toString(readStream, 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 : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) InputStream(java.io.InputStream) FsStorageUtil(org.apache.archiva.repository.storage.fs.FsStorageUtil) ZipEntry(java.util.zip.ZipEntry) RepositoryException(org.apache.archiva.repository.RepositoryException) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent)

Example 9 with ArchivaItemSelector

use of org.apache.archiva.repository.content.base.ArchivaItemSelector 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);
            ArchivaItemSelector itemSelector = ArchivaItemSelector.builder().withNamespace(groupId).withProjectId(artifactId).withVersion(version).withClassifier(classifier).withType(StringUtils.isEmpty(type) ? "jar" : type).withArtifactId(artifactId).build();
            org.apache.archiva.repository.content.Artifact archivaArtifact = managedRepositoryContent.getItem(itemSelector).adapt(org.apache.archiva.repository.content.Artifact.class);
            StorageAsset file = archivaArtifact.getAsset();
            if (file.exists()) {
                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 : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) RepositoryException(org.apache.archiva.repository.RepositoryException) IOException(java.io.IOException) RepositoryNotFoundException(org.apache.archiva.repository.RepositoryNotFoundException) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent)

Example 10 with ArchivaItemSelector

use of org.apache.archiva.repository.content.base.ArchivaItemSelector 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;
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) 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) MetadataSessionException(org.apache.archiva.metadata.repository.MetadataSessionException) ContentItem(org.apache.archiva.repository.content.ContentItem) ItemNotFoundException(org.apache.archiva.repository.content.ItemNotFoundException)

Aggregations

ArchivaItemSelector (org.apache.archiva.repository.content.base.ArchivaItemSelector)19 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)14 ContentItem (org.apache.archiva.repository.content.ContentItem)10 Test (org.junit.Test)8 ManagedRepository (org.apache.archiva.repository.ManagedRepository)7 RepositoryException (org.apache.archiva.repository.RepositoryException)7 Artifact (org.apache.archiva.repository.content.Artifact)7 BaseRepositoryContentLayout (org.apache.archiva.repository.content.BaseRepositoryContentLayout)7 Version (org.apache.archiva.repository.content.Version)7 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)7 Path (java.nio.file.Path)6 MavenManagedRepository (org.apache.archiva.maven.repository.MavenManagedRepository)6 EditableManagedRepository (org.apache.archiva.repository.EditableManagedRepository)6 ItemNotFoundException (org.apache.archiva.repository.content.ItemNotFoundException)6 LayoutException (org.apache.archiva.repository.content.LayoutException)6 StorageAsset (org.apache.archiva.repository.storage.StorageAsset)6 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)5 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)5 MetadataSessionException (org.apache.archiva.metadata.repository.MetadataSessionException)4 RepositorySession (org.apache.archiva.metadata.repository.RepositorySession)4