use of co.cask.common.http.HttpRequest in project cdap by caskdata.
the class StreamHandlerTest method createStream.
private HttpResponse createStream(StreamId streamId, int... allowedErrorCodes) throws Exception {
URL url = createURL(streamId.getNamespace(), "streams/" + streamId.getEntityName());
HttpRequest request = HttpRequest.put(url).build();
HttpResponse response = HttpRequests.execute(request);
int responseCode = response.getResponseCode();
if (!ArrayUtils.contains(allowedErrorCodes, responseCode)) {
Assert.assertEquals(200, responseCode);
}
return response;
}
use of co.cask.common.http.HttpRequest in project cdap by caskdata.
the class AbstractNamespaceQueryClient method list.
@Override
public List<NamespaceMeta> list() throws Exception {
HttpRequest request = HttpRequest.get(resolve("namespaces")).build();
HttpResponse response = execute(request);
if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
return ObjectResponse.fromJsonBody(response, new TypeToken<List<NamespaceMeta>>() {
}).getResponseObject();
}
throw new IOException(String.format("Cannot list namespaces. Reason: %s", response.getResponseBodyAsString()));
}
use of co.cask.common.http.HttpRequest in project cdap by caskdata.
the class ClientMessagingService method createTopic.
@Override
public void createTopic(TopicMetadata topicMetadata) throws TopicAlreadyExistsException, IOException {
TopicId topicId = topicMetadata.getTopicId();
HttpRequest request = remoteClient.requestBuilder(HttpMethod.PUT, createTopicPath(topicId)).withBody(GSON.toJson(topicMetadata.getProperties())).build();
HttpResponse response = remoteClient.execute(request);
if (response.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) {
throw new TopicAlreadyExistsException(topicId.getNamespace(), topicId.getTopic());
}
handleError(response, "Failed to create topic " + topicId);
}
use of co.cask.common.http.HttpRequest in project cdap by caskdata.
the class ClientMessagingService method performWriteRequest.
/**
* Makes a request to the server for writing to the messaging system
*
* @param request contains information about what to write
* @param publish {@code true} to make publish call, {@code false} to make store call.
* @return the response from the server
* @throws IOException if failed to perform the write operation
* @throws TopicNotFoundException if the topic to write to does not exist
*/
private HttpResponse performWriteRequest(StoreRequest request, boolean publish) throws IOException, TopicNotFoundException {
GenericRecord record = new GenericData.Record(Schemas.V1.PublishRequest.SCHEMA);
if (request.isTransactional()) {
record.put("transactionWritePointer", request.getTransactionWritePointer());
}
record.put("messages", convertPayloads(request));
// Encode the request as avro
ExposedByteArrayOutputStream os = new ExposedByteArrayOutputStream();
Encoder encoder = EncoderFactory.get().directBinaryEncoder(os, null);
DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(Schemas.V1.PublishRequest.SCHEMA);
datumWriter.write(record, encoder);
// Make the publish request
String writeType = publish ? "publish" : "store";
TopicId topicId = request.getTopicId();
HttpRequest httpRequest = remoteClient.requestBuilder(HttpMethod.POST, createTopicPath(topicId) + "/" + writeType).addHeader(HttpHeaders.CONTENT_TYPE, "avro/binary").withBody(os.toByteBuffer()).build();
HttpResponse response = remoteClient.execute(httpRequest);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
}
handleError(response, "Failed to " + writeType + " message to topic " + topicId);
return response;
}
use of co.cask.common.http.HttpRequest in project cdap by caskdata.
the class ClientMessagingService method deleteTopic.
@Override
public void deleteTopic(TopicId topicId) throws TopicNotFoundException, IOException {
HttpRequest request = remoteClient.requestBuilder(HttpMethod.DELETE, 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);
}
Aggregations