From 3582eba5658819f22e76634ed342181d99c886a5 Mon Sep 17 00:00:00 2001 From: "miriam.baglioni" Date: Tue, 27 Oct 2020 17:31:33 +0100 Subject: [PATCH] - --- .../dhp/common/api/ZenodoAPIClient.java | 63 ++++++++++++++----- 1 file changed, 48 insertions(+), 15 deletions(-) diff --git a/dhp-common/src/main/java/eu/dnetlib/dhp/common/api/ZenodoAPIClient.java b/dhp-common/src/main/java/eu/dnetlib/dhp/common/api/ZenodoAPIClient.java index d66290a76e..8b66bd2dac 100644 --- a/dhp-common/src/main/java/eu/dnetlib/dhp/common/api/ZenodoAPIClient.java +++ b/dhp-common/src/main/java/eu/dnetlib/dhp/common/api/ZenodoAPIClient.java @@ -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 + * * @return response code * @throws IOException */ public int newDeposition() throws IOException { String json = "{}"; - OkHttpClient httpClient = new OkHttpClient(); + OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(600, TimeUnit.SECONDS).build(); + RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, json); @@ -87,17 +89,17 @@ public class ZenodoAPIClient implements Serializable { /** * Upload files in Zenodo. + * * @param is the inputStream for the file to upload * @param file_name the name of the file as it will appear on Zenodo * @param len the size of the file * @return the response code */ public int uploadIS(InputStream is, String file_name, long len) throws IOException { - OkHttpClient httpClient = new OkHttpClient - .Builder() - .connectTimeout(600, TimeUnit.SECONDS) + OkHttpClient httpClient = new OkHttpClient.Builder() + .writeTimeout(600, TimeUnit.SECONDS) .readTimeout(600, TimeUnit.SECONDS) - .writeTimeout(600, TimeUnit.SECONDS).build(); + .connectTimeout(600, TimeUnit.SECONDS).build(); Request request = new Request.Builder() .url(bucket + "/" + file_name) @@ -115,13 +117,14 @@ public class ZenodoAPIClient implements Serializable { /** * Associates metadata information to the current deposition + * * @param metadata the metadata * @return response code * @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); @@ -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 + * * @return response code * @throws IOException */ @@ -152,7 +156,7 @@ public class ZenodoAPIClient implements Serializable { String json = "{}"; - OkHttpClient httpClient = new OkHttpClient(); + OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(600, TimeUnit.SECONDS).build(); Request request = new Request.Builder() .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. - * It sets the deposition_id and the bucket to be used 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: - * DOI: 10.xxx/zenodo.656930 concept_rec_id = 656930 + * To create a new version of an already published deposition. It sets the deposition_id and the bucket to be used + * 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: DOI: 10.xxx/zenodo.656930 + * concept_rec_id = 656930 * @return response code * @throws IOException * @throws MissingConceptDoiException @@ -184,7 +189,7 @@ public class ZenodoAPIClient implements Serializable { setDepositionId(concept_rec_id); String json = "{}"; - OkHttpClient httpClient = new OkHttpClient(); + OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(600, TimeUnit.SECONDS).build(); Request request = new Request.Builder() .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 { ZenodoModelList zenodoModelList = new Gson().fromJson(getPrevDepositions(), ZenodoModelList.class); @@ -222,7 +254,7 @@ public class ZenodoAPIClient implements Serializable { } private String getPrevDepositions() throws IOException { - OkHttpClient httpClient = new OkHttpClient(); + OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(600, TimeUnit.SECONDS).build(); Request request = new Request.Builder() .url(urlString) @@ -243,7 +275,8 @@ public class ZenodoAPIClient implements Serializable { } 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() .url(url)