Search in sources :

Example 1 with BodyProducer

use of io.cdap.http.BodyProducer in project cdap by caskdata.

the class TransactionHttpHandler method getTxManagerSnapshot.

/**
 * Retrieve the state of the transaction manager.
 */
@Path("/transactions/state")
@GET
public void getTxManagerSnapshot(HttpRequest request, HttpResponder responder) throws TransactionCouldNotTakeSnapshotException, IOException {
    LOG.trace("Taking transaction manager snapshot at time {}", System.currentTimeMillis());
    LOG.trace("Took and retrieved transaction manager snapshot successfully.");
    final InputStream in = txClient.getSnapshotInputStream();
    try {
        responder.sendContent(HttpResponseStatus.OK, new BodyProducer() {

            @Override
            public ByteBuf nextChunk() throws Exception {
                ByteBuf buffer = Unpooled.buffer(4096);
                buffer.writeBytes(in, 4096);
                return buffer;
            }

            @Override
            public void finished() throws Exception {
                Closeables.closeQuietly(in);
            }

            @Override
            public void handleError(@Nullable Throwable cause) {
                Closeables.closeQuietly(in);
            }
        }, EmptyHttpHeaders.INSTANCE);
    } catch (Exception e) {
        Closeables.closeQuietly(in);
        throw e;
    }
}
Also used : BodyProducer(io.cdap.http.BodyProducer) InputStream(java.io.InputStream) ByteBuf(io.netty.buffer.ByteBuf) TransactionCouldNotTakeSnapshotException(org.apache.tephra.TransactionCouldNotTakeSnapshotException) IOException(java.io.IOException) InvalidTruncateTimeException(org.apache.tephra.InvalidTruncateTimeException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 2 with BodyProducer

use of io.cdap.http.BodyProducer in project cdap by caskdata.

the class TestHandler method chunk.

@POST
@Path("/chunk")
public void chunk(FullHttpRequest request, HttpResponder responder) {
    ByteBuf content = request.content().copy();
    responder.sendContent(HttpResponseStatus.OK, new BodyProducer() {

        int count = 0;

        @Override
        public ByteBuf nextChunk() {
            if (count++ < 10) {
                return content.copy();
            }
            return Unpooled.EMPTY_BUFFER;
        }

        @Override
        public void finished() {
        // no-op
        }

        @Override
        public void handleError(@Nullable Throwable cause) {
        // no-op
        }
    }, new DefaultHttpHeaders());
}
Also used : BodyProducer(io.cdap.http.BodyProducer) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) ByteBuf(io.netty.buffer.ByteBuf) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

BodyProducer (io.cdap.http.BodyProducer)2 ByteBuf (io.netty.buffer.ByteBuf)2 Path (javax.ws.rs.Path)2 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1 InvalidTruncateTimeException (org.apache.tephra.InvalidTruncateTimeException)1 TransactionCouldNotTakeSnapshotException (org.apache.tephra.TransactionCouldNotTakeSnapshotException)1