
com.sap.cloudfoundry.client.facade.rest.CloudControllerResponseErrorHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cloudfoundry-client-facade Show documentation
Show all versions of cloudfoundry-client-facade Show documentation
A facade of the official Cloud Foundry Java client
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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy