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

129 lines
3.4 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;
2020-06-18 18:38:54 +02:00
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
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;
2020-06-18 14:21:36 +02:00
2020-06-18 18:38:54 +02:00
import org.apache.http.entity.ContentType;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
2020-06-18 14:21:36 +02:00
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
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
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
2020-06-18 14:21:36 +02:00
HttpClient client = new DefaultHttpClient();
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();
2020-06-18 11:19:20 +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 11:19:20 +02:00
2020-06-18 18:38:54 +02:00
public int upload(File file, String file_name) throws IOException {
2020-06-18 14:21:36 +02:00
HttpClient client = new DefaultHttpClient();
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);
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
2020-06-18 14:21:36 +02:00
HttpClient client = new DefaultHttpClient();
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);
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 {
2020-06-18 14:21:36 +02:00
HttpClient client = new DefaultHttpClient();
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);
2020-06-18 18:38:54 +02:00
return response.getStatusLine().getStatusCode();
}
2020-06-18 11:19:20 +02:00
}