Search in sources :

Example 46 with StreamResponse

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

the class TestMIMEChainingAlternate method testAlternateSinglePartDataSource.

//This test has the server alternate between consuming a part and sending a part as a data source
//to a writer.
//Since we have four parts, the server will consume the 2nd and 4th and send out the 1st and 3rd.
//To make the test easier we will have two callbacks to send to the server to indicate
//the presence of each data source.
//This violates the typical client/server http pattern, but accomplishes the purpose of this test
//and it makes it easier to write.
@Test(dataProvider = "chunkSizes")
public void testAlternateSinglePartDataSource(final int chunkSize) throws Exception {
    final List<MultiPartMIMEDataSourceWriter> dataSources = generateInputStreamDataSources(chunkSize, _scheduledExecutorService);
    final MultiPartMIMEWriter writer = new MultiPartMIMEWriter.Builder().appendDataSources(dataSources).build();
    final StreamRequest streamRequest = mock(StreamRequest.class);
    when(streamRequest.getEntityStream()).thenReturn(writer.getEntityStream());
    final String contentTypeHeader = "multipart/mixed; boundary=" + writer.getBoundary();
    when(streamRequest.getHeader(MultiPartMIMEUtils.CONTENT_TYPE_HEADER)).thenReturn(contentTypeHeader);
    //Client side preparation to read the part back on the callback.
    //We have two callbacks here since we will get two responses.
    //Note the chunks size will carry over since the client is controlling how much data he gets back
    //based on the chunk size he writes.
    MIMETestUtils.MultiPartMIMEFullReaderCallback clientReceiverA = new MIMETestUtils.MultiPartMIMEFullReaderCallback();
    MIMETestUtils.MultiPartMIMEFullReaderCallback clientReceiverB = new MIMETestUtils.MultiPartMIMEFullReaderCallback();
    Callback<StreamResponse> callbackA = generateSuccessChainCallback(clientReceiverA);
    Callback<StreamResponse> callbackB = generateSuccessChainCallback(clientReceiverB);
    //Server side start
    MultiPartMIMEReader reader = MultiPartMIMEReader.createAndAcquireStream(streamRequest);
    final CountDownLatch latch = new CountDownLatch(1);
    ServerMultiPartMIMEAlternatorCallback serverSender = new ServerMultiPartMIMEAlternatorCallback(latch, callbackA, callbackB);
    reader.registerReaderCallback(serverSender);
    latch.await(_testTimeout, TimeUnit.MILLISECONDS);
    //Verify client
    Assert.assertEquals(clientReceiverA.getSinglePartMIMEReaderCallbacks().size(), 1);
    Assert.assertEquals(clientReceiverA.getSinglePartMIMEReaderCallbacks().get(0).getFinishedData(), BODY_A.getPartData());
    Assert.assertEquals(clientReceiverA.getSinglePartMIMEReaderCallbacks().get(0).getHeaders(), BODY_A.getPartHeaders());
    Assert.assertEquals(clientReceiverB.getSinglePartMIMEReaderCallbacks().size(), 1);
    Assert.assertEquals(clientReceiverB.getSinglePartMIMEReaderCallbacks().get(0).getFinishedData(), BODY_C.getPartData());
    Assert.assertEquals(clientReceiverB.getSinglePartMIMEReaderCallbacks().get(0).getHeaders(), BODY_C.getPartHeaders());
    //Verify server
    Assert.assertEquals(serverSender.getSinglePartMIMEReaderCallbacks().size(), 2);
    Assert.assertEquals(serverSender.getSinglePartMIMEReaderCallbacks().get(0).getFinishedData(), BODY_B.getPartData());
    Assert.assertEquals(serverSender.getSinglePartMIMEReaderCallbacks().get(0).getHeaders(), BODY_B.getPartHeaders());
    Assert.assertEquals(serverSender.getSinglePartMIMEReaderCallbacks().get(1).getFinishedData(), BODY_D.getPartData());
    Assert.assertEquals(serverSender.getSinglePartMIMEReaderCallbacks().get(1).getHeaders(), BODY_D.getPartHeaders());
}
Also used : StreamResponse(com.linkedin.r2.message.stream.StreamResponse) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) MIMETestUtils(com.linkedin.multipart.utils.MIMETestUtils) Test(org.testng.annotations.Test)

