Search in sources :

Example 6 with LayoutException

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

the class DefaultRepositoriesService method copyArtifact.

@Override
public ActionStatus copyArtifact(ArtifactTransferRequest artifactTransferRequest) throws ArchivaRestServiceException {
    // check parameters
    String userName = getAuditInformation().getUser().getUsername();
    if (StringUtils.isBlank(userName)) {
        throw new ArchivaRestServiceException("copyArtifact call: userName not found", null);
    }
    if (StringUtils.isBlank(artifactTransferRequest.getRepositoryId())) {
        throw new ArchivaRestServiceException("copyArtifact call: sourceRepositoryId cannot be null", null);
    }
    if (StringUtils.isBlank(artifactTransferRequest.getTargetRepositoryId())) {
        throw new ArchivaRestServiceException("copyArtifact call: targetRepositoryId cannot be null", null);
    }
    ManagedRepository source = null;
    source = repositoryRegistry.getManagedRepository(artifactTransferRequest.getRepositoryId());
    if (source == null) {
        throw new ArchivaRestServiceException("cannot find repository with id " + artifactTransferRequest.getRepositoryId(), null);
    }
    ManagedRepository target = null;
    target = repositoryRegistry.getManagedRepository(artifactTransferRequest.getTargetRepositoryId());
    if (target == null) {
        throw new ArchivaRestServiceException("cannot find repository with id " + artifactTransferRequest.getTargetRepositoryId(), null);
    }
    if (StringUtils.isBlank(artifactTransferRequest.getGroupId())) {
        throw new ArchivaRestServiceException("groupId is mandatory", null);
    }
    if (StringUtils.isBlank(artifactTransferRequest.getArtifactId())) {
        throw new ArchivaRestServiceException("artifactId is mandatory", null);
    }
    if (StringUtils.isBlank(artifactTransferRequest.getVersion())) {
        throw new ArchivaRestServiceException("version is mandatory", null);
    }
    if (VersionUtil.isSnapshot(artifactTransferRequest.getVersion())) {
        throw new ArchivaRestServiceException("copy of SNAPSHOT not supported", null);
    }
    // end check parameters
    User user = null;
    try {
        user = securitySystem.getUserManager().findUser(userName);
    } catch (UserNotFoundException e) {
        throw new ArchivaRestServiceException("user " + userName + " not found", e);
    } catch (UserManagerException e) {
        throw new ArchivaRestServiceException("ArchivaRestServiceException:" + e.getMessage(), e);
    }
    // check karma on source : read
    AuthenticationResult authn = new AuthenticationResult(true, userName, null);
    SecuritySession securitySession = new DefaultSecuritySession(authn, user);
    try {
        boolean authz = securitySystem.isAuthorized(securitySession, ArchivaRoleConstants.OPERATION_READ_REPOSITORY, artifactTransferRequest.getRepositoryId());
        if (!authz) {
            throw new ArchivaRestServiceException("not authorized to access repo:" + artifactTransferRequest.getRepositoryId(), null);
        }
    } catch (AuthorizationException e) {
        log.error("error reading permission: {}", e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), e);
    }
    // check karma on target: write
    try {
        boolean authz = securitySystem.isAuthorized(securitySession, ArchivaRoleConstants.OPERATION_ADD_ARTIFACT, artifactTransferRequest.getTargetRepositoryId());
        if (!authz) {
            throw new ArchivaRestServiceException("not authorized to write to repo:" + artifactTransferRequest.getTargetRepositoryId(), null);
        }
    } catch (AuthorizationException e) {
        log.error("error reading permission: {}", e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), e);
    }
    // sounds good we can continue !
    String packaging = StringUtils.trim(artifactTransferRequest.getPackaging());
    ItemSelector selector = ArchivaItemSelector.builder().withProjectId(artifactTransferRequest.getArtifactId()).withArtifactId(artifactTransferRequest.getArtifactId()).withNamespace(artifactTransferRequest.getGroupId()).withArtifactVersion(artifactTransferRequest.getVersion()).withClassifier(artifactTransferRequest.getClassifier()).withExtension(StringUtils.isEmpty(packaging) ? "jar" : packaging).build();
    try {
        ManagedRepositoryContent sourceRepository = getManagedRepositoryContent(artifactTransferRequest.getRepositoryId());
        BaseRepositoryContentLayout layout = sourceRepository.getLayout(BaseRepositoryContentLayout.class);
        // String artifactSourcePath = sourceRepository.toPath( selector );
        org.apache.archiva.repository.content.Artifact sourceArtifact = layout.getArtifact(selector);
        if (!sourceArtifact.exists()) {
            log.error("cannot find artifact {}", artifactTransferRequest);
            throw new ArchivaRestServiceException("cannot find artifact " + artifactTransferRequest.toString(), null);
        }
        StorageAsset artifactFile = sourceArtifact.getAsset();
        ManagedRepositoryContent targetRepository = getManagedRepositoryContent(artifactTransferRequest.getTargetRepositoryId());
        String artifactPath = artifactFile.getPath();
        int lastIndex = artifactPath.lastIndexOf('/');
        String path = artifactPath.substring(0, lastIndex);
        StorageAsset targetDir = target.getAsset(path);
        Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
        int newBuildNumber = 1;
        String timestamp = null;
        StorageAsset versionMetadataFile = target.getAsset(path + "/" + MetadataTools.MAVEN_METADATA);
        /* unused */
        getMetadata(targetRepository.getRepository().getType(), versionMetadataFile);
        if (!targetDir.exists()) {
            targetDir = target.addAsset(targetDir.getPath(), true);
            targetDir.create();
        }
        String filename = artifactPath.substring(lastIndex + 1);
        boolean fixChecksums = !(archivaAdministration.getKnownContentConsumers().contains("create-missing-checksums"));
        StorageAsset targetFile = target.getAsset(targetDir.getPath() + "/" + filename);
        if (targetFile.exists() && target.blocksRedeployments()) {
            throw new ArchivaRestServiceException("artifact already exists in target repo: " + artifactTransferRequest.getTargetRepositoryId() + " and redeployment blocked", null);
        } else {
            copyFile(artifactFile, targetFile, fixChecksums);
            queueRepositoryTask(target.getId(), targetFile);
        }
        // copy source pom to target repo
        String pomFilename = filename;
        if (StringUtils.isNotBlank(artifactTransferRequest.getClassifier())) {
            pomFilename = StringUtils.remove(pomFilename, "-" + artifactTransferRequest.getClassifier());
        }
        pomFilename = FilenameUtils.removeExtension(pomFilename) + ".pom";
        StorageAsset pomFile = source.getAsset(artifactPath.substring(0, artifactPath.lastIndexOf('/')) + "/" + pomFilename);
        if (pomFile != null && pomFile.exists()) {
            StorageAsset targetPomFile = target.getAsset(targetDir.getPath() + "/" + pomFilename);
            copyFile(pomFile, targetPomFile, fixChecksums);
            queueRepositoryTask(target.getId(), targetPomFile);
        }
        // explicitly update only if metadata-updater consumer is not enabled!
        if (!archivaAdministration.getKnownContentConsumers().contains("metadata-updater")) {
            updateProjectMetadata(target.getType(), target, targetDir, lastUpdatedTimestamp, timestamp, newBuildNumber, fixChecksums, artifactTransferRequest);
        }
        String msg = "Artifact \'" + artifactTransferRequest.getGroupId() + ":" + artifactTransferRequest.getArtifactId() + ":" + artifactTransferRequest.getVersion() + "\' was successfully deployed to repository \'" + artifactTransferRequest.getTargetRepositoryId() + "\'";
        log.debug("copyArtifact {}", msg);
    } catch (RepositoryException | LayoutException e) {
        log.error("RepositoryException: {}", e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), e);
    } catch (RepositoryAdminException e) {
        log.error("RepositoryAdminException: {}", e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), e);
    } catch (IOException e) {
        log.error("IOException: {}", e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), e);
    }
    return ActionStatus.SUCCESS;
}
Also used : UserNotFoundException(org.apache.archiva.redback.users.UserNotFoundException) ManagedRepository(org.apache.archiva.repository.ManagedRepository) User(org.apache.archiva.redback.users.User) AuthorizationException(org.apache.archiva.redback.authorization.AuthorizationException) AuthenticationResult(org.apache.archiva.redback.authentication.AuthenticationResult) LayoutException(org.apache.archiva.repository.content.LayoutException) DefaultSecuritySession(org.apache.archiva.redback.system.DefaultSecuritySession) SecuritySession(org.apache.archiva.redback.system.SecuritySession) DefaultSecuritySession(org.apache.archiva.redback.system.DefaultSecuritySession) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) IOException(java.io.IOException) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) Date(java.util.Date) ItemSelector(org.apache.archiva.repository.content.ItemSelector) ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) UserManagerException(org.apache.archiva.redback.users.UserManagerException) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent)

