예제 #1
0
파일: GetIpcn.java 프로젝트: ChMrWang/vote
 public static String getByFluent(String uri) throws ClientProtocolException, IOException {
   Request req = Request.Get(uri);
   req.addHeader("User-Agent", "curl");
   Response res = req.execute();
   String back = res.returnContent().asString();
   return back;
 }
예제 #2
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);
    }
  }
  protected String retrieveServerResponse(String text) {
    Request req = Request.Post(getTagMeServiceUrl().toString());

    req.addHeader("Content-Type", "application/x-www-form-urlencoded");
    req.bodyForm(
        Form.form()
            .add("text", text)
            .add("gcube-token", getApiKey())
            .add("lang", getLanguageCode())
            .add("tweet", getIsTweet().toString())
            .add("include_abstract", "false")
            .add("include_categories", "false")
            .add("include_all_spots", "false")
            .add("long_text", "0")
            .add("epsilon", getEpsilon().toString())
            .build(),
        Consts.UTF_8);
    logger.debug("Request is " + req);
    Response res = null;
    try {
      res = req.execute();
    } catch (Exception ex) {
      throw new GateRuntimeException("Problem executing HTTP request: " + req, ex);
    }
    Content cont = null;
    try {
      cont = res.returnContent();
    } catch (Exception ex) {
      throw new GateRuntimeException("Problem getting HTTP response content: " + res, ex);
    }
    String ret = cont.asString();
    logger.debug("TagMe server response " + ret);
    return ret;
  }
예제 #4
0
 @When("^\"([^\"]*)\" checks for the availability of the upload endpoint$")
 public void optionUpload(String username) throws Throwable {
   AccessToken accessToken = userStepdefs.tokenByUser.get(username);
   Request request = Request.Options(uploadUri);
   if (accessToken != null) {
     request.addHeader("Authorization", accessToken.serialize());
   }
   response = request.execute().returnResponse();
 }
예제 #5
0
 @When("^\"([^\"]*)\" upload a content without content type$")
 public void userUploadContentWithoutContentType(String username) throws Throwable {
   AccessToken accessToken = userStepdefs.tokenByUser.get(username);
   Request request = Request.Post(uploadUri).bodyByteArray("some text".getBytes(Charsets.UTF_8));
   if (accessToken != null) {
     request.addHeader("Authorization", accessToken.serialize());
   }
   response = request.execute().returnResponse();
 }
예제 #6
0
 @Then("^\"([^\"]*)\" should be able to retrieve the content$")
 public void contentShouldBeRetrievable(String username) throws Exception {
   AccessToken accessToken = userStepdefs.tokenByUser.get(username);
   Request request =
       Request.Get(mainStepdefs.baseUri().setPath("/download/" + _1M_ZEROED_FILE_BLOB_ID).build());
   if (accessToken != null) {
     request.addHeader("Authorization", accessToken.serialize());
   }
   response = request.execute().returnResponse();
   httpAuthorizedStatus();
 }
예제 #7
0
 @When("^\"([^\"]*)\" upload a too big content$")
 public void userUploadTooBigContent(String username) throws Throwable {
   AccessToken accessToken = userStepdefs.tokenByUser.get(username);
   Request request =
       Request.Post(uploadUri)
           .bodyStream(
               new BufferedInputStream(new ZeroedInputStream(_10M), _10M),
               org.apache.http.entity.ContentType.DEFAULT_BINARY);
   if (accessToken != null) {
     request.addHeader("Authorization", accessToken.serialize());
   }
   response = request.execute().returnResponse();
 }
예제 #8
0
 /**
  * Returns the latest api version supported by the catalog
  *
  * @throws JackanException on error
  */
 private synchronized int getApiVersion(int number) {
   String fullUrl = catalogURL + "/api/" + number;
   logger.log(Level.FINE, "getting {0}", fullUrl);
   try {
     Request request = Request.Get(fullUrl);
     if (proxy != null) {
       request.viaProxy(proxy);
     }
     String json = request.execute().returnContent().asString();
     return getObjectMapper().readValue(json, ApiVersionResponse.class).version;
   } catch (Exception ex) {
     throw new JackanException("Error while fetching api version!", this, ex);
   }
 }