-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathFileUploader.java
More file actions
98 lines (83 loc) · 3.58 KB
/
FileUploader.java
File metadata and controls
98 lines (83 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
*
*/
package com.researchspace.dataverse.sword;
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;
import org.swordapp.client.Deposit;
import org.swordapp.client.DepositReceipt;
import org.swordapp.client.ProtocolViolationException;
import org.swordapp.client.SWORDClient;
import org.swordapp.client.SWORDClientException;
import org.swordapp.client.SWORDError;
import lombok.extern.slf4j.Slf4j;
/**
* Uploads using SWORD client library
* Copyright 2016 ResearchSpace
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
* This is an internal package for calling SWORD-API.
* @author rspace
*
*/
@Slf4j
public class FileUploader {
private static final String APPLICATION_ZIP = "application/zip";
private static final String ZIP_PACKAGING = "http://purl.org/net/sword/package/SimpleZip";
/**
* @param file
* @param apiKey
* @param dataverseServer Server root e.g. "https://apitest.dataverse.org"
* @param doi FRagment e.g. "10.5072/FK2/MGADL1"
* @return
* @throws IOException
* @throws SWORDClientException
* @throws SWORDError
* @throws ProtocolViolationException
*/
public DepositReceipt deposit(File file, String apiKey, URI dataverseServer, String doi, String protocol)
throws IOException, SWORDClientException, SWORDError, ProtocolViolationException {
return this.deposit(new FileInputStream(file), file.getName(), apiKey, dataverseServer, doi, protocol);
}
/**
* 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.
* @param protocol The protocol used for persistent identification
* @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, String protocol)
throws IOException, SWORDClientException, SWORDError, ProtocolViolationException {
SWORDClient cli = new SWORDClient();
Deposit dep = new Deposit();
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/"
+ protocol + ":" + doi;
DepositReceipt rct = cli.deposit(depositURI, dep, cred);
log.info("Deposit received with status {}" ,rct.getStatusCode());
return rct;
}
}