Search in sources :

Example 1 with MultiPartMIMEWriter

use of com.linkedin.multipart.MultiPartMIMEWriter in project rest.li by linkedin.

the class RestClient method buildStreamRequest.

private StreamRequest buildStreamRequest(URI uri, ResourceMethod method, DataMap dataMap, Map<String, String> headers, List<String> cookies, ProtocolVersion protocolVersion, ContentType contentType, List<AcceptType> acceptTypes, boolean acceptResponseAttachments, List<Object> streamingAttachments) throws Exception {
    StreamRequestBuilder requestBuilder = new StreamRequestBuilder(uri).setMethod(method.getHttpMethod().toString());
    requestBuilder.setHeaders(headers);
    requestBuilder.setCookies(cookies);
    addAcceptHeaders(requestBuilder, acceptTypes, acceptResponseAttachments);
    addProtocolVersionHeader(requestBuilder, protocolVersion);
    if (method.getHttpMethod() == HttpMethod.POST) {
        requestBuilder.setHeader(RestConstants.HEADER_RESTLI_REQUEST_METHOD, method.toString());
    }
    //This request builders enforce this invariant.
    if (streamingAttachments != null) {
        final ByteStringWriter firstPartWriter;
        final ContentType type = resolveContentType(requestBuilder, dataMap, contentType);
        //with empty action parameters will have an empty JSON ({}) as the body.
        assert (type != null);
        switch(type) {
            case PSON:
                firstPartWriter = new ByteStringWriter(ByteString.copy(PSON_DATA_CODEC.mapToBytes(dataMap)));
                break;
            case JSON:
                firstPartWriter = new ByteStringWriter(ByteString.copy(JACKSON_DATA_CODEC.mapToBytes(dataMap)));
                break;
            default:
                throw new IllegalStateException("Unknown ContentType:" + type);
        }
        //Our protocol does not use an epilogue or a preamble.
        final MultiPartMIMEWriter.Builder attachmentsBuilder = new MultiPartMIMEWriter.Builder();
        for (final Object dataSource : streamingAttachments) {
            assert (dataSource instanceof RestLiAttachmentDataSourceWriter || dataSource instanceof RestLiDataSourceIterator);
            if (dataSource instanceof RestLiAttachmentDataSourceWriter) {
                AttachmentUtils.appendSingleAttachmentToBuilder(attachmentsBuilder, (RestLiAttachmentDataSourceWriter) dataSource);
            } else {
                AttachmentUtils.appendMultipleAttachmentsToBuilder(attachmentsBuilder, (RestLiDataSourceIterator) dataSource);
            }
        }
        final MultiPartMIMEWriter multiPartMIMEWriter = AttachmentUtils.createMultiPartMIMEWriter(firstPartWriter, type.getHeaderKey(), attachmentsBuilder);
        final String contentTypeHeader = MultiPartMIMEUtils.buildMIMEContentTypeHeader(AttachmentUtils.RESTLI_MULTIPART_SUBTYPE, multiPartMIMEWriter.getBoundary(), Collections.<String, String>emptyMap());
        requestBuilder.setHeader(MultiPartMIMEUtils.CONTENT_TYPE_HEADER, contentTypeHeader);
        return requestBuilder.build(multiPartMIMEWriter.getEntityStream());
    } else {
        //taken the RestRequest code path.
        assert (acceptResponseAttachments == true);
        return Messages.toStreamRequest(buildRestRequest(uri, method, dataMap, headers, cookies, protocolVersion, contentType, acceptTypes, acceptResponseAttachments));
    }
}
Also used : RestLiAttachmentDataSourceWriter(com.linkedin.restli.common.attachments.RestLiAttachmentDataSourceWriter) RestLiDataSourceIterator(com.linkedin.restli.common.attachments.RestLiDataSourceIterator) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) MessageHeadersBuilder(com.linkedin.r2.message.MessageHeadersBuilder) MultiplexerUriBuilder(com.linkedin.restli.client.uribuilders.MultiplexerUriBuilder) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) ByteString(com.linkedin.data.ByteString) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) MultiPartMIMEWriter(com.linkedin.multipart.MultiPartMIMEWriter)

Example 2 with MultiPartMIMEWriter

use of com.linkedin.multipart.MultiPartMIMEWriter in project rest.li by linkedin.

the class RestClient method buildStreamRequest.

