Search in sources :

Example 16 with TransportResponse

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();
}
Also used : TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) RequestContext(com.linkedin.r2.message.RequestContext) URISyntaxException(java.net.URISyntaxException) TransportResponse(com.linkedin.r2.transport.common.bridge.common.TransportResponse) StreamRequest(com.linkedin.r2.message.stream.StreamRequest)

Example 17 with TransportResponse

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());
    }
}
Also used : StreamResponse(com.linkedin.r2.message.stream.StreamResponse)

Example 18 with TransportResponse

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()));
    }
}
Also used : TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) RestResponse(com.linkedin.r2.message.rest.RestResponse) Channel(io.netty.channel.Channel) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) TransportResponse(com.linkedin.r2.transport.common.bridge.common.TransportResponse)

Example 19 with TransportResponse

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());
}
Also used : RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder)

Aggregations

StreamResponse (com.linkedin.r2.message.stream.StreamResponse)11 RequestContext (com.linkedin.r2.message.RequestContext)8 TransportResponse (com.linkedin.r2.transport.common.bridge.common.TransportResponse)8 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)7 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)7 RestResponse (com.linkedin.r2.message.rest.RestResponse)6 ByteString (com.linkedin.data.ByteString)4 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)4 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)4 HttpServerBuilder (com.linkedin.r2.testutils.server.HttpServerBuilder)4 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 Server (org.eclipse.jetty.server.Server)4 Test (org.testng.annotations.Test)4 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)3 FutureTransportCallback (com.linkedin.r2.transport.common.bridge.common.FutureTransportCallback)3 HttpClientBuilder (com.linkedin.r2.transport.http.client.HttpClientBuilder)3 RetriableRequestException (com.linkedin.r2.RetriableRequestException)2 RestRequest (com.linkedin.r2.message.rest.RestRequest)2 DrainReader (com.linkedin.r2.message.stream.entitystream.DrainReader)2