package com.centurylink.cloud.sdk.core.client.errors;

import com.centurylink.cloud.sdk.core.client.ClcClientException;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientResponseContext;
import javax.ws.rs.client.ClientResponseFilter;
import java.io.IOException;

import static java.util.Arrays.asList;
import static javax.ws.rs.core.Response.Status.*;

/**
 * @author ilya.drabenia
 */
public class ErrorProcessingFilter implements ClientResponseFilter {

    @Override
    public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
        if (BAD_REQUEST.getStatusCode() == responseContext.getStatusInfo().getStatusCode()) {
            throw new ClcClientException(objectMapper().writeValueAsString(responseContext.getEntityStream()));
        }
    }

    /**
     * Returns true if response contains status codes:
     * {@code OK}
     * {@code CREATED}
     * {@code ACCEPTED}
     * {@code NO_CONTENT}
     * {@code NOT_FOUND} - added because client returns only 404 status code and response doesn't contain body,
     * in case when requested resource not found
     *
     * @param responseContext
     * @return true if response contains acceptable status codes
     */
    private boolean isResponseSuccess(ClientResponseContext responseContext) {
        return asList(OK, CREATED, ACCEPTED, NO_CONTENT, NOT_FOUND).contains(responseContext.getStatusInfo());
    }

    private ObjectMapper objectMapper() {
        return new ObjectMapper();
    }

}

@@ -164,16 +164,54 @@
 tMapper;
+%0Aimport org.apache.commons.io.IOUtils;
 %0A%0Aimport
@@ -815,105 +815,138 @@
     
-throw new ClcClientException(objectMapper().writeValueAsString(responseContext.getEntityStream())
+String response = IOUtils.toString(responseContext.getEntityStream(), %22UTF-8%22);%0A%0A            throw new ClcClientException(response
 );%0A 

Implement processing of CLC error messages at responses with http bad request status #19