Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overload method void uploadFile(String doi, File file); to void uploadFile(String doi, InputStream is, String filename); #13

Merged
merged 4 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 file Stream of data to the contents of the file to upload.
* @param filename Name of the file.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the input stream doesn't come from a file, but instead from a network or whatever, then the 'file' terminology isn't quite accurate. I think 'filename' is the name you want it to appear as in Dataverse, is that correct?

*/
void uploadFile(String doi, InputStream file, String filename);
otter606 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Deletes a {@link Dataset}
* @param dsIdentifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
Expand Down Expand Up @@ -224,6 +225,23 @@ public void uploadFile (String doi, File file) {
}
}

@Override
public void uploadFile(String doi, InputStream file, String filename) {
FileUploader uploader = new FileUploader();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could uploadFile(string, file) call

uploadFile(doi, new FileInputStream(file), file.getName())

in order to reduce duplication?

try {
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)
* @see com.researchspace.dataverse.http.DataverseAPI#deleteDataset(com.researchspace.dataverse.entities.Identifier)
*/
Expand Down
29 changes: 24 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,39 @@ 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;

}

}