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 8f26e0e45 [ISSUE #3960]Code optimization.[RegistryHandler] (#3995)
8f26e0e45 is described below
commit 8f26e0e4521410d41910f6b93f447ce7f900c9bc
Author: Nitheesh Daram <[email protected]>
AuthorDate: Wed May 24 18:28:46 2023 +0530
[ISSUE #3960]Code optimization.[RegistryHandler] (#3995)
* Resolves #3960
* Updated EventMeshConstants.java
---
.../runtime/admin/handler/RegistryHandler.java | 33 +++++++++++++---------
1 file changed, 20 insertions(+), 13 deletions(-)
diff --git
a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/RegistryHandler.java
b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/RegistryHandler.java
index c70835974..cd85b1dba 100755
---
a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/RegistryHandler.java
+++
b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/RegistryHandler.java
@@ -19,11 +19,13 @@ package org.apache.eventmesh.runtime.admin.handler;
import org.apache.eventmesh.api.registry.dto.EventMeshDataInfo;
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.response.Error;
import org.apache.eventmesh.runtime.admin.response.GetRegistryResponse;
import org.apache.eventmesh.runtime.common.EventHttpHandler;
+import org.apache.eventmesh.runtime.constants.EventMeshConstants;
import org.apache.eventmesh.runtime.registry.Registry;
import java.io.IOException;
@@ -33,6 +35,7 @@ import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
+import java.util.Objects;
import com.sun.net.httpserver.HttpExchange;
@@ -55,9 +58,9 @@ public class RegistryHandler extends AbstractHttpHandler {
* OPTION /registry
*/
void preflight(HttpExchange httpExchange) throws IOException {
- httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin",
"*");
- httpExchange.getResponseHeaders().add("Access-Control-Allow-Method",
"*");
- 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_AGE,
EventMeshConstants.MAX_AGE);
httpExchange.sendResponseHeaders(200, 0);
OutputStream out = httpExchange.getResponseBody();
out.close();
@@ -68,8 +71,8 @@ public class RegistryHandler extends AbstractHttpHandler {
*/
void get(HttpExchange httpExchange) throws IOException {
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, "*");
try {
List<GetRegistryResponse> getRegistryResponseList = new
ArrayList<>();
@@ -87,12 +90,12 @@ public class RegistryHandler extends AbstractHttpHandler {
getRegistryResponseList.sort(Comparator.comparing(GetRegistryResponse::getEventMeshClusterName));
String result = JsonUtils.toJSONString(getRegistryResponseList);
- 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 (NullPointerException e) {
//registry not initialized, return empty list
String result = JsonUtils.toJSONString(new ArrayList<>());
- 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();
@@ -103,7 +106,7 @@ public class RegistryHandler 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);
out.write(result.getBytes(Constants.DEFAULT_CHARSET));
} finally {
if (out != null) {
@@ -118,11 +121,15 @@ public class RegistryHandler extends AbstractHttpHandler {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
- if ("OPTION".equals(httpExchange.getRequestMethod())) {
- preflight(httpExchange);
- }
- if ("GET".equals(httpExchange.getRequestMethod())) {
- get(httpExchange);
+ switch (HttpMethod.valueOf(httpExchange.getRequestMethod())) {
+ case OPTIONS:
+ preflight(httpExchange);
+ break;
+ case GET:
+ get(httpExchange);
+ break;
+ default:
+ break;
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]