Example 7 with LayoutException

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

the class CleanupReleasedSnapshotsRepositoryPurge method process.

@Override
public void process(String path) throws RepositoryPurgeException {
    try {
        StorageAsset artifactFile = repository.getRepository().getRoot().resolve(path);
        BaseRepositoryContentLayout layout = repository.getLayout(BaseRepositoryContentLayout.class);
        if (!artifactFile.exists()) {
            // Nothing to do here, file doesn't exist, skip it.
            return;
        }
        Artifact artifactRef = layout.getArtifact(path);
        if (!VersionUtil.isSnapshot(artifactRef.getVersion().getId())) {
            // Nothing to do here, not a snapshot, skip it.
            return;
        }
        ItemSelector projectSelector = ArchivaItemSelector.builder().withNamespace(artifactRef.getNamespace().getId()).withProjectId(artifactRef.getId()).build();
        // Gether the released versions
        List<String> releasedVersions = new ArrayList<>();
        Collection<org.apache.archiva.repository.ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
        for (org.apache.archiva.repository.ManagedRepository repo : repos) {
            if (repo.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE)) {
                BaseRepositoryContentLayout repoContent = repo.getContent().getLayout(BaseRepositoryContentLayout.class);
                Project proj = repoContent.getProject(projectSelector);
                for (Version version : repoContent.getVersions(proj)) {
                    if (!VersionUtil.isSnapshot(version.getId())) {
                        releasedVersions.add(version.getId());
                    }
                }
            }
        }
        Collections.sort(releasedVersions, VersionComparator.getInstance());
        // Now clean out any version that is earlier than the highest released version.
        boolean needsMetadataUpdate = false;
        ArchivaItemSelector.Builder versionSelectorBuilder = ArchivaItemSelector.builder().withNamespace(artifactRef.getNamespace().getId()).withProjectId(artifactRef.getId()).withArtifactId(artifactRef.getId());
        MetadataRepository metadataRepository = repositorySession.getRepository();
        if (releasedVersions.contains(VersionUtil.getReleaseVersion(artifactRef.getVersion().getId()))) {
            ArchivaItemSelector selector = versionSelectorBuilder.withVersion(artifactRef.getVersion().getId()).build();
            Version version = layout.getVersion(selector);
            if (version.exists()) {
                repository.deleteItem(version);
            }
            for (RepositoryListener listener : listeners) {
                listener.deleteArtifact(metadataRepository, repository.getId(), artifactRef.getNamespace().getId(), artifactRef.getId(), artifactRef.getVersion().getId(), artifactFile.getName());
            }
            metadataRepository.removeProjectVersion(repositorySession, repository.getId(), artifactRef.getNamespace().getId(), artifactRef.getId(), artifactRef.getVersion().getId());
            needsMetadataUpdate = true;
        }
        if (needsMetadataUpdate) {
            updateMetadata(artifactRef);
        }
    } catch (LayoutException e) {
        log.debug("Not processing file that is not an artifact: {}", e.getMessage());
    } catch (MetadataRepositoryException e) {
        log.error("Could not remove metadata during cleanup of released snapshots of {}", path, e);
    } catch (ContentAccessException e) {
        e.printStackTrace();
    } catch (ItemNotFoundException e) {
        log.error("Could not find item to delete {}", e.getMessage(), e);
    }
}
Also used : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) ArrayList(java.util.ArrayList) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) Version(org.apache.archiva.repository.content.Version) LayoutException(org.apache.archiva.repository.content.LayoutException) ContentAccessException(org.apache.archiva.repository.content.ContentAccessException) RepositoryListener(org.apache.archiva.metadata.audit.RepositoryListener) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) Artifact(org.apache.archiva.repository.content.Artifact) Project(org.apache.archiva.repository.content.Project) ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) ItemSelector(org.apache.archiva.repository.content.ItemSelector) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ItemNotFoundException(org.apache.archiva.repository.content.ItemNotFoundException)

