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