Example 47 with StreamResponse

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

the class TestMIMEChainingSinglePart method testSinglePartDataSource.

//Verifies that a single part mime reader can be used as a data source to the writer.
//To make the test easier to write, we simply chain back to the client in the form of simulating a response.
@Test(dataProvider = "chunkSizes")
public void testSinglePartDataSource(final int chunkSize) throws Exception {
    final List<MultiPartMIMEDataSourceWriter> dataSources = generateInputStreamDataSources(chunkSize, _scheduledExecutorService);
    final MultiPartMIMEWriter writer = new MultiPartMIMEWriter.Builder().appendDataSources(dataSources).build();
    final StreamRequest streamRequest = mock(StreamRequest.class);
    when(streamRequest.getEntityStream()).thenReturn(writer.getEntityStream());
    final String contentTypeHeader = "multipart/mixed; boundary=" + writer.getBoundary();
    when(streamRequest.getHeader(MultiPartMIMEUtils.CONTENT_TYPE_HEADER)).thenReturn(contentTypeHeader);
    //Client side preparation to read the part back on the callback
    //Note the chunks size will carry over since the client is controlling how much data he gets back
    //based on the chunk size he writes.
    MIMETestUtils.MultiPartMIMEFullReaderCallback clientReceiver = new MIMETestUtils.MultiPartMIMEFullReaderCallback();
    Callback<StreamResponse> callback = generateSuccessChainCallback(clientReceiver);
    //Server side start
    MultiPartMIMEReader reader = MultiPartMIMEReader.createAndAcquireStream(streamRequest);
    final CountDownLatch latch = new CountDownLatch(1);
    ServerMultiPartMIMEReaderSinglePartSenderCallback serverSender = new ServerMultiPartMIMEReaderSinglePartSenderCallback(latch, callback);
    reader.registerReaderCallback(serverSender);
    latch.await(_testTimeout, TimeUnit.MILLISECONDS);
    //Verify client
    Assert.assertEquals(clientReceiver.getSinglePartMIMEReaderCallbacks().size(), 1);
    Assert.assertEquals(clientReceiver.getSinglePartMIMEReaderCallbacks().get(0).getFinishedData(), BODY_A.getPartData());
    Assert.assertEquals(clientReceiver.getSinglePartMIMEReaderCallbacks().get(0).getHeaders(), BODY_A.getPartHeaders());
    //Verify server
    List<MIMETestUtils.SinglePartMIMEFullReaderCallback> singlePartMIMEReaderCallbacks = serverSender.getSinglePartMIMEReaderCallbacks();
    Assert.assertEquals(singlePartMIMEReaderCallbacks.size(), 3);
    Assert.assertEquals(singlePartMIMEReaderCallbacks.get(0).getFinishedData(), BODY_B.getPartData());
    Assert.assertEquals(singlePartMIMEReaderCallbacks.get(0).getHeaders(), BODY_B.getPartHeaders());
    Assert.assertEquals(singlePartMIMEReaderCallbacks.get(1).getFinishedData(), BODY_C.getPartData());
    Assert.assertEquals(singlePartMIMEReaderCallbacks.get(1).getHeaders(), BODY_C.getPartHeaders());
    Assert.assertEquals(singlePartMIMEReaderCallbacks.get(2).getFinishedData(), BODY_D.getPartData());
    Assert.assertEquals(singlePartMIMEReaderCallbacks.get(2).getHeaders(), BODY_D.getPartHeaders());
}
Also used : StreamResponse(com.linkedin.r2.message.stream.StreamResponse) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) SinglePartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback) MIMETestUtils(com.linkedin.multipart.utils.MIMETestUtils) Test(org.testng.annotations.Test)

Example 48 with StreamResponse

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

the class TestMIMEChainingMultipleSources method generateSuccessChainCallback.

