Search in sources :

Example 1 with ContentType

use of com.linkedin.restli.common.ContentType in project rest.li by linkedin.

the class RestClient method buildRestRequest.

// This throws Exception to remind the caller to deal with arbitrary exceptions including RuntimeException
// in a way appropriate for the public method that was originally invoked.
private RestRequest buildRestRequest(URI uri, ResourceMethod method, DataMap dataMap, Map<String, String> headers, List<String> cookies, ProtocolVersion protocolVersion, ContentType contentType, List<ContentType> acceptTypes, boolean acceptResponseAttachments) throws Exception {
    RestRequestBuilder requestBuilder = new RestRequestBuilder(uri).setMethod(method.getHttpMethod().toString());
    requestBuilder.setHeaders(headers);
    requestBuilder.setCookies(cookies);
    addAcceptHeaders(requestBuilder, acceptTypes, acceptResponseAttachments);
    final ContentType type = resolveContentType(requestBuilder, dataMap, contentType, uri);
    if (type != null) {
        requestBuilder.setHeader(RestConstants.HEADER_CONTENT_TYPE, type.getHeaderKey());
        requestBuilder.setEntity(type.getCodec().mapToByteString(dataMap));
    }
    addProtocolVersionHeader(requestBuilder, protocolVersion);
    if (method.getHttpMethod() == HttpMethod.POST) {
        requestBuilder.setHeader(RestConstants.HEADER_RESTLI_REQUEST_METHOD, method.toString());
    }
    return requestBuilder.build();
}
Also used : ContentType(com.linkedin.restli.common.ContentType) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder)

Example 2 with ContentType

use of com.linkedin.restli.common.ContentType 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 ContentType

use of com.linkedin.restli.common.ContentType in project rest.li by linkedin.

the class TestContentType method testJSONContentType.

@Test
public void testJSONContentType() throws MimeTypeParseException {
    ContentType contentType = ContentType.getContentType("application/json").get();
    Assert.assertEquals(contentType, ContentType.JSON);
    ContentType contentTypeWithParameter = ContentType.getContentType("application/json; charset=utf-8").get();
    Assert.assertEquals(contentTypeWithParameter, ContentType.JSON);
}
Also used : ContentType(com.linkedin.restli.common.ContentType) Test(org.testng.annotations.Test)

Example 4 with ContentType

use of com.linkedin.restli.common.ContentType in project rest.li by linkedin.

the class TestContentType method testGetResponseProtobuf2ContentType.

@Test
public void testGetResponseProtobuf2ContentType() throws MimeTypeParseException {
    ContentType contentType = ContentType.getResponseContentType(RestConstants.HEADER_VALUE_APPLICATION_PROTOBUF2, TEST_URI, Collections.emptyMap()).get();
    Assert.assertEquals("application/x-protobuf2; symbol-table=\"abc.linkedin.com:123|HahaResponse\"", contentType.getHeaderKey());
}
Also used : ContentType(com.linkedin.restli.common.ContentType) Test(org.testng.annotations.Test)

Example 5 with ContentType

use of com.linkedin.restli.common.ContentType in project rest.li by linkedin.

the class TestContentType method testGetResponseJSONContentType.

@Test
public void testGetResponseJSONContentType() throws MimeTypeParseException {
    ContentType contentType = ContentType.getResponseContentType(RestConstants.HEADER_VALUE_APPLICATION_JSON, TEST_URI, Collections.emptyMap()).get();
    Assert.assertEquals(ContentType.JSON, contentType);
}
Also used : ContentType(com.linkedin.restli.common.ContentType) Test(org.testng.annotations.Test)

Aggregations

ContentType (com.linkedin.restli.common.ContentType)21 Test (org.testng.annotations.Test)14 ByteString (com.linkedin.data.ByteString)4 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)4 MimeTypeParseException (javax.activation.MimeTypeParseException)3 RestRequest (com.linkedin.r2.message.rest.RestRequest)2 RestResponse (com.linkedin.r2.message.rest.RestResponse)2 MultiplexerUriBuilder (com.linkedin.restli.client.uribuilders.MultiplexerUriBuilder)2 IOException (java.io.IOException)2 DataMap (com.linkedin.data.DataMap)1 StreamDataCodec (com.linkedin.data.codec.entitystream.StreamDataCodec)1 DefaultSymbolTableProvider (com.linkedin.data.codec.symbol.DefaultSymbolTableProvider)1 SymbolTable (com.linkedin.data.codec.symbol.SymbolTable)1 SymbolTableProvider (com.linkedin.data.codec.symbol.SymbolTableProvider)1 MultiPartMIMEWriter (com.linkedin.multipart.MultiPartMIMEWriter)1 MessageHeadersBuilder (com.linkedin.r2.message.MessageHeadersBuilder)1 RequestContext (com.linkedin.r2.message.RequestContext)1 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)1 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)1 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)1