All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cloudfoundry.client.facade.rest.CloudControllerResponseErrorHandler Maven / Gradle / Ivy

There is a newer version: 2.56.0
Show newest version
package com.sap.cloudfoundry.client.facade.rest;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sap.cloudfoundry.client.facade.CloudOperationException;
import com.sap.cloudfoundry.client.facade.util.CloudUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestClientException;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class CloudControllerResponseErrorHandler extends DefaultResponseErrorHandler {

    private static CloudOperationException getException(ClientHttpResponse response) throws IOException {
        HttpStatus statusCode = HttpStatus.valueOf(response.getStatusCode()
                                                           .value());
        String statusText = response.getStatusText();

        ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally

        if (response.getBody() != null) {
            try {
                @SuppressWarnings("unchecked") Map responseBody = mapper.readValue(response.getBody(), Map.class);
                String description = getTrimmedDescription(responseBody);
                return new CloudOperationException(statusCode, statusText, description);
            } catch (IOException e) {
                // Fall through. Handled below.
            }
        }
        return new CloudOperationException(statusCode, statusText);
    }

    private static String getTrimmedDescription(Map responseBody) {
        String description = getDescription(responseBody);
        return description == null ? null : description.trim();
    }

    private static String getDescription(Map responseBody) {
        String description = getV2Description(responseBody);
        return description == null ? getV3Description(responseBody) : description;
    }

    private static String getV2Description(Map responseBody) {
        return CloudUtil.parse(String.class, responseBody.get("description"));
    }

    @SuppressWarnings("unchecked")
    private static String getV3Description(Map responseBody) {
        List> errors = (List>) responseBody.get("errors");
        return errors == null ? null : concatenateErrorMessages(errors);
    }

    private static String concatenateErrorMessages(List> errors) {
        return errors.stream()
                     .map(error -> (String) error.get("detail"))
                     .collect(Collectors.joining("\n"));
    }

    @Override
    public void handleError(ClientHttpResponse response) throws IOException {
        HttpStatus statusCode = HttpStatus.valueOf(response.getStatusCode()
                                                           .value());
        switch (statusCode.series()) {
            case CLIENT_ERROR:
            case SERVER_ERROR:
                throw getException(response);
            default:
                throw new RestClientException("Unknown status code [" + statusCode + "]");
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy