use of co.cask.http.BodyProducer in project cdap by caskdata.
the class AbstractHttpHandlerDelegator method wrapResponder.
/**
* Returns a new instance of {@link DelayedHttpServiceResponder} that wraps around the given {@link HttpResponder}
* object. This method is called from handler class generated by {@link HttpHandlerGenerator}.
*/
@SuppressWarnings("unused")
protected final DelayedHttpServiceResponder wrapResponder(HttpResponder responder) {
MetricsContext collector = this.metricsContext;
HttpServiceContext serviceContext = context.getServiceContext();
Preconditions.checkState(serviceContext instanceof TransactionalHttpServiceContext, "This instance of HttpServiceContext does not support transactions.");
if (serviceContext.getSpecification() != null) {
collector = metricsContext.childContext(Constants.Metrics.Tag.HANDLER, serviceContext.getSpecification().getName());
}
return new DelayedHttpServiceResponder(responder, new BodyProducerFactory() {
@Override
public BodyProducer create(HttpContentProducer contentProducer, TransactionalHttpServiceContext serviceContext) {
final ClassLoader programContextClassLoader = new CombineClassLoader(null, ImmutableList.of(contentProducer.getClass().getClassLoader(), getClass().getClassLoader()));
// Capture the context since we need to keep it until the end of the content producing.
// We don't need to worry about double capturing of the context when HttpContentConsumer is used.
// This is because when HttpContentConsumer is used, the responder constructed here will get closed and this
// BodyProducerFactory won't be used.
final Cancellable contextReleaser = context.capture();
return new BodyProducerAdapter(contentProducer, serviceContext, programContextClassLoader, contextReleaser);
}
}, (TransactionalHttpServiceContext) serviceContext, collector);
}
use of co.cask.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;
}
}
Aggregations