forked from D-Net/dnet-hadoop
mergin with branch beta
This commit is contained in:
commit
ce03f3ee62
|
@ -3,6 +3,8 @@ package eu.dnetlib.dhp.common.api;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.apache.http.HttpHeaders;
|
import org.apache.http.HttpHeaders;
|
||||||
|
@ -13,6 +15,7 @@ import com.google.gson.Gson;
|
||||||
import eu.dnetlib.dhp.common.api.zenodo.ZenodoModel;
|
import eu.dnetlib.dhp.common.api.zenodo.ZenodoModel;
|
||||||
import eu.dnetlib.dhp.common.api.zenodo.ZenodoModelList;
|
import eu.dnetlib.dhp.common.api.zenodo.ZenodoModelList;
|
||||||
import okhttp3.*;
|
import okhttp3.*;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
public class ZenodoAPIClient implements Serializable {
|
public class ZenodoAPIClient implements Serializable {
|
||||||
|
|
||||||
|
@ -60,33 +63,31 @@ public class ZenodoAPIClient implements Serializable {
|
||||||
*/
|
*/
|
||||||
public int newDeposition() throws IOException {
|
public int newDeposition() throws IOException {
|
||||||
String json = "{}";
|
String json = "{}";
|
||||||
OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(600, TimeUnit.SECONDS).build();
|
|
||||||
|
|
||||||
RequestBody body = RequestBody.create(json, MEDIA_TYPE_JSON);
|
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
|
||||||
.url(urlString)
|
|
||||||
.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()) // add request headers
|
|
||||||
.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + access_token)
|
|
||||||
.post(body)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
try (Response response = httpClient.newCall(request).execute()) {
|
|
||||||
|
|
||||||
if (!response.isSuccessful())
|
|
||||||
throw new IOException("Unexpected code " + response + response.body().string());
|
|
||||||
|
|
||||||
// Get response body
|
|
||||||
json = response.body().string();
|
|
||||||
|
|
||||||
ZenodoModel newSubmission = new Gson().fromJson(json, ZenodoModel.class);
|
|
||||||
this.bucket = newSubmission.getLinks().getBucket();
|
|
||||||
this.deposition_id = newSubmission.getId();
|
|
||||||
|
|
||||||
return response.code();
|
|
||||||
|
|
||||||
|
URL url = new URL(urlString);
|
||||||
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
|
conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
|
||||||
|
conn.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + access_token);
|
||||||
|
conn.setRequestMethod("POST");
|
||||||
|
conn.setDoOutput(true);
|
||||||
|
try (OutputStream os = conn.getOutputStream()) {
|
||||||
|
byte[] input = json.getBytes("utf-8");
|
||||||
|
os.write(input, 0, input.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String body = getBody(conn);
|
||||||
|
|
||||||
|
int responseCode = conn.getResponseCode();
|
||||||
|
conn.disconnect();
|
||||||
|
|
||||||
|
if(!checkOKStatus(responseCode))
|
||||||
|
throw new IOException("Unexpected code " + responseCode + body);
|
||||||
|
|
||||||
|
ZenodoModel newSubmission = new Gson().fromJson(body, ZenodoModel.class);
|
||||||
|
this.bucket = newSubmission.getLinks().getBucket();
|
||||||
|
this.deposition_id = newSubmission.getId();
|
||||||
|
|
||||||
|
return responseCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -94,28 +95,48 @@ public class ZenodoAPIClient implements Serializable {
|
||||||
*
|
*
|
||||||
* @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
|
|
||||||
* @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) throws IOException {
|
||||||
OkHttpClient httpClient = new OkHttpClient.Builder()
|
|
||||||
.writeTimeout(600, TimeUnit.SECONDS)
|
|
||||||
.readTimeout(600, TimeUnit.SECONDS)
|
|
||||||
.connectTimeout(600, TimeUnit.SECONDS)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
URL url = new URL(bucket + "/" + file_name);
|
||||||
.url(bucket + "/" + file_name)
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
.addHeader(HttpHeaders.CONTENT_TYPE, "application/zip") // add request headers
|
conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, "application/zip");
|
||||||
.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + access_token)
|
conn.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + access_token);
|
||||||
.put(InputStreamRequestBody.create(MEDIA_TYPE_ZIP, is, len))
|
conn.setDoOutput(true);
|
||||||
.build();
|
conn.setRequestMethod("PUT");
|
||||||
|
|
||||||
|
byte[] buf = new byte[8192];
|
||||||
|
int length;
|
||||||
|
try (OutputStream os = conn.getOutputStream()) {
|
||||||
|
while ((length = is.read(buf)) != -1) {
|
||||||
|
os.write(buf, 0, length);
|
||||||
|
}
|
||||||
|
|
||||||
try (Response response = httpClient.newCall(request).execute()) {
|
|
||||||
if (!response.isSuccessful())
|
|
||||||
throw new IOException("Unexpected code " + response + response.body().string());
|
|
||||||
return response.code();
|
|
||||||
}
|
}
|
||||||
|
int responseCode = conn.getResponseCode();
|
||||||
|
if(! checkOKStatus(responseCode)){
|
||||||
|
throw new IOException("Unexpected code " + responseCode + getBody(conn));
|
||||||
|
}
|
||||||
|
|
||||||
|
return responseCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private String getBody(HttpURLConnection conn) throws IOException {
|
||||||
|
String body = "{}";
|
||||||
|
try (BufferedReader br = new BufferedReader(
|
||||||
|
new InputStreamReader(conn.getInputStream(), "utf-8"))) {
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
String responseLine = null;
|
||||||
|
while ((responseLine = br.readLine()) != null) {
|
||||||
|
response.append(responseLine.trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
body = response.toString();
|
||||||
|
|
||||||
|
}
|
||||||
|
return body;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -127,26 +148,36 @@ public class ZenodoAPIClient implements Serializable {
|
||||||
*/
|
*/
|
||||||
public int sendMretadata(String metadata) throws IOException {
|
public int sendMretadata(String metadata) throws IOException {
|
||||||
|
|
||||||
OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(600, TimeUnit.SECONDS).build();
|
URL url = new URL(urlString + "/" + deposition_id);
|
||||||
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
|
conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
|
||||||
|
conn.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + access_token);
|
||||||
|
conn.setDoOutput(true);
|
||||||
|
conn.setRequestMethod("PUT");
|
||||||
|
|
||||||
RequestBody body = RequestBody.create(metadata, MEDIA_TYPE_JSON);
|
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
try (OutputStream os = conn.getOutputStream()) {
|
||||||
.url(urlString + "/" + deposition_id)
|
byte[] input = metadata.getBytes("utf-8");
|
||||||
.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()) // add request headers
|
os.write(input, 0, input.length);
|
||||||
.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + access_token)
|
|
||||||
.put(body)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
try (Response response = httpClient.newCall(request).execute()) {
|
|
||||||
|
|
||||||
if (!response.isSuccessful())
|
|
||||||
throw new IOException("Unexpected code " + response + response.body().string());
|
|
||||||
|
|
||||||
return response.code();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final int responseCode = conn.getResponseCode();
|
||||||
|
conn.disconnect();
|
||||||
|
if(!checkOKStatus(responseCode))
|
||||||
|
throw new IOException("Unexpected code " + responseCode + getBody(conn));
|
||||||
|
|
||||||
|
return responseCode;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean checkOKStatus(int responseCode) {
|
||||||
|
|
||||||
|
if(HttpURLConnection.HTTP_OK != responseCode ||
|
||||||
|
HttpURLConnection.HTTP_CREATED != responseCode)
|
||||||
|
return true ;
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -155,6 +186,7 @@ public class ZenodoAPIClient implements Serializable {
|
||||||
* @return response code
|
* @return response code
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public int publish() throws IOException {
|
public int publish() throws IOException {
|
||||||
|
|
||||||
String json = "{}";
|
String json = "{}";
|
||||||
|
@ -194,28 +226,35 @@ public class ZenodoAPIClient implements Serializable {
|
||||||
setDepositionId(concept_rec_id, 1);
|
setDepositionId(concept_rec_id, 1);
|
||||||
String json = "{}";
|
String json = "{}";
|
||||||
|
|
||||||
OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(600, TimeUnit.SECONDS).build();
|
URL url = new URL(urlString + "/" + deposition_id + "/actions/newversion");
|
||||||
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
|
|
||||||
RequestBody body = RequestBody.create(json, MEDIA_TYPE_JSON);
|
conn.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + access_token);
|
||||||
|
conn.setDoOutput(true);
|
||||||
|
conn.setRequestMethod("POST");
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
|
||||||
.url(urlString + "/" + deposition_id + "/actions/newversion")
|
|
||||||
.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + access_token)
|
|
||||||
.post(body)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
try (Response response = httpClient.newCall(request).execute()) {
|
try (OutputStream os = conn.getOutputStream()) {
|
||||||
|
byte[] input = json.getBytes("utf-8");
|
||||||
if (!response.isSuccessful())
|
os.write(input, 0, input.length);
|
||||||
throw new IOException("Unexpected code " + response + response.body().string());
|
|
||||||
|
|
||||||
ZenodoModel zenodoModel = new Gson().fromJson(response.body().string(), ZenodoModel.class);
|
|
||||||
String latest_draft = zenodoModel.getLinks().getLatest_draft();
|
|
||||||
deposition_id = latest_draft.substring(latest_draft.lastIndexOf("/") + 1);
|
|
||||||
bucket = getBucket(latest_draft);
|
|
||||||
return response.code();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String body = getBody(conn);
|
||||||
|
|
||||||
|
int responseCode = conn.getResponseCode();
|
||||||
|
|
||||||
|
conn.disconnect();
|
||||||
|
if(!checkOKStatus(responseCode))
|
||||||
|
throw new IOException("Unexpected code " + responseCode + body);
|
||||||
|
|
||||||
|
ZenodoModel zenodoModel = new Gson().fromJson(body, ZenodoModel.class);
|
||||||
|
String latest_draft = zenodoModel.getLinks().getLatest_draft();
|
||||||
|
deposition_id = latest_draft.substring(latest_draft.lastIndexOf("/") + 1);
|
||||||
|
bucket = getBucket(latest_draft);
|
||||||
|
|
||||||
|
return responseCode;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -233,24 +272,33 @@ public class ZenodoAPIClient implements Serializable {
|
||||||
|
|
||||||
this.deposition_id = deposition_id;
|
this.deposition_id = deposition_id;
|
||||||
|
|
||||||
OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(600, TimeUnit.SECONDS).build();
|
String json = "{}";
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
URL url = new URL(urlString + "/" + deposition_id);
|
||||||
.url(urlString + "/" + deposition_id)
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
.addHeader("Authorization", "Bearer " + access_token)
|
|
||||||
.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();
|
|
||||||
|
|
||||||
|
conn.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + access_token);
|
||||||
|
conn.setRequestMethod("POST");
|
||||||
|
conn.setDoOutput(true);
|
||||||
|
try (OutputStream os = conn.getOutputStream()) {
|
||||||
|
byte[] input = json.getBytes("utf-8");
|
||||||
|
os.write(input, 0, input.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String body = getBody(conn);
|
||||||
|
|
||||||
|
int responseCode = conn.getResponseCode();
|
||||||
|
conn.disconnect();
|
||||||
|
|
||||||
|
if(!checkOKStatus(responseCode))
|
||||||
|
throw new IOException("Unexpected code " + responseCode + body);
|
||||||
|
|
||||||
|
ZenodoModel zenodoModel = new Gson().fromJson(body, ZenodoModel.class);
|
||||||
|
bucket = zenodoModel.getLinks().getBucket();
|
||||||
|
|
||||||
|
|
||||||
|
return responseCode;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setDepositionId(String concept_rec_id, Integer page) throws IOException, MissingConceptDoiException {
|
private void setDepositionId(String concept_rec_id, Integer page) throws IOException, MissingConceptDoiException {
|
||||||
|
@ -273,53 +321,56 @@ public class ZenodoAPIClient implements Serializable {
|
||||||
|
|
||||||
private String getPrevDepositions(String page) throws IOException {
|
private String getPrevDepositions(String page) throws IOException {
|
||||||
|
|
||||||
OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(600, TimeUnit.SECONDS).build();
|
|
||||||
|
|
||||||
HttpUrl.Builder urlBuilder = HttpUrl.parse(urlString).newBuilder();
|
HttpUrl.Builder urlBuilder = HttpUrl.parse(urlString).newBuilder();
|
||||||
urlBuilder.addQueryParameter("page", page);
|
urlBuilder.addQueryParameter("page", page);
|
||||||
String url = urlBuilder.build().toString();
|
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
URL url = new URL(urlBuilder.build().toString());
|
||||||
.url(url)
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()) // add request headers
|
conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
|
||||||
.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + access_token)
|
conn.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + access_token);
|
||||||
.get()
|
conn.setDoOutput(true);
|
||||||
.build();
|
conn.setRequestMethod("GET");
|
||||||
|
|
||||||
try (Response response = httpClient.newCall(request).execute()) {
|
|
||||||
|
|
||||||
if (!response.isSuccessful())
|
|
||||||
throw new IOException("Unexpected code " + response + response.body().string());
|
|
||||||
|
|
||||||
return response.body().string();
|
String body = getBody(conn);
|
||||||
|
|
||||||
|
int responseCode = conn.getResponseCode();
|
||||||
|
|
||||||
|
conn.disconnect();
|
||||||
|
if(!checkOKStatus(responseCode))
|
||||||
|
throw new IOException("Unexpected code " + responseCode + body);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return body;
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getBucket(String url) throws IOException {
|
private String getBucket(String inputUurl) throws IOException {
|
||||||
OkHttpClient httpClient = new OkHttpClient.Builder()
|
|
||||||
.connectTimeout(600, TimeUnit.SECONDS)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
URL url = new URL(inputUurl);
|
||||||
.url(url)
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()) // add request headers
|
conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
|
||||||
.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + access_token)
|
conn.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + access_token);
|
||||||
.get()
|
conn.setDoOutput(true);
|
||||||
.build();
|
conn.setRequestMethod("GET");
|
||||||
|
|
||||||
try (Response response = httpClient.newCall(request).execute()) {
|
String body = getBody(conn);
|
||||||
|
|
||||||
if (!response.isSuccessful())
|
int responseCode = conn.getResponseCode();
|
||||||
throw new IOException("Unexpected code " + response + response.body().string());
|
|
||||||
|
|
||||||
// Get response body
|
conn.disconnect();
|
||||||
ZenodoModel zenodoModel = new Gson().fromJson(response.body().string(), ZenodoModel.class);
|
if(!checkOKStatus(responseCode))
|
||||||
|
throw new IOException("Unexpected code " + responseCode + body);
|
||||||
|
|
||||||
|
ZenodoModel zenodoModel = new Gson().fromJson(body, ZenodoModel.class);
|
||||||
|
|
||||||
|
return zenodoModel.getLinks().getBucket();
|
||||||
|
|
||||||
return zenodoModel.getLinks().getBucket();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ class ZenodoAPIClientTest {
|
||||||
|
|
||||||
InputStream is = new FileInputStream(file);
|
InputStream is = new FileInputStream(file);
|
||||||
|
|
||||||
Assertions.assertEquals(200, client.uploadIS(is, "COVID-19.json.gz", file.length()));
|
Assertions.assertEquals(200, client.uploadIS(is, "COVID-19.json.gz"));
|
||||||
|
|
||||||
String metadata = IOUtils.toString(getClass().getResourceAsStream("/eu/dnetlib/dhp/common/api/metadata.json"));
|
String metadata = IOUtils.toString(getClass().getResourceAsStream("/eu/dnetlib/dhp/common/api/metadata.json"));
|
||||||
|
|
||||||
|
@ -56,7 +56,7 @@ class ZenodoAPIClientTest {
|
||||||
|
|
||||||
InputStream is = new FileInputStream(file);
|
InputStream is = new FileInputStream(file);
|
||||||
|
|
||||||
Assertions.assertEquals(200, client.uploadIS(is, "COVID-19.json.gz", file.length()));
|
Assertions.assertEquals(200, client.uploadIS(is, "COVID-19.json.gz"));
|
||||||
|
|
||||||
String metadata = IOUtils.toString(getClass().getResourceAsStream("/eu/dnetlib/dhp/common/api/metadata.json"));
|
String metadata = IOUtils.toString(getClass().getResourceAsStream("/eu/dnetlib/dhp/common/api/metadata.json"));
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ class ZenodoAPIClientTest {
|
||||||
|
|
||||||
InputStream is = new FileInputStream(file);
|
InputStream is = new FileInputStream(file);
|
||||||
|
|
||||||
Assertions.assertEquals(200, client.uploadIS(is, "newVersion_deposition", file.length()));
|
Assertions.assertEquals(200, client.uploadIS(is, "newVersion_deposition"));
|
||||||
|
|
||||||
Assertions.assertEquals(202, client.publish());
|
Assertions.assertEquals(202, client.publish());
|
||||||
|
|
||||||
|
@ -100,7 +100,7 @@ class ZenodoAPIClientTest {
|
||||||
|
|
||||||
InputStream is = new FileInputStream(file);
|
InputStream is = new FileInputStream(file);
|
||||||
|
|
||||||
Assertions.assertEquals(200, client.uploadIS(is, "newVersion_deposition", file.length()));
|
Assertions.assertEquals(200, client.uploadIS(is, "newVersion_deposition"));
|
||||||
|
|
||||||
Assertions.assertEquals(202, client.publish());
|
Assertions.assertEquals(202, client.publish());
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue