Search in sources :

Example 76 with ItemSelector

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

the class DefaultFileUploadService method saveFile.

protected void saveFile(String repositoryId, FileMetadata fileMetadata, boolean generatePom, String groupId, String artifactId, String version, String packaging) throws ArchivaRestServiceException {
    log.debug("Saving file");
    try {
        org.apache.archiva.repository.ManagedRepository repository = repositoryRegistry.getManagedRepository(repositoryId);
        log.debug("Finding artifact path for {}, {}, {}, {}", groupId, artifactId, version, packaging);
        ItemSelector selector = ArchivaItemSelector.builder().withNamespace(groupId).withProjectId(artifactId).withArtifactId(artifactId).withArtifactVersion(version).withExtension(packaging).build();
        StorageAsset artifactPath = repository.getContent().getItem(selector).getAsset();
        StorageAsset targetPath = artifactPath.getParent();
        log.debug("artifactPath: {} found targetPath: {}", artifactPath, targetPath);
        Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
        int newBuildNumber = -1;
        String timestamp = null;
        StorageAsset versionMetadataFile = targetPath.resolve(MetadataTools.MAVEN_METADATA);
        ArchivaRepositoryMetadata versionMetadata = getMetadata(versionMetadataFile);
        if (VersionUtil.isSnapshot(version)) {
            TimeZone timezone = TimeZone.getTimeZone("UTC");
            DateFormat fmt = new SimpleDateFormat("yyyyMMdd.HHmmss");
            fmt.setTimeZone(timezone);
            timestamp = fmt.format(lastUpdatedTimestamp);
            if (versionMetadata.getSnapshotVersion() != null) {
                newBuildNumber = versionMetadata.getSnapshotVersion().getBuildNumber() + 1;
            } else {
                newBuildNumber = 1;
            }
        }
        if (!targetPath.exists()) {
            targetPath.create();
        }
        String filename = artifactPath.getName().toString();
        if (VersionUtil.isSnapshot(version)) {
            filename = filename.replaceAll(VersionUtil.SNAPSHOT, timestamp + "-" + newBuildNumber);
        }
        // We always fix checksums for newly uploaded files, even if the content consumer is active.
        boolean fixChecksums = true;
        try {
            StorageAsset targetFile = targetPath.resolve(filename);
            if (targetFile.exists() && !VersionUtil.isSnapshot(version) && repository.blocksRedeployments()) {
                throw new ArchivaRestServiceException("Overwriting released artifacts in repository '" + repository.getId() + "' is not allowed.", Response.Status.BAD_REQUEST.getStatusCode(), null);
            } else {
                copyFile(Paths.get(fileMetadata.getServerFileName()), targetPath, filename, fixChecksums);
                triggerAuditEvent(repository.getId(), artifactPath.toString(), AuditEvent.UPLOAD_FILE);
                queueRepositoryTask(repository.getId(), targetFile);
            }
        } catch (IOException ie) {
            log.error("IOException copying file: {}", ie.getMessage(), ie);
            throw new ArchivaRestServiceException("Overwriting released artifacts in repository '" + repository.getId() + "' is not allowed.", Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ie);
        }
        if (generatePom) {
            String pomFilename = filename;
            if (StringUtils.isNotEmpty(fileMetadata.getClassifier())) {
                pomFilename = StringUtils.remove(pomFilename, "-" + fileMetadata.getClassifier());
            }
            pomFilename = FilenameUtils.removeExtension(pomFilename) + ".pom";
            try {
                StorageAsset generatedPomFile = createPom(targetPath, pomFilename, fileMetadata, groupId, artifactId, version, packaging);
                triggerAuditEvent(repository.getId(), targetPath.resolve(pomFilename).toString(), AuditEvent.UPLOAD_FILE);
                if (fixChecksums) {
                    fixChecksums(generatedPomFile);
                }
                queueRepositoryTask(repository.getId(), generatedPomFile);
            } catch (IOException ie) {
                throw new ArchivaRestServiceException("Error encountered while writing pom file: " + ie.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ie);
            }
        }
        // explicitly update only if metadata-updater consumer is not enabled!
        if (!archivaAdministration.getKnownContentConsumers().contains("metadata-updater")) {
            updateProjectMetadata(targetPath, lastUpdatedTimestamp, timestamp, newBuildNumber, fixChecksums, fileMetadata, groupId, artifactId, version, packaging);
            if (VersionUtil.isSnapshot(version)) {
                updateVersionMetadata(versionMetadata, versionMetadataFile, lastUpdatedTimestamp, timestamp, newBuildNumber, fixChecksums, fileMetadata, groupId, artifactId, version, packaging);
            }
        }
    } catch (RepositoryException rep) {
        log.error("RepositoryException during save {}", rep.getMessage());
        throw new ArchivaRestServiceException("Repository exception: " + rep.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rep);
    } catch (RepositoryAdminException e) {
        log.error("RepositoryAdminException during save {}", e.getMessage());
        throw new ArchivaRestServiceException("RepositoryAdmin exception: " + e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    } catch (IOException e) {
        log.error("IOException during save {}", e.getMessage());
        throw new ArchivaRestServiceException("Repository exception " + e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    }
}
Also used : RepositoryException(org.apache.archiva.repository.RepositoryException) IOException(java.io.IOException) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) Date(java.util.Date) TimeZone(java.util.TimeZone) ItemSelector(org.apache.archiva.repository.content.ItemSelector) ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

ItemSelector (org.apache.archiva.repository.content.ItemSelector)76 ArchivaItemSelector (org.apache.archiva.repository.content.base.ArchivaItemSelector)74 Test (org.junit.Test)41 Path (java.nio.file.Path)16 Artifact (org.apache.archiva.repository.content.Artifact)16 StorageAsset (org.apache.archiva.repository.storage.StorageAsset)13 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)12 BaseRepositoryContentLayout (org.apache.archiva.repository.content.BaseRepositoryContentLayout)12 LayoutException (org.apache.archiva.repository.content.LayoutException)12 IOException (java.io.IOException)11 ContentItem (org.apache.archiva.repository.content.ContentItem)11 Project (org.apache.archiva.repository.content.Project)11 ArchivaContentItem (org.apache.archiva.repository.content.base.ArchivaContentItem)10 Namespace (org.apache.archiva.repository.content.Namespace)9 Version (org.apache.archiva.repository.content.Version)9 VersionComparator (org.apache.archiva.common.utils.VersionComparator)8 EditableManagedRepository (org.apache.archiva.repository.EditableManagedRepository)7 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 Stream (java.util.stream.Stream)6