|
|
|
@ -101,49 +101,81 @@ public class ZenodoAPIClient implements Serializable {
|
|
|
|
|
return responseCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int uploadIS2(InputStream is, String fileName) throws IOException {
|
|
|
|
|
// public int newDeposition() throws IOException {
|
|
|
|
|
// 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("Content-Type", "application/json") // add request headers
|
|
|
|
|
// .addHeader("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();
|
|
|
|
|
//
|
|
|
|
|
// eu.dnetlib.dhp.common.api.zenodo.ZenodoModel newSubmission = new Gson()
|
|
|
|
|
// .fromJson(json, eu.dnetlib.dhp.common.api.zenodo.ZenodoModel.class);
|
|
|
|
|
// this.bucket = newSubmission.getLinks().getBucket();
|
|
|
|
|
// this.deposition_id = newSubmission.getId();
|
|
|
|
|
//
|
|
|
|
|
// return response.code();
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
final String crlf = "\r\n";
|
|
|
|
|
final String twoHyphens = "--";
|
|
|
|
|
final String boundary = "*****";
|
|
|
|
|
// public int uploadIS2(InputStream is, String fileName) throws IOException {
|
|
|
|
|
//
|
|
|
|
|
// final String crlf = "\r\n";
|
|
|
|
|
// final String twoHyphens = "--";
|
|
|
|
|
// final String boundary = "*****";
|
|
|
|
|
//
|
|
|
|
|
// HttpPut put = new HttpPut(bucket + "/" + fileName);
|
|
|
|
|
//
|
|
|
|
|
// put.addHeader(HttpHeaders.CONTENT_TYPE, "application/zip");
|
|
|
|
|
// put.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + access_token);
|
|
|
|
|
//
|
|
|
|
|
// put.setEntity(new InputStreamEntity(is));
|
|
|
|
|
//
|
|
|
|
|
// int statusCode;
|
|
|
|
|
// try (CloseableHttpClient client = HttpClients.createDefault()) {
|
|
|
|
|
// CloseableHttpResponse response = client.execute(put);
|
|
|
|
|
// statusCode = response.getStatusLine().getStatusCode();
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// if (!checkOKStatus(statusCode)) {
|
|
|
|
|
// throw new IOException("Unexpected code " + statusCode);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// return statusCode;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
HttpPut put = new HttpPut(bucket + "/" + fileName);
|
|
|
|
|
|
|
|
|
|
put.addHeader(HttpHeaders.CONTENT_TYPE, "application/zip");
|
|
|
|
|
put.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + access_token);
|
|
|
|
|
|
|
|
|
|
put.setEntity(new InputStreamEntity(is));
|
|
|
|
|
|
|
|
|
|
int statusCode;
|
|
|
|
|
try (CloseableHttpClient client = HttpClients.createDefault()) {
|
|
|
|
|
CloseableHttpResponse response = client.execute(put);
|
|
|
|
|
statusCode = response.getStatusLine().getStatusCode();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!checkOKStatus(statusCode)) {
|
|
|
|
|
throw new IOException("Unexpected code " + statusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return statusCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int publish() throws IOException {
|
|
|
|
|
String json = "{}";
|
|
|
|
|
HttpPost post = new HttpPost(urlString + "/" + deposition_id + "/actions/publish");
|
|
|
|
|
post.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
|
|
|
|
post.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + access_token);
|
|
|
|
|
post.setEntity(new StringEntity(json));
|
|
|
|
|
int statusCode;
|
|
|
|
|
try (CloseableHttpClient client = HttpClients.createDefault()) {
|
|
|
|
|
CloseableHttpResponse response = client.execute(post);
|
|
|
|
|
statusCode = response.getStatusLine().getStatusCode();
|
|
|
|
|
}
|
|
|
|
|
if (!checkOKStatus(statusCode)) {
|
|
|
|
|
throw new IOException("Unexpected code " + statusCode);
|
|
|
|
|
}
|
|
|
|
|
return statusCode;
|
|
|
|
|
}
|
|
|
|
|
// public int publish() throws IOException {
|
|
|
|
|
// String json = "{}";
|
|
|
|
|
// HttpPost post = new HttpPost(urlString + "/" + deposition_id + "/actions/publish");
|
|
|
|
|
// post.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
|
|
|
|
// post.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + access_token);
|
|
|
|
|
// post.setEntity(new StringEntity(json));
|
|
|
|
|
// int statusCode;
|
|
|
|
|
// try (CloseableHttpClient client = HttpClients.createDefault()) {
|
|
|
|
|
// CloseableHttpResponse response = client.execute(post);
|
|
|
|
|
// statusCode = response.getStatusLine().getStatusCode();
|
|
|
|
|
// }
|
|
|
|
|
// if (!checkOKStatus(statusCode)) {
|
|
|
|
|
// throw new IOException("Unexpected code " + statusCode);
|
|
|
|
|
// }
|
|
|
|
|
// return statusCode;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Upload files in Zenodo.
|
|
|
|
@ -266,38 +298,65 @@ public class ZenodoAPIClient implements Serializable {
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws MissingConceptDoiException
|
|
|
|
|
*/
|
|
|
|
|
// public int newVersion(String concept_rec_id) throws Exception, MissingConceptDoiException {
|
|
|
|
|
// setDepositionId(concept_rec_id, 1);
|
|
|
|
|
// String json = "{}";
|
|
|
|
|
//
|
|
|
|
|
// URL url = new URL(urlString + "/" + deposition_id + "/actions/newversion");
|
|
|
|
|
// HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
|
|
//
|
|
|
|
|
// conn.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + access_token);
|
|
|
|
|
// conn.setDoOutput(true);
|
|
|
|
|
// conn.setRequestMethod("POST");
|
|
|
|
|
//
|
|
|
|
|
// 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);
|
|
|
|
|
// String latest_draft = zenodoModel.getLinks().getLatest_draft();
|
|
|
|
|
// deposition_id = latest_draft.substring(latest_draft.lastIndexOf("/") + 1);
|
|
|
|
|
// bucket = getBucket(latest_draft);
|
|
|
|
|
//
|
|
|
|
|
// return responseCode;
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
public int newVersion(String concept_rec_id) throws Exception, MissingConceptDoiException {
|
|
|
|
|
setDepositionId(concept_rec_id, 1);
|
|
|
|
|
String json = "{}";
|
|
|
|
|
|
|
|
|
|
URL url = new URL(urlString + "/" + deposition_id + "/actions/newversion");
|
|
|
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
|
|
OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(600, TimeUnit.SECONDS).build();
|
|
|
|
|
|
|
|
|
|
conn.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + access_token);
|
|
|
|
|
conn.setDoOutput(true);
|
|
|
|
|
conn.setRequestMethod("POST");
|
|
|
|
|
RequestBody body = RequestBody.create(json, MEDIA_TYPE_JSON);
|
|
|
|
|
|
|
|
|
|
try (OutputStream os = conn.getOutputStream()) {
|
|
|
|
|
byte[] input = json.getBytes("utf-8");
|
|
|
|
|
os.write(input, 0, input.length);
|
|
|
|
|
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()) {
|
|
|
|
|
|
|
|
|
|
if (!response.isSuccessful())
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -361,20 +420,46 @@ public class ZenodoAPIClient implements Serializable {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getPrevDepositions(String page) throws Exception {
|
|
|
|
|
// private String getPrevDepositions(String page) throws Exception {
|
|
|
|
|
//
|
|
|
|
|
// HttpGet get = new HttpGet(urlString);
|
|
|
|
|
// URI uri = new URIBuilder(get.getURI()).addParameter("page", page).build();
|
|
|
|
|
//
|
|
|
|
|
// get.setURI(uri);
|
|
|
|
|
//
|
|
|
|
|
// get.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
|
|
|
|
|
// get.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + access_token);
|
|
|
|
|
// try (CloseableHttpClient client = HttpClients.createDefault()) {
|
|
|
|
|
// CloseableHttpResponse response = client.execute(get);
|
|
|
|
|
// final String body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
|
|
|
|
// return body;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
HttpGet get = new HttpGet(urlString);
|
|
|
|
|
URI uri = new URIBuilder(get.getURI()).addParameter("page", page).build();
|
|
|
|
|
private String getPrevDepositions(String page) throws IOException {
|
|
|
|
|
|
|
|
|
|
get.setURI(uri);
|
|
|
|
|
OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(600, TimeUnit.SECONDS).build();
|
|
|
|
|
|
|
|
|
|
HttpUrl.Builder urlBuilder = HttpUrl.parse(urlString).newBuilder();
|
|
|
|
|
urlBuilder.addQueryParameter("page", page);
|
|
|
|
|
String url = urlBuilder.build().toString();
|
|
|
|
|
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(url)
|
|
|
|
|
.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()) // add request headers
|
|
|
|
|
.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + access_token)
|
|
|
|
|
.get()
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
try (Response response = httpClient.newCall(request).execute()) {
|
|
|
|
|
|
|
|
|
|
if (!response.isSuccessful())
|
|
|
|
|
throw new IOException("Unexpected code " + response + response.body().string());
|
|
|
|
|
|
|
|
|
|
return response.body().string();
|
|
|
|
|
|
|
|
|
|
get.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
|
|
|
|
|
get.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + access_token);
|
|
|
|
|
try (CloseableHttpClient client = HttpClients.createDefault()) {
|
|
|
|
|
CloseableHttpResponse response = client.execute(get);
|
|
|
|
|
final String body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
|
|
|
|
return body;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getBucket(String inputUurl) throws IOException {
|
|
|
|
|