use of com.linkedin.r2.transport.http.common.HttpBridge in project rest.li by linkedin.
the class PipelineStreamHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, RestRequest request) throws Exception {
final Channel ch = ctx.channel();
TransportCallback<StreamResponse> writeResponseCallback = new TransportCallback<StreamResponse>() {
@Override
public void onResponse(final TransportResponse<StreamResponse> response) {
if (response.hasError()) {
// This onError is only getting called in cases where:
// (1) the exception was thrown by the handleRequest() method, and the upper layer
// dispatcher did not catch the exception or caught it and passed it here without
// turning it into a Response, or
// (2) the HttpBridge-installed callback's onError declined to convert the exception to a
// response and passed it along to here.
writeError(ch, response, response.getError());
} else {
Messages.toRestResponse(response.getResponse(), new Callback<RestResponse>() {
@Override
public void onError(Throwable e) {
writeError(ch, response, e);
}
@Override
public void onSuccess(RestResponse result) {
writeResponse(ch, response, result);
}
});
}
}
};
try {
_dispatcher.handleRequest(Messages.toStreamRequest(request), writeResponseCallback);
} catch (Exception ex) {
writeResponseCallback.onResponse(TransportResponseImpl.<StreamResponse>error(ex, Collections.<String, String>emptyMap()));
}
}
use of com.linkedin.r2.transport.http.common.HttpBridge in project rest.li by linkedin.
the class PipelineRestHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, RestRequest request) throws Exception {
final Channel ch = ctx.channel();
TransportCallback<RestResponse> writeResponseCallback = new TransportCallback<RestResponse>() {
@Override
public void onResponse(TransportResponse<RestResponse> response) {
final RestResponseBuilder responseBuilder;
if (response.hasError()) {
// This onError is only getting called in cases where:
// (1) the exception was thrown by the handleRequest() method, and the upper layer
// dispatcher did not catch the exception or caught it and passed it here without
// turning it into a Response, or
// (2) the HttpBridge-installed callback's onError declined to convert the exception to a
// response and passed it along to here.
responseBuilder = new RestResponseBuilder(RestStatus.responseForError(RestStatus.INTERNAL_SERVER_ERROR, response.getError()));
} else {
responseBuilder = new RestResponseBuilder(response.getResponse());
}
responseBuilder.unsafeOverwriteHeaders(WireAttributeHelper.toWireAttributes(response.getWireAttributes())).build();
ch.writeAndFlush(responseBuilder.build());
}
};
try {
_dispatcher.handleRequest(request, writeResponseCallback);
} catch (Exception ex) {
writeResponseCallback.onResponse(TransportResponseImpl.<RestResponse>error(ex, Collections.<String, String>emptyMap()));
}
}
Aggregations