use of co.cask.cdap.proto.id.TopicId in project cdap by caskdata.
the class ClientMessagingService method listTopics.
@Override
public List<TopicId> listTopics(NamespaceId namespaceId) throws IOException {
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 co.cask.cdap.proto.id.TopicId in project cdap by caskdata.
the class StoreHandler method rollback.
@POST
@Path("/rollback")
public void rollback(FullHttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception {
TopicId topicId = new NamespaceId(namespace).topic(topic);
Decoder decoder = DecoderFactory.get().directBinaryDecoder(new ByteBufInputStream(request.content()), null);
DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(Schemas.V1.PublishResponse.SCHEMA);
messagingService.rollback(topicId, new GenericRecordRollbackDetail(datumReader.read(null, decoder)));
responder.sendStatus(HttpResponseStatus.OK);
}
use of co.cask.cdap.proto.id.TopicId in project cdap by caskdata.
the class StoreHandler method store.
@POST
@Path("/store")
public void store(FullHttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception {
TopicId topicId = new NamespaceId(namespace).topic(topic);
StoreRequest storeRequest = createStoreRequest(topicId, request);
// It must be transactional with payload for store request
if (!storeRequest.isTransactional() || !storeRequest.hasNext()) {
throw new BadRequestException("Store request must be transactional with payload. Topic: " + topicId);
}
messagingService.storePayload(storeRequest);
responder.sendStatus(HttpResponseStatus.OK);
}
use of co.cask.cdap.proto.id.TopicId in project cdap by caskdata.
the class StoreHandler method publish.
@POST
@Path("/publish")
public void publish(FullHttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception {
TopicId topicId = new NamespaceId(namespace).topic(topic);
StoreRequest storeRequest = createStoreRequest(topicId, request);
// Empty payload is only allowed for transactional publish
if (!storeRequest.isTransactional() && !storeRequest.hasNext()) {
throw new BadRequestException("Empty payload is only allowed for publishing transactional message. Topic: " + topicId);
}
// Publish the message and response with the rollback information
RollbackDetail rollbackInfo = messagingService.publish(storeRequest);
if (rollbackInfo == null) {
// Non-tx publish doesn't have rollback info.
responder.sendStatus(HttpResponseStatus.OK);
return;
}
ByteBuf response = encodeRollbackDetail(rollbackInfo);
responder.sendContent(HttpResponseStatus.OK, response, new DefaultHttpHeaders().set(HttpHeaderNames.CONTENT_TYPE, "avro/binary"));
}
use of co.cask.cdap.proto.id.TopicId in project cdap by caskdata.
the class FetchHandler method poll.
@POST
@Path("poll")
public void poll(FullHttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception {
TopicId topicId = new NamespaceId(namespace).topic(topic);
// Currently only support avro
if (!"avro/binary".equals(request.headers().get(HttpHeaderNames.CONTENT_TYPE))) {
throw new BadRequestException("Only avro/binary content type is supported.");
}
// Decode the poll request
Decoder decoder = DecoderFactory.get().directBinaryDecoder(new ByteBufInputStream(request.content()), null);
DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(Schemas.V1.ConsumeRequest.SCHEMA);
// Fetch the messages
CloseableIterator<RawMessage> iterator = fetchMessages(datumReader.read(null, decoder), topicId);
try {
responder.sendContent(HttpResponseStatus.OK, new MessagesBodyProducer(iterator, messageChunkSize), new DefaultHttpHeaders().set(HttpHeaderNames.CONTENT_TYPE, "avro/binary"));
} catch (Throwable t) {
iterator.close();
throw t;
}
}
Aggregations