2020-06-18 11:22:58 +02:00
|
|
|
|
2020-06-18 11:19:20 +02:00
|
|
|
package eu.dnetlib.dhp.oa.graph.dump;
|
|
|
|
|
|
|
|
import java.io.*;
|
2020-06-18 11:22:58 +02:00
|
|
|
import java.io.IOException;
|
2020-07-07 18:09:06 +02:00
|
|
|
|
2020-06-18 11:34:15 +02:00
|
|
|
import com.google.gson.Gson;
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-07-07 18:09:06 +02:00
|
|
|
import eu.dnetlib.dhp.oa.graph.dump.zenodo.ZenodoModel;
|
|
|
|
import okhttp3.*;
|
2020-06-19 17:39:46 +02:00
|
|
|
|
2020-06-18 11:22:58 +02:00
|
|
|
public class APIClient implements Serializable {
|
|
|
|
|
|
|
|
String urlString;
|
|
|
|
String bucket;
|
2020-06-18 14:21:36 +02:00
|
|
|
|
2020-06-18 11:22:58 +02:00
|
|
|
String deposition_id;
|
2020-06-18 18:38:54 +02:00
|
|
|
String access_token;
|
2020-06-18 11:22:58 +02:00
|
|
|
|
2020-07-07 18:09:06 +02:00
|
|
|
public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
|
2020-07-02 16:01:34 +02:00
|
|
|
|
2020-07-07 18:09:06 +02:00
|
|
|
private static final MediaType MEDIA_TYPE_ZIP = MediaType.parse("application/zip");
|
2020-07-02 16:01:34 +02:00
|
|
|
|
2020-06-18 11:22:58 +02:00
|
|
|
public String getUrlString() {
|
|
|
|
return urlString;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setUrlString(String urlString) {
|
|
|
|
this.urlString = urlString;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getBucket() {
|
|
|
|
return bucket;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBucket(String bucket) {
|
|
|
|
this.bucket = bucket;
|
|
|
|
}
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-06-18 18:38:54 +02:00
|
|
|
public APIClient(String urlString, String access_token) throws IOException {
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-06-18 11:22:58 +02:00
|
|
|
this.urlString = urlString;
|
2020-06-18 18:38:54 +02:00
|
|
|
this.access_token = access_token;
|
2020-06-18 11:22:58 +02:00
|
|
|
}
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-07-07 18:09:06 +02:00
|
|
|
public int connect() throws IOException {
|
2020-06-18 11:22:58 +02:00
|
|
|
String json = "{}";
|
2020-07-02 16:01:34 +02:00
|
|
|
OkHttpClient httpClient = new OkHttpClient();
|
|
|
|
|
2020-07-07 18:09:06 +02:00
|
|
|
RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, json);
|
2020-07-02 16:01:34 +02:00
|
|
|
|
|
|
|
Request request = new Request.Builder()
|
2020-07-07 18:09:06 +02:00
|
|
|
.url(urlString)
|
|
|
|
.addHeader("Content-Type", "application/json") // add request headers
|
|
|
|
.addHeader("Authorization", "Bearer " + access_token)
|
|
|
|
.post(body)
|
|
|
|
.build();
|
2020-07-02 16:01:34 +02:00
|
|
|
|
|
|
|
try (Response response = httpClient.newCall(request).execute()) {
|
|
|
|
|
2020-07-07 18:09:06 +02:00
|
|
|
if (!response.isSuccessful())
|
|
|
|
throw new IOException("Unexpected code " + response + response.body().string());
|
2020-07-02 16:01:34 +02:00
|
|
|
|
|
|
|
// Get response body
|
|
|
|
json = response.body().string();
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-07-02 16:01:34 +02:00
|
|
|
ZenodoModel newSubmission = new Gson().fromJson(json, ZenodoModel.class);
|
|
|
|
this.bucket = newSubmission.getLinks().getBucket();
|
|
|
|
this.deposition_id = newSubmission.getId();
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-07-02 16:01:34 +02:00
|
|
|
return response.code();
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-07-02 16:01:34 +02:00
|
|
|
}
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-07-02 16:01:34 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 18:09:06 +02:00
|
|
|
public int upload(File file, String file_name) {
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-07-02 16:01:34 +02:00
|
|
|
OkHttpClient httpClient = new OkHttpClient();
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-07-02 16:01:34 +02:00
|
|
|
Request request = new Request.Builder()
|
2020-07-07 18:09:06 +02:00
|
|
|
.url(bucket + "/" + file_name)
|
|
|
|
.addHeader("Content-Type", "application/zip") // add request headers
|
|
|
|
.addHeader("Authorization", "Bearer " + access_token)
|
|
|
|
.put(RequestBody.create(MEDIA_TYPE_ZIP, file))
|
|
|
|
.build();
|
2020-06-22 17:20:07 +02:00
|
|
|
|
2020-07-02 16:01:34 +02:00
|
|
|
try (Response response = httpClient.newCall(request).execute()) {
|
2020-07-07 18:09:06 +02:00
|
|
|
if (!response.isSuccessful())
|
|
|
|
throw new IOException("Unexpected code " + response + response.body().string());
|
2020-07-02 16:01:34 +02:00
|
|
|
return response.code();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-07-02 16:01:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2020-06-18 11:22:58 +02:00
|
|
|
}
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-08-05 14:18:25 +02:00
|
|
|
public int uploadIS(InputStream is, String file_name){
|
|
|
|
OkHttpClient httpClient = new OkHttpClient();
|
|
|
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
.url(bucket + "/" + file_name)
|
|
|
|
.addHeader("Content-Type", "application/zip") // add request headers
|
|
|
|
.addHeader("Authorization", "Bearer " + access_token)
|
|
|
|
.put(InputStreamRequestBody.create(MEDIA_TYPE_ZIP, is))
|
|
|
|
.build();
|
|
|
|
|
|
|
|
try (Response response = httpClient.newCall(request).execute()) {
|
|
|
|
if (!response.isSuccessful())
|
|
|
|
throw new IOException("Unexpected code " + response + response.body().string());
|
|
|
|
return response.code();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-07-02 16:01:34 +02:00
|
|
|
public int sendMretadata(String metadata) throws IOException {
|
|
|
|
|
|
|
|
OkHttpClient httpClient = new OkHttpClient();
|
|
|
|
|
2020-07-07 18:09:06 +02:00
|
|
|
RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, metadata);
|
2020-07-02 16:01:34 +02:00
|
|
|
|
|
|
|
Request request = new Request.Builder()
|
2020-07-07 18:09:06 +02:00
|
|
|
.url(urlString + "/" + deposition_id)
|
|
|
|
.addHeader("Content-Type", "application/json") // add request headers
|
|
|
|
.addHeader("Authorization", "Bearer " + access_token)
|
|
|
|
.put(body)
|
|
|
|
.build();
|
2020-07-02 16:01:34 +02:00
|
|
|
|
|
|
|
try (Response response = httpClient.newCall(request).execute()) {
|
|
|
|
|
2020-07-07 18:09:06 +02:00
|
|
|
if (!response.isSuccessful())
|
|
|
|
throw new IOException("Unexpected code " + response + response.body().string());
|
2020-07-02 16:01:34 +02:00
|
|
|
|
|
|
|
return response.code();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public int publish() throws IOException {
|
|
|
|
|
|
|
|
String json = "{}";
|
|
|
|
|
|
|
|
OkHttpClient httpClient = new OkHttpClient();
|
|
|
|
|
|
|
|
Request request = new Request.Builder()
|
2020-07-07 18:09:06 +02:00
|
|
|
.url(urlString + "/" + deposition_id + "/actions/publish")
|
|
|
|
.addHeader("Authorization", "Bearer " + access_token)
|
|
|
|
.post(RequestBody.create(MEDIA_TYPE_JSON, json))
|
|
|
|
.build();
|
2020-07-02 16:01:34 +02:00
|
|
|
|
|
|
|
try (Response response = httpClient.newCall(request).execute()) {
|
|
|
|
|
2020-07-07 18:09:06 +02:00
|
|
|
if (!response.isSuccessful())
|
|
|
|
throw new IOException("Unexpected code " + response + response.body().string());
|
2020-07-02 16:01:34 +02:00
|
|
|
|
|
|
|
return response.code();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 18:09:06 +02:00
|
|
|
// public int connect() throws IOException {
|
2020-07-02 16:01:34 +02:00
|
|
|
//
|
|
|
|
// String json = "{}";
|
|
|
|
//
|
|
|
|
// HttpClient client = new DefaultHttpClient();
|
|
|
|
//
|
|
|
|
// HttpPost post = new HttpPost(urlString);
|
|
|
|
//
|
|
|
|
// StringEntity input = new StringEntity(json);
|
|
|
|
// post.setEntity(input);
|
|
|
|
// post.addHeader("Content-Type", "application/json");
|
|
|
|
// post.setHeader("Authorization", "Bearer " + access_token);
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// HttpResponse response = client.execute(post);
|
|
|
|
//
|
|
|
|
// json = EntityUtils.toString(response.getEntity());
|
|
|
|
//
|
|
|
|
// ZenodoModel newSubmission = new Gson().fromJson(json, ZenodoModel.class);
|
|
|
|
// this.bucket = newSubmission.getLinks().getBucket();
|
|
|
|
// this.deposition_id = newSubmission.getId();
|
|
|
|
//
|
|
|
|
// return response.getStatusLine().getStatusCode();
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
|
2020-06-19 17:39:46 +02:00
|
|
|
// public int upload(InputStream is, String file_name) throws IOException {
|
|
|
|
// HttpClient client = new DefaultHttpClient();
|
|
|
|
//
|
|
|
|
// HttpPut put = new HttpPut(bucket + "/" + file_name);
|
|
|
|
// put.setHeader("Authorization", "Bearer " + access_token);
|
|
|
|
// put.addHeader("Content-Type", "application/zip");
|
|
|
|
//
|
|
|
|
// HttpEntity data = MultipartEntityBuilder
|
|
|
|
// .create()
|
|
|
|
// // .addPart("file", new ByteArrayInputStream(is));
|
|
|
|
// .addBinaryBody(file_name, is, ContentType.APPLICATION_OCTET_STREAM, file_name)
|
|
|
|
// .build();
|
|
|
|
// put.setEntity(data);
|
|
|
|
//
|
|
|
|
// HttpResponse response = client.execute(put);
|
|
|
|
//
|
|
|
|
// return response.getStatusLine().getStatusCode();
|
|
|
|
// }
|
2020-06-18 18:38:54 +02:00
|
|
|
|
2020-07-02 16:01:34 +02:00
|
|
|
// public int upload(File file, String file_name) throws IOException {
|
|
|
|
// HttpClient client = new DefaultHttpClient();
|
|
|
|
//
|
|
|
|
// HttpPut put = new HttpPut(bucket + "/" + file_name);
|
|
|
|
// put.setHeader("Authorization", "Bearer " + access_token);
|
|
|
|
// put.addHeader("Content-Type", "application/zip");
|
|
|
|
// InputStreamEntity data = new InputStreamEntity(new FileInputStream(file), -1);
|
|
|
|
// data.setContentType("binary/octet-stream");
|
|
|
|
// data.setChunked(true); // Send in multiple parts if needed
|
|
|
|
//// ByteArrayInputStream bais = new ByteArrayInputStream(FileUtils.readFileToByteArray(file));
|
|
|
|
//// EntityUtils.toByteArray(new ByteArrayInputStream(FileUtils.readFileToByteArray(file)));
|
|
|
|
//// InputStream targetStream = new FileInputStream(file);
|
|
|
|
//// DataInputStream tmp = new DataInputStream(targetStream);
|
|
|
|
//// HttpEntity data = new ByteArrayEntity(tmp.);
|
|
|
|
//// HttpEntity data = MultipartEntityBuilder.create().addBinaryBody(file_name, file).build();
|
|
|
|
// put.setEntity(data);
|
|
|
|
//
|
|
|
|
// HttpResponse response = client.execute(put);
|
|
|
|
//
|
|
|
|
// return response.getStatusLine().getStatusCode();
|
|
|
|
//
|
|
|
|
// }
|
2020-06-18 14:21:36 +02:00
|
|
|
|
2020-07-02 16:01:34 +02:00
|
|
|
// public int sendMretadata(String metadata) throws IOException {
|
|
|
|
//
|
|
|
|
// HttpClient client = new DefaultHttpClient();
|
|
|
|
// HttpPut post = new HttpPut(urlString + "/" + deposition_id);
|
|
|
|
// post.setHeader("Authorization", "Bearer " + access_token);
|
|
|
|
// post.addHeader("Content-Type", "application/json");
|
|
|
|
// StringEntity entity = new StringEntity(metadata, StandardCharsets.UTF_8);
|
|
|
|
// post.setEntity(entity);
|
|
|
|
//
|
|
|
|
// HttpResponse response = client.execute(post);
|
|
|
|
//
|
|
|
|
// return response.getStatusLine().getStatusCode();
|
|
|
|
//
|
|
|
|
// }
|
2020-06-22 17:20:07 +02:00
|
|
|
|
2020-07-02 16:01:34 +02:00
|
|
|
// public int publish() throws IOException {
|
|
|
|
// HttpClient client = new DefaultHttpClient();
|
|
|
|
// HttpPost post = new HttpPost(urlString + "/" + deposition_id + "/actions/publish");
|
|
|
|
// post.setHeader("Authorization", "Bearer " + access_token);
|
|
|
|
//
|
|
|
|
// HttpResponse response = client.execute(post);
|
|
|
|
//
|
|
|
|
// return response.getStatusLine().getStatusCode();
|
|
|
|
// }
|
2020-06-18 11:19:20 +02:00
|
|
|
|
|
|
|
}
|