changed the way to PUT the files to the rest API
This commit is contained in:
parent
250fd1c854
commit
afa19b0c84
|
@ -5,13 +5,16 @@ import java.io.*;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.client.HttpClient;
|
import org.apache.http.client.HttpClient;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
import org.apache.http.client.methods.HttpPut;
|
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.StringEntity;
|
||||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
//import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.impl.client.HttpClients;
|
||||||
|
@ -57,7 +60,7 @@ public class APIClient implements Serializable {
|
||||||
|
|
||||||
String json = "{}";
|
String json = "{}";
|
||||||
|
|
||||||
CloseableHttpClient client = HttpClients.createDefault();
|
HttpClient client = new DefaultHttpClient();
|
||||||
|
|
||||||
HttpPost post = new HttpPost(urlString);
|
HttpPost post = new HttpPost(urlString);
|
||||||
|
|
||||||
|
@ -73,7 +76,7 @@ public class APIClient implements Serializable {
|
||||||
ZenodoModel newSubmission = new Gson().fromJson(json, ZenodoModel.class);
|
ZenodoModel newSubmission = new Gson().fromJson(json, ZenodoModel.class);
|
||||||
this.bucket = newSubmission.getLinks().getBucket();
|
this.bucket = newSubmission.getLinks().getBucket();
|
||||||
this.deposition_id = newSubmission.getId();
|
this.deposition_id = newSubmission.getId();
|
||||||
client.close();
|
|
||||||
return response.getStatusLine().getStatusCode();
|
return response.getStatusLine().getStatusCode();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -98,23 +101,31 @@ public class APIClient implements Serializable {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
public int upload(File file, String file_name) throws IOException {
|
public int upload(File file, String file_name) throws IOException {
|
||||||
CloseableHttpClient client = HttpClients.createDefault();
|
HttpClient client = new DefaultHttpClient();
|
||||||
|
|
||||||
HttpPut put = new HttpPut(bucket + "/" + file_name);
|
HttpPut put = new HttpPut(bucket + "/" + file_name);
|
||||||
put.setHeader("Authorization", "Bearer " + access_token);
|
put.setHeader("Authorization", "Bearer " + access_token);
|
||||||
put.addHeader("Content-Type", "application/zip");
|
put.addHeader("Content-Type", "application/zip");
|
||||||
HttpEntity data = MultipartEntityBuilder.create().addBinaryBody(file_name, file).build();
|
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);
|
put.setEntity(data);
|
||||||
|
|
||||||
HttpResponse response = client.execute(put);
|
HttpResponse response = client.execute(put);
|
||||||
client.close();
|
|
||||||
return response.getStatusLine().getStatusCode();
|
return response.getStatusLine().getStatusCode();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int sendMretadata(String metadata) throws IOException {
|
public int sendMretadata(String metadata) throws IOException {
|
||||||
|
|
||||||
CloseableHttpClient client = HttpClients.createDefault();
|
HttpClient client = new DefaultHttpClient();
|
||||||
HttpPut post = new HttpPut(urlString + "/" + deposition_id);
|
HttpPut post = new HttpPut(urlString + "/" + deposition_id);
|
||||||
post.setHeader("Authorization", "Bearer " + access_token);
|
post.setHeader("Authorization", "Bearer " + access_token);
|
||||||
post.addHeader("Content-Type", "application/json");
|
post.addHeader("Content-Type", "application/json");
|
||||||
|
@ -122,18 +133,18 @@ public class APIClient implements Serializable {
|
||||||
post.setEntity(entity);
|
post.setEntity(entity);
|
||||||
|
|
||||||
HttpResponse response = client.execute(post);
|
HttpResponse response = client.execute(post);
|
||||||
client.close();
|
|
||||||
return response.getStatusLine().getStatusCode();
|
return response.getStatusLine().getStatusCode();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int publish() throws IOException {
|
public int publish() throws IOException {
|
||||||
CloseableHttpClient client = HttpClients.createDefault();
|
HttpClient client = new DefaultHttpClient();
|
||||||
HttpPost post = new HttpPost(urlString + "/" + deposition_id + "/actions/publish");
|
HttpPost post = new HttpPost(urlString + "/" + deposition_id + "/actions/publish");
|
||||||
post.setHeader("Authorization", "Bearer " + access_token);
|
post.setHeader("Authorization", "Bearer " + access_token);
|
||||||
|
|
||||||
HttpResponse response = client.execute(post);
|
HttpResponse response = client.execute(post);
|
||||||
client.close();
|
|
||||||
return response.getStatusLine().getStatusCode();
|
return response.getStatusLine().getStatusCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue