Package co.cask.cdap.common.http

Examples of co.cask.cdap.common.http.HttpResponse


    if (baseURI == null) {
      throw new IllegalStateException("Connection information not set!");
    }

    LOG.debug("Try to get the authentication URI from the gateway server: {}.", baseURI);
    HttpResponse response = HttpRequests.execute(HttpRequest.get(baseURI.toURL()).build(), getHttpRequestConfig());

    LOG.debug("Got response {} - {} from {}", response.getResponseCode(), response.getResponseMessage(), baseURI);
    if (response.getResponseCode() != HttpURLConnection.HTTP_UNAUTHORIZED) {
      return "";
    }

    Map<String, List<String>> responseMap =
      ObjectResponse.fromJsonBody(response,
View Full Code Here


   * @return {@link AccessToken} object containing the access token
   * @throws IOException IOException in case of a problem or the connection was aborted or if the access token is not
   * received successfully from the authentication server
   */
  private AccessToken execute(HttpRequest request) throws IOException {
    HttpResponse response = HttpRequests.execute(request, getHttpRequestConfig());

    LOG.debug("Got response {} - {} from {}", response.getResponseCode(), response.getResponseMessage(), baseURI);
    if (response.getResponseCode() != HttpURLConnection.HTTP_OK) {
      throw new HttpFailureException(response.getResponseMessage(), response.getResponseCode());
    }

    Map<String, String> responseMap =
      ObjectResponse.fromJsonBody(response, new TypeToken<Map<String, String>>() { }).getResponseObject();
    String tokenValue = responseMap.get(ACCESS_TOKEN_KEY);
View Full Code Here

    };
  }

  public List<ACL> listAcls(EntityId entityId) throws IOException {
    URL url = resolveURL(String.format("/v2/admin/acls/%s/%s", entityId.getType().getPluralForm(), entityId.getId()));
    HttpResponse response = HttpRequests.execute(HttpRequest.builder(HttpMethod.GET, url).build());
    return ObjectResponse.fromJsonBody(response, new TypeToken<List<ACL>>() { }).getResponseObject();
  }
View Full Code Here

  }

  public List<ACL> listAcls(EntityId entityId, String userId) throws IOException {
    URL url = resolveURL(String.format("/v2/admin/acls/%s/%s/user/%s", entityId.getType().getPluralForm(),
                                       entityId.getId(), userId));
    HttpResponse response = HttpRequests.execute(HttpRequest.builder(HttpMethod.GET, url).build());
    return ObjectResponse.fromJsonBody(response, new TypeToken<List<ACL>>() { }).getResponseObject();
  }
View Full Code Here

    });
  }

  @Nullable
  public DatasetMeta getInstance(String instanceName) throws DatasetManagementException {
    HttpResponse response = doGet("datasets/" + instanceName);
    if (HttpResponseStatus.NOT_FOUND.getCode() == response.getResponseCode()) {
      return null;
    }
    if (HttpResponseStatus.OK.getCode() != response.getResponseCode()) {
      throw new DatasetManagementException(String.format("Cannot retrieve dataset instance %s info, details: %s",
                                                         instanceName, getDetails(response)));
    }

    return GSON.fromJson(new String(response.getResponseBody(), Charsets.UTF_8), DatasetMeta.class);
  }
View Full Code Here

                     .build(), allowedErrorCodes);
  }

  private HttpResponse execute(HttpRequest request, int... allowedErrorCodes) throws IOException,
    UnAuthorizedAccessTokenException {
    HttpResponse response = HttpRequests.execute(request, defaultConfig);
    int responseCode = response.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
      throw new UnAuthorizedAccessTokenException("Unauthorized status code received from the server.");
    } else if (!isSuccessful(responseCode) && !ArrayUtils.contains(allowedErrorCodes, responseCode)) {
      throw new IOException(responseCode + ": " + response.getResponseBodyAsString());
    }
    return response;
  }
View Full Code Here

    return response;
  }

  public HttpResponse upload(HttpRequest request, AccessToken accessToken, int... allowedErrorCodes)
    throws IOException {
    HttpResponse response = HttpRequests.execute(
      HttpRequest.builder(request).addHeaders(getAuthHeaders(accessToken)).build(), uploadConfig);
    int responseCode = response.getResponseCode();
    if (!isSuccessful(responseCode) && !ArrayUtils.contains(allowedErrorCodes, responseCode)) {
      throw new IOException(response.getResponseBodyAsString());
    }
    return response;
  }
View Full Code Here

    return GSON.fromJson(new String(response.getResponseBody(), Charsets.UTF_8), DatasetMeta.class);
  }

  public Collection<DatasetSpecification> getAllInstances() throws DatasetManagementException {
    HttpResponse response = doGet("datasets");
    if (HttpResponseStatus.OK.getCode() != response.getResponseCode()) {
      throw new DatasetManagementException(String.format("Cannot retrieve all dataset instances, details: %s",
                                                         getDetails(response)));
    }

    return GSON.fromJson(new String(response.getResponseBody(), Charsets.UTF_8),
                         new TypeToken<List<DatasetSpecification>>() { }.getType());
  }
View Full Code Here

    return GSON.fromJson(new String(response.getResponseBody(), Charsets.UTF_8),
                         new TypeToken<List<DatasetSpecification>>() { }.getType());
  }

  public Collection<DatasetModuleMeta> getAllModules() throws DatasetManagementException {
    HttpResponse response = doGet("modules");
    if (HttpResponseStatus.OK.getCode() != response.getResponseCode()) {
      throw new DatasetManagementException(String.format("Cannot retrieve all dataset instances, details: %s",
                                                         getDetails(response)));
    }

    return GSON.fromJson(new String(response.getResponseBody(), Charsets.UTF_8),
                         new TypeToken<List<DatasetModuleMeta>>() { }.getType());
  }
View Full Code Here

    return GSON.fromJson(new String(response.getResponseBody(), Charsets.UTF_8),
                         new TypeToken<List<DatasetModuleMeta>>() { }.getType());
  }

  public DatasetTypeMeta getType(String typeName) throws DatasetManagementException {
    HttpResponse response = doGet("types/" + typeName);
    if (HttpResponseStatus.NOT_FOUND.getCode() == response.getResponseCode()) {
      return null;
    }
    if (HttpResponseStatus.OK.getCode() != response.getResponseCode()) {
      throw new DatasetManagementException(String.format("Cannot retrieve dataset type %s info, details: %s",
                                                         typeName, getDetails(response)));
    }
    return GSON.fromJson(new String(response.getResponseBody(), Charsets.UTF_8), DatasetTypeMeta.class);
  }
View Full Code Here

TOP

Related Classes of co.cask.cdap.common.http.HttpResponse

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.