Search in sources :

Example 1 with BodyProducer

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);
}
Also used : BodyProducer(co.cask.http.BodyProducer) CombineClassLoader(co.cask.cdap.common.lang.CombineClassLoader) HttpServiceContext(co.cask.cdap.api.service.http.HttpServiceContext) Cancellable(org.apache.twill.common.Cancellable) MetricsContext(co.cask.cdap.api.metrics.MetricsContext) HttpContentProducer(co.cask.cdap.api.service.http.HttpContentProducer) CombineClassLoader(co.cask.cdap.common.lang.CombineClassLoader)

Example 2 with BodyProducer

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;
    }
}
Also used : BodyProducer(co.cask.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)

Aggregations

BodyProducer (co.cask.http.BodyProducer)2 MetricsContext (co.cask.cdap.api.metrics.MetricsContext)1 HttpContentProducer (co.cask.cdap.api.service.http.HttpContentProducer)1 HttpServiceContext (co.cask.cdap.api.service.http.HttpServiceContext)1 CombineClassLoader (co.cask.cdap.common.lang.CombineClassLoader)1 ByteBuf (io.netty.buffer.ByteBuf)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 InvalidTruncateTimeException (org.apache.tephra.InvalidTruncateTimeException)1 TransactionCouldNotTakeSnapshotException (org.apache.tephra.TransactionCouldNotTakeSnapshotException)1 Cancellable (org.apache.twill.common.Cancellable)1