in src/main/java/org/opensearch/performanceanalyzer/http_action/config/PerformanceAnalyzerResourceProvider.java [158:217]
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client)
throws IOException {
StringBuilder response = new StringBuilder();
String inputLine;
int responseCode;
URL url = getAgentUri(request);
// 'url' is null if no correct mapping for input uri is found
if (url == null) {
return channel -> {
RestResponse finalResponse = new BytesRestResponse(RestStatus.NOT_FOUND, "");
channel.sendResponse(finalResponse);
};
} else {
HttpURLConnection httpURLConnection =
isHttpsEnabled ? createHttpsURLConnection(url) : createHttpURLConnection(url);
// Build Response in buffer
responseCode = httpURLConnection.getResponseCode();
InputStream inputStream =
(responseCode == HttpsURLConnection.HTTP_OK)
? httpURLConnection.getInputStream()
: httpURLConnection.getErrorStream();
try (BufferedReader in = new BufferedReader(new InputStreamReader(inputStream))) {
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
LOG.debug("Response received - {}", response);
} catch (Exception ex) {
LOG.error("Error receiving response for Request Uri {} - {}", request.uri(), ex);
return channel -> {
channel.sendResponse(
new BytesRestResponse(
RestStatus.INTERNAL_SERVER_ERROR,
"Encountered error possibly with downstream APIs"));
};
}
RestResponse finalResponse =
new BytesRestResponse(
RestStatus.fromCode(responseCode), String.valueOf(response));
LOG.debug("finalResponse: {}", finalResponse);
return channel -> {
try {
Map<String, List<String>> map = httpURLConnection.getHeaderFields();
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
finalResponse.addHeader(entry.getKey(), entry.getValue().toString());
}
// Send Response back to callee
channel.sendResponse(finalResponse);
} catch (Exception ex) {
LOG.error("Error sending response", ex);
channel.sendResponse(
new BytesRestResponse(
RestStatus.INTERNAL_SERVER_ERROR, "Something went wrong"));
}
};
}
}