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 2fed6c7d6 [ISSUE #3955 ]Code optimization.[ConfigurationHandler]
(#3972)
2fed6c7d6 is described below
commit 2fed6c7d6fdef99d957b9b2921c54948418295ed
Author: Nitheesh Daram <[email protected]>
AuthorDate: Tue May 30 07:26:25 2023 +0530
[ISSUE #3955 ]Code optimization.[ConfigurationHandler] (#3972)
* Resolves ISSUE #3955
* Issue-3955-Moved-to-Switch-Case
* Update ConfigurationHandler.java
* Update EventMeshConstants.java
---------
Co-authored-by: mike_xwm <[email protected]>
---
.../admin/handler/ConfigurationHandler.java | 33 +++++++++++++---------
1 file changed, 20 insertions(+), 13 deletions(-)
diff --git
a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/ConfigurationHandler.java
b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/ConfigurationHandler.java
index 89b3c6c0f..3232273ec 100644
---
a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/ConfigurationHandler.java
+++
b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/ConfigurationHandler.java
@@ -18,6 +18,7 @@
package org.apache.eventmesh.runtime.admin.handler;
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;
@@ -26,11 +27,13 @@ import org.apache.eventmesh.runtime.common.EventHttpHandler;
import org.apache.eventmesh.runtime.configuration.EventMeshGrpcConfiguration;
import org.apache.eventmesh.runtime.configuration.EventMeshHTTPConfiguration;
import org.apache.eventmesh.runtime.configuration.EventMeshTCPConfiguration;
+import org.apache.eventmesh.runtime.constants.EventMeshConstants;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
+import java.util.Objects;
import com.sun.net.httpserver.HttpExchange;
@@ -64,10 +67,10 @@ public class ConfigurationHandler extends
AbstractHttpHandler {
* OPTIONS /configuration
*/
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();
@@ -77,8 +80,8 @@ public class ConfigurationHandler extends AbstractHttpHandler
{
* GET /config Return a response that contains the EventMesh configuration
*/
void get(HttpExchange httpExchange) throws IOException {
- 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 (OutputStream out = httpExchange.getResponseBody()) {
try {
GetConfigurationResponse getConfigurationResponse = new
GetConfigurationResponse(
@@ -103,7 +106,7 @@ public class ConfigurationHandler extends
AbstractHttpHandler {
);
String result =
JsonUtils.toJSONString(getConfigurationResponse);
- 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();
@@ -114,7 +117,7 @@ public class ConfigurationHandler 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));
}
}
@@ -122,11 +125,15 @@ public class ConfigurationHandler extends
AbstractHttpHandler {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
- if ("OPTIONS".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: //do nothing
+ break;
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]