Example 8 with LayoutException

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

the class CleanupReleasedSnapshotsRepositoryPurge method updateMetadata.

/*
     * TODO: Uses a deprecated API, but if we use the API with location string, it does not work as expected
     * -> not sure what needs to be changed here.
     */
@SuppressWarnings("deprecation")
private void updateMetadata(Artifact artifact) {
    ItemSelector versionRef = ArchivaItemSelector.builder().withNamespace(artifact.getNamespace().getId()).withProjectId(artifact.getId()).withVersion(artifact.getVersion().getId()).build();
    ItemSelector projectRef = ArchivaItemSelector.builder().withNamespace(artifact.getNamespace().getId()).withProjectId(artifact.getId()).build();
    try {
        metadataTools.updateVersionMetadata(repository, versionRef);
    } catch (ContentNotFoundException e) {
    // Ignore. (Just means we have no snapshot versions left to reference).
    } catch (RepositoryMetadataException | IOException | LayoutException e) {
    // Ignore.
    }
    try {
        metadataTools.updateProjectMetadata(repository, projectRef);
    } catch (ContentNotFoundException | RepositoryMetadataException | IOException | LayoutException e) {
    // Ignore. (Just means we have no snapshot versions left to reference).
    }
}
Also used : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) ItemSelector(org.apache.archiva.repository.content.ItemSelector) RepositoryMetadataException(org.apache.archiva.repository.metadata.RepositoryMetadataException) ContentNotFoundException(org.apache.archiva.repository.content.ContentNotFoundException) LayoutException(org.apache.archiva.repository.content.LayoutException) IOException(java.io.IOException)

