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;
}
}
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());
}
Aggregations