Search in sources :

Example 61 with HttpResponse

use of co.cask.common.http.HttpResponse in project cdap by caskdata.

the class SecureStoreClient method getKeyMetadata.

/**
   * Get the metadata associated with the given secure key
   *
   * @param secureKeyId {@link SecureKeyId} secure key name
   * @return {@link SecureStoreMetadata} metadata associated with the key
   * @throws IOException if a network error occurred
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   * @throws SecureKeyNotFoundException if secure key or the namespace is not found
   */
public SecureStoreMetadata getKeyMetadata(SecureKeyId secureKeyId) throws IOException, UnauthenticatedException, SecureKeyNotFoundException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(secureKeyId.getParent(), String.format("%s/metadata", getSecureKeyPath(secureKeyId)));
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new SecureKeyNotFoundException(secureKeyId);
    }
    return GSON.fromJson(response.getResponseBodyAsString(), SecureStoreMetadata.class);
}
Also used : SecureKeyNotFoundException(co.cask.cdap.common.SecureKeyNotFoundException) HttpResponse(co.cask.common.http.HttpResponse) URL(java.net.URL)

Example 62 with HttpResponse

use of co.cask.common.http.HttpResponse in project cdap by caskdata.

the class SecureStoreClient method deleteKey.

/**
   * Delete the secure key
   * @param secureKeyId {@link SecureKeyId} secure key name
   * @throws IOException if a network error occurred
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   * @throws SecureKeyNotFoundException if secure key or the namespace is not found
   */
public void deleteKey(SecureKeyId secureKeyId) throws IOException, UnauthenticatedException, SecureKeyNotFoundException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(secureKeyId.getParent(), getSecureKeyPath(secureKeyId));
    HttpResponse response = restClient.execute(HttpMethod.DELETE, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new SecureKeyNotFoundException(secureKeyId);
    }
}
Also used : SecureKeyNotFoundException(co.cask.cdap.common.SecureKeyNotFoundException) HttpResponse(co.cask.common.http.HttpResponse) URL(java.net.URL)

Example 63 with HttpResponse

use of co.cask.common.http.HttpResponse in project cdap by caskdata.

the class ServiceClient method get.

/**
   * Gets a {@link ServiceSpecification} for a {@link Service}.
   *
   * @param service ID of the service
   * @return {@link ServiceSpecification} representing the service
   * @throws IOException if a network error occurred
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   * @throws NotFoundException if the app or service could not be found
   */
public ServiceSpecification get(ProgramId service) throws IOException, UnauthenticatedException, NotFoundException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(service.getNamespaceId(), String.format("apps/%s/versions/%s/services/%s", service.getApplication(), service.getVersion(), service.getProgram()));
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(service);
    }
    return ObjectResponse.fromJsonBody(response, ServiceSpecification.class).getResponseObject();
}
Also used : ServiceSpecification(co.cask.cdap.api.service.ServiceSpecification) HttpResponse(co.cask.common.http.HttpResponse) NotFoundException(co.cask.cdap.common.NotFoundException) URL(java.net.URL)

Example 64 with HttpResponse

use of co.cask.common.http.HttpResponse in project cdap by caskdata.

the class DatasetClient method update.

/**
   * Updates the properties of a dataset.
   *
   * @param instance the dataset to update
   * @param properties properties to set
   * @throws NotFoundException if the dataset is not found
   * @throws IOException if a network error occurred
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   */
public void update(DatasetId instance, Map<String, String> properties) throws NotFoundException, IOException, UnauthenticatedException, ConflictException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(instance.getParent(), String.format("data/datasets/%s/properties", instance.getDataset()));
    HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(properties)).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_CONFLICT);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(instance);
    } else if (response.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) {
        throw new ConflictException(response.getResponseBodyAsString());
    }
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) ConflictException(co.cask.cdap.common.ConflictException) HttpResponse(co.cask.common.http.HttpResponse) DatasetNotFoundException(co.cask.cdap.common.DatasetNotFoundException) DatasetTypeNotFoundException(co.cask.cdap.common.DatasetTypeNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) URL(java.net.URL)

Example 65 with HttpResponse

use of co.cask.common.http.HttpResponse in project cdap by caskdata.

the class DatasetClient method exists.

/**
   * Checks if a dataset exists.
   *
   * @param instance the dataset to check
   * @return true if the dataset exists
   * @throws IOException if a network error occurred
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   */
public boolean exists(DatasetId instance) throws IOException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(instance.getParent(), String.format("data/datasets/%s", instance.getDataset()));
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    return response.getResponseCode() != HttpURLConnection.HTTP_NOT_FOUND;
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) URL(java.net.URL)

Aggregations

HttpResponse (co.cask.common.http.HttpResponse)204 URL (java.net.URL)145 HttpRequest (co.cask.common.http.HttpRequest)77 NotFoundException (co.cask.cdap.common.NotFoundException)41 TypeToken (com.google.common.reflect.TypeToken)26 ProgramNotFoundException (co.cask.cdap.common.ProgramNotFoundException)24 Test (org.junit.Test)24 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)19 BadRequestException (co.cask.cdap.common.BadRequestException)17 IOException (java.io.IOException)14 ExploreException (co.cask.cdap.explore.service.ExploreException)12 ServiceManager (co.cask.cdap.test.ServiceManager)12 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)10 ApplicationManager (co.cask.cdap.test.ApplicationManager)10 StreamNotFoundException (co.cask.cdap.common.StreamNotFoundException)8 AccessToken (co.cask.cdap.security.authentication.client.AccessToken)6 TypeToken (com.google.gson.reflect.TypeToken)6 List (java.util.List)6 TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)5 DatasetTypeNotFoundException (co.cask.cdap.common.DatasetTypeNotFoundException)5