Skip to content

Commit 6374773

Browse files
Aleix Mariné Tenaotter606
andauthored
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 <otter606@users.noreply.github.com>
1 parent a5fbaf6 commit 6374773

3 files changed

Lines changed: 49 additions & 10 deletions

File tree

src/main/java/com/researchspace/dataverse/api/v1/DatasetOperations.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package com.researchspace.dataverse.api.v1;
55

66
import java.io.File;
7+
import java.io.InputStream;
78
import java.util.List;
89

910
import com.researchspace.dataverse.entities.DataSetMetadataBlock;
@@ -69,6 +70,15 @@ public interface DatasetOperations {
6970
*/
7071
void uploadFile(String doi, File file);
7172

73+
/**
74+
* Uploads a file using a data stream.
75+
*
76+
* @param doi Identifier of the dataset that we are sending the data to.
77+
* @param inputStream Stream of data to upload as a file in Dataverse.
78+
* @param filename Contents of the field "name" that will appear as in Dataverse.
79+
*/
80+
void uploadFile(String doi, InputStream inputStream, String filename);
81+
7282
/**
7383
* Deletes a {@link Dataset}
7484
* @param dsIdentifier

src/main/java/com/researchspace/dataverse/http/DataverseOperationsImplV1.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
import static org.apache.commons.lang.Validate.isTrue;
88
import static org.apache.commons.lang.Validate.noNullElements;
99

10-
import java.io.File;
11-
import java.io.IOException;
10+
import java.io.*;
1211
import java.net.URI;
1312
import java.net.URISyntaxException;
1413
import java.util.List;
@@ -210,18 +209,28 @@ public List<DatasetVersion> getDatasetVersions (Identifier dsIdentifier) {
210209
*/
211210
@Override
212211
public void uploadFile (String doi, File file) {
212+
try {
213+
this.uploadFile(doi, new FileInputStream(file), file.getName());
214+
} catch (FileNotFoundException e) {
215+
e.printStackTrace();
216+
}
217+
}
218+
219+
@Override
220+
public void uploadFile(String doi, InputStream file, String filename) {
213221
FileUploader uploader = new FileUploader();
214222
try {
215-
uploader.deposit(file, apiKey, new URI(serverURL), doi);
216-
} catch (IOException | SWORDClientException | ProtocolViolationException | URISyntaxException e) {
217-
log.error("Couldn't upload file {} with doi {} : {}", file.getName(), doi.toString(), e.getMessage());
223+
uploader.deposit(file, filename, apiKey, new URI(serverURL), doi);
224+
} catch (IOException | SWORDClientException | ProtocolViolationException | URISyntaxException e) {
225+
log.error("Couldn't upload file {} with doi {} : {}", filename, doi.toString(), e.getMessage());
218226
throw new RestClientException(e.getMessage());
219227
} catch (SWORDError error) {
220228
if (!StringUtils.isEmpty(error.getErrorBody())) {
221229
log.error("SwordError: {}", error.getErrorBody());
222230
throw new RestClientException(error.getErrorBody());
223231
}
224232
}
233+
225234
}
226235

227236
/* (non-Javadoc)

src/main/java/com/researchspace/dataverse/sword/FileUploader.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.io.File;
77
import java.io.FileInputStream;
88
import java.io.IOException;
9+
import java.io.InputStream;
910
import java.net.URI;
1011

1112
import org.swordapp.client.AuthCredentials;
@@ -57,21 +58,40 @@ public class FileUploader {
5758
*/
5859
public DepositReceipt deposit(File file, String apiKey, URI dataverseServer, String doi)
5960
throws IOException, SWORDClientException, SWORDError, ProtocolViolationException {
61+
return this.deposit(new FileInputStream(file), file.getName(), apiKey, dataverseServer, doi);
62+
}
63+
64+
65+
/**
66+
* Creates a deposit object to upload a file into a dataverse instance using the SWORD library client.
67+
*
68+
* @param is Data coming as a stream.
69+
* @param filename Name of the file to upload.
70+
* @param apiKey Key used to authenticate actions into the goal dataverse instance.
71+
* @param dataverseServer URL of the dataverse instance to attack.
72+
* @param doi To identify the dataset that is the goal of the file upload.
73+
* @return Information of the result of the upload via a {@code DepositReceipt} instance.
74+
* @throws IOException Thrown when a IO error occurs, which is a general error.
75+
* @throws SWORDClientException Thrown when an exception happens inside the SWORD client.
76+
* @throws SWORDError Thrown when an exception happens inside the SWORD client.
77+
* @throws ProtocolViolationException Thrown for unknown reasons.
78+
*/
79+
public DepositReceipt deposit(InputStream is, String filename, String apiKey, URI dataverseServer, String doi)
80+
throws IOException, SWORDClientException, SWORDError, ProtocolViolationException {
6081
SWORDClient cli = new SWORDClient();
6182
Deposit dep = new Deposit();
62-
dep.setFilename(file.getName());
63-
dep.setFile(new FileInputStream(file));
83+
dep.setFilename(filename);
84+
dep.setFile(is);
6485
dep.setMimeType(APPLICATION_ZIP);
6586
dep.setPackaging(ZIP_PACKAGING);
66-
87+
6788
AuthCredentials cred = new AuthCredentials(apiKey, "");
68-
89+
6990
String depositURI = dataverseServer.toString() + "/dvn/api/data-deposit/v1.1/swordv2/edit-media/study/doi:"
7091
+ doi;
7192
DepositReceipt rct = cli.deposit(depositURI, dep, cred);
7293
log.info("Deposit received with status {}" ,rct.getStatusCode());
7394
return rct;
74-
7595
}
7696

7797
}

0 commit comments

Comments
 (0)