Search in sources :

Example 6 with FullEntityReader

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

the class TestMIMEWriter method testPrependDataSources.

@Test(dataProvider = "prependDataSources")
public void testPrependDataSources(final MultiPartMIMEWriter.Builder builder, final List<MultiPartMIMEDataSourceWriter> prependDataSources, final List<MIMEDataPart> expectedParts, final String expectedPreamble, final int expectedSize) throws Exception {
    for (final MultiPartMIMEDataSourceWriter dataSourceWriter : prependDataSources) {
        builder.prependDataSource(dataSourceWriter);
    }
    Assert.assertEquals(builder.getCurrentSize(), expectedSize);
    final MultiPartMIMEWriter writer = builder.build();
    final FutureCallback<ByteString> futureCallback = new FutureCallback<>();
    final FullEntityReader fullEntityReader = new FullEntityReader(futureCallback);
    writer.getEntityStream().setReader(fullEntityReader);
    futureCallback.get(_testTimeout, TimeUnit.MILLISECONDS);
    final StreamRequest multiPartMIMEStreamRequest = MultiPartMIMEStreamRequestFactory.generateMultiPartMIMEStreamRequest(URI.create("localhost"), "mixed", writer, Collections.<String, String>emptyMap());
    final JavaxMailMultiPartMIMEReader javaxMailMultiPartMIMEReader = new JavaxMailMultiPartMIMEReader(multiPartMIMEStreamRequest.getHeader(MultiPartMIMEUtils.CONTENT_TYPE_HEADER), futureCallback.get());
    javaxMailMultiPartMIMEReader.parseRequestIntoParts();
    List<MIMEDataPart> dataSourceList = javaxMailMultiPartMIMEReader._dataSourceList;
    Assert.assertEquals(dataSourceList.size(), 3);
    for (int i = 0; i < dataSourceList.size(); i++) {
        Assert.assertEquals(dataSourceList.get(i), expectedParts.get(i));
    }
    // Javax mail incorrectly adds the CRLF for the first boundary to the end of the preamble, so we trim
    Assert.assertEquals(javaxMailMultiPartMIMEReader._preamble != null ? javaxMailMultiPartMIMEReader._preamble.trim() : null, expectedPreamble);
}
Also used : FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) ByteString(com.linkedin.data.ByteString) MIMEDataPart(com.linkedin.multipart.utils.MIMEDataPart) FutureCallback(com.linkedin.common.callback.FutureCallback) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 7 with FullEntityReader

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

the class TestMIMEWriter method testSingleDataSource.

@Test(dataProvider = "singleDataSources")
public void testSingleDataSource(final ByteString body, final Map<String, String> headers) throws Exception {
    final MIMEDataPart expectedMultiPartMIMEDataPart = new MIMEDataPart(body, headers);
    final MultiPartMIMEInputStream singleDataSource = new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(body.copyBytes()), _scheduledExecutorService, headers).build();
    final MultiPartMIMEWriter multiPartMIMEWriter = new MultiPartMIMEWriter.Builder("preamble", "epilogue").appendDataSource(singleDataSource).build();
    final FutureCallback<ByteString> futureCallback = new FutureCallback<>();
    final FullEntityReader fullEntityReader = new FullEntityReader(futureCallback);
    multiPartMIMEWriter.getEntityStream().setReader(fullEntityReader);
    futureCallback.get(_testTimeout, TimeUnit.MILLISECONDS);
    final StreamRequest multiPartMIMEStreamRequest = MultiPartMIMEStreamRequestFactory.generateMultiPartMIMEStreamRequest(URI.create("localhost"), "mixed", multiPartMIMEWriter, Collections.<String, String>emptyMap());
    JavaxMailMultiPartMIMEReader javaxMailMultiPartMIMEReader = new JavaxMailMultiPartMIMEReader(multiPartMIMEStreamRequest.getHeader(MultiPartMIMEUtils.CONTENT_TYPE_HEADER), futureCallback.get());
    javaxMailMultiPartMIMEReader.parseRequestIntoParts();
    List<MIMEDataPart> dataSourceList = javaxMailMultiPartMIMEReader._dataSourceList;
    Assert.assertEquals(dataSourceList.size(), 1);
    Assert.assertEquals(dataSourceList.get(0), expectedMultiPartMIMEDataPart);
    // Javax mail incorrectly adds the CRLF for the first boundary to the end of the preamble, so we trim
    Assert.assertEquals(javaxMailMultiPartMIMEReader._preamble.trim(), "preamble");
}
Also used : ByteString(com.linkedin.data.ByteString) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) ByteArrayInputStream(java.io.ByteArrayInputStream) MIMEDataPart(com.linkedin.multipart.utils.MIMEDataPart) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 8 with FullEntityReader

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

the class TestClientStreamCompressionFilter method testRequestCompressionRules.

