From fb803530181be906c8704a80bb0952eea75d28e8 Mon Sep 17 00:00:00 2001 From: "miriam.baglioni" Date: Thu, 18 Jun 2020 14:21:36 +0200 Subject: [PATCH] - --- .../dnetlib/dhp/oa/graph/dump/APIClient.java | 59 ++++++++++++----- .../dhp/oa/graph/dump/ZenodoUploadTest.java | 65 +++++++++++++++---- 2 files changed, 95 insertions(+), 29 deletions(-) diff --git a/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/dump/APIClient.java b/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/dump/APIClient.java index ce479355df..74d02aacf9 100644 --- a/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/dump/APIClient.java +++ b/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/dump/APIClient.java @@ -3,26 +3,20 @@ package eu.dnetlib.dhp.oa.graph.dump; import java.io.*; import java.io.IOException; +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.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; -// 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; import com.google.gson.Gson; import eu.dnetlib.dhp.oa.graph.dump.zenodo.ZenodoModel; @@ -31,7 +25,7 @@ public class APIClient implements Serializable { String urlString; String bucket; - HttpClient client; + String deposition_id; final String ACCESS_TOKEN = "5ImUj0VC1ICg4ifK5dc3AGzJhcfAB4osxrFlsr8WxHXxjaYgCE0hY8HZcDoe"; @@ -61,7 +55,7 @@ public class APIClient implements Serializable { String json = "{}"; - client = new DefaultHttpClient(); + HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(urlString); @@ -80,22 +74,51 @@ public class APIClient implements Serializable { return response.getStatusLine().getStatusCode(); + } public void upload(String filePath, String file_name) throws IOException { + HttpClient client = new DefaultHttpClient(); File file = new File(filePath); - HttpPut post = new HttpPut(bucket + "/" + file_name); - post.setHeader("Authorization", "Bearer " + ACCESS_TOKEN); - post.addHeader("Content-Type", "application/zip"); + HttpPut put = new HttpPut(bucket + "/" + file_name); + put.setHeader("Authorization", "Bearer " + ACCESS_TOKEN); + put.addHeader("Content-Type", "application/zip"); HttpEntity data = MultipartEntityBuilder.create().addBinaryBody(file_name, file).build(); - post.setEntity(data); + put.setEntity(data); - // HttpUriRequest request = RequestBuilder.post(bucket + "/" + file_name - // +"?access_token=5ImUj0VC1ICg4ifK5dc3AGzJhcfAB4osxrFlsr8WxHXxjaYgCE0hY8HZcDoe").setEntity(data).build(); + HttpResponse response = client.execute(put); + + String json = EntityUtils.toString(response.getEntity()); + + ZenodoModel newSubmission = new Gson().fromJson(json, ZenodoModel.class); - HttpResponse response = client.execute(post); System.out.println(response.getStatusLine().getStatusCode()); } + public void 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); + System.out.println(response.getStatusLine().getStatusCode()); + + + } + + public void 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); + + System.out.println(response.getStatusLine().getStatusCode()); + } + } diff --git a/dhp-workflows/dhp-graph-mapper/src/test/java/eu/dnetlib/dhp/oa/graph/dump/ZenodoUploadTest.java b/dhp-workflows/dhp-graph-mapper/src/test/java/eu/dnetlib/dhp/oa/graph/dump/ZenodoUploadTest.java index 09cb5ffb27..c6a3b0a4dc 100644 --- a/dhp-workflows/dhp-graph-mapper/src/test/java/eu/dnetlib/dhp/oa/graph/dump/ZenodoUploadTest.java +++ b/dhp-workflows/dhp-graph-mapper/src/test/java/eu/dnetlib/dhp/oa/graph/dump/ZenodoUploadTest.java @@ -2,7 +2,12 @@ package eu.dnetlib.dhp.oa.graph.dump; import java.io.IOException; +import java.util.Arrays; +import com.google.gson.Gson; +import eu.dnetlib.dhp.oa.graph.dump.zenodo.Creator; +import eu.dnetlib.dhp.oa.graph.dump.zenodo.Metadata; +import eu.dnetlib.dhp.oa.graph.dump.zenodo.ZenodoModel; import org.apache.commons.io.IOUtils; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -17,21 +22,57 @@ public class ZenodoUploadTest { Assertions.assertEquals(201, s.connect()); - final String sourcePath = getClass() - .getResource("/eu/dnetlib/dhp/oa/graph/dump/zenodo/ni") - .getPath(); + s.upload(getClass() + .getResource("/eu/dnetlib/dhp/oa/graph/dump/zenodo/ni") + .getPath(), "Neuroinformatics"); - s.upload(sourcePath, "Neuroinformatics"); + s.upload(getClass() + .getResource("/eu/dnetlib/dhp/oa/graph/dump/zenodo/dh-ch") + .getPath(), "DigitalHumanitiesandCulturalHeritage"); + + s.upload(getClass() + .getResource("/eu/dnetlib/dhp/oa/graph/dump/zenodo/egi") + .getPath(), "EGI"); + + s.upload(getClass() + .getResource("/eu/dnetlib/dhp/oa/graph/dump/zenodo/science-innovation-policy") + .getPath(), "ScienceandInnovationPolicyStudies"); + +// +// +// String data = "{\"metadata\": {\"title\": \"My first upload\", " + +// "\"upload_type\": \"poster\", " + +// "\"description\": \"This is my first upload\", " + +// "\"creators\": [{\"name\": \"Doe, John\", " + +// "\"affiliation': 'Zenodo'}] +//... } +//... } +// +// + ZenodoModel zenodo = new ZenodoModel(); + Metadata data = new Metadata(); + + data.setTitle("Dump of OpenAIRE Communities related graph"); + data.setUpload_type("dataset"); + data.setDescription("this is a fake uploade done for testing purposes"); + Creator c = new Creator(); + c.setName("Miriam Baglioni"); + c.setAffiliation("CNR _ISTI"); + data.setCreators(Arrays.asList(c)); + zenodo.setMetadata(data); + + s.sendMretadata(new Gson().toJson(zenodo)); + + s.publish(); } -// -// @Test -// public void testGet() throws IOException { -// APIClient s = new APIClient("https://sandbox.zenodo.org/api/deposit/depositions"); -// -// s.get(); -// } + + @Test + public void testPublish() throws IOException { + APIClient s = new APIClient("https://sandbox.zenodo.org/api/deposit/depositions"); + s.publish(); + } @Test public void testUpload() throws IOException { @@ -43,5 +84,7 @@ public class ZenodoUploadTest { s.upload(sourcePath, "Neuroinformatics"); + + } }