private StreamRequest buildStreamRequest(URI uri, ResourceMethod method, DataMap dataMap, Map<String, String> headers, List<String> cookies, ProtocolVersion protocolVersion, ContentType contentType, List<ContentType> acceptTypes, boolean acceptResponseAttachments, List<Object> streamingAttachments) throws Exception {
    StreamRequestBuilder requestBuilder = new StreamRequestBuilder(uri).setMethod(method.getHttpMethod().toString());
    requestBuilder.setHeaders(headers);
    requestBuilder.setCookies(cookies);
    addAcceptHeaders(requestBuilder, acceptTypes, acceptResponseAttachments);
    addProtocolVersionHeader(requestBuilder, protocolVersion);
    if (method.getHttpMethod() == HttpMethod.POST) {
        requestBuilder.setHeader(RestConstants.HEADER_RESTLI_REQUEST_METHOD, method.toString());
    }
    final ContentType type = resolveContentType(requestBuilder, dataMap, contentType, uri);
    // This request builders enforce this invariant.
    if (streamingAttachments != null) {
        final ByteStringWriter firstPartWriter;
        // with empty action parameters will have an empty JSON ({}) as the body.
        assert (type != null);
        firstPartWriter = new ByteStringWriter(type.getCodec().mapToByteString(dataMap));
        // Our protocol does not use an epilogue or a preamble.
        final MultiPartMIMEWriter.Builder attachmentsBuilder = new MultiPartMIMEWriter.Builder();
        for (final Object dataSource : streamingAttachments) {
            assert (dataSource instanceof RestLiAttachmentDataSourceWriter || dataSource instanceof RestLiDataSourceIterator);
            if (dataSource instanceof RestLiAttachmentDataSourceWriter) {
                AttachmentUtils.appendSingleAttachmentToBuilder(attachmentsBuilder, (RestLiAttachmentDataSourceWriter) dataSource);
            } else {
                AttachmentUtils.appendMultipleAttachmentsToBuilder(attachmentsBuilder, (RestLiDataSourceIterator) dataSource);
            }
        }
        final MultiPartMIMEWriter multiPartMIMEWriter = AttachmentUtils.createMultiPartMIMEWriter(firstPartWriter, type.getHeaderKey(), attachmentsBuilder);
        final String contentTypeHeader = MultiPartMIMEUtils.buildMIMEContentTypeHeader(AttachmentUtils.RESTLI_MULTIPART_SUBTYPE, multiPartMIMEWriter.getBoundary(), Collections.emptyMap());
        requestBuilder.setHeader(MultiPartMIMEUtils.CONTENT_TYPE_HEADER, contentTypeHeader);
        return requestBuilder.build(multiPartMIMEWriter.getEntityStream());
    } else {
        if (dataMap != null && type != null && type.supportsStreaming()) {
            requestBuilder.setHeader(RestConstants.HEADER_CONTENT_TYPE, type.getHeaderKey());
            return requestBuilder.build(EntityStreamAdapters.fromGenericEntityStream(type.getStreamCodec().encodeMap(dataMap)));
        } else {
            return Messages.toStreamRequest(buildRestRequest(uri, method, dataMap, headers, cookies, protocolVersion, contentType, acceptTypes, acceptResponseAttachments));
        }
    }
}
Also used : RestLiAttachmentDataSourceWriter(com.linkedin.restli.common.attachments.RestLiAttachmentDataSourceWriter) RestLiDataSourceIterator(com.linkedin.restli.common.attachments.RestLiDataSourceIterator) ContentType(com.linkedin.restli.common.ContentType) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) MessageHeadersBuilder(com.linkedin.r2.message.MessageHeadersBuilder) MultiplexerUriBuilder(com.linkedin.restli.client.uribuilders.MultiplexerUriBuilder) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) ByteString(com.linkedin.data.ByteString) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) MultiPartMIMEWriter(com.linkedin.multipart.MultiPartMIMEWriter)

Example 3 with MultiPartMIMEWriter

use of com.linkedin.multipart.MultiPartMIMEWriter in project rest.li by linkedin.

the class TestMIMEIntegrationReaderWriter method testEachSingleBodyDataSourceMultipleTimes.