Example 9 with LayoutException

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

the class DefaultRepositoriesService method deleteArtifact.

@Override
public ActionStatus deleteArtifact(Artifact artifact) throws ArchivaRestServiceException {
    String repositoryId = artifact.getContext();
    // so try both!!
    if (StringUtils.isEmpty(repositoryId)) {
        repositoryId = artifact.getRepositoryId();
    }
    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 (artifact == null) {
        throw new ArchivaRestServiceException("artifact cannot be null", 400, null);
    }
    if (StringUtils.isEmpty(artifact.getGroupId())) {
        throw new ArchivaRestServiceException("artifact.groupId cannot be null", 400, null);
    }
    if (StringUtils.isEmpty(artifact.getArtifactId())) {
        throw new ArchivaRestServiceException("artifact.artifactId cannot be null", 400, null);
    }
    // TODO more control on artifact fields
    boolean snapshotVersion = VersionUtil.isSnapshot(artifact.getVersion()) | VersionUtil.isGenericSnapshot(artifact.getVersion());
    String baseVersion = VersionUtil.getBaseVersion(artifact.getVersion());
    RepositorySession repositorySession = null;
    try {
        repositorySession = repositorySessionFactory.createSession();
    } catch (MetadataRepositoryException e) {
        e.printStackTrace();
    }
    try {
        Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
        TimeZone timezone = TimeZone.getTimeZone("UTC");
        DateFormat fmt = new SimpleDateFormat("yyyyMMdd.HHmmss");
        fmt.setTimeZone(timezone);
        ManagedRepository repo = repositoryRegistry.getManagedRepository(repositoryId);
        ManagedRepositoryContent repository = getManagedRepositoryContent(repositoryId);
        BaseRepositoryContentLayout layout = repository.getLayout(BaseRepositoryContentLayout.class);
        ArchivaItemSelector versionSelector = ArchivaItemSelector.builder().withNamespace(artifact.getGroupId()).withProjectId(artifact.getArtifactId()).withVersion(baseVersion).build();
        Version version1 = layout.getVersion(versionSelector);
        String path = repository.toPath(version1);
        ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace(artifact.getGroupId()).withProjectId(artifact.getArtifactId()).withVersion(baseVersion).withClassifier(artifact.getClassifier()).withArtifactId(artifact.getArtifactId()).withType(artifact.getType()).includeRelatedArtifacts().build();
        MetadataRepository metadataRepository = repositorySession.getRepository();
        if (StringUtils.isNotBlank(artifact.getClassifier())) {
            if (StringUtils.isBlank(artifact.getPackaging())) {
                throw new ArchivaRestServiceException("You must configure a type/packaging when using classifier", 400, null);
            }
            List<? extends org.apache.archiva.repository.content.Artifact> artifactItems = layout.getArtifacts(selector);
            for (org.apache.archiva.repository.content.Artifact aRef : artifactItems) {
                try {
                    repository.deleteItem(aRef);
                } catch (ItemNotFoundException e) {
                    log.error("Could not delete item, seems to be deleted by other thread. {}, {} ", aRef, e.getMessage());
                }
            }
        } else {
            int index = path.lastIndexOf('/');
            path = path.substring(0, index);
            StorageAsset targetPath = repo.getAsset(path);
            if (!targetPath.exists()) {
                // throw new ContentNotFoundException(
                // artifact.getNamespace() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() );
                log.warn("targetPath {} not found skip file deletion", targetPath);
                return ActionStatus.FAIL;
            }
            // delete from file system
            if (!snapshotVersion && version1.exists()) {
                try {
                    repository.deleteItem(version1);
                } catch (ItemNotFoundException e) {
                    log.error("Could not delete version item {}", e.getMessage());
                }
            } else {
                // We are deleting all version related artifacts for a snapshot version
                for (org.apache.archiva.repository.content.Artifact delArtifact : layout.getArtifacts(selector)) {
                    try {
                        repository.deleteItem(delArtifact);
                    } catch (ItemNotFoundException e) {
                        log.warn("Artifact that should be deleted, was not found: {}", delArtifact);
                    }
                }
                StorageAsset metadataFile = getMetadata(repo, targetPath.getPath());
                ArchivaRepositoryMetadata metadata = getMetadata(repository.getRepository().getType(), metadataFile);
                updateMetadata(metadata, metadataFile, lastUpdatedTimestamp, artifact);
            }
        }
        Collection<ArtifactMetadata> artifacts = Collections.emptyList();
        if (snapshotVersion) {
            artifacts = metadataRepository.getArtifacts(repositorySession, repositoryId, artifact.getGroupId(), artifact.getArtifactId(), baseVersion);
        } else {
            artifacts = metadataRepository.getArtifacts(repositorySession, repositoryId, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
        }
        log.debug("artifacts: {}", artifacts);
        if (artifacts.isEmpty()) {
            if (!snapshotVersion) {
                // verify metata repository doesn't contains anymore the version
                Collection<String> projectVersions = metadataRepository.getProjectVersions(repositorySession, repositoryId, artifact.getGroupId(), artifact.getArtifactId());
                if (projectVersions.contains(artifact.getVersion())) {
                    log.warn("artifact not found when deleted but version still here ! so force cleanup");
                    metadataRepository.removeProjectVersion(repositorySession, repositoryId, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
                }
            }
        }
        for (ArtifactMetadata artifactMetadata : artifacts) {
            // TODO: mismatch between artifact (snapshot) version and project (base) version here
            if (artifactMetadata.getVersion().equals(artifact.getVersion())) {
                if (StringUtils.isNotBlank(artifact.getClassifier())) {
                    if (StringUtils.isBlank(artifact.getPackaging())) {
                        throw new ArchivaRestServiceException("You must configure a type/packaging when using classifier", 400, null);
                    }
                    // cleanup facet which contains classifier information
                    MavenArtifactFacet mavenArtifactFacet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
                    if (StringUtils.equals(artifact.getClassifier(), mavenArtifactFacet.getClassifier())) {
                        artifactMetadata.removeFacet(MavenArtifactFacet.FACET_ID);
                        String groupId = artifact.getGroupId(), artifactId = artifact.getArtifactId(), version = artifact.getVersion();
                        MavenArtifactFacet mavenArtifactFacetToCompare = new MavenArtifactFacet();
                        mavenArtifactFacetToCompare.setClassifier(artifact.getClassifier());
                        metadataRepository.removeFacetFromArtifact(repositorySession, repositoryId, groupId, artifactId, version, mavenArtifactFacetToCompare);
                        repositorySession.save();
                    }
                } else {
                    if (snapshotVersion) {
                        metadataRepository.removeTimestampedArtifact(repositorySession, artifactMetadata, VersionUtil.getBaseVersion(artifact.getVersion()));
                    } else {
                        metadataRepository.removeArtifact(repositorySession, artifactMetadata.getRepositoryId(), artifactMetadata.getNamespace(), artifactMetadata.getProject(), artifact.getVersion(), artifactMetadata.getId());
                    }
                }
                // repository metadata to an artifact
                for (RepositoryListener listener : listeners) {
                    listener.deleteArtifact(metadataRepository, repository.getId(), artifactMetadata.getNamespace(), artifactMetadata.getProject(), artifactMetadata.getVersion(), artifactMetadata.getId());
                }
                triggerAuditEvent(repositoryId, path, AuditEvent.REMOVE_FILE);
            }
        }
    } catch (ContentNotFoundException e) {
        throw new ArchivaRestServiceException("Artifact does not exist: " + e.getMessage(), 400, e);
    } catch (RepositoryNotFoundException e) {
        throw new ArchivaRestServiceException("Target repository cannot be found: " + e.getMessage(), 400, e);
    } catch (RepositoryException e) {
        throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
    } catch (MetadataResolutionException | MetadataSessionException | MetadataRepositoryException | LayoutException e) {
        throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
    } finally {
        try {
            repositorySession.save();
        } catch (MetadataSessionException e) {
            log.error("Could not save sesion {}", e.getMessage());
        }
        repositorySession.close();
    }
    return ActionStatus.SUCCESS;
}
Also used : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) ManagedRepository(org.apache.archiva.repository.ManagedRepository) MavenArtifactFacet(org.apache.archiva.maven.metadata.model.MavenArtifactFacet) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) MetadataSessionException(org.apache.archiva.metadata.repository.MetadataSessionException) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) Version(org.apache.archiva.repository.content.Version) LayoutException(org.apache.archiva.repository.content.LayoutException) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositoryListener(org.apache.archiva.metadata.audit.RepositoryListener) ContentNotFoundException(org.apache.archiva.repository.content.ContentNotFoundException) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositoryNotFoundException(org.apache.archiva.repository.RepositoryNotFoundException) Date(java.util.Date) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException) TimeZone(java.util.TimeZone) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) SimpleDateFormat(java.text.SimpleDateFormat) ItemNotFoundException(org.apache.archiva.repository.content.ItemNotFoundException)

