Search in sources :

Example 6 with RestLiDecodingException

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);
}
Also used : FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) ContentType(javax.mail.internet.ContentType) ByteString(com.linkedin.data.ByteString) MultiPartMIMEReader(com.linkedin.multipart.MultiPartMIMEReader) RestLiDecodingException(com.linkedin.restli.client.RestLiDecodingException) ByteString(com.linkedin.data.ByteString) ParseException(javax.mail.internet.ParseException) MimeTypeParseException(javax.activation.MimeTypeParseException) ParseException(javax.mail.internet.ParseException) RestLiDecodingException(com.linkedin.restli.client.RestLiDecodingException) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MimeTypeParseException(javax.activation.MimeTypeParseException)

Aggregations

RestLiDecodingException (com.linkedin.restli.client.RestLiDecodingException)6 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)3 RestResponse (com.linkedin.r2.message.rest.RestResponse)3 IOException (java.io.IOException)3 MimeTypeParseException (javax.activation.MimeTypeParseException)3 ByteString (com.linkedin.data.ByteString)2 RestException (com.linkedin.r2.message.rest.RestException)2 RestLiResponseException (com.linkedin.restli.client.RestLiResponseException)2 ErrorResponse (com.linkedin.restli.common.ErrorResponse)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Callback (com.linkedin.common.callback.Callback)1 FutureCallback (com.linkedin.common.callback.FutureCallback)1 DataMap (com.linkedin.data.DataMap)1 MultiPartMIMEReader (com.linkedin.multipart.MultiPartMIMEReader)1 FullEntityReader (com.linkedin.r2.message.stream.entitystream.FullEntityReader)1 IndividualResponse (com.linkedin.restli.common.multiplexer.IndividualResponse)1 IndividualResponseMap (com.linkedin.restli.common.multiplexer.IndividualResponseMap)1 ContentType (javax.mail.internet.ContentType)1 ParseException (javax.mail.internet.ParseException)1 Test (org.testng.annotations.Test)1