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;
|
|
|
|
|
|
|
|
//import com.cloudera.org.apache.http.HttpResponse;
|
|
|
|
//import com.cloudera.org.apache.http.client.HttpClient;
|
|
|
|
//import com.cloudera.org.apache.http.client.methods.HttpPost;
|
|
|
|
//import com.cloudera.org.apache.http.entity.StringEntity;
|
|
|
|
//import com.cloudera.org.apache.http.impl.client.DefaultHttpClient;
|
|
|
|
//import com.cloudera.org.apache.http.util.EntityUtils;
|
2020-06-18 11:19:20 +02:00
|
|
|
import com.google.gson.Gson;
|
2020-06-18 11:22:58 +02:00
|
|
|
|
2020-06-18 11:19:20 +02:00
|
|
|
import eu.dnetlib.dhp.oa.graph.dump.zenodo.ZenodoModel;
|
2020-06-18 11:22:58 +02:00
|
|
|
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.client.methods.HttpUriRequest;
|
|
|
|
import org.apache.http.client.methods.RequestBuilder;
|
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
|
import org.apache.http.entity.mime.HttpMultipartMode;
|
|
|
|
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:22:58 +02:00
|
|
|
public class APIClient implements Serializable {
|
|
|
|
|
|
|
|
String urlString;
|
|
|
|
String bucket;
|
|
|
|
HttpClient client;
|
|
|
|
String deposition_id;
|
|
|
|
final String ACCESS_TOKEN = "5ImUj0VC1ICg4ifK5dc3AGzJhcfAB4osxrFlsr8WxHXxjaYgCE0hY8HZcDoe";
|
|
|
|
|
|
|
|
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 11:22:58 +02:00
|
|
|
public APIClient(String urlString) throws IOException {
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-06-18 11:22:58 +02:00
|
|
|
this.urlString = urlString;
|
|
|
|
//connect();
|
|
|
|
}
|
2020-06-18 11:19:20 +02:00
|
|
|
|
|
|
|
|
2020-06-18 11:22:58 +02:00
|
|
|
public int connect() throws IOException {
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-06-18 11:22:58 +02:00
|
|
|
String json = "{}";
|
2020-06-18 11:19:20 +02:00
|
|
|
|
|
|
|
|
2020-06-18 11:22:58 +02:00
|
|
|
client = new DefaultHttpClient();
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-06-18 11:22:58 +02:00
|
|
|
HttpPost post = new HttpPost(urlString);
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-06-18 11:22:58 +02:00
|
|
|
StringEntity input = new StringEntity(json);
|
|
|
|
post.setEntity(input);
|
|
|
|
post.addHeader("Content-Type", "application/json");
|
|
|
|
post.setHeader("Authorization", "Bearer " + ACCESS_TOKEN);
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-06-18 11:22:58 +02:00
|
|
|
HttpResponse response = client.execute(post);
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-06-18 11:22:58 +02:00
|
|
|
json = EntityUtils.toString(response.getEntity());
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-06-18 11:22:58 +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-06-18 11:22:58 +02:00
|
|
|
return response.getStatusLine().getStatusCode();
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-06-18 11:22:58 +02:00
|
|
|
}
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-06-18 11:22:58 +02:00
|
|
|
public void upload(String filePath, String file_name) throws IOException {
|
|
|
|
File file = new File(filePath);
|
|
|
|
HttpPut post = new HttpPut(bucket + "/" + file_name);
|
|
|
|
post.setHeader("Authorization", "Bearer " + ACCESS_TOKEN);
|
|
|
|
post.addHeader("Content-Type", "application/zip");
|
|
|
|
HttpEntity data = MultipartEntityBuilder.create().addBinaryBody(file_name, file).build();
|
|
|
|
post.setEntity(data);
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-06-18 11:22:58 +02:00
|
|
|
//HttpUriRequest request = RequestBuilder.post(bucket + "/" + file_name +"?access_token=5ImUj0VC1ICg4ifK5dc3AGzJhcfAB4osxrFlsr8WxHXxjaYgCE0hY8HZcDoe").setEntity(data).build();
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-06-18 11:22:58 +02:00
|
|
|
HttpResponse response = client.execute(post);
|
|
|
|
System.out.println(response.getStatusLine().getStatusCode());
|
2020-06-18 11:19:20 +02:00
|
|
|
|
2020-06-18 11:22:58 +02:00
|
|
|
}
|
2020-06-18 11:19:20 +02:00
|
|
|
|
|
|
|
}
|