Search in sources :

Example 1 with ContentItem

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

the class ManagedDefaultRepositoryContent method toItem.

@Override
public ContentItem toItem(String path) throws LayoutException {
    StorageAsset asset = getRepository().getAsset(path);
    ContentItem item = getItemFromPath(asset);
    if (item instanceof DataItem) {
        Artifact artifact = adaptItem(Artifact.class, item);
        if (asset.getParent() == null) {
            throw new LayoutException("Path too short for maven artifact " + path);
        }
        String version = asset.getParent().getName();
        if (asset.getParent().getParent() == null) {
            throw new LayoutException("Path too short for maven artifact " + path);
        }
        String project = item.getAsset().getParent().getParent().getName();
        DataItem dataItem = (DataItem) item;
        if (StringUtils.isEmpty(dataItem.getExtension())) {
            throw new LayoutException("Missing type on maven artifact");
        }
        if (!project.equals(artifact.getId())) {
            throw new LayoutException("The maven artifact id " + artifact.getId() + " does not match the project id: " + project);
        }
        boolean versionIsGenericSnapshot = VersionUtil.isGenericSnapshot(version);
        boolean artifactVersionIsSnapshot = VersionUtil.isSnapshot(artifact.getArtifactVersion());
        if (versionIsGenericSnapshot && !artifactVersionIsSnapshot) {
            throw new LayoutException("The maven artifact has no snapshot version in snapshot directory " + dataItem);
        }
        if (!versionIsGenericSnapshot && artifactVersionIsSnapshot) {
            throw new LayoutException("The maven artifact version " + artifact.getArtifactVersion() + " is a snapshot version but inside a non snapshot directory " + version);
        }
        if (!versionIsGenericSnapshot && !version.equals(artifact.getArtifactVersion())) {
            throw new LayoutException("The maven artifact version " + artifact.getArtifactVersion() + " does not match the version directory " + version);
        }
    }
    return item;
}
Also used : DataItem(org.apache.archiva.repository.content.DataItem) LayoutException(org.apache.archiva.repository.content.LayoutException) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ContentItem(org.apache.archiva.repository.content.ContentItem) ArchivaContentItem(org.apache.archiva.repository.content.base.ArchivaContentItem) Artifact(org.apache.archiva.repository.content.Artifact)

Example 2 with ContentItem

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

the class MetadataTransferTest method assertFetchVersionedFailed.

/**
 * Transfer the metadata file, not expected to succeed.
 *
 * @param requestedResource the requested resource
 * @throws Exception
 */
private void assertFetchVersionedFailed(String requestedResource) throws Exception {
    Path expectedFile = managedDefaultDir.resolve(requestedResource);
    ContentItem item = managedDefaultRepository.toItem(requestedResource);
    assertNotNull(item);
    BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout(BaseRepositoryContentLayout.class);
    Version version = layout.adaptItem(Version.class, item);
    assertNotNull(version);
    String metaPath = managedDefaultRepository.toPath(layout.getMetadataItem(version));
    assertNotNull(metaPath);
    StorageAsset downloadedFile = proxyHandler.fetchMetadataFromProxies(managedDefaultRepository.getRepository(), metaPath).getFile();
    assertNull(downloadedFile);
    assertNoTempFiles(expectedFile);
}
Also used : Path(java.nio.file.Path) Version(org.apache.archiva.repository.content.Version) SnapshotVersion(org.apache.archiva.model.SnapshotVersion) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ContentItem(org.apache.archiva.repository.content.ContentItem)

Example 3 with ContentItem

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

the class MetadataTransferTest method assertFetchProjectOrGroup.

/**
 * Transfer the metadata file.
 *
 * @param requestedResource the requested resource
 * @throws Exception
 */
private void assertFetchProjectOrGroup(String requestedResource) throws Exception {
    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();
    assertNotNull("Should 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)

Example 4 with ContentItem

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

the class DefaultMavenManagedRepositoryService method copyArtifact.

@Override
public Response copyArtifact(String srcRepositoryId, String dstRepositoryId, String path) throws ArchivaRestServiceException {
    final AuditInformation auditInformation = getAuditInformation();
    final String userName = auditInformation.getUser().getUsername();
    if (StringUtils.isEmpty(userName)) {
        httpServletResponse.setHeader("WWW-Authenticate", "Bearer realm=\"archiva\"");
        throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.NOT_AUTHENTICATED), 401);
    }
    ManagedRepository srcRepo = repositoryRegistry.getManagedRepository(srcRepositoryId);
    if (srcRepo == null) {
        throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.REPOSITORY_NOT_FOUND, srcRepositoryId), 404);
    }
    ManagedRepository dstRepo = repositoryRegistry.getManagedRepository(dstRepositoryId);
    if (dstRepo == null) {
        throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.REPOSITORY_NOT_FOUND, dstRepositoryId), 404);
    }
    checkAuthority(auditInformation.getUser().getUsername(), srcRepositoryId, dstRepositoryId);
    try {
        ContentItem srcItem = srcRepo.getContent().toItem(path);
        ContentItem dstItem = dstRepo.getContent().toItem(path);
        if (!srcItem.getAsset().exists()) {
            throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.ARTIFACT_NOT_FOUND, srcRepositoryId, path), 404);
        }
        if (dstItem.getAsset().exists()) {
            throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.ARTIFACT_EXISTS_AT_DEST, srcRepositoryId, path), 400);
        }
        FsStorageUtil.copyAsset(srcItem.getAsset(), dstItem.getAsset(), true);
    } catch (LayoutException e) {
        throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.REPOSITORY_LAYOUT_ERROR, e.getMessage()));
    } catch (IOException e) {
        throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.ARTIFACT_COPY_ERROR, e.getMessage()));
    }
    return Response.ok().build();
}
Also used : AuditInformation(org.apache.archiva.admin.model.AuditInformation) ManagedRepository(org.apache.archiva.repository.ManagedRepository) MavenManagedRepository(org.apache.archiva.rest.api.v2.model.MavenManagedRepository) LayoutException(org.apache.archiva.repository.content.LayoutException) ArchivaRestServiceException(org.apache.archiva.rest.api.v2.svc.ArchivaRestServiceException) IOException(java.io.IOException) ContentItem(org.apache.archiva.repository.content.ContentItem)

Example 5 with ContentItem

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

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