Search in sources :

Example 1 with SpillableBodyConsumer

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"));
            }
        }
    };
}
Also used : HttpResponder(io.cdap.http.HttpResponder) RollbackDetail(io.cdap.cdap.messaging.RollbackDetail) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) StoreRequest(io.cdap.cdap.messaging.StoreRequest) BadRequestException(io.cdap.cdap.common.BadRequestException) TopicId(io.cdap.cdap.proto.id.TopicId) SpillableBodyConsumer(io.cdap.cdap.common.http.SpillableBodyConsumer) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ByteBuf(io.netty.buffer.ByteBuf) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 2 with SpillableBodyConsumer

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);
        }
    };
}
Also used : HttpResponder(io.cdap.http.HttpResponder) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) StoreRequest(io.cdap.cdap.messaging.StoreRequest) BadRequestException(io.cdap.cdap.common.BadRequestException) TopicId(io.cdap.cdap.proto.id.TopicId) SpillableBodyConsumer(io.cdap.cdap.common.http.SpillableBodyConsumer) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

BadRequestException (io.cdap.cdap.common.BadRequestException)2 SpillableBodyConsumer (io.cdap.cdap.common.http.SpillableBodyConsumer)2 StoreRequest (io.cdap.cdap.messaging.StoreRequest)2 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)2 TopicId (io.cdap.cdap.proto.id.TopicId)2 HttpResponder (io.cdap.http.HttpResponder)2 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)2 InputStream (java.io.InputStream)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 RollbackDetail (io.cdap.cdap.messaging.RollbackDetail)1 ByteBuf (io.netty.buffer.ByteBuf)1 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)1