Search in sources :

Example 11 with ContentItem

use of org.apache.archiva.repository.content.ContentItem in project archiva by apache.

the class ContentItemTest method testWithAttribute.

@Test
void testWithAttribute() {
    ContentItem test = getBuilder().withAttribute("testkey1", "testvalue1").withAttribute("testkey2", "testvalue2").build();
    assertNotNull(test.getAttributes());
    assertEquals(2, test.getAttributes().size());
    assertEquals("testvalue1", test.getAttribute("testkey1"));
    assertEquals("testvalue2", test.getAttribute("testkey2"));
    assertNull(test.getAttribute("key123"));
}
Also used : ContentItem(org.apache.archiva.repository.content.ContentItem) Test(org.junit.jupiter.api.Test)

Example 12 with ContentItem

use of org.apache.archiva.repository.content.ContentItem in project archiva by apache.

the class ManagedDefaultRepositoryContent method newItemStream.

@Override
public Stream<? extends ContentItem> newItemStream(ItemSelector selector, boolean parallel) throws ContentAccessException, IllegalArgumentException {
    final Predicate<StorageAsset> filter = getItemFileFilterFromSelector(selector);
    StorageAsset startDir;
    if (selector.getNamespace().contains("*")) {
        startDir = getAsset("");
    } else if (selector.hasProjectId() && selector.getProjectId().contains("*")) {
        startDir = getAsset(selector.getNamespace());
    } else if (selector.hasProjectId() && selector.hasVersion() && selector.getVersion().contains("*")) {
        startDir = getAsset(selector.getNamespace(), selector.getProjectId());
    } else if (selector.hasProjectId() && selector.hasVersion()) {
        startDir = getAsset(selector.getNamespace(), selector.getProjectId(), selector.getVersion());
    } else if (selector.hasProjectId()) {
        startDir = getAsset(selector.getNamespace(), selector.getProjectId());
    } else {
        startDir = getAsset(selector.getNamespace());
        if (!selector.recurse()) {
            // We descend into 2 subdirectories (project and version)
            return startDir.list().stream().flatMap(a -> getChildrenDF(a, 1)).map(this::getItemFromPath);
        }
    }
    return StorageUtil.newAssetStream(startDir, parallel).filter(filter).map(this::getItemFromPath);
}
Also used : Arrays(java.util.Arrays) ArchivaNamespace(org.apache.archiva.repository.content.base.ArchivaNamespace) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) Namespace(org.apache.archiva.repository.content.Namespace) StringUtils(org.apache.commons.lang3.StringUtils) LayoutRuntimeException(org.apache.archiva.repository.content.LayoutRuntimeException) Matcher(java.util.regex.Matcher) FileUtils(org.apache.archiva.common.utils.FileUtils) MavenMetadataReader(org.apache.archiva.maven.metadata.MavenMetadataReader) ItemSelector(org.apache.archiva.repository.content.ItemSelector) Path(java.nio.file.Path) ContentItem(org.apache.archiva.repository.content.ContentItem) RepositoryStorage(org.apache.archiva.repository.storage.RepositoryStorage) FileTypes(org.apache.archiva.configuration.provider.FileTypes) ArtifactOptBuilder(org.apache.archiva.repository.content.base.builder.ArtifactOptBuilder) ArchivaContentItem(org.apache.archiva.repository.content.base.ArchivaContentItem) Predicate(java.util.function.Predicate) BaseArtifactTypes(org.apache.archiva.repository.content.BaseArtifactTypes) Collectors(java.util.stream.Collectors) ManagedRepository(org.apache.archiva.repository.ManagedRepository) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) Pattern(java.util.regex.Pattern) EditableManagedRepository(org.apache.archiva.repository.EditableManagedRepository) Artifact(org.apache.archiva.repository.content.Artifact) ItemDeleteStatus(org.apache.archiva.repository.ItemDeleteStatus) ArchivaProject(org.apache.archiva.repository.content.base.ArchivaProject) ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) Version(org.apache.archiva.repository.content.Version) ReferenceMap(org.apache.commons.collections4.map.ReferenceMap) Inject(javax.inject.Inject) Named(javax.inject.Named) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ContentAccessException(org.apache.archiva.repository.content.ContentAccessException) Project(org.apache.archiva.repository.content.Project) Files(java.nio.file.Files) FileLockManager(org.apache.archiva.common.filelock.FileLockManager) VersionUtil(org.apache.archiva.common.utils.VersionUtil) ManagedRepositoryContentLayout(org.apache.archiva.repository.content.ManagedRepositoryContentLayout) IOException(java.io.IOException) DataItem(org.apache.archiva.repository.content.DataItem) Consumer(java.util.function.Consumer) StorageUtil(org.apache.archiva.repository.storage.util.StorageUtil) ArchivaVersion(org.apache.archiva.repository.content.base.ArchivaVersion) LayoutException(org.apache.archiva.repository.content.LayoutException) Collections(java.util.Collections) ItemNotFoundException(org.apache.archiva.repository.content.ItemNotFoundException) StorageAsset(org.apache.archiva.repository.storage.StorageAsset)

