This is an automated email from the ASF dual-hosted git repository.
mikexue pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/eventmesh.git
The following commit(s) were added to refs/heads/master by this push:
new ad3be2461 [ISSUE #3962 ]Code optimization.[TopicHandler] (#3997)
ad3be2461 is described below
commit ad3be2461772988f490c70806cebb1fd4648a775
Author: Nitheesh Daram <[email protected]>
AuthorDate: Wed May 24 18:27:47 2023 +0530
[ISSUE #3962 ]Code optimization.[TopicHandler] (#3997)
* Resolves #3962
* Updated EventMeshConstants.java
---
.../runtime/admin/handler/TopicHandler.java | 61 ++++++++++++----------
.../runtime/constants/EventMeshConstants.java | 6 ++-
2 files changed, 39 insertions(+), 28 deletions(-)
diff --git
a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/TopicHandler.java
b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/TopicHandler.java
index a54901e47..aa636b1b0 100644
---
a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/TopicHandler.java
+++
b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/TopicHandler.java
@@ -19,6 +19,7 @@ package org.apache.eventmesh.runtime.admin.handler;
import org.apache.eventmesh.api.admin.TopicProperties;
import org.apache.eventmesh.common.Constants;
+import org.apache.eventmesh.common.enums.HttpMethod;
import org.apache.eventmesh.common.utils.JsonUtils;
import org.apache.eventmesh.runtime.admin.controller.HttpHandlerManager;
import org.apache.eventmesh.runtime.admin.request.CreateTopicRequest;
@@ -26,6 +27,7 @@ import
org.apache.eventmesh.runtime.admin.request.DeleteTopicRequest;
import org.apache.eventmesh.runtime.admin.response.Error;
import org.apache.eventmesh.runtime.admin.utils.HttpExchangeUtils;
import org.apache.eventmesh.runtime.common.EventHttpHandler;
+import org.apache.eventmesh.runtime.constants.EventMeshConstants;
import org.apache.eventmesh.runtime.core.plugin.MQAdminWrapper;
import java.io.IOException;
@@ -33,6 +35,7 @@ import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
+import java.util.Objects;
import com.sun.net.httpserver.HttpExchange;
@@ -65,10 +68,10 @@ public class TopicHandler extends AbstractHttpHandler {
* OPTIONS /topic
*/
void preflight(HttpExchange httpExchange) throws IOException {
- httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin",
"*");
- httpExchange.getResponseHeaders().add("Access-Control-Allow-Methods",
"*");
- httpExchange.getResponseHeaders().add("Access-Control-Allow-Headers",
"*");
- httpExchange.getResponseHeaders().add("Access-Control-Max-Age",
"86400");
+
httpExchange.getResponseHeaders().add(EventMeshConstants.HANDLER_ORIGIN, "*");
+
httpExchange.getResponseHeaders().add(EventMeshConstants.HANDLER_METHODS, "*");
+
httpExchange.getResponseHeaders().add(EventMeshConstants.HANDLER_HEADERS, "*");
+ httpExchange.getResponseHeaders().add(EventMeshConstants.HANDLER_AGE,
EventMeshConstants.MAX_AGE);
httpExchange.sendResponseHeaders(200, 0);
OutputStream out = httpExchange.getResponseBody();
out.close();
@@ -80,11 +83,11 @@ public class TopicHandler extends AbstractHttpHandler {
void get(HttpExchange httpExchange) throws IOException {
try (OutputStream out = httpExchange.getResponseBody()) {
- httpExchange.getResponseHeaders().add("Content-Type",
"application/json");
-
httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
+
httpExchange.getResponseHeaders().add(EventMeshConstants.CONTENT_TYPE,
EventMeshConstants.APPLICATION_JSON);
+
httpExchange.getResponseHeaders().add(EventMeshConstants.HANDLER_ORIGIN, "*");
List<TopicProperties> topicList = admin.getTopic();
String result = JsonUtils.toJSONString(topicList);
- httpExchange.sendResponseHeaders(200,
result.getBytes(Constants.DEFAULT_CHARSET).length);
+ 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();
@@ -95,7 +98,7 @@ public class TopicHandler extends AbstractHttpHandler {
Error error = new Error(e.toString(), stackTrace);
String result = JsonUtils.toJSONString(error);
- httpExchange.sendResponseHeaders(500,
result.getBytes(Constants.DEFAULT_CHARSET).length);
+ httpExchange.sendResponseHeaders(500,
Objects.requireNonNull(result).getBytes(Constants.DEFAULT_CHARSET).length);
log.error(result, e);
}
}
@@ -106,11 +109,11 @@ public class TopicHandler extends AbstractHttpHandler {
void post(HttpExchange httpExchange) throws IOException {
try (OutputStream out = httpExchange.getResponseBody()) {
- httpExchange.getResponseHeaders().add("Content-Type",
"application/json");
-
httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
+
httpExchange.getResponseHeaders().add(EventMeshConstants.CONTENT_TYPE,
EventMeshConstants.APPLICATION_JSON);
+
httpExchange.getResponseHeaders().add(EventMeshConstants.HANDLER_ORIGIN, "*");
String request =
HttpExchangeUtils.streamToString(httpExchange.getRequestBody());
CreateTopicRequest createTopicRequest =
JsonUtils.parseObject(request, CreateTopicRequest.class);
- String topicName = createTopicRequest.getName();
+ String topicName =
Objects.requireNonNull(createTopicRequest).getName();
admin.createTopic(topicName);
httpExchange.sendResponseHeaders(200, 0);
} catch (Exception e) {
@@ -122,7 +125,7 @@ public class TopicHandler extends AbstractHttpHandler {
Error error = new Error(e.toString(), stackTrace);
String result = JsonUtils.toJSONString(error);
- httpExchange.sendResponseHeaders(500,
result.getBytes(Constants.DEFAULT_CHARSET).length);
+ httpExchange.sendResponseHeaders(500,
Objects.requireNonNull(result).getBytes(Constants.DEFAULT_CHARSET).length);
log.error(result, e);
}
}
@@ -133,11 +136,11 @@ public class TopicHandler extends AbstractHttpHandler {
void delete(HttpExchange httpExchange) throws IOException {
try (OutputStream out = httpExchange.getResponseBody()) {
- httpExchange.getResponseHeaders().add("Content-Type",
"application/json");
-
httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
+
httpExchange.getResponseHeaders().add(EventMeshConstants.CONTENT_TYPE,
EventMeshConstants.APPLICATION_JSON);
+
httpExchange.getResponseHeaders().add(EventMeshConstants.HANDLER_ORIGIN, "*");
String request =
HttpExchangeUtils.streamToString(httpExchange.getRequestBody());
DeleteTopicRequest deleteTopicRequest =
JsonUtils.parseObject(request, DeleteTopicRequest.class);
- String topicName = deleteTopicRequest.getName();
+ String topicName =
Objects.requireNonNull(deleteTopicRequest).getName();
admin.deleteTopic(topicName);
httpExchange.sendResponseHeaders(200, 0);
} catch (Exception e) {
@@ -149,24 +152,28 @@ public class TopicHandler extends AbstractHttpHandler {
Error error = new Error(e.toString(), stackTrace);
String result = JsonUtils.toJSONString(error);
- httpExchange.sendResponseHeaders(500,
result.getBytes(Constants.DEFAULT_CHARSET).length);
+ httpExchange.sendResponseHeaders(500,
Objects.requireNonNull(result).getBytes(Constants.DEFAULT_CHARSET).length);
log.error(result, e);
}
}
@Override
public void handle(HttpExchange httpExchange) throws IOException {
- if ("OPTIONS".equals(httpExchange.getRequestMethod())) {
- preflight(httpExchange);
- }
- if ("POST".equals(httpExchange.getRequestMethod())) {
- post(httpExchange);
- }
- if ("DELETE".equals(httpExchange.getRequestMethod())) {
- delete(httpExchange);
- }
- if ("GET".equals(httpExchange.getRequestMethod())) {
- get(httpExchange);
+ switch (HttpMethod.valueOf(httpExchange.getRequestMethod())) {
+ case OPTIONS:
+ preflight(httpExchange);
+ break;
+ case POST:
+ post(httpExchange);
+ break;
+ case DELETE:
+ delete(httpExchange);
+ break;
+ case GET:
+ get(httpExchange);
+ break;
+ default:
+ break;
}
}
}
diff --git
a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshConstants.java
b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshConstants.java
index de240a95e..6ffa05b71 100644
---
a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshConstants.java
+++
b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/constants/EventMeshConstants.java
@@ -155,5 +155,9 @@ public class EventMeshConstants {
public static final String ACL = "acl";
public static final String MSG_TYPE = "msgtype";
public static final String PERSISTENT = "persistent";
-
+ public static final String HANDLER_ORIGIN = "Access-Control-Allow-Origin";
+ public static final String HANDLER_METHODS =
"Access-Control-Allow-Methods";
+ public static final String HANDLER_HEADERS =
"Access-Control-Allow-Headers";
+ public static final String HANDLER_AGE = "Access-Control-Max-Age";
+ public static final String MAX_AGE = "86400";
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]