Search in sources :

Example 1 with HttpContentProducer

use of io.cdap.cdap.api.service.http.HttpContentProducer in project cdap by caskdata.

the class DelayedHttpServiceResponder method execute.

/**
 * Calls to other responder methods in this class only cache the response to be sent. The response is actually
 * sent only when this method is called.
 *
 * @param keepAlive {@code true} to keep the connection open; {@code false} otherwise
 */
public void execute(boolean keepAlive) {
    Preconditions.checkState(bufferedResponse != null, "Can not call execute before one of the other responder methods are called.");
    try {
        HttpContentProducer contentProducer = bufferedResponse.getContentProducer();
        HttpHeaders headers = new DefaultHttpHeaders().add(bufferedResponse.getHeaders());
        headers.set(HttpHeaderNames.CONNECTION, keepAlive ? HttpHeaderValues.KEEP_ALIVE : HttpHeaderValues.CLOSE);
        if (!headers.contains(HttpHeaderNames.CONTENT_TYPE)) {
            headers.set(HttpHeaderNames.CONTENT_TYPE, bufferedResponse.getContentType());
        }
        if (contentProducer != null) {
            responder.sendContent(HttpResponseStatus.valueOf(bufferedResponse.getStatus()), new ReleasingBodyProducer(bodyProducerFactory.create(contentProducer, taskExecutor), taskExecutor), headers);
        } else {
            responder.sendContent(HttpResponseStatus.valueOf(bufferedResponse.getStatus()), bufferedResponse.getContentBuffer(), headers);
            taskExecutor.releaseCallResources();
        }
        emitMetrics(bufferedResponse.getStatus());
    } finally {
        close();
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) EmptyHttpHeaders(io.netty.handler.codec.http.EmptyHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpContentProducer(io.cdap.cdap.api.service.http.HttpContentProducer)

Aggregations

HttpContentProducer (io.cdap.cdap.api.service.http.HttpContentProducer)1 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)1 EmptyHttpHeaders (io.netty.handler.codec.http.EmptyHttpHeaders)1 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)1