master #59

Closed
claudio.atzori wants to merge 3221 commits from master into stable_ids
2 changed files with 80 additions and 29 deletions
Showing only changes of commit d9506035e4 - Show all commits

View File

@ -90,36 +90,68 @@ public class ZenodoAPIClient implements Serializable {
return responseCode; return responseCode;
} }
// /**
// * Upload files in Zenodo.
// *
// * @param is the inputStream for the file to upload
// * @param file_name the name of the file as it will appear on Zenodo
// * @return the response code
// */
// public int uploadIS(InputStream is, String file_name) throws IOException {
//
// URL url = new URL(bucket + "/" + file_name);
// HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// conn.setChunkedStreamingMode(8192);
// conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, "application/zip");
// conn.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + access_token);
// conn.setDoOutput(true);
// conn.setRequestMethod("PUT");
// conn.setRequestProperty("connection", "close");
//
// byte[] buf = new byte[8192];
// int length;
// try (OutputStream os = conn.getOutputStream()) {
//
// while ((length = is.read(buf)) != -1) {
// os.write(buf, 0, length);
// }
//
// }
// int responseCode = conn.getResponseCode();
// if (!checkOKStatus(responseCode)) {
// throw new IOException("Unexpected code " + responseCode + getBody(conn));
// }
//
// return responseCode;
// }
/** /**
* Upload files in Zenodo. * Upload files in Zenodo.
* *
* @param is the inputStream for the file to upload * @param is the inputStream for the file to upload
* @param file_name the name of the file as it will appear on Zenodo * @param file_name the name of the file as it will appear on Zenodo
* @param len the size of the file
* @return the response code * @return the response code
*/ */
public int uploadIS(InputStream is, String file_name) throws IOException { public int uploadIS(InputStream is, String file_name, long len) throws IOException {
OkHttpClient httpClient = new OkHttpClient.Builder()
.writeTimeout(600, TimeUnit.SECONDS)
.readTimeout(600, TimeUnit.SECONDS)
.connectTimeout(600, TimeUnit.SECONDS)
.build();
URL url = new URL(bucket + "/" + file_name); Request request = new Request.Builder()
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); .url(bucket + "/" + file_name)
conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, "application/zip"); .addHeader(HttpHeaders.CONTENT_TYPE, "application/zip") // add request headers
conn.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + access_token); .addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + access_token)
conn.setDoOutput(true); .put(InputStreamRequestBody.create(MEDIA_TYPE_ZIP, is, len))
conn.setRequestMethod("PUT"); .build();
byte[] buf = new byte[8192]; try (Response response = httpClient.newCall(request).execute()) {
int length; if (!response.isSuccessful())
try (OutputStream os = conn.getOutputStream()) { throw new IOException("Unexpected code " + response + response.body().string());
while ((length = is.read(buf)) != -1) { return response.code();
os.write(buf, 0, length);
} }
}
int responseCode = conn.getResponseCode();
if (!checkOKStatus(responseCode)) {
throw new IOException("Unexpected code " + responseCode + getBody(conn));
}
return responseCode;
} }
@NotNull @NotNull
@ -172,8 +204,8 @@ public class ZenodoAPIClient implements Serializable {
private boolean checkOKStatus(int responseCode) { private boolean checkOKStatus(int responseCode) {
if (HttpURLConnection.HTTP_OK != responseCode || if (HttpURLConnection.HTTP_OK == responseCode ||
HttpURLConnection.HTTP_CREATED != responseCode) HttpURLConnection.HTTP_CREATED == responseCode)
return true; return true;
return false; return false;
} }

View File

@ -15,7 +15,7 @@ import org.junit.jupiter.api.Test;
class ZenodoAPIClientTest { class ZenodoAPIClientTest {
private final String URL_STRING = "https://sandbox.zenodo.org/api/deposit/depositions"; private final String URL_STRING = "https://sandbox.zenodo.org/api/deposit/depositions";
private final String ACCESS_TOKEN = ""; private final String ACCESS_TOKEN = "OzzOsyucEIHxCEfhlpsMo3myEiwpCza3trCRL7ddfGTAK9xXkIP2MbXd6Vg4";
private final String CONCEPT_REC_ID = "657113"; private final String CONCEPT_REC_ID = "657113";
@ -33,7 +33,7 @@ class ZenodoAPIClientTest {
InputStream is = new FileInputStream(file); InputStream is = new FileInputStream(file);
Assertions.assertEquals(200, client.uploadIS(is, "COVID-19.json.gz")); Assertions.assertEquals(200, client.uploadIS(is, "COVID-19.json.gz", file.length()));
String metadata = IOUtils.toString(getClass().getResourceAsStream("/eu/dnetlib/dhp/common/api/metadata.json")); String metadata = IOUtils.toString(getClass().getResourceAsStream("/eu/dnetlib/dhp/common/api/metadata.json"));
@ -51,18 +51,18 @@ class ZenodoAPIClientTest {
Assertions.assertEquals(201, client.newDeposition()); Assertions.assertEquals(201, client.newDeposition());
File file = new File(getClass() File file = new File(getClass()
.getResource("/eu/dnetlib/dhp/common/api/COVID-19.json.gz") .getResource("/eu/dnetlib/dhp/common/api/newVersion")
.getPath()); .getPath());
InputStream is = new FileInputStream(file); InputStream is = new FileInputStream(file);
Assertions.assertEquals(200, client.uploadIS(is, "COVID-19.json.gz")); Assertions.assertEquals(200, client.uploadIS(is, "COVID-19.json.gz", file.length()));
String metadata = IOUtils.toString(getClass().getResourceAsStream("/eu/dnetlib/dhp/common/api/metadata.json")); String metadata = IOUtils.toString(getClass().getResourceAsStream("/eu/dnetlib/dhp/common/api/metadata.json"));
Assertions.assertEquals(200, client.sendMretadata(metadata)); Assertions.assertEquals(200, client.sendMretadata(metadata));
Assertions.assertEquals(202, client.publish()); // Assertions.assertEquals(202, client.publish());
} }
@ -80,7 +80,7 @@ class ZenodoAPIClientTest {
InputStream is = new FileInputStream(file); InputStream is = new FileInputStream(file);
Assertions.assertEquals(200, client.uploadIS(is, "newVersion_deposition")); Assertions.assertEquals(200, client.uploadIS(is, "newVersion_deposition", file.length()));
Assertions.assertEquals(202, client.publish()); Assertions.assertEquals(202, client.publish());
@ -100,10 +100,29 @@ class ZenodoAPIClientTest {
InputStream is = new FileInputStream(file); InputStream is = new FileInputStream(file);
Assertions.assertEquals(200, client.uploadIS(is, "newVersion_deposition")); Assertions.assertEquals(200, client.uploadIS(is, "newVersion_deposition", file.length()));
Assertions.assertEquals(202, client.publish()); Assertions.assertEquals(202, client.publish());
} }
@Test
void depositBigFile() throws MissingConceptDoiException, IOException {
ZenodoAPIClient client = new ZenodoAPIClient(URL_STRING,
ACCESS_TOKEN);
Assertions.assertEquals(201, client.newDeposition());
File file = new File("/Users/miriam.baglioni/Desktop/EOSC_DUMP/publication.tar");
// File file = new File(getClass()
// .getResource("/eu/dnetlib/dhp/common/api/newVersion2")
// .getPath());
InputStream is = new FileInputStream(file);
Assertions.assertEquals(200, client.uploadIS(is, "newVersion_deposition", file.length()));
//Assertions.assertEquals(202, client.publish());
}
} }