This commit is contained in:
Miriam Baglioni 2020-10-27 17:31:33 +01:00
parent d2374e3b9e
commit 3582eba565
1 changed files with 48 additions and 15 deletions

View File

@ -51,12 +51,14 @@ public class ZenodoAPIClient implements Serializable {
/** /**
* Brand new deposition in Zenodo. It sets the deposition_id and the bucket where to store the files to upload * Brand new deposition in Zenodo. It sets the deposition_id and the bucket where to store the files to upload
*
* @return response code * @return response code
* @throws IOException * @throws IOException
*/ */
public int newDeposition() throws IOException { public int newDeposition() throws IOException {
String json = "{}"; String json = "{}";
OkHttpClient httpClient = new OkHttpClient(); OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(600, TimeUnit.SECONDS).build();
RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, json); RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, json);
@ -87,17 +89,17 @@ public class ZenodoAPIClient implements Serializable {
/** /**
* Upload files in Zenodo. * Upload files in Zenodo.
*
* @param is the inputStream for the file to upload * @param is the inputStream for the file to upload
* @param file_name the name of the file as it will appear on Zenodo * @param file_name the name of the file as it will appear on Zenodo
* @param len the size of the file * @param len the size of the file
* @return the response code * @return the response code
*/ */
public int uploadIS(InputStream is, String file_name, long len) throws IOException { public int uploadIS(InputStream is, String file_name, long len) throws IOException {
OkHttpClient httpClient = new OkHttpClient OkHttpClient httpClient = new OkHttpClient.Builder()
.Builder() .writeTimeout(600, TimeUnit.SECONDS)
.connectTimeout(600, TimeUnit.SECONDS)
.readTimeout(600, TimeUnit.SECONDS) .readTimeout(600, TimeUnit.SECONDS)
.writeTimeout(600, TimeUnit.SECONDS).build(); .connectTimeout(600, TimeUnit.SECONDS).build();
Request request = new Request.Builder() Request request = new Request.Builder()
.url(bucket + "/" + file_name) .url(bucket + "/" + file_name)
@ -115,13 +117,14 @@ public class ZenodoAPIClient implements Serializable {
/** /**
* Associates metadata information to the current deposition * Associates metadata information to the current deposition
*
* @param metadata the metadata * @param metadata the metadata
* @return response code * @return response code
* @throws IOException * @throws IOException
*/ */
public int sendMretadata(String metadata) throws IOException { public int sendMretadata(String metadata) throws IOException {
OkHttpClient httpClient = new OkHttpClient(); OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(600, TimeUnit.SECONDS).build();
RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, metadata); RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, metadata);
@ -145,6 +148,7 @@ public class ZenodoAPIClient implements Serializable {
/** /**
* To publish the current deposition. It works for both new deposition or new version of an old deposition * To publish the current deposition. It works for both new deposition or new version of an old deposition
*
* @return response code * @return response code
* @throws IOException * @throws IOException
*/ */
@ -152,7 +156,7 @@ public class ZenodoAPIClient implements Serializable {
String json = "{}"; String json = "{}";
OkHttpClient httpClient = new OkHttpClient(); OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(600, TimeUnit.SECONDS).build();
Request request = new Request.Builder() Request request = new Request.Builder()
.url(urlString + "/" + deposition_id + "/actions/publish") .url(urlString + "/" + deposition_id + "/actions/publish")
@ -171,11 +175,12 @@ public class ZenodoAPIClient implements Serializable {
} }
/** /**
* To create a new version of an already published deposition. * To create a new version of an already published deposition. It sets the deposition_id and the bucket to be used
* It sets the deposition_id and the bucket to be used for the new version. * for the new version.
* @param concept_rec_id the concept record id of the deposition for which to create a new version. It is *
* the last part of the url for the DOI Zenodo suggests to use to cite all versions: * @param concept_rec_id the concept record id of the deposition for which to create a new version. It is the last
* DOI: 10.xxx/zenodo.656930 concept_rec_id = 656930 * part of the url for the DOI Zenodo suggests to use to cite all versions: DOI: 10.xxx/zenodo.656930
* concept_rec_id = 656930
* @return response code * @return response code
* @throws IOException * @throws IOException
* @throws MissingConceptDoiException * @throws MissingConceptDoiException
@ -184,7 +189,7 @@ public class ZenodoAPIClient implements Serializable {
setDepositionId(concept_rec_id); setDepositionId(concept_rec_id);
String json = "{}"; String json = "{}";
OkHttpClient httpClient = new OkHttpClient(); OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(600, TimeUnit.SECONDS).build();
Request request = new Request.Builder() Request request = new Request.Builder()
.url(urlString + "/" + deposition_id + "/actions/newversion") .url(urlString + "/" + deposition_id + "/actions/newversion")
@ -206,6 +211,33 @@ public class ZenodoAPIClient implements Serializable {
} }
} }
public int uploadOpenDeposition(String deposition_id) throws IOException, MissingConceptDoiException {
this.deposition_id = deposition_id;
String json = "{}";
OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(600, TimeUnit.SECONDS).build();
Request request = new Request.Builder()
.url(urlString + "/" + deposition_id)
.addHeader("Authorization", "Bearer " + access_token)
// .post(RequestBody.create(MEDIA_TYPE_JSON, json))
.build();
try (Response response = httpClient.newCall(request).execute()) {
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response + response.body().string());
ZenodoModel zenodoModel = new Gson().fromJson(response.body().string(), ZenodoModel.class);
bucket = zenodoModel.getLinks().getBucket();
return response.code();
}
}
private void setDepositionId(String concept_rec_id) throws IOException, MissingConceptDoiException { private void setDepositionId(String concept_rec_id) throws IOException, MissingConceptDoiException {
ZenodoModelList zenodoModelList = new Gson().fromJson(getPrevDepositions(), ZenodoModelList.class); ZenodoModelList zenodoModelList = new Gson().fromJson(getPrevDepositions(), ZenodoModelList.class);
@ -222,7 +254,7 @@ public class ZenodoAPIClient implements Serializable {
} }
private String getPrevDepositions() throws IOException { private String getPrevDepositions() throws IOException {
OkHttpClient httpClient = new OkHttpClient(); OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(600, TimeUnit.SECONDS).build();
Request request = new Request.Builder() Request request = new Request.Builder()
.url(urlString) .url(urlString)
@ -243,7 +275,8 @@ public class ZenodoAPIClient implements Serializable {
} }
private String getBucket(String url) throws IOException { private String getBucket(String url) throws IOException {
OkHttpClient httpClient = new OkHttpClient(); OkHttpClient httpClient = new OkHttpClient.Builder()
.connectTimeout(600, TimeUnit.SECONDS).build();
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)