Search in sources :

Example 16 with HttpResponse

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

the class StreamViewHttpHandlerTest method execute.

private <T> T execute(int expectedCode, HttpRequest request, Type type) throws IOException {
    HttpResponse response = HttpRequests.execute(request);
    Assert.assertEquals(String.format("Got '%s' for path '%s'", response.getResponseMessage(), request.getURL().toString()), expectedCode, response.getResponseCode());
    if (type != null) {
        try {
            return ObjectResponse.<T>fromJsonBody(response, type, GSON).getResponseObject();
        } catch (JsonSyntaxException e) {
            throw new RuntimeException("Couldn't decode body as JSON: " + response.getResponseBodyAsString(), e);
        }
    }
    return null;
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) HttpResponse(co.cask.common.http.HttpResponse)

Example 17 with HttpResponse

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

the class ClientMessagingService method rollback.

@Override
public void rollback(TopicId topicId, RollbackDetail rollbackDetail) throws TopicNotFoundException, IOException {
    ByteBuffer requestBody = (rollbackDetail instanceof ClientRollbackDetail) ? ByteBuffer.wrap(((ClientRollbackDetail) rollbackDetail).getEncoded()) : encodeRollbackDetail(rollbackDetail);
    HttpRequest httpRequest = remoteClient.requestBuilder(HttpMethod.POST, createTopicPath(topicId) + "/rollback").addHeader(HttpHeaders.CONTENT_TYPE, "avro/binary").withBody(requestBody).build();
    HttpResponse response = remoteClient.execute(httpRequest);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
    }
    handleError(response, "Failed to rollback message in topic " + topicId + " with rollback detail " + rollbackDetail);
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) HttpResponse(co.cask.common.http.HttpResponse) ByteBuffer(java.nio.ByteBuffer)

Example 18 with HttpResponse

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

the class ClientMessagingService method updateTopic.

@Override
public void updateTopic(TopicMetadata topicMetadata) throws TopicNotFoundException, IOException {
    TopicId topicId = topicMetadata.getTopicId();
    HttpRequest request = remoteClient.requestBuilder(HttpMethod.PUT, createTopicPath(topicId) + "/properties").withBody(GSON.toJson(topicMetadata.getProperties())).build();
    HttpResponse response = remoteClient.execute(request);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
    }
    handleError(response, "Failed to update topic " + topicId);
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) HttpResponse(co.cask.common.http.HttpResponse) TopicId(co.cask.cdap.proto.id.TopicId)

Example 19 with HttpResponse

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

the class ClientMessagingService method publish.

@Nullable
@Override
public RollbackDetail publish(StoreRequest request) throws TopicNotFoundException, IOException {
    HttpResponse response = performWriteRequest(request, true);
    byte[] body = response.getResponseBody();
    if (body.length == 0) {
        return null;
    }
    // It has rollback detail, verify the content-type and decode it
    verifyContentType(response.getHeaders().asMap(), "avro/binary");
    return new ClientRollbackDetail(body);
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) Nullable(javax.annotation.Nullable)

Example 20 with HttpResponse

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

the class ClientMessagingService method getTopic.

@Override
public TopicMetadata getTopic(TopicId topicId) throws TopicNotFoundException, IOException {
    HttpRequest request = remoteClient.requestBuilder(HttpMethod.GET, createTopicPath(topicId)).build();
    HttpResponse response = remoteClient.execute(request);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
    }
    handleError(response, "Failed to update topic " + topicId);
    Map<String, String> properties = GSON.fromJson(response.getResponseBodyAsString(), TOPIC_PROPERTY_TYPE);
    return new TopicMetadata(topicId, properties);
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) HttpResponse(co.cask.common.http.HttpResponse) TopicMetadata(co.cask.cdap.messaging.TopicMetadata)

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