Search in sources :

Example 11 with ContentType

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

the class RestLiSymbolTableProvider method fetchRemoteSymbolTable.

SymbolTable fetchRemoteSymbolTable(URI symbolTableUri, Map<String, String> requestHeaders, boolean returnEmptyOn404) {
    try {
        Map<String, String> headers = new HashMap<>(requestHeaders);
        headers.put(RestConstants.HEADER_FETCH_SYMBOL_TABLE, Boolean.TRUE.toString());
        Future<RestResponse> future = _client.restRequest(new RestRequestBuilder(symbolTableUri).setHeaders(headers).build());
        RestResponse restResponse = future.get(_timeout, TimeUnit.MILLISECONDS);
        int status = restResponse.getStatus();
        if (returnEmptyOn404 && status == HttpStatus.S_404_NOT_FOUND.getCode()) {
            return EmptySymbolTable.SHARED;
        }
        if (status == HttpStatus.S_200_OK.getCode()) {
            ByteString byteString = restResponse.getEntity();
            if (byteString == null) {
                throw new IOException("Empty body");
            }
            ContentType contentType = ContentType.getContentType(restResponse.getHeader(RestConstants.HEADER_CONTENT_TYPE)).orElseThrow(() -> new IOException("Could not parse response content type"));
            // Deserialize, and rename to replace url prefix with current url prefix.
            return SymbolTableSerializer.fromByteString(byteString, contentType.getCodec(), _symbolTableNameHandler::replaceServerNodeUri);
        }
        throw new IOException("Unexpected response status: " + status);
    } catch (ExecutionException ex) {
        LOGGER.error("Failed to fetch symbol table from " + symbolTableUri, ex.getCause());
    } catch (Exception ex) {
        LOGGER.error("Failed to fetch symbol table from " + symbolTableUri, ex);
    }
    return null;
}
Also used : ContentType(com.linkedin.restli.common.ContentType) HashMap(java.util.HashMap) RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) ByteString(com.linkedin.data.ByteString) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ExecutionException(java.util.concurrent.ExecutionException)

Example 12 with ContentType

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

the class RestClient method buildMultiplexedRequest.

private RestRequest buildMultiplexedRequest(MultiplexedRequest multiplexedRequest) throws IOException {
    URI requestUri = new MultiplexerUriBuilder(_uriPrefix).build();
    RestRequestBuilder requestBuilder = new RestRequestBuilder(requestUri).setMethod(HttpMethod.POST.toString());
    addAcceptHeaders(requestBuilder, multiplexedRequest.getRequestOptions().getAcceptTypes(), false);
    final DataMap multiplexedPayload = multiplexedRequest.getContent().data();
    final ContentType type = resolveContentType(requestBuilder, multiplexedPayload, multiplexedRequest.getRequestOptions().getContentType(), requestUri);
    assert (type != null);
    requestBuilder.setHeader(RestConstants.HEADER_CONTENT_TYPE, type.getHeaderKey());
    requestBuilder.setEntity(type.getCodec().mapToByteString(multiplexedPayload));
    requestBuilder.setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion().toString());
    return requestBuilder.build();
}
Also used : ContentType(com.linkedin.restli.common.ContentType) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) MultiplexerUriBuilder(com.linkedin.restli.client.uribuilders.MultiplexerUriBuilder) URI(java.net.URI) DataMap(com.linkedin.data.DataMap)

Example 13 with ContentType

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

the class TestContentType method testProtobuf2ContentType.

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

Example 14 with ContentType

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

the class TestContentType method testGetRequestJSONContentType.

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

Example 15 with ContentType

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

the class TestContentType method testGetRequestProtobuf2ContentType.

@Test
public void testGetRequestProtobuf2ContentType() throws MimeTypeParseException {
    ContentType contentType = ContentType.getRequestContentType(RestConstants.HEADER_VALUE_APPLICATION_PROTOBUF2, TEST_URI).get();
    Assert.assertEquals("application/x-protobuf2; symbol-table=HahaRequest", contentType.getHeaderKey());
}
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