Search in sources :

Example 26 with FilterChain

use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.

the class TestServerRetryFilter method testRetryFilter.

@Test
public void testRetryFilter() {
    String retryMessage = "this is a retry";
    ServerRetryFilter retryFilter = new ServerRetryFilter();
    RestFilter captureFilter = new RestFilter() {

        @Override
        public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            Assert.assertEquals(wireAttrs.get(R2Constants.RETRY_MESSAGE_ATTRIBUTE_KEY), retryMessage);
        }
    };
    FilterChain filterChain = FilterChains.createRestChain(captureFilter, retryFilter);
    FilterUtil.fireRestError(filterChain, new RestException(null, new RetriableRequestException(retryMessage)), new HashMap<String, String>());
}
Also used : RetriableRequestException(com.linkedin.r2.RetriableRequestException) RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) ServerRetryFilter(com.linkedin.r2.filter.transport.ServerRetryFilter) NextFilter(com.linkedin.r2.filter.NextFilter) FilterChain(com.linkedin.r2.filter.FilterChain) RestException(com.linkedin.r2.message.rest.RestException) RequestContext(com.linkedin.r2.message.RequestContext) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 27 with FilterChain

use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.

the class TestClientRetryFilter method testRetryFilter.

@Test
public void testRetryFilter() {
    String retryMessage = "this is a retry";
    ClientRetryFilter clientRetryFilter = new ClientRetryFilter();
    RestFilter captureFilter = new RestFilter() {

        @Override
        public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            Assert.assertTrue(ex instanceof RetriableRequestException);
            Assert.assertEquals(retryMessage, ex.getMessage());
        }
    };
    Map<String, String> wireAttributes = new HashMap<>();
    wireAttributes.put(R2Constants.RETRY_MESSAGE_ATTRIBUTE_KEY, retryMessage);
    FilterChain filterChain = FilterChains.createRestChain(captureFilter, clientRetryFilter);
    FilterUtil.fireRestError(filterChain, new RemoteInvocationException("exception"), wireAttributes);
}
Also used : RetriableRequestException(com.linkedin.r2.RetriableRequestException) RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) HashMap(java.util.HashMap) NextFilter(com.linkedin.r2.filter.NextFilter) FilterChain(com.linkedin.r2.filter.FilterChain) ClientRetryFilter(com.linkedin.r2.filter.transport.ClientRetryFilter) RequestContext(com.linkedin.r2.message.RequestContext) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 28 with FilterChain

use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.

the class TestClientRetryFilter method testNoWireAttribute.

@Test
public void testNoWireAttribute() {
    ClientRetryFilter clientRetryFilter = new ClientRetryFilter();
    RemoteInvocationException exception = new RemoteInvocationException("exception");
    RestFilter captureFilter = new RestFilter() {

        @Override
        public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            Assert.assertEquals(exception, ex);
        }
    };
    FilterChain filterChain = FilterChains.createRestChain(captureFilter, clientRetryFilter);
    FilterUtil.fireRestError(filterChain, exception, new HashMap<>());
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) NextFilter(com.linkedin.r2.filter.NextFilter) FilterChain(com.linkedin.r2.filter.FilterChain) ClientRetryFilter(com.linkedin.r2.filter.transport.ClientRetryFilter) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) RequestContext(com.linkedin.r2.message.RequestContext) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 29 with FilterChain

use of com.linkedin.r2.filter.FilterChain 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 30 with FilterChain

use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.

the class MockHttpServerFactory method create.

/**
   * Creates a {@link HttpServer} that contains a {@link RestLiServer} to be used for testing a set of Rest.li
   * resources.
   *
   * The {@link HttpServer} uses an empty {@link FilterChain} and uses "/" as the context path.
   *
   * If the server is run in async mode (by calling this function with the last parameter {@code true}), the
   * timeout used is {@link #ASYNC_TIMEOUT}.
   *
   * Both the async and sync servers will use {@link #NUM_THREADS} threads.
   *
   * @param port the port the server will run on on localhost
   * @param config the {@link RestLiConfig} to be used by the {@link RestLiServer}
   * @param beans beans you want to inject into your Rest.li resource.
   * @param enableAsync true if the server should be async , false otherwise
   * @return
   */
private static HttpServer create(int port, RestLiConfig config, Map<String, ?> beans, boolean enableAsync) {
    final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(NUM_THREADS);
    final ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
    EngineBuilder engineBuilder = new EngineBuilder().setTaskExecutor(scheduler).setTimerScheduler(scheduler);
    com.linkedin.parseq.AsyncCallableTask.register(engineBuilder, executor);
    final Engine engine = engineBuilder.build();
    ResourceFactory resourceFactory = createResourceFactory(beans);
    TransportDispatcher dispatcher = new DelegatingTransportDispatcher(new RestLiServer(config, resourceFactory, engine));
    final FilterChain fc = FilterChains.empty().addLastRest(new SimpleLoggingFilter());
    final HttpServer server = new HttpServerFactory(fc).createServer(port, HttpServerFactory.DEFAULT_CONTEXT_PATH, NUM_THREADS, dispatcher, enableAsync ? HttpJettyServer.ServletType.ASYNC_EVENT : HttpJettyServer.ServletType.RAP, enableAsync ? ASYNC_TIMEOUT : -1);
    return new HttpServer() {

        @Override
        public void start() throws IOException {
            server.start();
        }

        @Override
        public void stop() throws IOException {
            server.stop();
            engine.shutdown();
            executor.shutdown();
            scheduler.shutdown();
        }

        @Override
        public void waitForStop() throws InterruptedException {
            server.waitForStop();
        }
    };
}
Also used : HttpServerFactory(com.linkedin.r2.transport.http.server.HttpServerFactory) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RestLiServer(com.linkedin.restli.server.RestLiServer) DelegatingTransportDispatcher(com.linkedin.restli.server.DelegatingTransportDispatcher) FilterChain(com.linkedin.r2.filter.FilterChain) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) HttpServer(com.linkedin.r2.transport.http.server.HttpServer) ResourceFactory(com.linkedin.restli.server.resources.ResourceFactory) InjectMockResourceFactory(com.linkedin.restli.server.mock.InjectMockResourceFactory) DelegatingTransportDispatcher(com.linkedin.restli.server.DelegatingTransportDispatcher) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher) EngineBuilder(com.linkedin.parseq.EngineBuilder) SimpleLoggingFilter(com.linkedin.r2.filter.logging.SimpleLoggingFilter) Engine(com.linkedin.parseq.Engine)

Aggregations

FilterChain (com.linkedin.r2.filter.FilterChain)45 Test (org.testng.annotations.Test)38 RequestContext (com.linkedin.r2.message.RequestContext)28 RestRequest (com.linkedin.r2.message.rest.RestRequest)20 RestResponse (com.linkedin.r2.message.rest.RestResponse)17 RestFilter (com.linkedin.r2.filter.message.rest.RestFilter)15 HashMap (java.util.HashMap)12 ByteString (com.linkedin.data.ByteString)11 Map (java.util.Map)11 RestException (com.linkedin.r2.message.rest.RestException)10 RestCountFilter (com.linkedin.r2.testutils.filter.RestCountFilter)10 StreamCountFilter (com.linkedin.r2.testutils.filter.StreamCountFilter)10 NextFilter (com.linkedin.r2.filter.NextFilter)9 Callback (com.linkedin.common.callback.Callback)8 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)8 FullEntityReader (com.linkedin.r2.message.stream.entitystream.FullEntityReader)8 CaptureLastCallFilter (com.linkedin.r2.testutils.filter.CaptureLastCallFilter)7 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)6 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)5 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)5