Search in sources :

Example 11 with FullEntityReader

use of com.linkedin.r2.message.stream.entitystream.FullEntityReader in project rest.li by linkedin.

the class TestStreamFilterAdapters method testResponseFilterAdapterChangeError.

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

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

        @Override
        public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            if (ex instanceof RestException) {
                RestResponse res = ((RestException) ex).getResponse();
                String newEntityStr = res.getEntity().asString("UTF8").replace('1', '0');
                nextFilter.onError(new RestException((res.builder().setEntity(newEntityStr.getBytes()).build())), requestContext, wireAttrs);
            } else {
                nextFilter.onError(new IllegalStateException(), requestContext, wireAttrs);
            }
        }
    });
    fc.onStreamError(simpleStreamException("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"), "02345");
        }
    }));
    fc.onStreamError(new IllegalArgumentException(), 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) FilterChain(com.linkedin.r2.filter.FilterChain) RestException(com.linkedin.r2.message.rest.RestException) 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) Test(org.testng.annotations.Test)

Example 12 with FullEntityReader

use of com.linkedin.r2.message.stream.entitystream.FullEntityReader in project rest.li by linkedin.

the class TestStreamFilterAdapters method testResponseFilterAdapterPassThrough.

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

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

        @Override
        public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            nextFilter.onError(ex, requestContext, wireAttrs);
        }
    });
    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"), "12345");
        }
    }));
    fc.onStreamError(simpleStreamException("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");
        }
    }));
}
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) 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) Test(org.testng.annotations.Test)

Example 13 with FullEntityReader

use of com.linkedin.r2.message.stream.entitystream.FullEntityReader in project rest.li by linkedin.

the class Messages method toRestRequest.

public static CompletionStage<RestRequest> toRestRequest(StreamRequest streamRequest) {
    CompletableFuture<RestRequest> completable = new CompletableFuture<>();
    final RestRequestBuilder builder = new RestRequestBuilder(streamRequest);
    streamRequest.getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {

        @Override
        public void onError(Throwable e) {
            completable.completeExceptionally(e);
        }

        @Override
        public void onSuccess(ByteString result) {
            RestRequest restRequest = builder.setEntity(result).build();
            completable.complete(restRequest);
        }
    }));
    return completable;
}
Also used : FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) CompletableFuture(java.util.concurrent.CompletableFuture) RestRequest(com.linkedin.r2.message.rest.RestRequest) Callback(com.linkedin.common.callback.Callback) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) ByteString(com.linkedin.data.ByteString) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder)

Example 14 with FullEntityReader

use of com.linkedin.r2.message.stream.entitystream.FullEntityReader in project rest.li by linkedin.

the class Messages method toRestResponse.

/**
 * Converts a StreamResponse to RestResponse
 * @param streamResponse the stream request to be converted
 * @param callback the callback to be invoked when the rest response is constructed
 * @param addContentLengthHeader whether the rest response should have content-length header
 */
public static void toRestResponse(StreamResponse streamResponse, final Callback<RestResponse> callback, final boolean addContentLengthHeader) {
    final RestResponseBuilder builder = new RestResponseBuilder(streamResponse);
    Callback<ByteString> assemblyCallback = new Callback<ByteString>() {

        @Override
        public void onError(Throwable e) {
            callback.onError(e);
        }

        @Override
        public void onSuccess(ByteString result) {
            if (addContentLengthHeader) {
                builder.setHeader(HttpConstants.CONTENT_LENGTH, String.valueOf(result.length()));
            }
            RestResponse restResponse = builder.setEntity(result).build();
            callback.onSuccess(restResponse);
        }
    };
    streamResponse.getEntityStream().setReader(new FullEntityReader(assemblyCallback));
}
Also used : FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) Callback(com.linkedin.common.callback.Callback) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) ByteString(com.linkedin.data.ByteString) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder)

Example 15 with FullEntityReader

use of com.linkedin.r2.message.stream.entitystream.FullEntityReader in project rest.li by linkedin.

the class Messages method toRestRequest.

/**
 * Converts a StreamRequest to RestRequest
 * @param streamRequest the stream request to be converted
 * @param callback the callback to be invoked when the rest request is constructed
 */
public static void toRestRequest(StreamRequest streamRequest, final Callback<RestRequest> callback) {
    final RestRequestBuilder builder = new RestRequestBuilder(streamRequest);
    Callback<ByteString> assemblyCallback = new Callback<ByteString>() {

        @Override
        public void onError(Throwable e) {
            callback.onError(e);
        }

        @Override
        public void onSuccess(ByteString result) {
            RestRequest restRequest = builder.setEntity(result).build();
            callback.onSuccess(restRequest);
        }
    };
    streamRequest.getEntityStream().setReader(new FullEntityReader(assemblyCallback));
}
Also used : FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) Callback(com.linkedin.common.callback.Callback) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) RestRequest(com.linkedin.r2.message.rest.RestRequest) ByteString(com.linkedin.data.ByteString) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder)

Aggregations

ByteString (com.linkedin.data.ByteString)22 FullEntityReader (com.linkedin.r2.message.stream.entitystream.FullEntityReader)22 Test (org.testng.annotations.Test)18 Callback (com.linkedin.common.callback.Callback)15 RequestContext (com.linkedin.r2.message.RequestContext)14 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)12 RestRequest (com.linkedin.r2.message.rest.RestRequest)10 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)10 RestResponse (com.linkedin.r2.message.rest.RestResponse)9 FilterChain (com.linkedin.r2.filter.FilterChain)8 RestFilter (com.linkedin.r2.filter.message.rest.RestFilter)8 BeforeTest (org.testng.annotations.BeforeTest)8 StreamException (com.linkedin.r2.message.stream.StreamException)7 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)7 URI (java.net.URI)7 FutureCallback (com.linkedin.common.callback.FutureCallback)5 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)5 AfterTest (org.testng.annotations.AfterTest)5 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)4 MIMEDataPart (com.linkedin.multipart.utils.MIMEDataPart)3