dnet-hadoop/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/dump/APIClient.java

141 lines
4.0 KiB
Java
Raw Normal View History

2020-06-18 11:19:20 +02:00
package eu.dnetlib.dhp.oa.graph.dump;
import java.io.*;
import java.io.IOException;
2020-06-18 14:21:36 +02:00
import java.nio.charset.StandardCharsets;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
2020-06-18 11:19:20 +02:00
2020-06-18 11:34:15 +02:00
import com.google.gson.Gson;
import eu.dnetlib.dhp.oa.graph.dump.zenodo.ZenodoModel;
2020-06-18 11:19:20 +02:00
2020-06-19 17:39:46 +02:00
//import org.apache.http.entity.mime.MultipartEntityBuilder;
public class APIClient implements Serializable {
String urlString;
String bucket;
2020-06-18 14:21:36 +02:00
String deposition_id;
2020-06-18 18:38:54 +02:00
String access_token;
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
this.urlString = urlString;
2020-06-18 18:38:54 +02:00
this.access_token = access_token;
}
2020-06-18 11:19:20 +02:00
public int connect() throws IOException {
2020-06-18 11:19:20 +02:00
String json = "{}";
2020-06-18 11:19:20 +02:00
CloseableHttpClient client = HttpClients.createDefault();
2020-06-18 11:19:20 +02:00
HttpPost post = new HttpPost(urlString);
2020-06-18 11:19:20 +02:00
StringEntity input = new StringEntity(json);
post.setEntity(input);
post.addHeader("Content-Type", "application/json");
2020-06-18 18:38:54 +02:00
post.setHeader("Authorization", "Bearer " + access_token);
2020-06-18 11:19:20 +02:00
HttpResponse response = client.execute(post);
2020-06-18 11:19:20 +02:00
json = EntityUtils.toString(response.getEntity());
2020-06-18 11:19:20 +02:00
ZenodoModel newSubmission = new Gson().fromJson(json, ZenodoModel.class);
this.bucket = newSubmission.getLinks().getBucket();
this.deposition_id = newSubmission.getId();
client.close();
return response.getStatusLine().getStatusCode();
2020-06-18 11:19:20 +02:00
}
2020-06-18 11:19:20 +02:00
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
public int upload(File file, String file_name) throws IOException {
CloseableHttpClient client = HttpClients.createDefault();
2020-06-18 18:38:54 +02:00
2020-06-18 14:21:36 +02:00
HttpPut put = new HttpPut(bucket + "/" + file_name);
2020-06-18 18:38:54 +02:00
put.setHeader("Authorization", "Bearer " + access_token);
2020-06-18 14:21:36 +02:00
put.addHeader("Content-Type", "application/zip");
HttpEntity data = MultipartEntityBuilder.create().addBinaryBody(file_name, file).build();
2020-06-18 14:21:36 +02:00
put.setEntity(data);
HttpResponse response = client.execute(put);
client.close();
2020-06-18 18:38:54 +02:00
return response.getStatusLine().getStatusCode();
2020-06-18 14:21:36 +02:00
}
2020-06-18 18:38:54 +02:00
public int sendMretadata(String metadata) throws IOException {
2020-06-18 11:19:20 +02:00
CloseableHttpClient client = HttpClients.createDefault();
2020-06-18 14:21:36 +02:00
HttpPut post = new HttpPut(urlString + "/" + deposition_id);
2020-06-18 18:38:54 +02:00
post.setHeader("Authorization", "Bearer " + access_token);
2020-06-18 14:21:36 +02:00
post.addHeader("Content-Type", "application/json");
StringEntity entity = new StringEntity(metadata, StandardCharsets.UTF_8);
post.setEntity(entity);
2020-06-18 11:19:20 +02:00
HttpResponse response = client.execute(post);
client.close();
2020-06-18 18:38:54 +02:00
return response.getStatusLine().getStatusCode();
2020-06-18 11:19:20 +02:00
2020-06-18 14:21:36 +02:00
}
2020-06-18 18:38:54 +02:00
public int publish() throws IOException {
CloseableHttpClient client = HttpClients.createDefault();
2020-06-19 17:39:46 +02:00
HttpPost post = new HttpPost(urlString + "/" + deposition_id + "/actions/publish");
2020-06-18 18:38:54 +02:00
post.setHeader("Authorization", "Bearer " + access_token);
2020-06-18 14:21:36 +02:00
HttpResponse response = client.execute(post);
client.close();
2020-06-18 18:38:54 +02:00
return response.getStatusLine().getStatusCode();
}
2020-06-18 11:19:20 +02:00
}