@Test(dataProvider = "eachSingleBodyDataSource", enabled = false)
public void testEachSingleBodyDataSourceMultipleTimes(final int chunkSize, final MIMEDataPart bodyPart) throws Exception {
    final List<MultiPartMIMEDataSourceWriter> dataSources = new ArrayList<>();
    for (int i = 0; i < 4; i++) {
        final MultiPartMIMEInputStream inputStreamDataSource = new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(bodyPart.getPartData().copyBytes()), scheduledExecutorService, bodyPart.getPartHeaders()).withWriteChunkSize(chunkSize).build();
        dataSources.add(inputStreamDataSource);
    }
    final MultiPartMIMEWriter writer = new MultiPartMIMEWriter.Builder("some preamble", "").appendDataSources(dataSources).build();
    executeRequestAndAssert(writer, Collections.unmodifiableList(Arrays.asList(bodyPart, bodyPart, bodyPart, bodyPart)));
}
Also used : MultiPartMIMEDataSourceWriter(com.linkedin.multipart.MultiPartMIMEDataSourceWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) TransportDispatcherBuilder(com.linkedin.r2.transport.common.bridge.server.TransportDispatcherBuilder) ArrayList(java.util.ArrayList) MultiPartMIMEWriter(com.linkedin.multipart.MultiPartMIMEWriter) MultiPartMIMEInputStream(com.linkedin.multipart.MultiPartMIMEInputStream) Test(org.testng.annotations.Test)

Example 4 with MultiPartMIMEWriter

use of com.linkedin.multipart.MultiPartMIMEWriter in project rest.li by linkedin.

the class TestMIMEIntegrationReaderWriter method testEmptyEnvelope.

@Test
public void testEmptyEnvelope() throws Exception {
    // Javax mail does not support this, hence we can only test this using our writer
    final MultiPartMIMEWriter writer = new MultiPartMIMEWriter.Builder("some preamble", "").build();
    executeRequestAndAssert(writer, Collections.<MIMEDataPart>emptyList());
}
Also used : MultiPartMIMEWriter(com.linkedin.multipart.MultiPartMIMEWriter) Test(org.testng.annotations.Test)

Example 5 with MultiPartMIMEWriter

use of com.linkedin.multipart.MultiPartMIMEWriter in project rest.li by linkedin.

the class AttachmentHandlingRestLiServer method createStreamResponseWithAttachment.

private static StreamResponse createStreamResponseWithAttachment(RestResponse structuredFirstPart, RestLiResponseAttachments attachments) {
    // Construct the StreamResponse and invoke the callback. The RestResponse entity should be the first part.
    // There may potentially be attachments included in the response. Note that unlike the client side request builders,
    // here it is possible to have a non-null attachment list with 0 attachments due to the way the builder in
    // RestLiResponseAttachments works. Therefore we have to make sure its a non zero size as well.
    final ByteStringWriter firstPartWriter = new ByteStringWriter(structuredFirstPart.getEntity());
    final MultiPartMIMEWriter multiPartMIMEWriter = AttachmentUtils.createMultiPartMIMEWriter(firstPartWriter, structuredFirstPart.getHeader(RestConstants.HEADER_CONTENT_TYPE), attachments.getMultiPartMimeWriterBuilder());
    // of course being the Content-Type header which will be overridden by MultiPartMIMEStreamResponseFactory.
    return MultiPartMIMEStreamResponseFactory.generateMultiPartMIMEStreamResponse(AttachmentUtils.RESTLI_MULTIPART_SUBTYPE, multiPartMIMEWriter, Collections.emptyMap(), structuredFirstPart.getHeaders(), structuredFirstPart.getStatus(), structuredFirstPart.getCookies());
}
Also used : ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) MultiPartMIMEWriter(com.linkedin.multipart.MultiPartMIMEWriter)

Aggregations

MultiPartMIMEWriter (com.linkedin.multipart.MultiPartMIMEWriter)12 Test (org.testng.annotations.Test)9 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)7 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)6 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)6 ByteString (com.linkedin.data.ByteString)5 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)5 RequestContext (com.linkedin.r2.message.RequestContext)4 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)4 TransportDispatcherBuilder (com.linkedin.r2.transport.common.bridge.server.TransportDispatcherBuilder)4 RestLiAttachmentReader (com.linkedin.restli.common.attachments.RestLiAttachmentReader)4 URI (java.net.URI)4 Callback (com.linkedin.common.callback.Callback)3 MultiPartMIMEInputStream (com.linkedin.multipart.MultiPartMIMEInputStream)3 MultiPartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback)3 SinglePartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback)3 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)3 ErrorResponseBuilder (com.linkedin.restli.internal.server.response.ErrorResponseBuilder)3 RestLiTestAttachmentDataSource (com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource)3 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)3