Pil0tXia commented on code in PR #4755:
URL: https://github.com/apache/eventmesh/pull/4755#discussion_r1462672787
##########
eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetaHandler.java:
##########
@@ -98,51 +98,48 @@ 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 npe) {
+ // 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) {
+ // exception might arise either in the top write() call or
+ // in getResponseBody() call
+ log.warn("out close failed...", e);
Review Comment:
I think this comment is obvious and there is no need to keep it here.
##########
eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetaHandler.java:
##########
@@ -98,51 +98,48 @@ 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 npe) {
+ // registry not initialized, return empty list
Review Comment:
It is enough to keep it `e` here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]