Skip to content

Instantly share code, notes, and snippets.

@palcu
Last active August 29, 2016 19:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palcu/5c8bf5d2c643e4f53ea6385260b8da0c to your computer and use it in GitHub Desktop.
Save palcu/5c8bf5d2c643e4f53ea6385260b8da0c to your computer and use it in GitHub Desktop.
CKAN Java create/update a resource
import java.io.File;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
public class CkanClient {
private final String ckanUrl;
private final String authorizationToken;
public CkanClient(String ckanUrl, String authorizationToken) {
this.ckanUrl = ckanUrl;
this.authorizationToken = authorizationToken;
}
public void createResource(String filePath, String packageId) {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
String uploadFileUrl = ckanUrl + "/api/action/resource_create";
HttpPost httpPostRequest = new HttpPost(uploadFileUrl);
httpPostRequest.setHeader("Authorization", authorizationToken);
HttpEntity mpEntity = MultipartEntityBuilder.create().addBinaryBody("upload", new File(filePath))
.addTextBody("package_id", packageId).addTextBody("url", "").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 "
+ uploadFileUrl + " was: " + response.getStatusLine());
}
HttpEntity responseEntity = response.getEntity();
System.out.println(responseEntity.toString());
} catch (IOException e) {
// log error
}
}
public void updateResource(String filePath, String resourceId) {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
String uploadFileUrl = ckanUrl + "/api/action/resource_update";
HttpPost httpPostRequest = new HttpPost(uploadFileUrl);
httpPostRequest.setHeader("Authorization", authorizationToken);
HttpEntity mpEntity = MultipartEntityBuilder.create().addBinaryBody("upload", new File(filePath))
.addTextBody("id", resourceId).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 "
+ uploadFileUrl + " was: " + response.getStatusLine());
}
HttpEntity responseEntity = response.getEntity();
System.out.println(responseEntity.toString());
} catch (IOException e) {
// log error
}
}
}
public class Main {
public static void main(String[] args) {
String resourceId = "04eccb05-50bd-4c44-9895-b5bb82021a6c";
String ckanUrl = "http://ec2-52-207-239-97.compute-1.amazonaws.com:5000";
String authorizationToken = "889277a1-1a57-4c64-a03c-3f0785d46cf5";
String filePath = "/Users/alex/gov/anaf/18_05/mari3.csv";
String packageId = "anaf";
CkanClient ckanClient = new CkanClient(ckanUrl, authorizationToken);
ckanClient.updateResource(filePath, resourceId);
ckanClient.createResource(filePath, packageId);
}
}
@fabiobarbosa1984
Copy link

This code helped me a lot!
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment