added method to upload a resource given a file

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@134577 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-11-23 11:52:08 +00:00
parent b3a3ae37d2
commit af25dfd8b0
2 changed files with 62 additions and 3 deletions

View File

@ -1,5 +1,6 @@
package org.gcube.datacatalogue.ckanutillibrary; package org.gcube.datacatalogue.ckanutillibrary;
import java.io.File;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -351,4 +352,15 @@ public interface DataCatalogue {
* @return a list of groups * @return a list of groups
*/ */
List<CkanGroup> getGroups(); List<CkanGroup> getGroups();
/**
* Upload a file to ckan and attach it as resource.
* @param file
* @param packageId
* @param authorizationToken
* @param fileName
* @param description
* @return true on success, false otherwise
*/
boolean uploadResourceFile(File file, String packageId, String authorizationToken, String fileName, String description);
} }

View File

@ -4,7 +4,9 @@ import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -38,11 +40,14 @@ import org.slf4j.LoggerFactory;
import eu.trentorise.opendata.jackan.CheckedCkanClient; import eu.trentorise.opendata.jackan.CheckedCkanClient;
import eu.trentorise.opendata.jackan.CkanClient; import eu.trentorise.opendata.jackan.CkanClient;
import eu.trentorise.opendata.jackan.exceptions.JackanException; import eu.trentorise.opendata.jackan.exceptions.JackanException;
import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpEntity;
import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpResponse; import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpResponse;
import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpStatus; import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpStatus;
import eu.trentorise.opendata.jackan.internal.org.apache.http.client.methods.HttpGet; import eu.trentorise.opendata.jackan.internal.org.apache.http.client.methods.HttpGet;
import eu.trentorise.opendata.jackan.internal.org.apache.http.client.methods.HttpPost; import eu.trentorise.opendata.jackan.internal.org.apache.http.client.methods.HttpPost;
import eu.trentorise.opendata.jackan.internal.org.apache.http.entity.ContentType;
import eu.trentorise.opendata.jackan.internal.org.apache.http.entity.StringEntity; import eu.trentorise.opendata.jackan.internal.org.apache.http.entity.StringEntity;
import eu.trentorise.opendata.jackan.internal.org.apache.http.entity.mime.MultipartEntityBuilder;
import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.CloseableHttpClient; import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.CloseableHttpClient;
import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.HttpClientBuilder; import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.HttpClientBuilder;
import eu.trentorise.opendata.jackan.model.CkanDataset; import eu.trentorise.opendata.jackan.model.CkanDataset;
@ -1669,4 +1674,46 @@ public class DataCatalogueImpl implements DataCatalogue{
} }
} }
@Override
public boolean uploadResourceFile(File file, String packageId,
String token, String name, String description) {
// checks
checkNotNull(file);
checkNotNull(packageId);
checkNotNull(token);
checkNotNull(name);
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
String apiRequestUrl = CKAN_CATALOGUE_URL + "/api/3/action/resource_create";
HttpPost httpPostRequest = new HttpPost(apiRequestUrl);
httpPostRequest.setHeader("Authorization", token);
HttpEntity mpEntity =
MultipartEntityBuilder.create()
.addTextBody("package_id", packageId)
.addTextBody("url", "upload")
.addTextBody("description", description == null ? "" : description)
.addTextBody("name", name)
.addBinaryBody("upload", file,
ContentType.create(
"application/octet-stream",
Charset.forName("UTF-8")),
name)
.build();
httpPostRequest.setEntity(mpEntity);
HttpResponse response = httpClient.execute(httpPostRequest);
if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() >= 300) {
throw new RuntimeException("failed to add the file to CKAN storage. response status line from "
+ apiRequestUrl + " was: " + response.getStatusLine());
}
} catch (Exception e) {
logger.error("Error while uploading file");
return false;
}
return true;
}
} }