use of com.linkedin.r2.transport.common.bridge.common.TransportResponse in project rest.li by linkedin.
the class AbstractR2StreamServlet method service.
@Override
protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
final SyncIOHandler ioHandler = new SyncIOHandler(req.getInputStream(), resp.getOutputStream(), req.getRemoteAddr(), 2, _ioHandlerTimeout, _logServletExceptions);
RequestContext requestContext = ServletHelper.readRequestContext(req);
StreamRequest streamRequest;
try {
streamRequest = ServletHelper.readFromServletRequest(req, ioHandler);
} catch (URISyntaxException e) {
ServletHelper.writeToServletError(resp, RestStatus.BAD_REQUEST, e.toString());
return;
}
TransportCallback<StreamResponse> callback = new TransportCallback<StreamResponse>() {
@Override
public void onResponse(TransportResponse<StreamResponse> response) {
StreamResponse streamResponse = ServletHelper.writeResponseHeadersToServletResponse(response, resp);
streamResponse.getEntityStream().setReader(ioHandler);
}
};
getDispatcher().handleRequest(streamRequest, requestContext, callback);
ioHandler.loop();
}
use of com.linkedin.r2.transport.common.bridge.common.TransportResponse in project rest.li by linkedin.
the class StreamExecutionCallback method onResponse.
@Override
public void onResponse(TransportResponse<StreamResponse> response) {
final TransportCallback<StreamResponse> callback = _callbackRef.getAndSet(null);
if (callback != null) {
final TransportResponse<StreamResponse> wrappedResponse;
if (response.hasError()) {
wrappedResponse = response;
} else {
final EventLoopConnector connector = new EventLoopConnector(response.getResponse().getEntityStream());
final StreamResponse newResponse = response.getResponse().builder().build(EntityStreams.newEntityStream(connector));
wrappedResponse = TransportResponseImpl.success(newResponse, response.getWireAttributes());
}
trySchedule(() -> callback.onResponse(wrappedResponse));
} else {
LOG.warn("Received response {} while _callback is null. Ignored.", response.getResponse());
}
}
use of com.linkedin.r2.transport.common.bridge.common.TransportResponse 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()));
}
}
use of com.linkedin.r2.transport.common.bridge.common.TransportResponse in project rest.li by linkedin.
the class PipelineStreamHandler method writeResponse.
private void writeResponse(Channel ch, TransportResponse<StreamResponse> response, RestResponse restResponse) {
RestResponseBuilder responseBuilder = restResponse.builder().unsafeOverwriteHeaders(WireAttributeHelper.toWireAttributes(response.getWireAttributes()));
ch.writeAndFlush(responseBuilder.build());
}
Aggregations