private Callback<StreamResponse> generateSuccessChainCallback(final ClientMultiPartReceiver receiver) {
    return new Callback<StreamResponse>() {

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

        @Override
        public void onSuccess(StreamResponse result) {
            final MultiPartMIMEReader reader = MultiPartMIMEReader.createAndAcquireStream(result);
            reader.registerReaderCallback(receiver);
        }
    };
}
Also used : FutureCallback(com.linkedin.common.callback.FutureCallback) Callback(com.linkedin.common.callback.Callback) MultiPartMIMEReaderCallback(com.linkedin.multipart.MultiPartMIMEReaderCallback) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) MultiPartMIMEReader(com.linkedin.multipart.MultiPartMIMEReader)

Example 49 with StreamResponse

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

the class TestMIMEChainingMultipleSources method generateServerAResponseCallback.

private Callback<StreamResponse> generateServerAResponseCallback(final StreamRequest incomingRequest, final Callback<StreamResponse> incomingRequestCallback) {
    return new Callback<StreamResponse>() {

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

        @Override
        public void onSuccess(StreamResponse result) {
            final MultiPartMIMEReader reader = MultiPartMIMEReader.createAndAcquireStream(result);
            _serverAMultiPartCallback = new ServerAMultiPartCallback(incomingRequest, incomingRequestCallback);
            reader.registerReaderCallback(_serverAMultiPartCallback);
        }
    };
}
Also used : FutureCallback(com.linkedin.common.callback.FutureCallback) Callback(com.linkedin.common.callback.Callback) MultiPartMIMEReaderCallback(com.linkedin.multipart.MultiPartMIMEReaderCallback) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) MultiPartMIMEReader(com.linkedin.multipart.MultiPartMIMEReader)

Example 50 with StreamResponse

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

the class ServerQueryTunnelFilter method onStreamRequest.

@Override
public void onStreamRequest(final StreamRequest req, final RequestContext requestContext, final Map<String, String> wireAttrs, final NextFilter<StreamRequest, StreamResponse> nextFilter) {
    Callback<StreamRequest> callback = new Callback<StreamRequest>() {

        @Override
        public void onError(Throwable e) {
            if (e instanceof MessagingException || e instanceof URISyntaxException) {
                RestResponse errorResponse = RestStatus.responseForStatus(RestStatus.BAD_REQUEST, e.toString());
                nextFilter.onResponse(Messages.toStreamResponse(errorResponse), requestContext, wireAttrs);
            } else {
                nextFilter.onError(e, requestContext, wireAttrs);
            }
        }

        @Override
        public void onSuccess(StreamRequest newReq) {
            nextFilter.onRequest(newReq, requestContext, wireAttrs);
        }
    };
    // the entire request would be buffered in memory if decoding is needed
    // this usually is not a problem as request with extremely query parameters is usually get request with no body
    QueryTunnelUtil.decode(req, requestContext, callback);
}
Also used : Callback(com.linkedin.common.callback.Callback) MessagingException(javax.mail.MessagingException) RestResponse(com.linkedin.r2.message.rest.RestResponse) URISyntaxException(java.net.URISyntaxException) StreamRequest(com.linkedin.r2.message.stream.StreamRequest)

Aggregations

StreamResponse (com.linkedin.r2.message.stream.StreamResponse)104 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)71 Test (org.testng.annotations.Test)68 RequestContext (com.linkedin.r2.message.RequestContext)59 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)53 ByteString (com.linkedin.data.ByteString)43 URI (java.net.URI)42 Callback (com.linkedin.common.callback.Callback)36 RestRequest (com.linkedin.r2.message.rest.RestRequest)33 CountDownLatch (java.util.concurrent.CountDownLatch)31 RestResponse (com.linkedin.r2.message.rest.RestResponse)28 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)27 StreamException (com.linkedin.r2.message.stream.StreamException)25 HashMap (java.util.HashMap)24 FutureCallback (com.linkedin.common.callback.FutureCallback)22 Map (java.util.Map)20 SinglePartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback)17 StreamResponseBuilder (com.linkedin.r2.message.stream.StreamResponseBuilder)17 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)17 MultiPartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback)16