use of com.linkedin.multipart.MultiPartMIMEReader in project rest.li by linkedin.
the class TestAttachmentUtils method testMultipleAttachments.
@Test
public void testMultipleAttachments() {
final MultiPartMIMEWriter.Builder builder = new MultiPartMIMEWriter.Builder();
final List<RestLiTestAttachmentDataSource> testAttachmentDataSources = generateTestDataSources();
final RestLiTestAttachmentDataSourceIterator dataSourceIterator = new RestLiTestAttachmentDataSourceIterator(testAttachmentDataSources, new IllegalArgumentException());
// Let each data source know its parent, so that when the data source is done, it can notify it's parent to call onNewDataSourceWriter()
for (final RestLiTestAttachmentDataSource dataSource : testAttachmentDataSources) {
dataSource.setParentDataSourceIterator(dataSourceIterator);
}
AttachmentUtils.appendMultipleAttachmentsToBuilder(builder, dataSourceIterator);
final StreamRequest streamRequest = MultiPartMIMEStreamRequestFactory.generateMultiPartMIMEStreamRequest(URI.create("foo"), "related", builder.build());
final MultiPartMIMEReader streamRequestReader = MultiPartMIMEReader.createAndAcquireStream(streamRequest);
final CountDownLatch streamRequestReaderLatch = new CountDownLatch(1);
final MIMETestUtils.MultiPartMIMEFullReaderCallback streamRequestReaderCallback = new MIMETestUtils.MultiPartMIMEFullReaderCallback(streamRequestReaderLatch);
streamRequestReader.registerReaderCallback(streamRequestReaderCallback);
try {
streamRequestReaderLatch.await(3000, TimeUnit.MILLISECONDS);
} catch (InterruptedException interruptedException) {
Assert.fail();
}
verifyAttachments(streamRequestReaderCallback.getSinglePartMIMEReaderCallbacks(), testAttachmentDataSources);
}
use of com.linkedin.multipart.MultiPartMIMEReader 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.multipart.MultiPartMIMEReader 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.multipart.MultiPartMIMEReader in project rest.li by linkedin.
the class TestRestLiAttachmentReader method testRestLiAttachmentReader.
@Test
public void testRestLiAttachmentReader() {
// Create a mock MultiPartMIMEReader and pass to the RestLiAttachmentReader. Verify that API calls are propagated accordingly.
final MultiPartMIMEReader multiPartMIMEReader = mock(MultiPartMIMEReader.class);
final RestLiAttachmentReader attachmentReader = new RestLiAttachmentReader(multiPartMIMEReader);
attachmentReader.drainAllAttachments();
attachmentReader.haveAllAttachmentsFinished();
final RestLiAttachmentReaderCallback dummyCallback = new RestLiAttachmentReaderCallback() {
// None of these should be called.
@Override
public void onNewAttachment(RestLiAttachmentReader.SingleRestLiAttachmentReader singleRestLiAttachmentReader) {
Assert.fail();
}
@Override
public void onFinished() {
Assert.fail();
}
@Override
public void onDrainComplete() {
Assert.fail();
}
@Override
public void onStreamError(Throwable throwable) {
Assert.fail();
}
};
attachmentReader.registerAttachmentReaderCallback(dummyCallback);
// Verify the calls above made it correctly to the layer below
verify(multiPartMIMEReader, times(1)).drainAllParts();
verify(multiPartMIMEReader, times(1)).haveAllPartsFinished();
verify(multiPartMIMEReader, times(1)).registerReaderCallback(isA(MultiPartMIMEReaderCallback.class));
verifyNoMoreInteractions(multiPartMIMEReader);
}
use of com.linkedin.multipart.MultiPartMIMEReader in project rest.li by linkedin.
the class AttachmentHandlingRestLiServer method handleRequestAttachments.
/**
* Handles multipart/related request as Rest.li payload with attachments.
*
* @return Whether or not the request is a multipart/related Rest.li request with attachments.
*/
private boolean handleRequestAttachments(StreamRequest request, RequestContext requestContext, Callback<StreamResponse> callback) {
// At this point we need to check the content-type to understand how we should handle the request.
String header = request.getHeader(RestConstants.HEADER_CONTENT_TYPE);
if (header != null) {
ContentType contentType;
try {
contentType = new ContentType(header);
} catch (ParseException e) {
callback.onError(Messages.toStreamException(RestException.forError(400, "Unable to parse Content-Type: " + header)));
return true;
}
if (contentType.getBaseType().equalsIgnoreCase(RestConstants.HEADER_VALUE_MULTIPART_RELATED)) {
// We need to reconstruct a RestRequest that has the first part of the multipart/related payload as the
// traditional rest.li payload of a RestRequest.
final MultiPartMIMEReader multiPartMIMEReader = MultiPartMIMEReader.createAndAcquireStream(request);
RoutingResult routingResult;
try {
routingResult = getRoutingResult(request, requestContext);
} catch (Exception e) {
callback.onError(buildPreRoutingStreamException(e, request));
return true;
}
final TopLevelReaderCallback firstPartReader = new TopLevelReaderCallback(routingResult, callback, multiPartMIMEReader, request);
multiPartMIMEReader.registerReaderCallback(firstPartReader);
return true;
}
}
return false;
}
Aggregations