Beispiel #1
0
 /**
  * Checks if the provided resource meets the requirements to be created to CKAN.
  *
  * @throws Throwable if requirements aren't met
  */
 private static void checkResource(CkanResourceMinimized resource) {
   checkNotNull(resource);
   checkNotNull(resource.getFormat(), "Ckan resource format must not be null!");
   checkNotEmpty(resource.getName(), "Ckan resource name can't be empty!");
   checkNotNull(resource.getDescription(), "Ckan resource description must not be null!");
   // todo do we need to check mimetype?? checkNotNull(resource.getMimetype());
   checkNotNull(resource.getPackageId(), "Ckan resource parent dataset must not be null!");
   checkNotNull(resource.getUrl(), "Ckan resource url must not be null!");
 }
Beispiel #2
0
  /**
   * Creates ckan resource on the server.
   *
   * @param resource ckan resource object with the minimal set of parameters required. See {@link
   *     CkanResource#CkanResource(java.lang.String, java.lang.String, java.lang.String,
   *     java.lang.String, java.lang.String, java.lang.String)}
   * @return the newly created resource
   * @throws JackanException
   */
  public synchronized CkanResource createResource(CkanResourceMinimized resource) {
    checkResource(resource);

    if (ckanToken == null) {
      throw new JackanException(
          "Tried to create resource" + resource.getName() + ", but ckan token was not set!");
    }

    ObjectMapper objMapper = CkanClient.getObjectMapper();
    String json = null;
    try {
      json = objMapper.writeValueAsString(resource);
    } catch (IOException e) {
      throw new JackanException("Couldn't serialize the provided CkanResource!", e);
    }
    return postHttp(
            ResourceResponse.class,
            "/api/3/action/resource_create",
            json,
            ContentType.APPLICATION_JSON)
        .result;
  }