Search in sources :

Example 16 with NextFilter

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

the class TestStreamFilterAdapters method testResponseFilterAdapterChangeResponse.

@Test
public void testResponseFilterAdapterChangeResponse() {
    FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {

        @Override
        public void onRestResponse(RestResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            nextFilter.onResponse(res.builder().setEntity(res.getEntity().asString("UTF8").replace('1', '0').getBytes()).build(), requestContext, wireAttrs);
        }

        @Override
        public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
        }
    });
    fc.onStreamResponse(simpleStreamResponse("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
    StreamResponse capturedResponse = _beforeFilter.getResponse();
    capturedResponse.getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {

        @Override
        public void onError(Throwable e) {
            Assert.fail("should not happen");
        }

        @Override
        public void onSuccess(ByteString result) {
            Assert.assertEquals(result.asString("UTF8"), "02345");
        }
    }));
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) FilterChain(com.linkedin.r2.filter.FilterChain) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ByteString(com.linkedin.data.ByteString) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) RestRequest(com.linkedin.r2.message.rest.RestRequest) Callback(com.linkedin.common.callback.Callback) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 17 with NextFilter

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

the class TestStreamFilterAdapters method testResponseFilterAdapterCallsOnErrorInOnResponse.

@Test
public void testResponseFilterAdapterCallsOnErrorInOnResponse() {
    FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {

        @Override
        public void onRestResponse(RestResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            nextFilter.onError(simpleRestException(res.getEntity().asString("UTF8")), requestContext, wireAttrs);
        }

        @Override
        public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
        }
    });
    fc.onStreamResponse(simpleStreamResponse("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
    Throwable capturedEx = _beforeFilter.getThrowable();
    Assert.assertTrue(capturedEx instanceof StreamException);
    ((StreamException) capturedEx).getResponse().getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {

        @Override
        public void onError(Throwable e) {
            Assert.fail("should not happen");
        }

        @Override
        public void onSuccess(ByteString result) {
            Assert.assertEquals(result.asString("UTF8"), "12345");
        }
    }));
    fc = adaptAndCreateFilterChain(new RestFilter() {

        @Override
        public void onRestResponse(RestResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            nextFilter.onError(new IllegalStateException(), requestContext, wireAttrs);
        }

        @Override
        public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
        }
    });
    fc.onStreamResponse(simpleStreamResponse("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
    capturedEx = _beforeFilter.getThrowable();
    Assert.assertTrue(capturedEx instanceof IllegalStateException);
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) NextFilter(com.linkedin.r2.filter.NextFilter) FilterChain(com.linkedin.r2.filter.FilterChain) ByteString(com.linkedin.data.ByteString) StreamException(com.linkedin.r2.message.stream.StreamException) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) RestRequest(com.linkedin.r2.message.rest.RestRequest) Callback(com.linkedin.common.callback.Callback) RequestContext(com.linkedin.r2.message.RequestContext) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 18 with NextFilter

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

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

use of com.linkedin.r2.filter.NextFilter 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)

Aggregations

RequestContext (com.linkedin.r2.message.RequestContext)23 Test (org.testng.annotations.Test)22 Map (java.util.Map)18 NextFilter (com.linkedin.r2.filter.NextFilter)17 FilterChain (com.linkedin.r2.filter.FilterChain)15 RestResponse (com.linkedin.r2.message.rest.RestResponse)15 RestFilter (com.linkedin.r2.filter.message.rest.RestFilter)14 RestRequest (com.linkedin.r2.message.rest.RestRequest)14 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)13 Callback (com.linkedin.common.callback.Callback)10 HashMap (java.util.HashMap)10 ByteString (com.linkedin.data.ByteString)9 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 FullEntityReader (com.linkedin.r2.message.stream.entitystream.FullEntityReader)8 URI (java.net.URI)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 StreamException (com.linkedin.r2.message.stream.StreamException)7 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)7 RestException (com.linkedin.r2.message.rest.RestException)6