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
This commit is contained in:
parent
504f947973
commit
9ac732dda2
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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<String, String> requestMap = new HashMap<String, String>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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"));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue