Skip to content

Commit

Permalink
Overload method void uploadFile(String doi, File file); to void uploa…
Browse files Browse the repository at this point in the history
…dFile(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>
  • Loading branch information
AleixMT and otter606 committed Nov 17, 2022
1 parent a5fbaf6 commit 6374773
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -210,18 +209,28 @@ public List<DatasetVersion> 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())) {
log.error("SwordError: {}", error.getErrorBody());
throw new RestClientException(error.getErrorBody());
}
}

}

/* (non-Javadoc)
Expand Down
30 changes: 25 additions & 5 deletions src/main/java/com/researchspace/dataverse/sword/FileUploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

}

}

0 comments on commit 6374773

Please sign in to comment.