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));
}
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);
}
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());
}
}
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);
}
}
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);
}
Aggregations