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;
}
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);
}
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);
}
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);
}
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);
}
Aggregations