diff --git a/src/main/java/com/global/api/gateways/Gateway.java b/src/main/java/com/global/api/gateways/Gateway.java index 4cb640c0..fbb3ea6e 100644 --- a/src/main/java/com/global/api/gateways/Gateway.java +++ b/src/main/java/com/global/api/gateways/Gateway.java @@ -1,10 +1,13 @@ package com.global.api.gateways; +import com.global.api.entities.exceptions.ApiException; import com.global.api.entities.exceptions.GatewayException; import com.global.api.entities.gpApi.GpApiRequest; import com.global.api.logging.IRequestLogger; import com.global.api.logging.RequestConsoleLogger; import com.global.api.logging.RequestFileLogger; +import com.global.api.utils.Element; +import com.global.api.utils.ElementTree; import com.global.api.utils.IOUtils; import com.global.api.utils.StringUtils; import lombok.Getter; @@ -127,7 +130,13 @@ else if (this.enableLogging || this.requestLogger != null) { logEntry.append("Request Params: ").append(queryString).append(lSChar); } - InputStream responseStream = conn.getInputStream(); + InputStream responseStream; + if (conn.getResponseCode() == 200) { + responseStream = conn.getInputStream(); + } else { + responseStream = conn.getErrorStream(); + } + String rawResponse = getRawResponse(responseStream); responseStream.close(); @@ -142,6 +151,10 @@ else if (this.enableLogging || this.requestLogger != null) { generateResponseLog(); } + if (conn.getResponseCode() != 200) { + throw new GatewayException(rawResponse, Integer.toString(conn.getResponseCode()), rawResponse); + } + GatewayResponse response = new GatewayResponse(); response.setStatusCode(conn.getResponseCode()); response.setRawResponse(rawResponse); @@ -225,7 +238,7 @@ protected GatewayResponse sendRequest(String endpoint, MultipartEntity content) conn.addRequestProperty("Content-Type", content.getContentType().getValue()); conn.addRequestProperty("Content-Length", String.valueOf(content.getContentLength())); - try(InputStream responseStream = conn.getInputStream(); + try(InputStream responseStream = conn.getResponseCode() == 200 ? conn.getInputStream() : conn.getErrorStream(); OutputStream out = conn.getOutputStream();) { if (this.enableLogging || this.requestLogger != null) { @@ -243,6 +256,11 @@ protected GatewayResponse sendRequest(String endpoint, MultipartEntity content) generateResponseLog(); } + if (conn.getResponseCode() != 200) { + String error = parseErrorMessage(rawResponse); + throw new GatewayException(error, Integer.toString(conn.getResponseCode()), error); + } + GatewayResponse response = new GatewayResponse(); response.setStatusCode(conn.getResponseCode()); response.setRawResponse(rawResponse); @@ -271,6 +289,14 @@ private String buildQueryString(HashMap queryStringParams) throw } return sb.toString(); } + + private String parseErrorMessage(String rawResponse) throws ApiException { + ElementTree tree = ElementTree.parse(rawResponse); + Element root = tree.get("soap:Fault"); + String error = root.getString("soap:Reason"); + return error; + } + private void setRequestMethod(final HttpURLConnection c, final String value) { try { @@ -389,4 +415,4 @@ private void generateResponseLog() { logEntry.delete(0, logEntry.length()); } -} \ No newline at end of file +}