forked from D-Net/dnet-hadoop
[ZenodoApi] gone back to okhttp3 to send the payload.
This commit is contained in:
parent
b64a5eb4a5
commit
d9506035e4
|
@ -90,36 +90,68 @@ public class ZenodoAPIClient implements Serializable {
|
|||
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.
|
||||
*
|
||||
* @param is the inputStream for the file to upload
|
||||
* @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
|
||||
*/
|
||||
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);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, "application/zip");
|
||||
conn.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + access_token);
|
||||
conn.setDoOutput(true);
|
||||
conn.setRequestMethod("PUT");
|
||||
Request request = new Request.Builder()
|
||||
.url(bucket + "/" + file_name)
|
||||
.addHeader(HttpHeaders.CONTENT_TYPE, "application/zip") // add request headers
|
||||
.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + access_token)
|
||||
.put(InputStreamRequestBody.create(MEDIA_TYPE_ZIP, is, len))
|
||||
.build();
|
||||
|
||||
byte[] buf = new byte[8192];
|
||||
int length;
|
||||
try (OutputStream os = conn.getOutputStream()) {
|
||||
while ((length = is.read(buf)) != -1) {
|
||||
os.write(buf, 0, length);
|
||||
try (Response response = httpClient.newCall(request).execute()) {
|
||||
if (!response.isSuccessful())
|
||||
throw new IOException("Unexpected code " + response + response.body().string());
|
||||
return response.code();
|
||||
}
|
||||
|
||||
}
|
||||
int responseCode = conn.getResponseCode();
|
||||
if (!checkOKStatus(responseCode)) {
|
||||
throw new IOException("Unexpected code " + responseCode + getBody(conn));
|
||||
}
|
||||
|
||||
return responseCode;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
@ -172,8 +204,8 @@ public class ZenodoAPIClient implements Serializable {
|
|||
|
||||
private boolean checkOKStatus(int responseCode) {
|
||||
|
||||
if (HttpURLConnection.HTTP_OK != responseCode ||
|
||||
HttpURLConnection.HTTP_CREATED != responseCode)
|
||||
if (HttpURLConnection.HTTP_OK == responseCode ||
|
||||
HttpURLConnection.HTTP_CREATED == responseCode)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ import org.junit.jupiter.api.Test;
|
|||
class ZenodoAPIClientTest {
|
||||
|
||||
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";
|
||||
|
||||
|
@ -33,7 +33,7 @@ class ZenodoAPIClientTest {
|
|||
|
||||
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"));
|
||||
|
||||
|
@ -51,18 +51,18 @@ class ZenodoAPIClientTest {
|
|||
Assertions.assertEquals(201, client.newDeposition());
|
||||
|
||||
File file = new File(getClass()
|
||||
.getResource("/eu/dnetlib/dhp/common/api/COVID-19.json.gz")
|
||||
.getResource("/eu/dnetlib/dhp/common/api/newVersion")
|
||||
.getPath());
|
||||
|
||||
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"));
|
||||
|
||||
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);
|
||||
|
||||
Assertions.assertEquals(200, client.uploadIS(is, "newVersion_deposition"));
|
||||
Assertions.assertEquals(200, client.uploadIS(is, "newVersion_deposition", file.length()));
|
||||
|
||||
Assertions.assertEquals(202, client.publish());
|
||||
|
||||
|
@ -100,10 +100,29 @@ class ZenodoAPIClientTest {
|
|||
|
||||
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());
|
||||
|
||||
}
|
||||
|
||||
@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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue