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; - } }