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 9577511e2 [ISSUE #3961 ]Code optimization.[TCPClientHandler] (#3996)
9577511e2 is described below
commit 9577511e2715985f17c39df48b2c22bb12c32800
Author: Nitheesh Daram <[email protected]>
AuthorDate: Wed May 24 18:28:19 2023 +0530
[ISSUE #3961 ]Code optimization.[TCPClientHandler] (#3996)
* Resolves #3961
* Updated EventMeshConstants.java
---
.../runtime/admin/handler/TCPClientHandler.java | 45 +++++++++++++---------
1 file changed, 26 insertions(+), 19 deletions(-)
diff --git
a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/TCPClientHandler.java
b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/TCPClientHandler.java
index 0eb2b13ef..ae0a2d677 100644
---
a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/TCPClientHandler.java
+++
b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/TCPClientHandler.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.protocol.tcp.UserAgent;
import org.apache.eventmesh.common.utils.JsonUtils;
import org.apache.eventmesh.runtime.admin.controller.HttpHandlerManager;
@@ -27,6 +28,7 @@ import
org.apache.eventmesh.runtime.admin.response.GetClientResponse;
import org.apache.eventmesh.runtime.admin.utils.HttpExchangeUtils;
import org.apache.eventmesh.runtime.boot.EventMeshTCPServer;
import org.apache.eventmesh.runtime.common.EventHttpHandler;
+import org.apache.eventmesh.runtime.constants.EventMeshConstants;
import
org.apache.eventmesh.runtime.core.protocol.tcp.client.EventMeshTcp2Client;
import
org.apache.eventmesh.runtime.core.protocol.tcp.client.group.ClientSessionGroupMapping;
import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session;
@@ -39,6 +41,7 @@ import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
@@ -67,10 +70,10 @@ public class TCPClientHandler extends AbstractHttpHandler {
* OPTIONS /client
*/
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();
@@ -84,7 +87,7 @@ public class TCPClientHandler extends AbstractHttpHandler {
try (OutputStream out = httpExchange.getResponseBody()) {
String request =
HttpExchangeUtils.streamToString(httpExchange.getRequestBody());
DeleteTCPClientRequest deleteTCPClientRequest =
JsonUtils.parseObject(request, DeleteTCPClientRequest.class);
- String host = deleteTCPClientRequest.getHost();
+ String host =
Objects.requireNonNull(deleteTCPClientRequest).getHost();
int port = deleteTCPClientRequest.getPort();
ClientSessionGroupMapping clientSessionGroupMapping =
eventMeshTCPServer.getClientSessionGroupMapping();
@@ -101,7 +104,7 @@ public class TCPClientHandler extends AbstractHttpHandler {
}
}
-
httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
+
httpExchange.getResponseHeaders().add(EventMeshConstants.HANDLER_ORIGIN, "*");
httpExchange.sendResponseHeaders(200, 0);
} catch (Exception e) {
StringWriter writer = new StringWriter();
@@ -112,7 +115,7 @@ public class TCPClientHandler 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);
}
}
@@ -123,8 +126,8 @@ public class TCPClientHandler extends AbstractHttpHandler {
void list(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, "*");
// Get the list of TCP clients
ClientSessionGroupMapping clientSessionGroupMapping =
eventMeshTCPServer.getClientSessionGroupMapping();
Map<InetSocketAddress, Session> sessionMap =
clientSessionGroupMapping.getSessionMap();
@@ -155,7 +158,7 @@ public class TCPClientHandler extends AbstractHttpHandler {
});
String result = JsonUtils.toJSONString(getClientResponseList);
- 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();
@@ -166,21 +169,25 @@ public class TCPClientHandler 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 ("GET".equals(httpExchange.getRequestMethod())) {
- list(httpExchange);
- }
- if ("DELETE".equals(httpExchange.getRequestMethod())) {
- delete(httpExchange);
+ switch (HttpMethod.valueOf(httpExchange.getRequestMethod())) {
+ case OPTIONS:
+ preflight(httpExchange);
+ break;
+ case GET:
+ list(httpExchange);
+ break;
+ case DELETE:
+ delete(httpExchange);
+ break;
+ default:
+ break;
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]