private <T> T deserializeResult(Response response, @Nullable String envelopeFieldName, TypeRef returnType) throws WebApplicationException {
        String contentType = readContentType(response);
        String body = readBody(response);

        if (contentType.startsWith("application/json")) {
            try {
                if (envelopeFieldName!=null) {
                    return jsonMarshaller.deserializeEnveloped(body, envelopeFieldName, returnType);
                } else {
                    return jsonMarshaller.deserialize(body, returnType);
                }
            } catch (JsonMarshallingException e) {
                String msg = "Failed unmarshalling JSON data from server to object: "+e.getMessage();
//                throw new ClientErrorException(Response.Status.BAD_REQUEST, e);

                Retry retry;
                if (response.getStatusInfo().getFamily() == Response.Status.Family.SERVER_ERROR) {
                    //it is likely that the server can handle it next time, and there won't be an error at all.
                    //then there won't be a problem unmarshalling the error info either.
                    retry = Retry.now();
                } else {
                    //success:      no, because it's unlikely the server sends a different response next time.
                    //client error: no, because the client's data is wrong anyway, we just can't understand the detail
                    //              why it's wrong, but it's still wrong next time.
                    //              this could happen when the client uses an outdated protocol. server rejects it, and
                    //              the client can't even understand (the detail of) the response.
                    retry = Retry.no();
                }
                throw new ClientServiceException(
                        new RestFaultInfo(
                                "Protocol", Blame.CLIENT,
                                msg, ""+ErrorCodes.Client.UNMARSHALLING_FAILED.getCode(),
                                null,
                                retry, retry,
                                response.getStatusInfo().getStatusCode(),
                                response.getStatusInfo().getReasonPhrase()
                        ), e
                );
            }
        } else if (returnType.getType().equals(String.class)) {
            // Expecting string, return the raw response body.
            return (T) body;
        } else {
            String msg = "Client requested content type >>>"+ACCEPT+"<<< but server sent >>>\"" + contentType + "\"<<< and that is not supported for type: " + returnType.getType();
//            throw new NotSupportedException(msg);
            throw new BadResponseServiceException(
                    new RestFaultInfo("BadResponse", Blame.SERVER,
                            msg, ""+ErrorCodes.Server.BAD_RESPONSE.getCode(),
                            null,
                            Retry.no(), Retry.no(),
                            response.getStatusInfo().getStatusCode(),
                            response.getStatusInfo().getReasonPhrase()
                    )
            );
        }
    }
private < T > T deserializeResult ( Response response , @ Nullable String envelopeFieldName , TypeRef returnType ) throws WebApplicationException { String contentType = readContentType ( response ) ; String body = readBody ( response ) ; if ( contentType . startsWith ( "application/json" ) ) { try { if ( envelopeFieldName != null ) { return jsonMarshaller . deserializeEnveloped ( body , envelopeFieldName , returnType ) ; } else { return jsonMarshaller . deserialize ( body , returnType ) ; } } catch ( JsonMarshallingException e ) { String msg = "Failed unmarshalling JSON data from server to object: " + e . getMessage ( ) ; Retry retry ; if ( response . getStatusInfo ( ) . getFamily ( ) == Response . Status . Family . SERVER_ERROR ) { retry = Retry . now ( ) ; } else { retry = Retry . no ( ) ; } throw new ClientServiceException ( new RestFaultInfo ( "Protocol" , Blame . CLIENT , msg , "" + ErrorCodes . Client . UNMARSHALLING_FAILED . getCode ( ) , null , retry , retry , response . getStatusInfo ( ) . getStatusCode ( ) , response . getStatusInfo ( ) . getReasonPhrase ( ) ) , e ) ; } } else if ( returnType . getType ( ) . equals ( String . class ) ) { return ( T ) body ; } else { String msg = "Client requested content type >>>" + ACCEPT + "<<< but server sent >>>\"" + contentType + "\"<<< and that is not supported for type: " + returnType . getType ( ) ; throw new BadResponseServiceException ( new RestFaultInfo ( "BadResponse" , Blame . SERVER , msg , "" + ErrorCodes . Server . BAD_RESPONSE . getCode ( ) , null , Retry . no ( ) , Retry . no ( ) , response . getStatusInfo ( ) . getStatusCode ( ) , response . getStatusInfo ( ) . getReasonPhrase ( ) ) ) ; } }
Deserialize response body as received from the server to Java object according to the Content-Type
(only JSON is supported for now).