From 637477346b19802c7c2c7b9c34e81978fb9b986a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleix=20Marin=C3=A9=20Tena?= Date: Fri, 18 Nov 2022 00:45:22 +0100 Subject: [PATCH] Overload method void uploadFile(String doi, File file); to void uploadFile(String doi, InputStream is, String filename); (#13) * ADDED: Overloaded method to upload file to accept an InputStream with a filename * Update src/main/java/com/researchspace/dataverse/api/v1/DatasetOperations.java * Update src/main/java/com/researchspace/dataverse/api/v1/DatasetOperations.java * UPDATED: Performing requested changes from pull request #13 or related to previous changes in this PR: Changed name of variable and changed javadoc from uploadFile method, delegated implementation of one of the uploadFile implementation to the other to avoid duplication, split line of deposit for being too long Co-authored-by: otter606 --- .../dataverse/api/v1/DatasetOperations.java | 10 +++++++ .../http/DataverseOperationsImplV1.java | 19 ++++++++---- .../dataverse/sword/FileUploader.java | 30 +++++++++++++++---- 3 files changed, 49 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/researchspace/dataverse/api/v1/DatasetOperations.java b/src/main/java/com/researchspace/dataverse/api/v1/DatasetOperations.java index 554b117..308f2e3 100644 --- a/src/main/java/com/researchspace/dataverse/api/v1/DatasetOperations.java +++ b/src/main/java/com/researchspace/dataverse/api/v1/DatasetOperations.java @@ -4,6 +4,7 @@ package com.researchspace.dataverse.api.v1; import java.io.File; +import java.io.InputStream; import java.util.List; import com.researchspace.dataverse.entities.DataSetMetadataBlock; @@ -69,6 +70,15 @@ public interface DatasetOperations { */ void uploadFile(String doi, File file); + /** + * Uploads a file using a data stream. + * + * @param doi Identifier of the dataset that we are sending the data to. + * @param inputStream Stream of data to upload as a file in Dataverse. + * @param filename Contents of the field "name" that will appear as in Dataverse. + */ + void uploadFile(String doi, InputStream inputStream, String filename); + /** * Deletes a {@link Dataset} * @param dsIdentifier diff --git a/src/main/java/com/researchspace/dataverse/http/DataverseOperationsImplV1.java b/src/main/java/com/researchspace/dataverse/http/DataverseOperationsImplV1.java index a08349f..3b621a4 100644 --- a/src/main/java/com/researchspace/dataverse/http/DataverseOperationsImplV1.java +++ b/src/main/java/com/researchspace/dataverse/http/DataverseOperationsImplV1.java @@ -7,8 +7,7 @@ import static org.apache.commons.lang.Validate.isTrue; import static org.apache.commons.lang.Validate.noNullElements; -import java.io.File; -import java.io.IOException; +import java.io.*; import java.net.URI; import java.net.URISyntaxException; import java.util.List; @@ -210,11 +209,20 @@ public List getDatasetVersions (Identifier dsIdentifier) { */ @Override public void uploadFile (String doi, File file) { + try { + this.uploadFile(doi, new FileInputStream(file), file.getName()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + @Override + public void uploadFile(String doi, InputStream file, String filename) { FileUploader uploader = new FileUploader(); try { - uploader.deposit(file, apiKey, new URI(serverURL), doi); - } catch (IOException | SWORDClientException | ProtocolViolationException | URISyntaxException e) { - log.error("Couldn't upload file {} with doi {} : {}", file.getName(), doi.toString(), e.getMessage()); + uploader.deposit(file, filename, apiKey, new URI(serverURL), doi); + } catch (IOException | SWORDClientException | ProtocolViolationException | URISyntaxException e) { + log.error("Couldn't upload file {} with doi {} : {}", filename, doi.toString(), e.getMessage()); throw new RestClientException(e.getMessage()); } catch (SWORDError error) { if (!StringUtils.isEmpty(error.getErrorBody())) { @@ -222,6 +230,7 @@ public void uploadFile (String doi, File file) { throw new RestClientException(error.getErrorBody()); } } + } /* (non-Javadoc) diff --git a/src/main/java/com/researchspace/dataverse/sword/FileUploader.java b/src/main/java/com/researchspace/dataverse/sword/FileUploader.java index 2c3ae27..4293ffd 100644 --- a/src/main/java/com/researchspace/dataverse/sword/FileUploader.java +++ b/src/main/java/com/researchspace/dataverse/sword/FileUploader.java @@ -6,6 +6,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.net.URI; import org.swordapp.client.AuthCredentials; @@ -57,21 +58,40 @@ public class FileUploader { */ public DepositReceipt deposit(File file, String apiKey, URI dataverseServer, String doi) throws IOException, SWORDClientException, SWORDError, ProtocolViolationException { + return this.deposit(new FileInputStream(file), file.getName(), apiKey, dataverseServer, doi); + } + + + /** + * Creates a deposit object to upload a file into a dataverse instance using the SWORD library client. + * + * @param is Data coming as a stream. + * @param filename Name of the file to upload. + * @param apiKey Key used to authenticate actions into the goal dataverse instance. + * @param dataverseServer URL of the dataverse instance to attack. + * @param doi To identify the dataset that is the goal of the file upload. + * @return Information of the result of the upload via a {@code DepositReceipt} instance. + * @throws IOException Thrown when a IO error occurs, which is a general error. + * @throws SWORDClientException Thrown when an exception happens inside the SWORD client. + * @throws SWORDError Thrown when an exception happens inside the SWORD client. + * @throws ProtocolViolationException Thrown for unknown reasons. + */ + public DepositReceipt deposit(InputStream is, String filename, String apiKey, URI dataverseServer, String doi) + throws IOException, SWORDClientException, SWORDError, ProtocolViolationException { SWORDClient cli = new SWORDClient(); Deposit dep = new Deposit(); - dep.setFilename(file.getName()); - dep.setFile(new FileInputStream(file)); + dep.setFilename(filename); + dep.setFile(is); dep.setMimeType(APPLICATION_ZIP); dep.setPackaging(ZIP_PACKAGING); - + AuthCredentials cred = new AuthCredentials(apiKey, ""); - + String depositURI = dataverseServer.toString() + "/dvn/api/data-deposit/v1.1/swordv2/edit-media/study/doi:" + doi; DepositReceipt rct = cli.deposit(depositURI, dep, cred); log.info("Deposit received with status {}" ,rct.getStatusCode()); return rct; - } }