Search in sources :

Example 1 with FileMetadata

use of org.apache.archiva.web.model.FileMetadata in project archiva by apache.

the class DefaultFileUploadService method save.

@Override
public Boolean save(String repositoryId, String groupId, String artifactId, String version, String packaging, boolean generatePom) throws ArchivaRestServiceException {
    repositoryId = StringUtils.trim(repositoryId);
    groupId = StringUtils.trim(groupId);
    artifactId = StringUtils.trim(artifactId);
    version = StringUtils.trim(version);
    packaging = StringUtils.trim(packaging);
    List<FileMetadata> fileMetadatas = getSessionFilesList();
    if (fileMetadatas == null || fileMetadatas.isEmpty()) {
        return Boolean.FALSE;
    }
    try {
        ManagedRepository managedRepository = managedRepositoryAdmin.getManagedRepository(repositoryId);
        if (managedRepository == null) {
            // TODO i18n ?
            throw new ArchivaRestServiceException("Cannot find managed repository with id " + repositoryId, Response.Status.BAD_REQUEST.getStatusCode(), null);
        }
        if (VersionUtil.isSnapshot(version) && !managedRepository.isSnapshots()) {
            // TODO i18n ?
            throw new ArchivaRestServiceException("Managed repository with id " + repositoryId + " do not accept snapshots", Response.Status.BAD_REQUEST.getStatusCode(), null);
        }
    } catch (RepositoryAdminException e) {
        throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    }
    // get from the session file with groupId/artifactId
    Iterable<FileMetadata> filesToAdd = Iterables.filter(fileMetadatas, new Predicate<FileMetadata>() {

        public boolean apply(FileMetadata fileMetadata) {
            return fileMetadata != null && !fileMetadata.isPomFile();
        }
    });
    Iterator<FileMetadata> iterator = filesToAdd.iterator();
    boolean pomGenerated = false;
    while (iterator.hasNext()) {
        FileMetadata fileMetadata = iterator.next();
        log.debug("fileToAdd: {}", fileMetadata);
        saveFile(repositoryId, fileMetadata, generatePom && !pomGenerated, groupId, artifactId, version, packaging);
        pomGenerated = true;
        deleteFile(fileMetadata.getServerFileName());
    }
    filesToAdd = Iterables.filter(fileMetadatas, new Predicate<FileMetadata>() {

        @Override
        public boolean apply(FileMetadata fileMetadata) {
            return fileMetadata != null && fileMetadata.isPomFile();
        }
    });
    iterator = filesToAdd.iterator();
    while (iterator.hasNext()) {
        FileMetadata fileMetadata = iterator.next();
        log.debug("fileToAdd: {}", fileMetadata);
        savePomFile(repositoryId, fileMetadata, groupId, artifactId, version, packaging);
        deleteFile(fileMetadata.getServerFileName());
    }
    return Boolean.TRUE;
}
Also used : ManagedRepository(org.apache.archiva.admin.model.beans.ManagedRepository) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) FileMetadata(org.apache.archiva.web.model.FileMetadata) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) Predicate(com.google.common.base.Predicate)

Example 2 with FileMetadata

use of org.apache.archiva.web.model.FileMetadata in project archiva by apache.

the class DefaultFileUploadService method post.

@Override
public FileMetadata post(MultipartBody multipartBody) throws ArchivaRestServiceException {
    try {
        String classifier = getStringValue(multipartBody, "classifier");
        String packaging = getStringValue(multipartBody, "packaging");
        // skygo: http header form pomFile was once sending 1 for true and void for false
        // leading to permanent false value for pomFile if using toBoolean(); use , "1", ""
        boolean pomFile = BooleanUtils.toBoolean(getStringValue(multipartBody, "pomFile"));
        Attachment file = multipartBody.getAttachment("files[]");
        // Content-Disposition: form-data; name="files[]"; filename="org.apache.karaf.features.command-2.2.2.jar"
        String fileName = file.getContentDisposition().getParameter("filename");
        Path tmpFile = Files.createTempFile("upload-artifact", ".tmp");
        tmpFile.toFile().deleteOnExit();
        IOUtils.copy(file.getDataHandler().getInputStream(), new FileOutputStream(tmpFile.toFile()));
        FileMetadata fileMetadata = new FileMetadata(fileName, Files.size(tmpFile), "theurl");
        fileMetadata.setServerFileName(tmpFile.toString());
        fileMetadata.setClassifier(classifier);
        fileMetadata.setDeleteUrl(tmpFile.getFileName().toString());
        fileMetadata.setPomFile(pomFile);
        fileMetadata.setPackaging(packaging);
        log.info("uploading file: {}", fileMetadata);
        List<FileMetadata> fileMetadatas = getSessionFilesList();
        fileMetadatas.add(fileMetadata);
        return fileMetadata;
    } catch (IOException e) {
        throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    }
}
Also used : Path(java.nio.file.Path) FileOutputStream(java.io.FileOutputStream) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) FileMetadata(org.apache.archiva.web.model.FileMetadata) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) IOException(java.io.IOException)

Example 3 with FileMetadata

use of org.apache.archiva.web.model.FileMetadata in project archiva by apache.

the class DefaultFileUploadService method clearUploadedFiles.

@Override
public Boolean clearUploadedFiles() throws ArchivaRestServiceException {
    List<FileMetadata> fileMetadatas = new ArrayList<>(getSessionFileMetadatas());
    for (FileMetadata fileMetadata : fileMetadatas) {
        deleteFile(Paths.get(fileMetadata.getServerFileName()).toString());
    }
    getSessionFileMetadatas().clear();
    return Boolean.TRUE;
}
Also used : FileMetadata(org.apache.archiva.web.model.FileMetadata) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList)

Example 4 with FileMetadata

use of org.apache.archiva.web.model.FileMetadata in project archiva by apache.

the class DefaultFileUploadService method deleteFile.

@Override
public Boolean deleteFile(String fileName) throws ArchivaRestServiceException {
    Path file = SystemUtils.getJavaIoTmpDir().toPath().resolve(fileName);
    log.debug("delete file:{},exists:{}", file, Files.exists(file));
    boolean removed = getSessionFileMetadatas().remove(new FileMetadata(fileName));
    // try with full name as ui only know the file name
    if (!removed) {
        /* unused */
        getSessionFileMetadatas().remove(new FileMetadata(file.toString()));
    }
    try {
        Files.deleteIfExists(file);
    } catch (IOException e) {
        log.error("Could not delete file {}: {}", file, e.getMessage(), e);
    }
    return Boolean.FALSE;
}
Also used : Path(java.nio.file.Path) FileMetadata(org.apache.archiva.web.model.FileMetadata) IOException(java.io.IOException)

Aggregations

FileMetadata (org.apache.archiva.web.model.FileMetadata)4 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)2 Predicate (com.google.common.base.Predicate)1 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)1 ManagedRepository (org.apache.archiva.admin.model.beans.ManagedRepository)1 Attachment (org.apache.cxf.jaxrs.ext.multipart.Attachment)1