Example 10 with LayoutException

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

the class ArtifactBuilder method build.

public Artifact build() {
    String type = null, classifier = null;
    MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
    if (facet != null) {
        type = facet.getType();
        classifier = facet.getClassifier();
    }
    ArchivaItemSelector.Builder selectorBuilder = ArchivaItemSelector.builder().withNamespace(artifactMetadata.getNamespace()).withProjectId(artifactMetadata.getProject()).withVersion(artifactMetadata.getProjectVersion()).withArtifactId(artifactMetadata.getProject()).withArtifactVersion(artifactMetadata.getVersion());
    if (StringUtils.isNotEmpty(type)) {
        selectorBuilder.withType(type);
    }
    if (StringUtils.isNotEmpty(classifier)) {
        selectorBuilder.withClassifier(classifier);
    }
    BaseRepositoryContentLayout layout;
    try {
        layout = managedRepositoryContent.getLayout(BaseRepositoryContentLayout.class);
    } catch (LayoutException e) {
        throw new RuntimeException("Could not convert to layout " + e.getMessage());
    }
    org.apache.archiva.repository.content.Artifact repoArtifact = layout.getArtifact(selectorBuilder.build());
    String extension = repoArtifact.getExtension();
    Artifact artifact = new Artifact(repoArtifact.getVersion().getProject().getNamespace().getId(), repoArtifact.getId(), repoArtifact.getArtifactVersion());
    artifact.setRepositoryId(artifactMetadata.getRepositoryId());
    artifact.setClassifier(classifier);
    artifact.setPackaging(type);
    artifact.setType(type);
    artifact.setFileExtension(extension);
    artifact.setPath(managedRepositoryContent.toPath(repoArtifact));
    // TODO: find a reusable formatter for this
    double s = this.artifactMetadata.getSize();
    String symbol = "b";
    if (s > 1024) {
        symbol = "K";
        s /= 1024;
        if (s > 1024) {
            symbol = "M";
            s /= 1024;
            if (s > 1024) {
                symbol = "G";
                s /= 1024;
            }
        }
    }
    artifact.setContext(managedRepositoryContent.getId());
    DecimalFormat df = new DecimalFormat("#,###.##", new DecimalFormatSymbols(Locale.US));
    artifact.setSize(df.format(s) + " " + symbol);
    artifact.setId(repoArtifact.getId() + "-" + repoArtifact.getArtifactVersion() + "." + repoArtifact.getType());
    return artifact;
}
Also used : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) MavenArtifactFacet(org.apache.archiva.maven.metadata.model.MavenArtifactFacet) Artifact(org.apache.archiva.maven.model.Artifact) LayoutException(org.apache.archiva.repository.content.LayoutException) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout)

