use of com.linkedin.r2.transport.common.bridge.common.TransportResponseImpl in project rest.li by linkedin.
the class TestHttpBridge method testHttpToRestErrorMessage.
@Test
public void testHttpToRestErrorMessage() throws TimeoutException, InterruptedException, ExecutionException {
FutureCallback<RestResponse> futureCallback = new FutureCallback<RestResponse>();
TransportCallback<RestResponse> callback = new TransportCallbackAdapter<RestResponse>(futureCallback);
TransportCallback<RestResponse> bridgeCallback = HttpBridge.httpToRestCallback(callback);
RestResponse restResponse = new RestResponseBuilder().build();
// 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.<RestResponse>error(new RestException(restResponse)));
RestResponse 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, restResponse);
}
use of com.linkedin.r2.transport.common.bridge.common.TransportResponseImpl 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);
}
use of com.linkedin.r2.transport.common.bridge.common.TransportResponseImpl in project rest.li by linkedin.
the class TestHttpBridge method testStreamToHttpErrorMessage.
@Test
public void testStreamToHttpErrorMessage() throws TimeoutException, InterruptedException {
URI uri = URI.create("http://some.host/thisShouldAppearInTheErrorMessage");
RestRequest r = new RestRequestBuilder(uri).build();
FutureCallback<StreamResponse> futureCallback = new FutureCallback<StreamResponse>();
TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<StreamResponse>(futureCallback);
TransportCallback<StreamResponse> bridgeCallback = HttpBridge.streamToHttpCallback(callback, Messages.toStreamRequest(r));
bridgeCallback.onResponse(TransportResponseImpl.<StreamResponse>error(new Exception()));
try {
futureCallback.get(30, TimeUnit.SECONDS);
Assert.fail("get should have thrown exception");
} catch (ExecutionException e) {
Assert.assertTrue(e.getCause().getMessage().contains(uri.toString()));
}
}
use of com.linkedin.r2.transport.common.bridge.common.TransportResponseImpl in project rest.li by linkedin.
the class TestHttpBridge method testRestToHttpErrorMessage.
@Test
public void testRestToHttpErrorMessage() throws TimeoutException, InterruptedException {
URI uri = URI.create("http://some.host/thisShouldAppearInTheErrorMessage");
RestRequest r = new RestRequestBuilder(uri).build();
FutureCallback<RestResponse> futureCallback = new FutureCallback<RestResponse>();
TransportCallback<RestResponse> callback = new TransportCallbackAdapter<RestResponse>(futureCallback);
TransportCallback<RestResponse> bridgeCallback = HttpBridge.restToHttpCallback(callback, r);
bridgeCallback.onResponse(TransportResponseImpl.<RestResponse>error(new Exception()));
try {
futureCallback.get(30, TimeUnit.SECONDS);
Assert.fail("get should have thrown exception");
} catch (ExecutionException e) {
Assert.assertTrue(e.getCause().getMessage().contains(uri.toString()));
}
}
use of com.linkedin.r2.transport.common.bridge.common.TransportResponseImpl in project rest.li by linkedin.
the class TransportDispatcherImpl method handleRestRequest.
@Override
public void handleRestRequest(RestRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<RestResponse> callback) {
final URI address = req.getURI();
RestRequestHandler handler = _restHandlers.get(address);
if (handler == null) {
callback.onResponse(TransportResponseImpl.success(RestStatus.responseForStatus(RestStatus.NOT_FOUND, "No resource for URI:" + address)));
return;
}
try {
handler.handleRequest(req, requestContext, new TransportCallbackAdapter<RestResponse>(callback));
} catch (Exception e) {
callback.onResponse(TransportResponseImpl.<RestResponse>error(RestException.forError(RestStatus.INTERNAL_SERVER_ERROR, e)));
}
}
Aggregations