Search in sources :

Example 26 with StreamResponseBuilder

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

the class ClientEntityStreamHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof StreamResponseBuilder) {
        final StreamResponseBuilder builder = (StreamResponseBuilder) msg;
        final Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
        headers.putAll(builder.getHeaders());
        final Map<String, String> wireAttrs = WireAttributeHelper.removeWireAttributes(headers);
        final StreamWriter writer = new StreamWriter(ctx, _maxContentLength);
        ctx.channel().attr(NettyChannelAttributes.RESPONSE_WRITER).set(writer);
        final StreamResponse response = builder.unsafeSetHeaders(headers).build(EntityStreams.newEntityStream(writer));
        final TransportCallback<StreamResponse> callback = ctx.channel().attr(NettyChannelAttributes.RESPONSE_CALLBACK).getAndSet(null);
        if (callback != null) {
            callback.onResponse(TransportResponseImpl.success(response, wireAttrs));
        }
    } else if (msg instanceof ByteString) {
        final StreamWriter writer = msg == StreamWriter.EOF ? ctx.channel().attr(NettyChannelAttributes.RESPONSE_WRITER).getAndSet(null) : ctx.channel().attr(NettyChannelAttributes.RESPONSE_WRITER).get();
        if (writer != null) {
            writer.onDataAvailable((ByteString) msg);
        }
    } else {
        ctx.fireChannelRead(msg);
    }
}
Also used : StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) ByteString(com.linkedin.data.ByteString) StreamWriter(com.linkedin.r2.netty.entitystream.StreamWriter) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ByteString(com.linkedin.data.ByteString) TreeMap(java.util.TreeMap)

Aggregations

StreamResponseBuilder (com.linkedin.r2.message.stream.StreamResponseBuilder)26 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)18 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)10 StreamException (com.linkedin.r2.message.stream.StreamException)9 ByteString (com.linkedin.data.ByteString)8 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)8 Map (java.util.Map)8 HashMap (java.util.HashMap)7 Test (org.testng.annotations.Test)7 Callback (com.linkedin.common.callback.Callback)6 RequestContext (com.linkedin.r2.message.RequestContext)6 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)6 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)4 StreamEncodingType (com.linkedin.r2.filter.compression.streaming.StreamEncodingType)4 StreamingCompressor (com.linkedin.r2.filter.compression.streaming.StreamingCompressor)4 RestRequest (com.linkedin.r2.message.rest.RestRequest)4 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)4 TimeoutException (java.util.concurrent.TimeoutException)4 None (com.linkedin.common.util.None)3 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)3