Search in sources :

Example 1 with MultiPartMIMEReader

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);
}
Also used : RestLiTestAttachmentDataSourceIterator(com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSourceIterator) MultiPartMIMEReader(com.linkedin.multipart.MultiPartMIMEReader) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) RestLiTestAttachmentDataSource(com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource) MultiPartMIMEWriter(com.linkedin.multipart.MultiPartMIMEWriter) MIMETestUtils(com.linkedin.multipart.utils.MIMETestUtils) Test(org.testng.annotations.Test)

Example 2 with MultiPartMIMEReader

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);
        }
    };
}
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 3 with MultiPartMIMEReader

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);
        }
    };
}
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 4 with MultiPartMIMEReader

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);
}
Also used : MultiPartMIMEReader(com.linkedin.multipart.MultiPartMIMEReader) MultiPartMIMEReaderCallback(com.linkedin.multipart.MultiPartMIMEReaderCallback) Test(org.testng.annotations.Test)

Example 5 with 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;
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) ContentType(javax.mail.internet.ContentType) MultiPartMIMEReader(com.linkedin.multipart.MultiPartMIMEReader) ByteString(com.linkedin.data.ByteString) ParseException(javax.mail.internet.ParseException) MimeTypeParseException(javax.activation.MimeTypeParseException) ParseException(javax.mail.internet.ParseException) RestException(com.linkedin.r2.message.rest.RestException) RestLiAttachmentReaderException(com.linkedin.restli.common.attachments.RestLiAttachmentReaderException) MimeTypeParseException(javax.activation.MimeTypeParseException) MultiPartIllegalFormatException(com.linkedin.multipart.exceptions.MultiPartIllegalFormatException)

Aggregations

MultiPartMIMEReader (com.linkedin.multipart.MultiPartMIMEReader)11 Test (org.testng.annotations.Test)6 MultiPartMIMEWriter (com.linkedin.multipart.MultiPartMIMEWriter)5 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)5 RestLiTestAttachmentDataSource (com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 MultiPartMIMEReaderCallback (com.linkedin.multipart.MultiPartMIMEReaderCallback)4 Callback (com.linkedin.common.callback.Callback)3 ByteString (com.linkedin.data.ByteString)3 MIMETestUtils (com.linkedin.multipart.utils.MIMETestUtils)3 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)3 ContentType (javax.mail.internet.ContentType)3 ParseException (javax.mail.internet.ParseException)3 FutureCallback (com.linkedin.common.callback.FutureCallback)2 MultiPartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback)2 SinglePartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback)2 RequestContext (com.linkedin.r2.message.RequestContext)2 RestRequest (com.linkedin.r2.message.rest.RestRequest)2 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)2 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)2