Example 13 with ContentItem

use of org.apache.archiva.repository.content.ContentItem in project archiva by apache.

the class MetadataTransferTest method testGetProjectMetadataProxiedNotLocalOnRemoteConnectoDisabled.

@Test
public void testGetProjectMetadataProxiedNotLocalOnRemoteConnectoDisabled() throws Exception {
    // New project metadata that does not exist locally but exists on remote.
    String requestedResource = "org/apache/maven/test/get-found-in-proxy/maven-metadata.xml";
    setupTestableManagedRepository(requestedResource);
    // Configure Connector (usually done within archiva.xml configuration)
    saveConnector(ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, true);
    assertResourceNotFound(requestedResource);
    assertNoRepoMetadata(ID_PROXIED1, requestedResource);
    Path expectedFile = managedDefaultDir.resolve(requestedResource);
    BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout(BaseRepositoryContentLayout.class);
    ContentItem metaItem = managedDefaultRepository.toItem(requestedResource);
    Project project = layout.adaptItem(Project.class, managedDefaultRepository.getParent(metaItem));
    assertNotNull(project);
    String metaPath = managedDefaultRepository.toPath(layout.getMetadataItem(project));
    StorageAsset downloadedFile = proxyHandler.fetchMetadataFromProxies(managedDefaultRepository.getRepository(), metaPath).getFile();
    assertNull("Should not have downloaded a file.", downloadedFile);
    assertNoTempFiles(expectedFile);
}
Also used : Path(java.nio.file.Path) Project(org.apache.archiva.repository.content.Project) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ContentItem(org.apache.archiva.repository.content.ContentItem) Test(org.junit.Test)

Example 14 with ContentItem

use of org.apache.archiva.repository.content.ContentItem 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)

Example 15 with ContentItem

use of org.apache.archiva.repository.content.ContentItem in project archiva by apache.

the class ArchivaDavResourceFactory method processRepository.

