package com.amazonaws.http; import com.amazonaws.AmazonClientException; import com.amazonaws.AmazonServiceException; import com.amazonaws.transform.JsonErrorUnmarshaller; import com.amazonaws.util.StringUtils; import com.amazonaws.util.json.JsonUtils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.List; import java.util.Map; /* loaded from: classes.dex */ public class JsonErrorResponseHandler implements HttpResponseHandler { public final List unmarshallerList; @Override // com.amazonaws.http.HttpResponseHandler public boolean needsConnectionLeftOpen() { return false; } public JsonErrorResponseHandler(List list) { this.unmarshallerList = list; } @Override // com.amazonaws.http.HttpResponseHandler public AmazonServiceException handle(HttpResponse httpResponse) { try { JsonErrorResponse fromResponse = JsonErrorResponse.fromResponse(httpResponse); AmazonServiceException runErrorUnmarshallers = runErrorUnmarshallers(fromResponse); if (runErrorUnmarshallers == null) { return null; } runErrorUnmarshallers.setStatusCode(httpResponse.getStatusCode()); if (httpResponse.getStatusCode() < 500) { runErrorUnmarshallers.setErrorType(AmazonServiceException.ErrorType.Client); } else { runErrorUnmarshallers.setErrorType(AmazonServiceException.ErrorType.Service); } runErrorUnmarshallers.setErrorCode(fromResponse.getErrorCode()); for (Map.Entry entry : httpResponse.getHeaders().entrySet()) { if ("X-Amzn-RequestId".equalsIgnoreCase((String) entry.getKey())) { runErrorUnmarshallers.setRequestId((String) entry.getValue()); } } return runErrorUnmarshallers; } catch (IOException e) { throw new AmazonClientException("Unable to parse error response", e); } } public final AmazonServiceException runErrorUnmarshallers(JsonErrorResponse jsonErrorResponse) { for (JsonErrorUnmarshaller jsonErrorUnmarshaller : this.unmarshallerList) { if (jsonErrorUnmarshaller.match(jsonErrorResponse)) { return jsonErrorUnmarshaller.unmarshall(jsonErrorResponse); } } return null; } public static final class JsonErrorResponse { public final String errorCode; public final Map map; public final String message = get("message"); public final int statusCode; public String getErrorCode() { return this.errorCode; } public String getMessage() { return this.message; } public JsonErrorResponse(int i, String str, Map map) { this.statusCode = i; this.errorCode = str; this.map = map; } public String get(String str) { if (str == null || str.length() == 0) { return null; } String str2 = StringUtils.lowerCase(str.substring(0, 1)) + str.substring(1); String str3 = StringUtils.upperCase(str.substring(0, 1)) + str.substring(1); if (this.map.containsKey(str3)) { return (String) this.map.get(str3); } return this.map.containsKey(str2) ? (String) this.map.get(str2) : ""; } public static JsonErrorResponse fromResponse(HttpResponse httpResponse) { int statusCode = httpResponse.getStatusCode(); Map jsonToStringMapWithList = JsonUtils.jsonToStringMapWithList(new BufferedReader(new InputStreamReader(httpResponse.getContent(), StringUtils.UTF8))); String str = (String) httpResponse.getHeaders().get("x-amzn-ErrorType"); if (str != null) { int indexOf = str.indexOf(58); if (indexOf != -1) { str = str.substring(0, indexOf); } } else if (jsonToStringMapWithList.containsKey("__type")) { String str2 = (String) jsonToStringMapWithList.get("__type"); str = str2.substring(str2.lastIndexOf("#") + 1); } return new JsonErrorResponse(statusCode, str, jsonToStringMapWithList); } } }