use of io.cdap.common.http.HttpRequest in project cdap by caskdata.
the class ClientMessagingService method listTopics.
@Override
public List<TopicId> listTopics(NamespaceId namespaceId) throws IOException, UnauthorizedException {
HttpRequest request = remoteClient.requestBuilder(HttpMethod.GET, namespaceId.getNamespace() + "/topics").build();
HttpResponse response = remoteClient.execute(request);
handleError(response, "Failed to list topics in namespace " + namespaceId);
List<String> topics = GSON.fromJson(response.getResponseBodyAsString(), TOPIC_LIST_TYPE);
List<TopicId> result = new ArrayList<>(topics.size());
for (String topic : topics) {
result.add(namespaceId.topic(topic));
}
return Collections.unmodifiableList(result);
}
use of io.cdap.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, UnauthorizedException {
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();
try (OutputStream encoderOutput = compressOutputStream(os)) {
Encoder encoder = EncoderFactory.get().directBinaryEncoder(encoderOutput, null);
DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(Schemas.V1.PublishRequest.SCHEMA);
datumWriter.write(record, encoder);
encoder.flush();
}
// Make the publish request
String writeType = publish ? "publish" : "store";
TopicId topicId = request.getTopicId();
Map<String, String> headers = new HashMap<>();
headers.put(HttpHeaders.CONTENT_TYPE, "avro/binary");
if (compressPayload) {
headers.put(HttpHeaders.CONTENT_ENCODING, "gzip");
}
HttpRequest httpRequest = remoteClient.requestBuilder(HttpMethod.POST, createTopicPath(topicId) + "/" + writeType).addHeaders(headers).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 io.cdap.common.http.HttpRequest in project cdap by caskdata.
the class ClientMessagingService method getTopic.
@Override
public TopicMetadata getTopic(TopicId topicId) throws TopicNotFoundException, IOException, UnauthorizedException {
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);
}
use of io.cdap.common.http.HttpRequest in project cdap by caskdata.
the class ClientMessagingService method deleteTopic.
@Override
public void deleteTopic(TopicId topicId) throws TopicNotFoundException, IOException, UnauthorizedException {
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);
}
use of io.cdap.common.http.HttpRequest in project cdap by caskdata.
the class RemoteSecureStore method get.
@Override
public SecureStoreData get(String namespace, String name) throws Exception {
// 1. Get metadata of the secure key
HttpRequest request = remoteClient.requestBuilder(HttpMethod.GET, createPath(namespace, name) + "/metadata").build();
HttpResponse response = remoteClient.execute(request);
handleResponse(response, namespace, name, String.format("Error occurred while getting metadata for key %s:%s", namespace, name));
SecureStoreMetadata metadata = GSON.fromJson(response.getResponseBodyAsString(), SecureStoreMetadata.class);
// 2. Get sensitive data for the secure key
request = remoteClient.requestBuilder(HttpMethod.GET, createPath(namespace, name)).build();
response = remoteClient.execute(request);
handleResponse(response, namespace, name, String.format("Error occurred while getting key %s:%s", namespace, name));
// response is not a json object
byte[] data = response.getResponseBody();
return new SecureStoreData(metadata, data);
}
Aggregations