Esempio n. 1
0
  /**
   * Method for http GET
   *
   * @param <T>
   * @param responseType a descendant of CkanResponse
   * @param path something like /api/3/package_show
   * @param params list of key, value parameters. They must be not be url encoded. i.e.
   *     "id","laghi-monitorati-trento"
   * @throws JackanException on error
   */
  <T extends CkanResponse> T getHttp(Class<T> responseType, String path, Object... params) {
    checkNotNull(responseType);
    checkNotNull(path);

    String fullUrl = calcFullUrl(path, params);

    try {
      logger.log(Level.FINE, "getting {0}", fullUrl);
      Request request = Request.Get(fullUrl);
      if (ckanToken != null) {
        request.addHeader("Authorization", ckanToken);
      }
      if (proxy != null) {
        request.viaProxy(proxy);
      }
      String json = request.execute().returnContent().asString();
      T dr = getObjectMapper().readValue(json, responseType);
      if (!dr.success) {
        // monkey patching error type
        throw new JackanException(
            "Reading from catalog "
                + catalogURL
                + " was not successful. Reason: "
                + CkanError.read(getObjectMapper().readTree(json).get("error").asText()));
      }
      return dr;
    } catch (Exception ex) {
      throw new JackanException("Error while performing GET. Request url was: " + fullUrl, ex);
    }
  }
Esempio n. 2
0
  /**
   * @param <T>
   * @param responseType a descendant of CkanResponse
   * @param path something like 1/api/3/action/package_create
   * @param body the body of the POST
   * @param the content type, i.e.
   * @param params list of key, value parameters. They must be not be url encoded. i.e.
   *     "id","laghi-monitorati-trento"
   * @throws JackanException on error
   */
  <T extends CkanResponse> T postHttp(
      Class<T> responseType, String path, String body, ContentType contentType, Object... params) {
    checkNotNull(responseType);
    checkNotNull(path);
    checkNotNull(body);
    checkNotNull(contentType);

    String fullUrl = calcFullUrl(path, params);

    try {

      logger.log(Level.FINE, "posting to url {0}", fullUrl);
      Request request = Request.Post(fullUrl);
      if (proxy != null) {
        request.viaProxy(proxy);
      }
      Response response =
          request.bodyString(body, contentType).addHeader("Authorization", ckanToken).execute();

      Content out = response.returnContent();
      String json = out.asString();

      T dr = getObjectMapper().readValue(json, responseType);
      if (!dr.success) {
        // monkey patching error type
        throw new JackanException(
            "posting to catalog "
                + catalogURL
                + " was not successful. Reason: "
                + CkanError.read(getObjectMapper().readTree(json).get("error").asText()));
      }
      return dr;
    } catch (Exception ex) {
      throw new JackanException("Error while performing a POST! Request url is:" + fullUrl, ex);
    }
  }