Search in sources :

Example 91 with StorageAsset

use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.

the class DefaultRepositoryTaskAdministration method scheduleIndexScan.

@Override
public void scheduleIndexScan(String repositoryId, String relativePath) throws RepositoryAdminException {
    if (StringUtils.isEmpty(repositoryId)) {
        throw RepositoryAdminException.ofKey("repository.id.invalid", "");
    }
    try {
        org.apache.archiva.repository.ManagedRepository repository = repositoryRegistry.getManagedRepository(repositoryId);
        if (repository == null) {
            throw RepositoryAdminException.ofKey("repository.not_found", repositoryId);
        }
        StorageAsset asset = repository.getAsset(relativePath);
        if (!asset.exists()) {
            throw RepositoryAdminException.ofKey("repository.file.not_found", repositoryId, relativePath);
        }
        ArtifactIndexingTask task = new ArtifactIndexingTask(repository, asset.getFilePath(), ArtifactIndexingTask.Action.FINISH, repository.getIndexingContext());
        task.setExecuteOnEntireRepo(false);
        task.setOnlyUpdate(true);
        indexingArchivaTaskScheduler.queueTask(task);
    } catch (TaskQueueException e) {
        log.error("Could not queue the task: {}", e.getMessage(), e);
        throw RepositoryAdminException.ofKey("repository.scan.task_queue_error", e, e.getMessage());
    }
}
Also used : ArtifactIndexingTask(org.apache.archiva.scheduler.indexing.ArtifactIndexingTask) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) TaskQueueException(org.apache.archiva.components.taskqueue.TaskQueueException)

Example 92 with StorageAsset

use of org.apache.archiva.repository.storage.StorageAsset 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 93 with StorageAsset

use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.

the class DefaultFileUploadService method updateProjectMetadata.

/**
 * Update artifact level metadata. If it does not exist, create the metadata and fix checksums if necessary.
 */
private void updateProjectMetadata(StorageAsset targetPath, Date lastUpdatedTimestamp, String timestamp, int buildNumber, boolean fixChecksums, FileMetadata fileMetadata, String groupId, String artifactId, String version, String packaging) throws RepositoryMetadataException {
    List<String> availableVersions = new ArrayList<>();
    String latestVersion = version;
    StorageAsset projectDir = targetPath.getParent();
    StorageAsset projectMetadataFile = projectDir.resolve(MetadataTools.MAVEN_METADATA);
    ArchivaRepositoryMetadata projectMetadata = getMetadata(projectMetadataFile);
    if (projectMetadataFile.exists()) {
        availableVersions = projectMetadata.getAvailableVersions();
        Collections.sort(availableVersions, VersionComparator.getInstance());
        if (!availableVersions.contains(version)) {
            availableVersions.add(version);
        }
        latestVersion = availableVersions.get(availableVersions.size() - 1);
    } else {
        availableVersions.add(version);
        projectMetadata.setGroupId(groupId);
        projectMetadata.setArtifactId(artifactId);
    }
    if (projectMetadata.getGroupId() == null) {
        projectMetadata.setGroupId(groupId);
    }
    if (projectMetadata.getArtifactId() == null) {
        projectMetadata.setArtifactId(artifactId);
    }
    projectMetadata.setLatestVersion(latestVersion);
    projectMetadata.setLastUpdatedTimestamp(lastUpdatedTimestamp);
    projectMetadata.setAvailableVersions(availableVersions);
    if (!VersionUtil.isSnapshot(version)) {
        projectMetadata.setReleasedVersion(latestVersion);
    }
    RepositoryMetadataWriter.write(projectMetadata, projectMetadataFile);
    if (fixChecksums) {
        fixChecksums(projectMetadataFile);
    }
}
Also used : StorageAsset(org.apache.archiva.repository.storage.StorageAsset) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata)

Example 94 with StorageAsset

use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.

the class DefaultFileUploadService method createPom.

private StorageAsset createPom(StorageAsset targetPath, String filename, FileMetadata fileMetadata, String groupId, String artifactId, String version, String packaging) throws IOException {
    Model projectModel = new Model();
    projectModel.setModelVersion("4.0.0");
    projectModel.setGroupId(groupId);
    projectModel.setArtifactId(artifactId);
    projectModel.setVersion(version);
    projectModel.setPackaging(packaging);
    StorageAsset pomFile = targetPath.resolve(filename);
    MavenXpp3Writer writer = new MavenXpp3Writer();
    try (Writer w = new OutputStreamWriter(pomFile.getWriteStream(true))) {
        writer.write(w, projectModel);
    }
    return pomFile;
}
Also used : StorageAsset(org.apache.archiva.repository.storage.StorageAsset) Model(org.apache.maven.model.Model) OutputStreamWriter(java.io.OutputStreamWriter) MavenXpp3Writer(org.apache.maven.model.io.xpp3.MavenXpp3Writer) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) RepositoryMetadataWriter(org.apache.archiva.repository.metadata.base.RepositoryMetadataWriter) MavenXpp3Writer(org.apache.maven.model.io.xpp3.MavenXpp3Writer)

Example 95 with StorageAsset

use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.

the class Maven2RepositoryStorage method isProject.

private boolean isProject(StorageAsset dir, Filter<String> filter) {
    // scan directories for a valid project version subdirectory, meaning this must be a project directory
    final Predicate<StorageAsset> dFilter = new DirectoryFilter(filter);
    boolean projFound = dir.list().stream().filter(dFilter).anyMatch(path -> isProjectVersion(path));
    if (projFound) {
        return true;
    }
    // if a metadata file is present, check if this is the "artifactId" directory, marking it as a project
    ArchivaRepositoryMetadata metadata = readMetadata(dir);
    if (metadata != null && dir.getName().toString().equals(metadata.getArtifactId())) {
        return true;
    }
    return false;
}
Also used : StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata)

Aggregations

StorageAsset (org.apache.archiva.repository.storage.StorageAsset)191 Path (java.nio.file.Path)91 BaseRepositoryContentLayout (org.apache.archiva.repository.content.BaseRepositoryContentLayout)61 IOException (java.io.IOException)59 Test (org.junit.Test)59 Artifact (org.apache.archiva.repository.content.Artifact)54 ManagedRepository (org.apache.archiva.repository.ManagedRepository)27 ArchivaIndexingContext (org.apache.archiva.indexer.ArchivaIndexingContext)22 ArchivaRepositoryMetadata (org.apache.archiva.model.ArchivaRepositoryMetadata)22 List (java.util.List)20 Inject (javax.inject.Inject)20 RepositoryMetadataException (org.apache.archiva.repository.metadata.RepositoryMetadataException)20 Collectors (java.util.stream.Collectors)19 RemoteRepository (org.apache.archiva.repository.RemoteRepository)19 IndexingContext (org.apache.maven.index.context.IndexingContext)19 FilesystemStorage (org.apache.archiva.repository.storage.fs.FilesystemStorage)18 StringUtils (org.apache.commons.lang3.StringUtils)18 Logger (org.slf4j.Logger)18 LoggerFactory (org.slf4j.LoggerFactory)18 Map (java.util.Map)17