Search in sources :

Example 1 with StreamWriter

use of com.linkedin.r2.netty.entitystream.StreamWriter 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

ByteString (com.linkedin.data.ByteString)1 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)1 StreamResponseBuilder (com.linkedin.r2.message.stream.StreamResponseBuilder)1 StreamWriter (com.linkedin.r2.netty.entitystream.StreamWriter)1 TreeMap (java.util.TreeMap)1