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;
}
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();
}
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);
}
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);
}
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());
}
Aggregations