Skip to content

Commit

Permalink
Merge pull request wso2#12429 from SavinduDimal/42crunch-initial-req
Browse files Browse the repository at this point in the history
Add retry when fetching API definition security audit
  • Loading branch information
SavinduDimal authored Apr 22, 2024
2 parents 36bad50 + a6b2924 commit a8a3d64
Showing 1 changed file with 41 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -380,37 +380,52 @@ public static JSONObject getAuditReport(API api, JSONObject securityAuditPropert
httpGet.setHeader(APIConstants.HEADER_API_TOKEN, apiToken);
httpGet.setHeader(APIConstants.HEADER_USER_AGENT, APIConstants.USER_AGENT_APIM);
// Code block for the processing of the response
try (CloseableHttpResponse response = getHttpClient.execute(httpGet)) {
if (isDebugEnabled) {
log.debug(HTTP_STATUS_LOG + response.getStatusLine().getStatusCode());
}
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
StringBuilder responseString = new StringBuilder();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8))) {
String inputLine;
while ((inputLine = reader.readLine()) != null) {
responseString.append(inputLine);
int retryCount = 0;
while (true) {
try (CloseableHttpResponse response = getHttpClient.execute(httpGet)) {
if (isDebugEnabled) {
log.debug(HTTP_STATUS_LOG + response.getStatusLine().getStatusCode());
}
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
StringBuilder responseString = new StringBuilder();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8))) {
String inputLine;
while ((inputLine = reader.readLine()) != null) {
responseString.append(inputLine);
}
}
JSONObject responseJson = (JSONObject) new JSONParser().parse(responseString.toString());
String report = responseJson.get(APIConstants.DATA).toString();
String grade = (String) ((JSONObject) ((JSONObject) responseJson.get(APIConstants.ATTR))
.get(APIConstants.DATA)).get(APIConstants.GRADE);
Integer numErrors = Integer.valueOf(
(String) ((JSONObject) ((JSONObject) responseJson.get(APIConstants.ATTR))
.get(APIConstants.DATA)).get(APIConstants.NUM_ERRORS));
String decodedReport = new String(Base64Utils.decode(report), StandardCharsets.UTF_8);
JSONObject output = new JSONObject();
output.put("decodedReport", decodedReport);
output.put("grade", grade);
output.put("numErrors", numErrors);
output.put("auditUuid", auditUuid);
return output;
} else {
retryCount++;
if (retryCount <= 5) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// Ignore
}
} else {
throw new APIManagementException(
"Error while retrieving data from the API Definition Security Audit. "
+ "Found http status " + response.getStatusLine());
}
}
JSONObject responseJson = (JSONObject) new JSONParser().parse(responseString.toString());
String report = responseJson.get(APIConstants.DATA).toString();
String grade = (String) ((JSONObject) ((JSONObject) responseJson.get(APIConstants.ATTR))
.get(APIConstants.DATA)).get(APIConstants.GRADE);
Integer numErrors = Integer.valueOf(
(String) ((JSONObject) ((JSONObject) responseJson.get(APIConstants.ATTR))
.get(APIConstants.DATA)).get(APIConstants.NUM_ERRORS));
String decodedReport = new String(Base64Utils.decode(report), StandardCharsets.UTF_8);
JSONObject output = new JSONObject();
output.put("decodedReport", decodedReport);
output.put("grade", grade);
output.put("numErrors", numErrors);
output.put("auditUuid", auditUuid);
return output;
}
}
}
return new JSONObject(Collections.emptyMap());
}

/**
Expand Down

0 comments on commit a8a3d64

Please sign in to comment.