Search in sources :

Example 6 with StreamResponseBuilder

use of com.linkedin.r2.message.stream.StreamResponseBuilder in project rest.li by linkedin.

the class TestMessages method testToStreamTransportCallbackSuccess.

@Test
public void testToStreamTransportCallbackSuccess() {
    TransportCallback<RestResponse> restCallback = response -> {
        Assert.assertFalse(response.hasError());
        Assert.assertNotNull(response.getResponse());
        Assert.assertSame(response.getResponse().getEntity(), DATA);
        Assert.assertNotNull(response.getWireAttributes());
        Assert.assertEquals(response.getWireAttributes(), WIRE_ATTR);
    };
    TransportCallback<StreamResponse> streamCallback = Messages.toStreamTransportCallback(restCallback);
    StreamResponseBuilder builder = new StreamResponseBuilder();
    StreamResponse streamResponse = builder.build(EntityStreams.newEntityStream(new ByteStringWriter(DATA)));
    streamCallback.onResponse(TransportResponseImpl.success(streamResponse, WIRE_ATTR));
}
Also used : ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) Callback(com.linkedin.common.callback.Callback) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) Messages(com.linkedin.r2.message.Messages) RestResponse(com.linkedin.r2.message.rest.RestResponse) EntityStreams(com.linkedin.r2.message.stream.entitystream.EntityStreams) TransportResponseImpl(com.linkedin.r2.transport.common.bridge.common.TransportResponseImpl) Assert(org.testng.Assert) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) ByteString(com.linkedin.data.ByteString) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) Map(java.util.Map) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) StreamException(com.linkedin.r2.message.stream.StreamException) RestException(com.linkedin.r2.message.rest.RestException) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) RestResponse(com.linkedin.r2.message.rest.RestResponse) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) Test(org.testng.annotations.Test)

Example 7 with StreamResponseBuilder

use of com.linkedin.r2.message.stream.StreamResponseBuilder in project rest.li by linkedin.

the class TestHttpBridge method testHttpToStreamErrorMessage.

@Test
public void testHttpToStreamErrorMessage() throws TimeoutException, InterruptedException, ExecutionException {
    FutureCallback<StreamResponse> futureCallback = new FutureCallback<StreamResponse>();
    TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<StreamResponse>(futureCallback);
    TransportCallback<StreamResponse> bridgeCallback = HttpBridge.httpToStreamCallback(callback);
    StreamResponse streamResponse = new StreamResponseBuilder().build(EntityStreams.emptyStream());
    // Note: FutureCallback will fail if called twice. An exception would be raised on the current
    // thread because we begin the callback sequence here in onResponse.
    // (test originally added due to bug with double callback invocation)
    bridgeCallback.onResponse(TransportResponseImpl.<StreamResponse>error(new StreamException(streamResponse)));
    StreamResponse resp = futureCallback.get(30, TimeUnit.SECONDS);
    // should have unpacked restResponse from the RestException that we passed in without
    // propagating the actual exception
    Assert.assertSame(resp, streamResponse);
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) FutureCallback(com.linkedin.common.callback.FutureCallback) StreamException(com.linkedin.r2.message.stream.StreamException) Test(org.testng.annotations.Test)

Example 8 with StreamResponseBuilder

use of com.linkedin.r2.message.stream.StreamResponseBuilder in project rest.li by linkedin.

the class RAPStreamResponseHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, StreamResponse response) throws Exception {
    final Map<String, String> headers = new HashMap<String, String>(response.getHeaders());
    final Map<String, String> wireAttrs = new HashMap<String, String>(WireAttributeHelper.removeWireAttributes(headers));
    final StreamResponse newResponse = new StreamResponseBuilder(response).unsafeSetHeaders(headers).build(response.getEntityStream());
    // In general there should always be a callback to handle a received message,
    // but it could have been removed due to a previous exception or closure on the
    // channel
    TransportCallback<StreamResponse> callback = ctx.channel().attr(CALLBACK_ATTR_KEY).getAndRemove();
    if (callback != null) {
        LOG.debug("{}: handling a response", ctx.channel().remoteAddress());
        callback.onResponse(TransportResponseImpl.success(newResponse, wireAttrs));
    } else {
        LOG.debug("{}: dropped a response", ctx.channel().remoteAddress());
    }
}
Also used : StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) HashMap(java.util.HashMap) StreamResponse(com.linkedin.r2.message.stream.StreamResponse)

Example 9 with StreamResponseBuilder

use of com.linkedin.r2.message.stream.StreamResponseBuilder in project rest.li by linkedin.

the class RAPResponseDecoder method channelRead0.

