use of io.cdap.cdap.common.http.SpillableBodyConsumer in project cdap by caskdata.
the class StoreHandler method publish.
@POST
@Path("/publish")
public BodyConsumer publish(HttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception {
TopicId topicId = new NamespaceId(namespace).topic(topic);
return new SpillableBodyConsumer(Files.createTempFile(tempDir, "tms.publish", ".tmp"), bufferSize) {
@Override
protected void processInput(InputStream inputStream, HttpResponder responder) throws Exception {
StoreRequest storeRequest = createStoreRequest(topicId, request, inputStream);
// Empty payload is only allowed for transactional publish
if (!storeRequest.isTransactional() && !storeRequest.hasPayload()) {
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);
} else {
ByteBuf response = encodeRollbackDetail(rollbackInfo);
responder.sendContent(HttpResponseStatus.OK, response, new DefaultHttpHeaders().set(HttpHeaderNames.CONTENT_TYPE, "avro/binary"));
}
}
};
}
use of io.cdap.cdap.common.http.SpillableBodyConsumer in project cdap by caskdata.
the class StoreHandler method store.
@POST
@Path("/store")
public BodyConsumer store(HttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception {
TopicId topicId = new NamespaceId(namespace).topic(topic);
return new SpillableBodyConsumer(Files.createTempFile(tempDir, "tms.store", ".tmp"), bufferSize) {
@Override
protected void processInput(InputStream inputStream, HttpResponder responder) throws Exception {
StoreRequest storeRequest = createStoreRequest(topicId, request, inputStream);
// It must be transactional with payload for store request
if (!storeRequest.isTransactional() || !storeRequest.hasPayload()) {
throw new BadRequestException("Store request must be transactional with payload. Topic: " + topicId);
}
messagingService.storePayload(storeRequest);
responder.sendStatus(HttpResponseStatus.OK);
}
};
}
Aggregations