Exemplo n.º 1
0
  public PingInfo ping(String host, int restPort, boolean noProxy) throws Exception {

    PingInfo myPingInfo = pingInfoProvider.createPingInfo();

    RequestConfig.Builder requestConfigBuilder =
        httpClientProvider.createRequestConfigBuilder(host, noProxy);

    String url = String.format(URL_PATTERN, host, restPort) + PingHandler.URL;
    HttpUriRequest request =
        RequestBuilder.post(url)
            .setConfig(requestConfigBuilder.build())
            .addParameter(PingHandler.PING_INFO_INPUT_NAME, gson.toJson(myPingInfo))
            .build();

    CloseableHttpResponse response = httpClientProvider.executeRequest(request);
    try {
      StatusLine statusLine = response.getStatusLine();
      if (statusLine.getStatusCode() != HttpStatus.SC_OK) {
        EntityUtils.consumeQuietly(response.getEntity());
        throw new Exception(statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
      }

      HttpEntity entity = response.getEntity();
      String content = EntityUtils.toString(entity);
      EntityUtils.consumeQuietly(entity);
      PingInfo receivedPingInfo = gson.fromJson(content, PingInfo.class);
      receivedPingInfo.getAgentId().setHost(request.getURI().getHost());
      return receivedPingInfo;
    } finally {
      response.close();
    }
  }