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());
}
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());
}
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);
}
};
}
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);
}
};
}
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);
}
Aggregations