Search in sources :

Example 6 with TransportResponseImpl

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);
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RestException(com.linkedin.r2.message.rest.RestException) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 7 with TransportResponseImpl

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);
}
Also used : HttpServerFactory(com.linkedin.r2.transport.http.server.HttpServerFactory) TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.server.TransportCallbackAdapter) DelegatingTransportDispatcher(com.linkedin.restli.server.DelegatingTransportDispatcher) DefaultDocumentationRequestHandler(com.linkedin.restli.docgen.DefaultDocumentationRequestHandler) DelegatingTransportDispatcher(com.linkedin.restli.server.DelegatingTransportDispatcher) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher) ParseqTraceDebugRequestHandler(com.linkedin.restli.server.ParseqTraceDebugRequestHandler) HashGroupMembershipMgr(com.linkedin.restli.examples.groups.server.impl.HashGroupMembershipMgr) GroupMembershipMgr(com.linkedin.restli.examples.groups.server.api.GroupMembershipMgr) HashMapGroupMgr(com.linkedin.restli.examples.groups.server.impl.HashMapGroupMgr) GroupsRestApplication(com.linkedin.restli.examples.groups.server.rest.impl.GroupsRestApplication) RequestContext(com.linkedin.r2.message.RequestContext) RestLiServer(com.linkedin.restli.server.RestLiServer) SimpleBeanProvider(com.linkedin.restli.server.mock.SimpleBeanProvider) RestResponse(com.linkedin.r2.message.rest.RestResponse) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) InjectMockResourceFactory(com.linkedin.restli.server.mock.InjectMockResourceFactory) ResourceFactory(com.linkedin.restli.server.resources.ResourceFactory) RestException(com.linkedin.r2.message.rest.RestException) IOException(java.io.IOException) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) HashMapGroupMgr(com.linkedin.restli.examples.groups.server.impl.HashMapGroupMgr) GroupMgr(com.linkedin.restli.examples.groups.server.api.GroupMgr) StreamRequestHandler(com.linkedin.r2.transport.common.StreamRequestHandler) RestRequest(com.linkedin.r2.message.rest.RestRequest) InjectMockResourceFactory(com.linkedin.restli.server.mock.InjectMockResourceFactory) HashGroupMembershipMgr(com.linkedin.restli.examples.groups.server.impl.HashGroupMembershipMgr) RestLiConfig(com.linkedin.restli.server.RestLiConfig)

Example 8 with TransportResponseImpl

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()));
    }
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) RestRequest(com.linkedin.r2.message.rest.RestRequest) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) FutureCallback(com.linkedin.common.callback.FutureCallback) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) StreamException(com.linkedin.r2.message.stream.StreamException) RestException(com.linkedin.r2.message.rest.RestException) Test(org.testng.annotations.Test)

Example 9 with TransportResponseImpl

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()));
    }
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) FutureCallback(com.linkedin.common.callback.FutureCallback) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) StreamException(com.linkedin.r2.message.stream.StreamException) RestException(com.linkedin.r2.message.rest.RestException) Test(org.testng.annotations.Test)

Example 10 with TransportResponseImpl

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)));
    }
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) URI(java.net.URI) RestRequestHandler(com.linkedin.r2.transport.common.RestRequestHandler) RestException(com.linkedin.r2.message.rest.RestException)

Aggregations

StreamResponse (com.linkedin.r2.message.stream.StreamResponse)7 RestException (com.linkedin.r2.message.rest.RestException)6 URI (java.net.URI)6 RestRequest (com.linkedin.r2.message.rest.RestRequest)5 RestResponse (com.linkedin.r2.message.rest.RestResponse)5 FutureCallback (com.linkedin.common.callback.FutureCallback)4 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)4 StreamRequestHandler (com.linkedin.r2.transport.common.StreamRequestHandler)4 TransportCallbackAdapter (com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter)4 Test (org.testng.annotations.Test)4 ByteString (com.linkedin.data.ByteString)3 RequestContext (com.linkedin.r2.message.RequestContext)3 StreamException (com.linkedin.r2.message.stream.StreamException)3 DrainReader (com.linkedin.r2.message.stream.entitystream.DrainReader)3 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)3 TransportDispatcher (com.linkedin.r2.transport.common.bridge.server.TransportDispatcher)3 HttpServerFactory (com.linkedin.r2.transport.http.server.HttpServerFactory)3 HashMap (java.util.HashMap)3 FilterChain (com.linkedin.r2.filter.FilterChain)2 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)2