From 9ac732dda298b25b2306a8cc20eb8851722494b5 Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Thu, 24 Nov 2016 14:48:43 +0000 Subject: [PATCH] fixed method to patch resource (needed for grsf publisher) git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@134685 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../ckanutillibrary/DataCatalogue.java | 16 +++++- .../ckanutillibrary/DataCatalogueImpl.java | 55 ++++++++++++++++++- .../ckanutillibrary/TestDataCatalogueLib.java | 8 +++ 3 files changed, 75 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogue.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogue.java index 96ee29f..dafd40e 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogue.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogue.java @@ -357,10 +357,22 @@ public interface DataCatalogue { * Upload a file to ckan and attach it as resource. * @param file * @param packageId - * @param authorizationToken + * @param apiKey * @param fileName * @param description * @return true on success, false otherwise */ - boolean uploadResourceFile(File file, String packageId, String authorizationToken, String fileName, String description); + boolean uploadResourceFile(File file, String packageId, String apiKey, String fileName, String description); + + /** + * Allows to change the url, the name and the description of a resource. + * @param resourceId + * @param url + * @param name + * @param description + * @param urlType + * @param apiKey + * @return + */ + boolean patchResource(String resourceId, String url, String name, String description, String urlType, String apiKey); } diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogueImpl.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogueImpl.java index 7eb0902..abba010 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogueImpl.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogueImpl.java @@ -1678,7 +1678,7 @@ public class DataCatalogueImpl implements DataCatalogue{ @Override public boolean uploadResourceFile(File file, String packageId, String token, String name, String description) { - + // checks checkNotNull(file); checkNotNull(packageId); @@ -1700,7 +1700,7 @@ public class DataCatalogueImpl implements DataCatalogue{ "application/octet-stream", Charset.forName("UTF-8")), name) - .build(); + .build(); httpPostRequest.setEntity(mpEntity); HttpResponse response = httpClient.execute(httpPostRequest); @@ -1716,4 +1716,55 @@ public class DataCatalogueImpl implements DataCatalogue{ return true; } + + @Override + public boolean patchResource(String resourceId, String url, + String name, String description, String urlType, String apiKey) { + + // checks + checkNotNull(resourceId); + checkNotNull(apiKey); + + logger.debug("Going to change resource with id " + resourceId +"." + + " Request comes from user with key " + apiKey.substring(0, 5) + "****************"); + + + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { + String apiRequestUrl = CKAN_CATALOGUE_URL + "/api/3/action/resource_patch"; + HttpPost httpPostRequest = new HttpPost(apiRequestUrl); + httpPostRequest.setHeader("Authorization", apiKey); + + // Request parameters to be replaced + JSONObject jsonRequest = new JSONObject(); + Map requestMap = new HashMap(); + + requestMap.put("id", resourceId); + if(url != null && !url.isEmpty()) + requestMap.put("url", url); + if(description != null) + requestMap.put("description", description); + if(name != null && !name.isEmpty()) + requestMap.put("name", name); + if(urlType != null) + requestMap.put("url_type", urlType); + + jsonRequest.putAll(requestMap); + StringEntity params = new StringEntity(jsonRequest.toJSONString()); + httpPostRequest.setEntity(params); + + HttpResponse response = httpClient.execute(httpPostRequest); + + if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() >= 300) { + throw new RuntimeException("failed to patch the resource. response status line from " + + apiRequestUrl + " was: " + response.getStatusLine()); + } + + return true; + + }catch(Exception e){ + logger.error("Failed to update the resource ", e); + } + + return false; + } } diff --git a/src/test/java/org/gcube/datacatalogue/ckanutillibrary/TestDataCatalogueLib.java b/src/test/java/org/gcube/datacatalogue/ckanutillibrary/TestDataCatalogueLib.java index f475367..9ebca67 100644 --- a/src/test/java/org/gcube/datacatalogue/ckanutillibrary/TestDataCatalogueLib.java +++ b/src/test/java/org/gcube/datacatalogue/ckanutillibrary/TestDataCatalogueLib.java @@ -393,4 +393,12 @@ public class TestDataCatalogueLib { } + //@Test + public void testResourcePatch() throws Exception{ + DataCatalogueImpl instance = factory.getUtilsPerScope(scope); + String id = "858f5e77-80c2-4cb2-bcfc-77529693dc9a"; + instance.patchResource(id, "http://ftp.d4science.org/previews/69cc0769-de6f-45eb-a842-7be2807e8887.jpg", "new_name_for_testing_patch.csv", "description test", "", instance.getApiKeyFromUsername("costantino_perciante")); + + } + }