use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class StoreHandler method publish.
@POST
@Path("/publish")
public void publish(HttpRequest 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;
}
ChannelBuffer response = encodeRollbackDetail(rollbackInfo);
responder.sendContent(HttpResponseStatus.OK, response, "avro/binary", null);
}
use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class StoreHandler method createStoreRequest.
/**
* Creates a {@link StoreRequest} instance based on the given {@link HttpRequest}.
*/
private StoreRequest createStoreRequest(TopicId topicId, HttpRequest request) throws Exception {
// Currently only support avro
if (!"avro/binary".equals(request.getHeader(HttpHeaders.Names.CONTENT_TYPE))) {
throw new BadRequestException("Only avro/binary content type is supported.");
}
Decoder decoder = DecoderFactory.get().directBinaryDecoder(new ChannelBufferInputStream(request.getContent()), null);
DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(Schemas.V1.PublishRequest.SCHEMA);
return new GenericRecordStoreRequest(topicId, datumReader.read(null, decoder));
}
Aggregations