This commit is contained in:
Miriam Baglioni 2020-07-07 18:09:06 +02:00
parent 0208bc18f3
commit 4c8d86493c
2 changed files with 39 additions and 42 deletions

View File

@ -3,10 +3,11 @@ package eu.dnetlib.dhp.oa.graph.dump;
import java.io.*; import java.io.*;
import java.io.IOException; import java.io.IOException;
import okhttp3.*;
import com.google.gson.Gson;
import eu.dnetlib.dhp.oa.graph.dump.zenodo.ZenodoModel;
import com.google.gson.Gson;
import eu.dnetlib.dhp.oa.graph.dump.zenodo.ZenodoModel;
import okhttp3.*;
public class APIClient implements Serializable { public class APIClient implements Serializable {
@ -16,11 +17,9 @@ public class APIClient implements Serializable {
String deposition_id; String deposition_id;
String access_token; String access_token;
public static final MediaType MEDIA_TYPE_JSON public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
= MediaType.parse("application/json; charset=utf-8");
private static final MediaType MEDIA_TYPE_ZIP private static final MediaType MEDIA_TYPE_ZIP = MediaType.parse("application/zip");
= MediaType.parse("application/zip");
public String getUrlString() { public String getUrlString() {
return urlString; return urlString;
@ -44,11 +43,11 @@ public class APIClient implements Serializable {
this.access_token = access_token; this.access_token = access_token;
} }
public int connect() throws IOException{ public int connect() throws IOException {
String json = "{}"; String json = "{}";
OkHttpClient httpClient = new OkHttpClient(); OkHttpClient httpClient = new OkHttpClient();
RequestBody body = RequestBody.create(json, MEDIA_TYPE_JSON); RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, json);
Request request = new Request.Builder() Request request = new Request.Builder()
.url(urlString) .url(urlString)
@ -57,11 +56,10 @@ public class APIClient implements Serializable {
.post(body) .post(body)
.build(); .build();
try (Response response = httpClient.newCall(request).execute()) { try (Response response = httpClient.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response + response.body().string()); if (!response.isSuccessful())
throw new IOException("Unexpected code " + response + response.body().string());
// Get response body // Get response body
json = response.body().string(); json = response.body().string();
@ -76,8 +74,7 @@ public class APIClient implements Serializable {
} }
public int upload(File file, String file_name) {
public int upload(File file, String file_name){
OkHttpClient httpClient = new OkHttpClient(); OkHttpClient httpClient = new OkHttpClient();
@ -85,11 +82,12 @@ public class APIClient implements Serializable {
.url(bucket + "/" + file_name) .url(bucket + "/" + file_name)
.addHeader("Content-Type", "application/zip") // add request headers .addHeader("Content-Type", "application/zip") // add request headers
.addHeader("Authorization", "Bearer " + access_token) .addHeader("Authorization", "Bearer " + access_token)
.put(RequestBody.create(file, MEDIA_TYPE_ZIP)) .put(RequestBody.create(MEDIA_TYPE_ZIP, file))
.build(); .build();
try (Response response = httpClient.newCall(request).execute()) { try (Response response = httpClient.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response + response.body().string()); if (!response.isSuccessful())
throw new IOException("Unexpected code " + response + response.body().string());
return response.code(); return response.code();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -99,12 +97,11 @@ public class APIClient implements Serializable {
return -1; return -1;
} }
public int sendMretadata(String metadata) throws IOException { public int sendMretadata(String metadata) throws IOException {
OkHttpClient httpClient = new OkHttpClient(); OkHttpClient httpClient = new OkHttpClient();
RequestBody body = RequestBody.create(metadata, MEDIA_TYPE_JSON); RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, metadata);
Request request = new Request.Builder() Request request = new Request.Builder()
.url(urlString + "/" + deposition_id) .url(urlString + "/" + deposition_id)
@ -113,10 +110,10 @@ public class APIClient implements Serializable {
.put(body) .put(body)
.build(); .build();
try (Response response = httpClient.newCall(request).execute()) { try (Response response = httpClient.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response + response.body().string()); if (!response.isSuccessful())
throw new IOException("Unexpected code " + response + response.body().string());
return response.code(); return response.code();
@ -130,16 +127,16 @@ public class APIClient implements Serializable {
OkHttpClient httpClient = new OkHttpClient(); OkHttpClient httpClient = new OkHttpClient();
Request request = new Request.Builder() Request request = new Request.Builder()
.url(urlString + "/" + deposition_id + "/actions/publish") .url(urlString + "/" + deposition_id + "/actions/publish")
.addHeader("Authorization", "Bearer " + access_token) .addHeader("Authorization", "Bearer " + access_token)
.post(RequestBody.create(json, MEDIA_TYPE_JSON)) .post(RequestBody.create(MEDIA_TYPE_JSON, json))
.build(); .build();
try (Response response = httpClient.newCall(request).execute()) { try (Response response = httpClient.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response + response.body().string()); if (!response.isSuccessful())
throw new IOException("Unexpected code " + response + response.body().string());
return response.code(); return response.code();

View File

@ -137,7 +137,7 @@ public class Mapper implements Serializable {
Optional<eu.dnetlib.dhp.schema.oaf.Qualifier> oar = Optional.ofNullable(input.getBestaccessright()); Optional<eu.dnetlib.dhp.schema.oaf.Qualifier> oar = Optional.ofNullable(input.getBestaccessright());
if (oar.isPresent()) { if (oar.isPresent()) {
if (Constants.accessRightsCoarMap.containsKey(oar.get().getClassid())) { if (Constants.accessRightsCoarMap.containsKey(oar.get().getClassid())) {
String code = Constants.accessRightsCoarMap.get(oar.get().getClassid()); String code = Constants.accessRightsCoarMap.get(oar.get().getClassname());
out out
.setBestaccessright( .setBestaccessright(
AccessRight AccessRight