Aggregations

LayoutException (org.apache.archiva.repository.content.LayoutException)29 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)14 StorageAsset (org.apache.archiva.repository.storage.StorageAsset)14 BaseRepositoryContentLayout (org.apache.archiva.repository.content.BaseRepositoryContentLayout)13 Artifact (org.apache.archiva.repository.content.Artifact)12 ArchivaItemSelector (org.apache.archiva.repository.content.base.ArchivaItemSelector)12 IOException (java.io.IOException)9 ManagedRepository (org.apache.archiva.repository.ManagedRepository)8 ContentItem (org.apache.archiva.repository.content.ContentItem)8 ItemSelector (org.apache.archiva.repository.content.ItemSelector)8 ContentAccessException (org.apache.archiva.repository.content.ContentAccessException)6 Version (org.apache.archiva.repository.content.Version)6 DavException (org.apache.jackrabbit.webdav.DavException)6 ArrayList (java.util.ArrayList)5 ArchivaRepositoryMetadata (org.apache.archiva.model.ArchivaRepositoryMetadata)4 ItemNotFoundException (org.apache.archiva.repository.content.ItemNotFoundException)4 Project (org.apache.archiva.repository.content.Project)4 DavResource (org.apache.jackrabbit.webdav.DavResource)4 Path (java.nio.file.Path)3 HashSet (java.util.HashSet)3