@Test(dataProvider = "requestData")
public void testRequestCompressionRules(CompressionConfig requestCompressionConfig, CompressionOption requestCompressionOverride, boolean headerShouldBePresent, String operation) throws CompressionException, URISyntaxException, InterruptedException, ExecutionException, TimeoutException {
    Executor executor = Executors.newCachedThreadPool();
    ClientStreamCompressionFilter clientCompressionFilter = new ClientStreamCompressionFilter(StreamEncodingType.GZIP.getHttpName(), requestCompressionConfig, ACCEPT_COMPRESSIONS, new CompressionConfig(Integer.MAX_VALUE), Arrays.asList(ClientCompressionHelper.COMPRESS_ALL_RESPONSES_INDICATOR), executor);
    // The entity should be compressible for this test.
    int original = 100;
    byte[] entity = new byte[original];
    Arrays.fill(entity, (byte) 'A');
    StreamRequest streamRequest = new StreamRequestBuilder(new URI(URI)).setMethod(RestMethod.POST).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(entity))));
    int compressed = EncodingType.GZIP.getCompressor().deflate(new ByteArrayInputStream(entity)).length;
    RequestContext context = new RequestContext();
    if (operation != null) {
        context.putLocalAttr(R2Constants.OPERATION, operation);
    }
    context.putLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE, requestCompressionOverride);
    int entityLength = headerShouldBePresent ? compressed : original;
    FutureCallback<ByteString> callback = new FutureCallback<>();
    FullEntityReader reader = new FullEntityReader(callback);
    HeaderCaptureFilter captureFilter = new HeaderCaptureFilter(HttpConstants.CONTENT_ENCODING, headerShouldBePresent, entityLength, reader);
    clientCompressionFilter.onStreamRequest(streamRequest, context, Collections.<String, String>emptyMap(), captureFilter);
    ByteString entityRead = callback.get(10, TimeUnit.SECONDS);
    Assert.assertEquals(entityRead.length(), entityLength);
}
Also used : ByteString(com.linkedin.data.ByteString) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) Executor(java.util.concurrent.Executor) ByteArrayInputStream(java.io.ByteArrayInputStream) RequestContext(com.linkedin.r2.message.RequestContext) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) FutureCallback(com.linkedin.common.callback.FutureCallback) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) Test(org.testng.annotations.Test)

Example 9 with FullEntityReader

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

the class TestRestLiServer method testValidReactiveUnstructuredDataRequest.

@Test(dataProvider = "validClientProtocolVersionDataStreamOnly")
public void testValidReactiveUnstructuredDataRequest(RestLiServer server, ProtocolVersion clientProtocolVersion, String headerConstant) throws URISyntaxException, IOException {
    StreamRequest streamRequest = new StreamRequestBuilder(new URI("/reactiveFeedDownloads/1")).setHeader(headerConstant, clientProtocolVersion.toString()).build(EntityStreams.emptyStream());
    final FeedDownloadResourceReactive resource = getMockResource(FeedDownloadResourceReactive.class);
    resource.get(eq(1L), anyObject());
    EasyMock.expectLastCall().andDelegateTo(new FeedDownloadResourceReactive()).once();
    replay(resource);
    @SuppressWarnings("unchecked") Callback<StreamResponse> r2Callback = createMock(Callback.class);
    final Capture<StreamResponse> streamResponse = EasyMock.newCapture();
    r2Callback.onSuccess(capture(streamResponse));
    expectLastCall().once();
    replay(r2Callback);
    RequestContext requestContext = new RequestContext();
    server.handleRequest(streamRequest, requestContext, r2Callback);
    verify(resource);
    verify(r2Callback);
    assertNotNull(streamResponse);
    assertEquals(streamResponse.getValue().getHeader(RestConstants.HEADER_CONTENT_TYPE), FeedDownloadResourceReactive.CONTENT_TYPE);
    FullEntityReader fullEntityReader = new FullEntityReader(new Callback<ByteString>() {

        @Override
        public void onError(Throwable e) {
            fail("Error inside callback!! Failed to read response data from stream!", e);
        }

        @Override
        public void onSuccess(ByteString result) {
            assertEquals(result, FeedDownloadResourceReactive.CONTENT);
        }
    });
    streamResponse.getValue().getEntityStream().setReader(fullEntityReader);
}
Also used : ByteString(com.linkedin.data.ByteString) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) FeedDownloadResourceReactive(com.linkedin.restli.server.twitter.FeedDownloadResourceReactive) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 10 with FullEntityReader

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

the class TestStreamFilterAdapters method testRequestFilterAdapterCallsOnResponse.

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

        @Override
        public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            nextFilter.onResponse(simpleRestResponse(req.getEntity().asString("UTF8")), requestContext, wireAttrs);
        }
    });
    fc.onStreamRequest(simpleStreamRequest("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
    StreamResponse capturedReq = _beforeFilter.getResponse();
    capturedReq.getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {

        @Override
        public void onError(Throwable e) {
            Assert.fail("shouldn't have error");
        }

        @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) 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)

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