@Override
protected void channelRead0(final ChannelHandlerContext ctx, HttpObject msg) throws Exception {
    if (msg instanceof HttpResponse) {
        HttpResponse m = (HttpResponse) msg;
        _shouldCloseConnection = !HttpUtil.isKeepAlive(m);
        if (HttpUtil.is100ContinueExpected(m)) {
            ctx.writeAndFlush(CONTINUE).addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (!future.isSuccess()) {
                        ctx.fireExceptionCaught(future.cause());
                    }
                }
            });
        }
        if (!m.decoderResult().isSuccess()) {
            ctx.fireExceptionCaught(m.decoderResult().cause());
            return;
        }
        // remove chunked encoding.
        if (HttpUtil.isTransferEncodingChunked(m)) {
            HttpUtil.setTransferEncodingChunked(m, false);
        }
        Timeout<None> timeout = ctx.channel().attr(TIMEOUT_ATTR_KEY).getAndRemove();
        if (timeout == null) {
            LOG.debug("dropped a response after channel inactive or exception had happened.");
            return;
        }
        final TimeoutBufferedWriter writer = new TimeoutBufferedWriter(ctx, _maxContentLength, BUFFER_HIGH_WATER_MARK, BUFFER_LOW_WATER_MARK, timeout);
        EntityStream entityStream = EntityStreams.newEntityStream(writer);
        _chunkedMessageWriter = writer;
        StreamResponseBuilder builder = new StreamResponseBuilder();
        builder.setStatus(m.status().code());
        for (Map.Entry<String, String> e : m.headers()) {
            String key = e.getKey();
            String value = e.getValue();
            if (key.equalsIgnoreCase(HttpConstants.RESPONSE_COOKIE_HEADER_NAME)) {
                builder.addCookie(value);
            } else {
                builder.unsafeAddHeaderValue(key, value);
            }
        }
        ctx.fireChannelRead(builder.build(entityStream));
    } else if (msg instanceof HttpContent) {
        HttpContent chunk = (HttpContent) msg;
        TimeoutBufferedWriter currentWriter = _chunkedMessageWriter;
        // Sanity check
        if (currentWriter == null) {
            throw new IllegalStateException("received " + HttpContent.class.getSimpleName() + " without " + HttpResponse.class.getSimpleName());
        }
        if (!chunk.decoderResult().isSuccess()) {
            this.exceptionCaught(ctx, chunk.decoderResult().cause());
        }
        currentWriter.processHttpChunk(chunk);
        if (chunk instanceof LastHttpContent) {
            _chunkedMessageWriter = null;
        }
    } else {
        // something must be wrong, but let's proceed so that
        // handler after us has a chance to process it.
        ctx.fireChannelRead(msg);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) ByteString(com.linkedin.data.ByteString) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) ChannelFutureListener(io.netty.channel.ChannelFutureListener) TimeoutException(java.util.concurrent.TimeoutException) ClosedChannelException(java.nio.channels.ClosedChannelException) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) IOException(java.io.IOException) EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) None(com.linkedin.common.util.None) Map(java.util.Map) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent)

Example 10 with StreamResponseBuilder

use of com.linkedin.r2.message.stream.StreamResponseBuilder in project rest.li by linkedin.

the class Http2StreamResponseHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof ResponseWithCallback) {
        @SuppressWarnings("unchecked") ResponseWithCallback<StreamResponse, TransportCallback<StreamResponse>> responseWithCallback = (ResponseWithCallback<StreamResponse, TransportCallback<StreamResponse>>) msg;
        StreamResponse response = responseWithCallback.response();
        TransportCallback<StreamResponse> callback = responseWithCallback.callback();
        Map<String, String> headers = new HashMap<>(response.getHeaders());
        Map<String, String> wireAttrs = new HashMap<>(WireAttributeHelper.removeWireAttributes(headers));
        StreamResponse newResponse = new StreamResponseBuilder(response).unsafeSetHeaders(headers).build(response.getEntityStream());
        LOG.debug("{}: handling a response", ctx.channel().remoteAddress());
        callback.onResponse(TransportResponseImpl.success(newResponse, wireAttrs));
    }
    ctx.fireChannelRead(msg);
}
Also used : TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) HashMap(java.util.HashMap) ResponseWithCallback(com.linkedin.r2.transport.common.bridge.common.ResponseWithCallback) StreamResponse(com.linkedin.r2.message.stream.StreamResponse)

Aggregations

StreamResponseBuilder (com.linkedin.r2.message.stream.StreamResponseBuilder)16 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)11 StreamException (com.linkedin.r2.message.stream.StreamException)6 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)6 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)6 Map (java.util.Map)6 Test (org.testng.annotations.Test)6 Callback (com.linkedin.common.callback.Callback)5 HashMap (java.util.HashMap)5 ByteString (com.linkedin.data.ByteString)4 StreamEncodingType (com.linkedin.r2.filter.compression.streaming.StreamEncodingType)4 StreamingCompressor (com.linkedin.r2.filter.compression.streaming.StreamingCompressor)4 RequestContext (com.linkedin.r2.message.RequestContext)4 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)4 RestResponse (com.linkedin.r2.message.rest.RestResponse)3 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)3 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)3 None (com.linkedin.common.util.None)2 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)2 Messages (com.linkedin.r2.message.Messages)2