Skip to content

Commit

Permalink
[ISSUE #3010] Method manually handles closing an auto-closeable resou…
Browse files Browse the repository at this point in the history
…rce [MetaHandler] (#4755)
  • Loading branch information
arsenalzp authored Jan 26, 2024
1 parent 8bb8b8f commit 378aa75
Showing 1 changed file with 34 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,51 +98,46 @@ void preflight(HttpExchange httpExchange) throws IOException {
* @throws IOException if an I/O error occurs while handling the request
*/
void get(HttpExchange httpExchange) throws IOException {
OutputStream out = httpExchange.getResponseBody();
httpExchange.getResponseHeaders().add(EventMeshConstants.CONTENT_TYPE, EventMeshConstants.APPLICATION_JSON);
httpExchange.getResponseHeaders().add(EventMeshConstants.HANDLER_ORIGIN, "*");

try {
List<GetRegistryResponse> getRegistryResponseList = new ArrayList<>();
List<EventMeshDataInfo> eventMeshDataInfos = eventMeshMetaStorage.findAllEventMeshInfo();
for (EventMeshDataInfo eventMeshDataInfo : eventMeshDataInfos) {
GetRegistryResponse getRegistryResponse = new GetRegistryResponse(
eventMeshDataInfo.getEventMeshClusterName(),
eventMeshDataInfo.getEventMeshName(),
eventMeshDataInfo.getEndpoint(),
eventMeshDataInfo.getLastUpdateTimestamp(),
eventMeshDataInfo.getMetadata().toString());
getRegistryResponseList.add(getRegistryResponse);
}
getRegistryResponseList.sort(Comparator.comparing(GetRegistryResponse::getEventMeshClusterName));
try (OutputStream out = httpExchange.getResponseBody()) {
try {
List<GetRegistryResponse> getRegistryResponseList = new ArrayList<>();
List<EventMeshDataInfo> eventMeshDataInfos = eventMeshMetaStorage.findAllEventMeshInfo();
for (EventMeshDataInfo eventMeshDataInfo : eventMeshDataInfos) {
GetRegistryResponse getRegistryResponse = new GetRegistryResponse(
eventMeshDataInfo.getEventMeshClusterName(),
eventMeshDataInfo.getEventMeshName(),
eventMeshDataInfo.getEndpoint(),
eventMeshDataInfo.getLastUpdateTimestamp(),
eventMeshDataInfo.getMetadata().toString());
getRegistryResponseList.add(getRegistryResponse);
}
getRegistryResponseList.sort(Comparator.comparing(GetRegistryResponse::getEventMeshClusterName));

String result = JsonUtils.toJSONString(getRegistryResponseList);
httpExchange.sendResponseHeaders(200, Objects.requireNonNull(result).getBytes(Constants.DEFAULT_CHARSET).length);
out.write(result.getBytes(Constants.DEFAULT_CHARSET));
} catch (NullPointerException e) {
// registry not initialized, return empty list
String result = JsonUtils.toJSONString(new ArrayList<>());
httpExchange.sendResponseHeaders(200, Objects.requireNonNull(result).getBytes(Constants.DEFAULT_CHARSET).length);
out.write(result.getBytes(Constants.DEFAULT_CHARSET));
} catch (Exception e) {
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
e.printStackTrace(printWriter);
printWriter.flush();
String stackTrace = writer.toString();
String result = JsonUtils.toJSONString(getRegistryResponseList);
httpExchange.sendResponseHeaders(200, Objects.requireNonNull(result).getBytes(Constants.DEFAULT_CHARSET).length);
out.write(result.getBytes(Constants.DEFAULT_CHARSET));
} catch (NullPointerException e) {
// registry not initialized, return empty list
String result = JsonUtils.toJSONString(new ArrayList<>());
httpExchange.sendResponseHeaders(200, Objects.requireNonNull(result).getBytes(Constants.DEFAULT_CHARSET).length);
out.write(result.getBytes(Constants.DEFAULT_CHARSET));
} catch (Exception e) {
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
e.printStackTrace(printWriter);
printWriter.flush();
String stackTrace = writer.toString();

Error error = new Error(e.toString(), stackTrace);
String result = JsonUtils.toJSONString(error);
httpExchange.sendResponseHeaders(500, Objects.requireNonNull(result).getBytes(Constants.DEFAULT_CHARSET).length);
out.write(result.getBytes(Constants.DEFAULT_CHARSET));
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
log.warn("out close failed...", e);
}
Error error = new Error(e.toString(), stackTrace);
String result = JsonUtils.toJSONString(error);
httpExchange.sendResponseHeaders(500, Objects.requireNonNull(result).getBytes(Constants.DEFAULT_CHARSET).length);
out.write(result.getBytes(Constants.DEFAULT_CHARSET));
}
} catch (IOException e) {
log.warn("out close failed...", e);
}
}

Expand Down

0 comments on commit 378aa75

Please sign in to comment.