@Override protected final void handle( final SimpleHttpRequest request, final AsyncServerRequestHandler.ResponseTrigger responseTrigger, final HttpContext context) throws HttpException, IOException { final SimpleHttpResponse response = handle(request, HttpCoreContext.adapt(context)); final SimpleBody body = response.getBody(); final AsyncEntityProducer entityProducer; if (body != null) { if (body.isText()) { entityProducer = new StringAsyncEntityProducer(body.getBodyText(), body.getContentType()); } else { entityProducer = new BasicAsyncEntityProducer(body.getBodyBytes(), body.getContentType()); } else { entityProducer = null; responseTrigger.submitResponse(new BasicResponseProducer(response, entityProducer), context);
private
void triggerResponse(
final SimpleHttpResponse cacheResponse,
final AsyncExecChain.Scope scope,
final AsyncExecCallback asyncExecCallback) {
scope.clientContext.setAttribute(HttpCoreContext.HTTP_RESPONSE, cacheResponse);
scope.execRuntime.releaseEndpoint();
final SimpleBody body = cacheResponse.getBody();
final byte[] content = body != null ? body.getBodyBytes() : null;
final ContentType contentType = body != null ? body.getContentType() : null;
try {
final AsyncDataConsumer dataConsumer = asyncExecCallback.handleResponse(
cacheResponse,
content != null ? new BasicEntityDetails(content.length, contentType) : null);
if (dataConsumer != null) {
dataConsumer.consume(ByteBuffer.wrap(content));
dataConsumer.streamEnd(null);
asyncExecCallback.completed();
} catch (final HttpException | IOException ex) {
asyncExecCallback.failed(ex);
public byte[] getBodyBytes() {
return body != null ? body.getBodyBytes() : null;
public String getBodyText() {
return body != null ? body.getBodyText() : null;
public ContentType getContentType() {
return body != null ? body.getContentType() : null;
public void setBodyText(final String bodyText, final ContentType contentType) {
this.body = SimpleBody.create(bodyText, contentType);
static SimpleBody create(final String body, final ContentType contentType) {
Args.notNull(body, "Body");
if (body.length() > 2048) {
return new SimpleBody(null, body, contentType);
final Charset charset = (contentType != null ? contentType : ContentType.DEFAULT_TEXT).getCharset();
final byte[] bytes = body.getBytes(charset != null ? charset : StandardCharsets.US_ASCII);
return new SimpleBody(bytes, null, contentType);
private void triggerResponse(
final SimpleHttpResponse cacheResponse,
final AsyncExecChain.Scope scope,
final AsyncExecCallback asyncExecCallback) {
scope.clientContext.setAttribute(HttpCoreContext.HTTP_RESPONSE, cacheResponse);
scope.execRuntime.releaseEndpoint();
final SimpleBody body = cacheResponse.getBody();
final byte[] content = body != null ? body.getBodyBytes() : null;
final ContentType contentType = body != null ? body.getContentType() : null;
try {
final AsyncDataConsumer dataConsumer = asyncExecCallback.handleResponse(
cacheResponse,
content != null ? new BasicEntityDetails(content.length, contentType) : null);
if (dataConsumer != null) {
dataConsumer.consume(ByteBuffer.wrap(content));
dataConsumer.streamEnd(null);
asyncExecCallback.completed();
} catch (final HttpException | IOException ex) {
asyncExecCallback.failed(ex);
public byte[] getBodyBytes() {
return body != null ? body.getBodyBytes() : null;
public String getBodyText() {
return body != null ? body.getBodyText() : null;
public ContentType getContentType() {
return body != null ? body.getContentType() : null;
public void setBodyBytes(final byte[] bodyBytes, final ContentType contentType) {
this.body = SimpleBody.create(bodyBytes, contentType);
static SimpleBody create(final byte[] body, final ContentType contentType) {
Args.notNull(body, "Body");
return new SimpleBody(body, null, contentType);
private
static ClassicHttpResponse convert(final SimpleHttpResponse cacheResponse, final ExecChain.Scope scope) {
if (cacheResponse == null) {
return null;
final ClassicHttpResponse response = new BasicClassicHttpResponse(cacheResponse.getCode(), cacheResponse.getReasonPhrase());
for (final Iterator<Header> it = cacheResponse.headerIterator(); it.hasNext(); ) {
response.addHeader(it.next());
response.setVersion(cacheResponse.getVersion() != null ? cacheResponse.getVersion() : HttpVersion.DEFAULT);
final SimpleBody body = cacheResponse.getBody();
if (body != null) {
if (body.isText()) {
response.setEntity(new StringEntity(body.getBodyText(), body.getContentType()));
} else {
response.setEntity(new ByteArrayEntity(body.getBodyBytes(), body.getContentType()));
scope.clientContext.setAttribute(HttpCoreContext.HTTP_RESPONSE, response);
return response;
public
void setBodyText(final String bodyText, final ContentType contentType) {
this.body = SimpleBody.create(bodyText, contentType);
public static SimpleRequestProducer create(final SimpleHttpRequest request) {
Args.notNull(request, "Request");
final SimpleBody body = request.getBody();
final AsyncEntityProducer entityProducer;
if (body != null) {
if (body.isText()) {
entityProducer = new StringAsyncEntityProducer(body.getBodyText(), body.getContentType());
} else {
entityProducer = new BasicAsyncEntityProducer(body.getBodyBytes(), body.getContentType());
} else {
entityProducer = null;
return new SimpleRequestProducer(request, entityProducer);
public void setBodyBytes(final byte[] bodyBytes, final ContentType contentType) {
this.body = SimpleBody.create(bodyBytes, contentType);
private static ClassicHttpResponse convert(final SimpleHttpResponse cacheResponse, final ExecChain.Scope scope) {
if (cacheResponse == null) {
return null;
final ClassicHttpResponse response = new BasicClassicHttpResponse(cacheResponse.getCode(), cacheResponse.getReasonPhrase());
for (final Iterator<Header> it = cacheResponse.headerIterator(); it.hasNext(); ) {
response.addHeader(it.next());
response.setVersion(cacheResponse.getVersion() != null ? cacheResponse.getVersion() : HttpVersion.DEFAULT);
final SimpleBody body = cacheResponse.getBody();
if (body != null) {
if (body.isText()) {
response.setEntity(new StringEntity(body.getBodyText(), body.getContentType()));
} else {
response.setEntity(new ByteArrayEntity(body.getBodyBytes(), body.getContentType()));
scope.clientContext.setAttribute(HttpCoreContext.HTTP_RESPONSE, response);
return response;