1
0
Fork 0

changed code to use other lib

This commit is contained in:
Miriam Baglioni 2020-07-02 16:01:34 +02:00
parent 8fdc9e070c
commit d7f6f0c216
1 changed files with 169 additions and 78 deletions

View File

@ -3,28 +3,10 @@ package eu.dnetlib.dhp.oa.graph.dump;
import java.io.*; import java.io.*;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import okhttp3.*;
import org.apache.commons.io.FileUtils;
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.ByteArrayEntity;
import org.apache.http.entity.InputStreamEntity;
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;
import com.google.gson.Gson; import com.google.gson.Gson;
import eu.dnetlib.dhp.oa.graph.dump.zenodo.ZenodoModel; import eu.dnetlib.dhp.oa.graph.dump.zenodo.ZenodoModel;
//import org.apache.http.entity.mime.MultipartEntityBuilder;
public class APIClient implements Serializable { public class APIClient implements Serializable {
@ -34,6 +16,12 @@ public class APIClient implements Serializable {
String deposition_id; String deposition_id;
String access_token; String access_token;
public static final MediaType MEDIA_TYPE_JSON
= MediaType.parse("application/json; charset=utf-8");
private static final MediaType MEDIA_TYPE_ZIP
= MediaType.parse("application/zip");
public String getUrlString() { public String getUrlString() {
return urlString; return urlString;
} }
@ -56,31 +44,134 @@ 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 = "{}";
OkHttpClient httpClient = new OkHttpClient();
RequestBody body = RequestBody.create(json, MEDIA_TYPE_JSON);
Request request = new Request.Builder()
.url(urlString)
.addHeader("Content-Type", "application/json") // add request headers
.addHeader("Authorization", "Bearer " + access_token)
.post(body)
.build();
try (Response response = httpClient.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response + response.body().string());
// Get response body
json = response.body().string();
ZenodoModel newSubmission = new Gson().fromJson(json, ZenodoModel.class);
this.bucket = newSubmission.getLinks().getBucket();
this.deposition_id = newSubmission.getId();
return response.code();
}
}
public int upload(File file, 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(RequestBody.create(file, MEDIA_TYPE_ZIP))
.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;
}
public int sendMretadata(String metadata) throws IOException {
OkHttpClient httpClient = new OkHttpClient();
RequestBody body = RequestBody.create(metadata, MEDIA_TYPE_JSON);
Request request = new Request.Builder()
.url(urlString + "/" + deposition_id)
.addHeader("Content-Type", "application/json") // add request headers
.addHeader("Authorization", "Bearer " + access_token)
.put(body)
.build();
try (Response response = httpClient.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response + response.body().string());
return response.code();
}
}
public int publish() throws IOException {
String json = "{}"; String json = "{}";
HttpClient client = new DefaultHttpClient(); OkHttpClient httpClient = new OkHttpClient();
HttpPost post = new HttpPost(urlString);
StringEntity input = new StringEntity(json); Request request = new Request.Builder()
post.setEntity(input); .url(urlString + "/" + deposition_id + "/actions/publish")
post.addHeader("Content-Type", "application/json"); .addHeader("Authorization", "Bearer " + access_token)
post.setHeader("Authorization", "Bearer " + access_token); .post(RequestBody.create(json, MEDIA_TYPE_JSON))
.build();
HttpResponse response = client.execute(post); try (Response response = httpClient.newCall(request).execute()) {
json = EntityUtils.toString(response.getEntity()); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response + response.body().string());
ZenodoModel newSubmission = new Gson().fromJson(json, ZenodoModel.class); return response.code();
this.bucket = newSubmission.getLinks().getBucket();
this.deposition_id = newSubmission.getId();
return response.getStatusLine().getStatusCode();
}
} }
// public int connect() throws IOException {
//
// 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();
//
// }
// public int upload(InputStream is, String file_name) throws IOException { // public int upload(InputStream is, String file_name) throws IOException {
// HttpClient client = new DefaultHttpClient(); // HttpClient client = new DefaultHttpClient();
// //
@ -100,52 +191,52 @@ public class APIClient implements Serializable {
// return response.getStatusLine().getStatusCode(); // return response.getStatusLine().getStatusCode();
// } // }
public int upload(File file, String file_name) throws IOException { // public int upload(File file, String file_name) throws IOException {
HttpClient client = new DefaultHttpClient(); // 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();
//
// }
HttpPut put = new HttpPut(bucket + "/" + file_name); // public int sendMretadata(String metadata) throws IOException {
put.setHeader("Authorization", "Bearer " + access_token); //
put.addHeader("Content-Type", "application/zip"); // HttpClient client = new DefaultHttpClient();
InputStreamEntity data = new InputStreamEntity(new FileInputStream(file), -1); // HttpPut post = new HttpPut(urlString + "/" + deposition_id);
data.setContentType("binary/octet-stream"); // post.setHeader("Authorization", "Bearer " + access_token);
data.setChunked(true); // Send in multiple parts if needed // post.addHeader("Content-Type", "application/json");
// ByteArrayInputStream bais = new ByteArrayInputStream(FileUtils.readFileToByteArray(file)); // StringEntity entity = new StringEntity(metadata, StandardCharsets.UTF_8);
// EntityUtils.toByteArray(new ByteArrayInputStream(FileUtils.readFileToByteArray(file))); // post.setEntity(entity);
// InputStream targetStream = new FileInputStream(file); //
// DataInputStream tmp = new DataInputStream(targetStream); // HttpResponse response = client.execute(post);
// HttpEntity data = new ByteArrayEntity(tmp.); //
// HttpEntity data = MultipartEntityBuilder.create().addBinaryBody(file_name, file).build(); // return response.getStatusLine().getStatusCode();
put.setEntity(data); //
// }
HttpResponse response = client.execute(put); // public int publish() throws IOException {
// HttpClient client = new DefaultHttpClient();
return response.getStatusLine().getStatusCode(); // HttpPost post = new HttpPost(urlString + "/" + deposition_id + "/actions/publish");
// post.setHeader("Authorization", "Bearer " + access_token);
} //
// HttpResponse response = client.execute(post);
public int sendMretadata(String metadata) throws IOException { //
// return response.getStatusLine().getStatusCode();
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();
}
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();
}
} }