private DavResource processRepository(final DavServletRequest request, ArchivaDavResourceLocator archivaLocator, String activePrincipal, ManagedRepositoryContent managedRepositoryContent, org.apache.archiva.repository.ManagedRepository managedRepository) throws DavException {
    DavResource resource = null;
    if (isAuthorized(request, managedRepositoryContent.getId())) {
        boolean readMethod = WebdavMethodUtil.isReadMethod(request.getMethod());
        // Maven Centric part ask evaluation if -SNAPSHOT
        // MRM-1846 test if read method to prevent issue with maven 2.2.1 and uniqueVersion false
        String path = readMethod ? evaluatePathWithVersion(archivaLocator, managedRepositoryContent, request.getContextPath()) : getLogicalResource(archivaLocator, managedRepository, false);
        if (path.startsWith("/")) {
            path = path.substring(1);
        }
        LogicalResource logicalResource = new LogicalResource(path);
        StorageAsset repoAsset = managedRepository.getAsset(path);
        // Path resourceFile = Paths.get( managedRepositoryContent.getRepoRoot(), path );
        try {
            resource = new ArchivaDavResource(repoAsset, path, managedRepository, request.getRemoteAddr(), activePrincipal, request.getDavSession(), archivaLocator, this, mimeTypes, auditListeners, scheduler);
        } catch (LayoutException e) {
            log.error("Incompatible layout: {}", e.getMessage(), e);
            throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
        }
        if (WebdavMethodUtil.isReadMethod(request.getMethod())) {
            if (archivaLocator.getHref(false).endsWith("/") && !repoAsset.isContainer()) {
                // force a resource not found
                throw new DavException(HttpServletResponse.SC_NOT_FOUND, "Resource does not exist");
            } else {
                if (!resource.isCollection()) {
                    boolean previouslyExisted = repoAsset.exists();
                    boolean fromProxy = fetchContentFromProxies(managedRepository, request, logicalResource);
                    StorageAsset resourceAsset = null;
                    // legacy layout format.
                    try {
                        // Perform an adjustment of the resource to the managed
                        // repository expected path.
                        // String localResourcePath = managedRepository.getRequestInfo().toNativePath( logicalResource.getPath() );
                        resourceAsset = managedRepository.getAsset(logicalResource.getPath());
                        resource = new ArchivaDavResource(resourceAsset, logicalResource.getPath(), managedRepository, request.getRemoteAddr(), activePrincipal, request.getDavSession(), archivaLocator, this, mimeTypes, auditListeners, scheduler);
                    } catch (LayoutException e) {
                        if (resourceAsset == null || !resourceAsset.exists()) {
                            throw new DavException(HttpServletResponse.SC_NOT_FOUND, e);
                        }
                    }
                    if (fromProxy) {
                        String action = (previouslyExisted ? AuditEvent.MODIFY_FILE : AuditEvent.CREATE_FILE) + PROXIED_SUFFIX;
                        log.debug("Proxied artifact '{}' in repository '{}' (current user '{}')", resourceAsset.getName(), managedRepositoryContent.getId(), activePrincipal);
                        triggerAuditEvent(request.getRemoteAddr(), archivaLocator.getRepositoryId(), logicalResource.getPath(), action, activePrincipal);
                    }
                    if (!resourceAsset.exists()) {
                        throw new DavException(HttpServletResponse.SC_NOT_FOUND, "Resource does not exist");
                    }
                }
            }
        }
        if (request.getMethod().equals(HTTP_PUT_METHOD)) {
            String resourcePath = logicalResource.getPath();
            RepositoryRequestInfo repositoryRequestInfo = managedRepository.getRequestInfo();
            // we suppose that release-artifacts can be deployed only to repos enabled for releases
            if (managedRepositoryContent.getRepository().getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) && !repositoryRequestInfo.isMetadata(resourcePath) && !repositoryRequestInfo.isSupportFile(resourcePath)) {
                // ArtifactReference artifact = null;
                Artifact artifact = null;
                try {
                    BaseRepositoryContentLayout layout = managedRepositoryContent.getLayout(BaseRepositoryContentLayout.class);
                    ContentItem artifactItem = managedRepositoryContent.toItem(resourcePath);
                    artifact = layout.adaptItem(Artifact.class, artifactItem);
                    if (!VersionUtil.isSnapshot(artifact.getVersion().getId())) {
                        // check if artifact already exists and if artifact re-deployment to the repository is allowed
                        if (artifactItem.exists() && managedRepositoryContent.getRepository().blocksRedeployments()) {
                            log.warn("Overwriting released artifacts in repository '{}' is not allowed.", managedRepositoryContent.getId());
                            throw new DavException(HttpServletResponse.SC_CONFLICT, "Overwriting released artifacts is not allowed.");
                        }
                    }
                } catch (LayoutException e) {
                    log.warn("Artifact path '{}' is invalid.", resourcePath);
                } catch (ContentAccessException e) {
                    e.printStackTrace();
                }
            }
            /*
                 * Create parent directories that don't exist when writing a file This actually makes this
                 * implementation not compliant to the WebDAV RFC - but we have enough knowledge about how the
                 * collection is being used to do this reasonably and some versions of Maven's WebDAV don't correctly
                 * create the collections themselves.
                 */
            StorageAsset rootDirectory = managedRepositoryContent.getRepository().getRoot();
            StorageAsset destDir = rootDirectory.resolve(logicalResource.getPath()).getParent();
            if (!destDir.exists()) {
                try {
                    destDir.create();
                } catch (IOException e) {
                    log.error("Could not create directory {}: {}", destDir, e.getMessage(), e);
                    throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not create directory " + destDir);
                }
                String relPath = PathUtil.getRelative(rootDirectory.getPath(), destDir.getPath());
                log.debug("Creating destination directory '{}' (current user '{}')", destDir.getName(), activePrincipal);
                triggerAuditEvent(request.getRemoteAddr(), managedRepositoryContent.getId(), relPath, AuditEvent.CREATE_DIR, activePrincipal);
            }
        }
    }
    return resource;
}
Also used : DavResource(org.apache.jackrabbit.webdav.DavResource) DavException(org.apache.jackrabbit.webdav.DavException) IOException(java.io.IOException) RepositoryRequestInfo(org.apache.archiva.repository.RepositoryRequestInfo) Artifact(org.apache.archiva.repository.content.Artifact) LayoutException(org.apache.archiva.repository.content.LayoutException) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ContentItem(org.apache.archiva.repository.content.ContentItem) ContentAccessException(org.apache.archiva.repository.content.ContentAccessException)

Aggregations

ContentItem (org.apache.archiva.repository.content.ContentItem)29 ArchivaItemSelector (org.apache.archiva.repository.content.base.ArchivaItemSelector)16 ArchivaContentItem (org.apache.archiva.repository.content.base.ArchivaContentItem)15 Test (org.junit.Test)14 Path (java.nio.file.Path)12 StorageAsset (org.apache.archiva.repository.storage.StorageAsset)12 Artifact (org.apache.archiva.repository.content.Artifact)10 BaseRepositoryContentLayout (org.apache.archiva.repository.content.BaseRepositoryContentLayout)10 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)9 Project (org.apache.archiva.repository.content.Project)9 ManagedRepository (org.apache.archiva.repository.ManagedRepository)8 LayoutException (org.apache.archiva.repository.content.LayoutException)8 Version (org.apache.archiva.repository.content.Version)8 EditableManagedRepository (org.apache.archiva.repository.EditableManagedRepository)7 ItemSelector (org.apache.archiva.repository.content.ItemSelector)7 MavenManagedRepository (org.apache.archiva.maven.repository.MavenManagedRepository)6 DataItem (org.apache.archiva.repository.content.DataItem)6 Namespace (org.apache.archiva.repository.content.Namespace)6 IOException (java.io.IOException)5 ItemNotFoundException (org.apache.archiva.repository.content.ItemNotFoundException)5