use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.
the class RAPRequestEncoder method write.
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof StreamRequest) {
StreamRequest request = (StreamRequest) msg;
HttpRequest nettyRequest = NettyRequestAdapter.toNettyRequest(request);
ctx.write(nettyRequest, promise);
_currentReader = new BufferedReader(ctx, MAX_BUFFERED_CHUNKS, FLUSH_THRESHOLD);
request.getEntityStream().setReader(_currentReader);
} else {
_currentReader = null;
ctx.write(msg, promise);
}
}
use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.
the class TestNettyRequestAdapter method testStreamToNettyRequest.
@Test
public void testStreamToNettyRequest() throws Exception {
StreamRequestBuilder streamRequestBuilder = new StreamRequestBuilder(new URI(ANY_URI));
streamRequestBuilder.setMethod("POST");
streamRequestBuilder.setHeader("Content-Length", Integer.toString(ANY_ENTITY.length()));
streamRequestBuilder.setHeader("Content-Type", "application/json");
streamRequestBuilder.setCookies(Collections.singletonList("anyCookie"));
StreamRequest streamRequest = streamRequestBuilder.build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(ANY_ENTITY.getBytes()))));
HttpRequest nettyRequest = NettyRequestAdapter.toNettyRequest(streamRequest);
Assert.assertEquals(nettyRequest.uri(), "/foo/bar?q=baz");
Assert.assertEquals(nettyRequest.method(), HttpMethod.POST);
Assert.assertEquals(nettyRequest.protocolVersion(), HttpVersion.HTTP_1_1);
Assert.assertNull(nettyRequest.headers().get("Content-Length"));
Assert.assertEquals(nettyRequest.headers().get("Content-Type"), "application/json");
Assert.assertEquals(nettyRequest.headers().get("Cookie"), ANY_COOKIE);
}
use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.
the class TestNettyRequestAdapter method testStreamToNettyRequestContentLengthIgnoreCase.
@Test
public void testStreamToNettyRequestContentLengthIgnoreCase() throws Exception {
StreamRequestBuilder streamRequestBuilder = new StreamRequestBuilder(new URI(ANY_URI));
streamRequestBuilder.setHeader("CONTENT-LENGTH", Integer.toString(ANY_ENTITY.length()));
StreamRequest streamRequest = streamRequestBuilder.build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(ANY_ENTITY.getBytes()))));
HttpRequest nettyRequest = NettyRequestAdapter.toNettyRequest(streamRequest);
Assert.assertNull(nettyRequest.headers().get("Content-Length"));
}
use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.
the class TestStreamRequest method testErrorReceiver.
@Test
public void testErrorReceiver() throws Exception {
for (Client client : clients()) {
final long totalBytes = SMALL_BYTES_NUM;
EntityStream entityStream = EntityStreams.newEntityStream(new BytesWriter(totalBytes, BYTE));
StreamRequestBuilder builder = new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, ERROR_RECEIVER_URI));
StreamRequest request = builder.setMethod("POST").build(entityStream);
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
Callback<StreamResponse> callback = new Callback<StreamResponse>() {
@Override
public void onError(Throwable e) {
error.set(e);
latch.countDown();
}
@Override
public void onSuccess(StreamResponse result) {
latch.countDown();
}
};
client.streamRequest(request, callback);
latch.await();
Assert.assertNotNull(error.get());
}
}
use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.
the class RestLiIntTestServer method createServer.
public static HttpServer createServer(final Engine engine, int port, boolean useAsyncServletApi, int asyncTimeOut, List<? extends Filter> filters, final FilterChain filterChain, final boolean forceUseRestServer) {
RestLiConfig config = new RestLiConfig();
config.addResourcePackageNames(RESOURCE_PACKAGE_NAMES);
config.setServerNodeUri(URI.create("http://localhost:" + port));
config.setDocumentationRequestHandler(new DefaultDocumentationRequestHandler());
config.addDebugRequestHandlers(new ParseqTraceDebugRequestHandler());
config.setFilters(filters);
GroupMembershipMgr membershipMgr = new HashGroupMembershipMgr();
GroupMgr groupMgr = new HashMapGroupMgr(membershipMgr);
GroupsRestApplication app = new GroupsRestApplication(groupMgr, membershipMgr);
SimpleBeanProvider beanProvider = new SimpleBeanProvider();
beanProvider.add("GroupsRestApplication", app);
//using InjectMockResourceFactory to keep examples spring-free
ResourceFactory factory = new InjectMockResourceFactory(beanProvider);
//Todo this will have to change further to accomodate streaming tests - this is temporary
final TransportDispatcher dispatcher;
if (forceUseRestServer) {
dispatcher = new DelegatingTransportDispatcher(new RestLiServer(config, factory, engine));
} else {
final StreamRequestHandler streamRequestHandler = new RestLiServer(config, factory, engine);
dispatcher = new TransportDispatcher() {
@Override
public void handleRestRequest(RestRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<RestResponse> callback) {
throw new UnsupportedOperationException("This server only accepts streaming");
}
@Override
public void handleStreamRequest(StreamRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<StreamResponse> callback) {
try {
streamRequestHandler.handleRequest(req, requestContext, new TransportCallbackAdapter<>(callback));
} catch (Exception e) {
final Exception ex = RestException.forError(RestStatus.INTERNAL_SERVER_ERROR, e);
callback.onResponse(TransportResponseImpl.<StreamResponse>error(ex));
}
}
};
}
return new HttpServerFactory(filterChain).createServer(port, HttpServerFactory.DEFAULT_CONTEXT_PATH, HttpServerFactory.DEFAULT_THREAD_POOL_SIZE, dispatcher, useAsyncServletApi ? HttpJettyServer.ServletType.ASYNC_EVENT : HttpJettyServer.ServletType.RAP, asyncTimeOut, !forceUseRestServer);
}
Aggregations