Search in sources :

Example 26 with HttpResponse

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

the class RemoteSparkManager method checkAvailability.

/**
 * Checks if a user service is available by hitting the availability endpoint.
 */
private void checkAvailability() throws IOException, UnauthenticatedException, NotFoundException {
    URL url = clientConfig.resolveNamespacedURLV3(programId.getNamespaceId(), String.format("apps/%s/versions/%s/%s/%s/available", programId.getApplication(), programId.getVersion(), programId.getType().getCategoryName(), programId.getProgram()));
    HttpResponse response = restClient.execute(HttpMethod.GET, url, clientConfig.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST, HttpURLConnection.HTTP_UNAVAILABLE);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(programId);
    }
    if (response.getResponseCode() == HttpURLConnection.HTTP_UNAVAILABLE) {
        throw new ServiceUnavailableException(programId.toString());
    }
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) NotFoundException(co.cask.cdap.common.NotFoundException) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) URL(java.net.URL)

Example 27 with HttpResponse

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

the class AbstractNamespaceClient method delete.

@Override
public void delete(NamespaceId namespaceId) throws Exception {
    URL url = resolve(String.format("unrecoverable/namespaces/%s", namespaceId.getNamespace()));
    HttpResponse response = execute(HttpRequest.delete(url).build());
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NamespaceNotFoundException(namespaceId);
    } else if (HttpURLConnection.HTTP_FORBIDDEN == response.getResponseCode()) {
        throw new NamespaceCannotBeDeletedException(namespaceId, response.getResponseBodyAsString());
    } else if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
        return;
    }
    throw new IOException(String.format("Cannot delete namespace %s. Reason: %s", namespaceId, response.getResponseBodyAsString()));
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) IOException(java.io.IOException) NamespaceCannotBeDeletedException(co.cask.cdap.common.NamespaceCannotBeDeletedException) URL(java.net.URL) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException)

Example 28 with HttpResponse

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

the class AbstractNamespaceClient method create.

@Override
public void create(NamespaceMeta metadata) throws Exception {
    URL url = resolve(String.format("namespaces/%s", metadata.getName()));
    HttpResponse response = execute(HttpRequest.put(url).withBody(GSON.toJson(metadata)).build());
    String responseBody = response.getResponseBodyAsString();
    if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
        if (responseBody.equals(String.format("Namespace '%s' already exists.", metadata.getName()))) {
            throw new NamespaceAlreadyExistsException(metadata.getNamespaceId());
        }
        return;
    }
    if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException("Bad request: " + responseBody);
    }
    throw new IOException(String.format("Cannot create namespace %s. Reason: %s", metadata.getName(), responseBody));
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) BadRequestException(co.cask.cdap.common.BadRequestException) NamespaceAlreadyExistsException(co.cask.cdap.common.NamespaceAlreadyExistsException) IOException(java.io.IOException) URL(java.net.URL)

Example 29 with HttpResponse

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

the class AbstractNamespaceClient method deleteDatasets.

@Override
public void deleteDatasets(NamespaceId namespaceId) throws Exception {
    URL url = resolve(String.format("unrecoverable/namespaces/%s/datasets", namespaceId.getNamespace()));
    HttpResponse response = execute(HttpRequest.delete(url).build());
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NamespaceNotFoundException(namespaceId);
    } else if (HttpURLConnection.HTTP_FORBIDDEN == response.getResponseCode()) {
        String msg = String.format("Datasets in the namespace '%s' cannot be deleted. Reason: '%s'.", namespaceId, response.getResponseBodyAsString());
        throw new NamespaceCannotBeDeletedException(namespaceId, msg);
    } else if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
        return;
    }
    throw new IOException(String.format("Cannot delete datasets in namespace %s. Reason: %s", namespaceId, response.getResponseBodyAsString()));
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) IOException(java.io.IOException) NamespaceCannotBeDeletedException(co.cask.cdap.common.NamespaceCannotBeDeletedException) URL(java.net.URL) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException)

Example 30 with HttpResponse

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

the class AbstractNamespaceClient method updateProperties.

@Override
public void updateProperties(NamespaceId namespaceId, NamespaceMeta metadata) throws Exception {
    URL url = resolve(String.format("namespaces/%s/properties", namespaceId.getNamespace()));
    HttpResponse response = execute(HttpRequest.put(url).withBody(GSON.toJson(metadata)).build());
    String responseBody = response.getResponseBodyAsString();
    if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
        return;
    }
    if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException("Bad request: " + responseBody);
    }
    throw new IOException(String.format("Cannot update namespace %s. Reason: %s", namespaceId, responseBody));
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) BadRequestException(co.cask.cdap.common.BadRequestException) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

HttpResponse (co.cask.common.http.HttpResponse)216 URL (java.net.URL)147 HttpRequest (co.cask.common.http.HttpRequest)80 NotFoundException (co.cask.cdap.common.NotFoundException)42 TypeToken (com.google.common.reflect.TypeToken)26 Test (org.junit.Test)26 ProgramNotFoundException (co.cask.cdap.common.ProgramNotFoundException)24 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)21 BadRequestException (co.cask.cdap.common.BadRequestException)20 IOException (java.io.IOException)16 ExploreException (co.cask.cdap.explore.service.ExploreException)13 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 HashMap (java.util.HashMap)7 List (java.util.List)7 AccessToken (co.cask.cdap.security.authentication.client.AccessToken)6 TypeToken (com.google.gson.reflect.TypeToken)6 TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)5