use of com.linkedin.restli.client.RestLiDecodingException in project rest.li by linkedin.
the class RestResponseDecoder method decodeResponse.
public void decodeResponse(final StreamResponse streamResponse, final Callback<Response<T>> responseCallback) throws RestLiDecodingException {
//Determine content type and take appropriate action.
//If 'multipart/related', then use MultiPartMIMEReader to read first part (which can be json or pson).
final String contentTypeString = streamResponse.getHeader(RestConstants.HEADER_CONTENT_TYPE);
if (contentTypeString != null) {
ContentType contentType = null;
try {
contentType = new ContentType(contentTypeString);
} catch (ParseException parseException) {
responseCallback.onError(new RestLiDecodingException("Could not decode Content-Type header in response", parseException));
return;
}
if (contentType.getBaseType().equalsIgnoreCase(RestConstants.HEADER_VALUE_MULTIPART_RELATED)) {
final MultiPartMIMEReader multiPartMIMEReader = MultiPartMIMEReader.createAndAcquireStream(streamResponse);
final TopLevelReaderCallback topLevelReaderCallback = new TopLevelReaderCallback(responseCallback, streamResponse, multiPartMIMEReader);
multiPartMIMEReader.registerReaderCallback(topLevelReaderCallback);
return;
}
}
//Otherwise if the whole body is json/pson then read everything in.
//This will not have an extra copy due to assembly since FullEntityReader uses a compound ByteString.
final FullEntityReader fullEntityReader = new FullEntityReader(new Callback<ByteString>() {
@Override
public void onError(Throwable e) {
responseCallback.onError(e);
}
@Override
public void onSuccess(ByteString result) {
try {
responseCallback.onSuccess(createResponse(streamResponse.getHeaders(), streamResponse.getStatus(), result, streamResponse.getCookies()));
} catch (Exception exception) {
onError(exception);
}
}
});
streamResponse.getEntityStream().setReader(fullEntityReader);
}
Aggregations