change MEDIA_TYPE to avoid error on Zenodo side

This commit is contained in:
Miriam Baglioni 2023-10-24 12:45:55 +02:00
parent f2b890f8a8
commit 176d6d7f2b
1 changed files with 35 additions and 5 deletions

View File

@ -37,6 +37,8 @@ public class ZenodoAPIClient implements Serializable {
private static final MediaType MEDIA_TYPE_ZIP = MediaType.parse("application/zip");
private static final MediaType MEDIA_TYPE_TAR = MediaType.parse("application/octet-stream");
public String getUrlString() {
return urlString;
}
@ -69,12 +71,40 @@ public class ZenodoAPIClient implements Serializable {
* @return response code
* @throws IOException
*/
public int newDeposition2() throws IOException {
String json = "{}";
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
conn.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + access_token);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
try (OutputStream os = conn.getOutputStream()) {
byte[] input = json.getBytes("utf-8");
os.write(input, 0, input.length);
}
String body = getBody(conn);
int responseCode = conn.getResponseCode();
conn.disconnect();
if (!checkOKStatus(responseCode))
throw new IOException("Unexpected code " + responseCode + body);
ZenodoModel newSubmission = new Gson().fromJson(body, ZenodoModel.class);
this.bucket = newSubmission.getLinks().getBucket();
this.deposition_id = newSubmission.getId();
return responseCode;
}
public int newDeposition() throws IOException {
String json = "{}";
OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(600, TimeUnit.SECONDS).build();
RequestBody body = RequestBody.create(json, MEDIA_TYPE_JSON);
RequestBody body = RequestBody.create(json.getBytes());
Request request = new Request.Builder()
.url(urlString)
@ -161,14 +191,14 @@ public class ZenodoAPIClient implements Serializable {
Request request = new Request.Builder()
.url(bucket + "/" + file_name)
.addHeader(HttpHeaders.CONTENT_TYPE, "application/zip") // add request headers
// .addHeader(HttpHeaders.CONTENT_TYPE, "application/json") // add request headers
.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + access_token)
.put(InputStreamRequestBody.create(MEDIA_TYPE_ZIP, is, len))
.put(InputStreamRequestBody.create(MEDIA_TYPE_TAR, is, len))
.build();
try (Response response = httpClient.newCall(request).execute()) {
// if (!response.isSuccessful())
// throw new IOException("Unexpected code " + response + response.body().string());
if (!response.isSuccessful())
System.out.println("Unexpected code " + response + response.body().string());
return response.code();
}
}