[ISSUE #3012] Method modified to handle auto-closeable resource [TopicHandler] (#3782)

This commit is contained in:
Sam V Jose 2023-05-04 07:43:30 +05:30 committed by GitHub
parent 51ff20ac73
commit 3e62b68ab2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 39 deletions

View File

@ -78,11 +78,10 @@ public class TopicHandler extends AbstractHttpHandler {
* GET /topic Return a response that contains the list of topics * GET /topic Return a response that contains the list of topics
*/ */
void get(HttpExchange httpExchange) throws IOException { void get(HttpExchange httpExchange) throws IOException {
OutputStream out = httpExchange.getResponseBody();
httpExchange.getResponseHeaders().add("Content-Type", "application/json");
httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
try { try (OutputStream out = httpExchange.getResponseBody()) {
httpExchange.getResponseHeaders().add("Content-Type", "application/json");
httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
List<TopicProperties> topicList = admin.getTopic(); List<TopicProperties> topicList = admin.getTopic();
String result = JsonUtils.toJSONString(topicList); String result = JsonUtils.toJSONString(topicList);
httpExchange.sendResponseHeaders(200, result.getBytes(Constants.DEFAULT_CHARSET).length); httpExchange.sendResponseHeaders(200, result.getBytes(Constants.DEFAULT_CHARSET).length);
@ -97,15 +96,7 @@ public class TopicHandler extends AbstractHttpHandler {
Error error = new Error(e.toString(), stackTrace); Error error = new Error(e.toString(), stackTrace);
String result = JsonUtils.toJSONString(error); String result = JsonUtils.toJSONString(error);
httpExchange.sendResponseHeaders(500, result.getBytes(Constants.DEFAULT_CHARSET).length); httpExchange.sendResponseHeaders(500, result.getBytes(Constants.DEFAULT_CHARSET).length);
out.write(result.getBytes(Constants.DEFAULT_CHARSET)); log.error(result, e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
log.warn("out close failed...", e);
}
}
} }
} }
@ -113,11 +104,10 @@ public class TopicHandler extends AbstractHttpHandler {
* POST /topic Create a topic if it doesn't exist * POST /topic Create a topic if it doesn't exist
*/ */
void post(HttpExchange httpExchange) throws IOException { void post(HttpExchange httpExchange) throws IOException {
OutputStream out = httpExchange.getResponseBody();
httpExchange.getResponseHeaders().add("Content-Type", "application/json");
httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
try { try (OutputStream out = httpExchange.getResponseBody()) {
httpExchange.getResponseHeaders().add("Content-Type", "application/json");
httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
String request = HttpExchangeUtils.streamToString(httpExchange.getRequestBody()); String request = HttpExchangeUtils.streamToString(httpExchange.getRequestBody());
CreateTopicRequest createTopicRequest = JsonUtils.parseObject(request, CreateTopicRequest.class); CreateTopicRequest createTopicRequest = JsonUtils.parseObject(request, CreateTopicRequest.class);
String topicName = createTopicRequest.getName(); String topicName = createTopicRequest.getName();
@ -133,15 +123,7 @@ public class TopicHandler extends AbstractHttpHandler {
Error error = new Error(e.toString(), stackTrace); Error error = new Error(e.toString(), stackTrace);
String result = JsonUtils.toJSONString(error); String result = JsonUtils.toJSONString(error);
httpExchange.sendResponseHeaders(500, result.getBytes(Constants.DEFAULT_CHARSET).length); httpExchange.sendResponseHeaders(500, result.getBytes(Constants.DEFAULT_CHARSET).length);
out.write(result.getBytes(Constants.DEFAULT_CHARSET)); log.error(result, e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
log.warn("out close failed...", e);
}
}
} }
} }
@ -149,11 +131,10 @@ public class TopicHandler extends AbstractHttpHandler {
* DELETE /topic Delete a topic if it exists * DELETE /topic Delete a topic if it exists
*/ */
void delete(HttpExchange httpExchange) throws IOException { void delete(HttpExchange httpExchange) throws IOException {
OutputStream out = httpExchange.getResponseBody();
httpExchange.getResponseHeaders().add("Content-Type", "application/json");
httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
try { try (OutputStream out = httpExchange.getResponseBody()) {
httpExchange.getResponseHeaders().add("Content-Type", "application/json");
httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
String request = HttpExchangeUtils.streamToString(httpExchange.getRequestBody()); String request = HttpExchangeUtils.streamToString(httpExchange.getRequestBody());
DeleteTopicRequest deleteTopicRequest = JsonUtils.parseObject(request, DeleteTopicRequest.class); DeleteTopicRequest deleteTopicRequest = JsonUtils.parseObject(request, DeleteTopicRequest.class);
String topicName = deleteTopicRequest.getName(); String topicName = deleteTopicRequest.getName();
@ -169,15 +150,7 @@ public class TopicHandler extends AbstractHttpHandler {
Error error = new Error(e.toString(), stackTrace); Error error = new Error(e.toString(), stackTrace);
String result = JsonUtils.toJSONString(error); String result = JsonUtils.toJSONString(error);
httpExchange.sendResponseHeaders(500, result.getBytes(Constants.DEFAULT_CHARSET).length); httpExchange.sendResponseHeaders(500, result.getBytes(Constants.DEFAULT_CHARSET).length);
out.write(result.getBytes(Constants.DEFAULT_CHARSET)); log.error(result, e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
log.warn("out close failed...", e);
}
}
} }
} }