use of com.linkedin.r2.transport.common.bridge.common.TransportCallback in project rest.li by linkedin.
the class TestMessages method testToStreamTransportCallbackStreamException.
@Test
public void testToStreamTransportCallbackStreamException() {
TransportCallback<RestResponse> restCallback = response -> {
Assert.assertTrue(response.hasError());
Assert.assertNotNull(response.getError());
Assert.assertTrue(response.getError() instanceof RestException);
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.error(new StreamException(streamResponse, new IllegalStateException()), WIRE_ATTR));
}
use of com.linkedin.r2.transport.common.bridge.common.TransportCallback in project rest.li by linkedin.
the class TestJetty404 method setup.
@BeforeClass
public void setup() throws IOException {
_clientFactory = new HttpClientFactory();
_client = new TransportClientAdapter(_clientFactory.getClient(Collections.<String, String>emptyMap()), true);
_server = new HttpServerFactory().createH2cServer(PORT, "/correct-path", 50, new TransportDispatcher() {
@Override
public void handleRestRequest(RestRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<RestResponse> callback) {
callback.onResponse(TransportResponseImpl.success(new RestResponseBuilder().build()));
}
@Override
public void handleStreamRequest(StreamRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<StreamResponse> callback) {
req.getEntityStream().setReader(new DrainReader());
callback.onResponse(TransportResponseImpl.success(new StreamResponseBuilder().build(EntityStreams.emptyStream())));
}
}, true);
_server.start();
}
use of com.linkedin.r2.transport.common.bridge.common.TransportCallback in project rest.li by linkedin.
the class MockClient method streamRequest.
@Override
public void streamRequest(StreamRequest request, RequestContext requestContext, Callback<StreamResponse> callback) {
TransportCallback<StreamResponse> adapter = HttpBridge.streamToHttpCallback(new TransportCallbackAdapter<StreamResponse>(callback), request);
RestResponse response = new RestResponseBuilder().setStatus(status()).setHeaders(headers()).setEntity(body()).build();
adapter.onResponse(TransportResponseImpl.success(Messages.toStreamResponse(response)));
}
use of com.linkedin.r2.transport.common.bridge.common.TransportCallback in project rest.li by linkedin.
the class Http2StreamCodec method write.
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (!(msg instanceof RequestWithCallback)) {
ctx.write(msg, promise);
return;
}
Request request = ((RequestWithCallback) msg).request();
Http2ConnectionEncoder encoder = encoder();
int streamId = connection().local().incrementAndGetNextStreamId();
if (request instanceof StreamRequest) {
LOG.debug("Writing StreamRequest...");
StreamRequest streamRequest = (StreamRequest) request;
Http2Headers http2Headers = NettyRequestAdapter.toHttp2Headers(streamRequest);
BufferedReader reader = new BufferedReader(ctx, encoder, streamId, ((RequestWithCallback) msg).handle());
streamRequest.getEntityStream().setReader(reader);
encoder.writeHeaders(ctx, streamId, http2Headers, NO_PADDING, NOT_END_STREAM, promise).addListener(future -> reader.request());
LOG.debug("Sent HTTP/2 HEADERS frame, stream={}, end={}, headers={}, padding={}bytes", new Object[] { streamId, NOT_END_STREAM, http2Headers.size(), NO_PADDING });
} else if (request instanceof RestRequest) {
LOG.debug("Writing RestRequest...");
PromiseCombiner promiseCombiner = new PromiseCombiner();
ChannelPromise headersPromise = ctx.channel().newPromise();
ChannelPromise dataPromise = ctx.channel().newPromise();
promiseCombiner.add(headersPromise);
promiseCombiner.add(dataPromise);
promiseCombiner.finish(promise);
RestRequest restRequest = (RestRequest) request;
Http2Headers headers = NettyRequestAdapter.toHttp2Headers(restRequest);
encoder.writeHeaders(ctx, streamId, headers, NO_PADDING, NOT_END_STREAM, headersPromise);
LOG.debug("Sent HTTP/2 HEADERS frame, stream={}, end={}, headers={}, padding={}bytes", new Object[] { streamId, NOT_END_STREAM, headers.size(), NO_PADDING });
ByteBuf data = Unpooled.wrappedBuffer(restRequest.getEntity().asByteBuffer());
encoder.writeData(ctx, streamId, data, NO_PADDING, END_STREAM, dataPromise);
LOG.debug("Sent HTTP/2 DATA frame, stream={}, end={}, data={}bytes, padding={}bytes", new Object[] { streamId, END_STREAM, data.readableBytes(), NO_PADDING });
} else {
// Request type is not supported. Returns channel back to the pool and throws exception.
ctx.fireChannelRead(((RequestWithCallback) msg).handle());
throw new IllegalArgumentException("Request is neither StreamRequest or RestRequest");
}
// Sets TransportCallback as a stream property to be retrieved later
TransportCallback<?> callback = ((RequestWithCallback) msg).callback();
Http2Connection.PropertyKey callbackKey = ctx.channel().attr(Http2ClientPipelineInitializer.CALLBACK_ATTR_KEY).get();
connection().stream(streamId).setProperty(callbackKey, callback);
// Sets AsyncPoolHandle as a stream property to be retrieved later
AsyncPoolHandle<?> handle = ((RequestWithCallback) msg).handle();
Http2Connection.PropertyKey handleKey = ctx.channel().attr(Http2ClientPipelineInitializer.CHANNEL_POOL_HANDLE_ATTR_KEY).get();
connection().stream(streamId).setProperty(handleKey, handle);
}
Aggregations