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 56d237445 [ISSUE #3958 ] Code optimization.[HTTPClientHandler] (#3993)
56d237445 is described below

commit 56d2374453665348ea4e897a7a980a39b8d6839c
Author: Nitheesh Daram <[email protected]>
AuthorDate: Thu May 25 08:06:24 2023 +0530

    [ISSUE #3958 ] Code optimization.[HTTPClientHandler] (#3993)
    
    * Resolves #3958
    
    * Resolves-3958
    
    * Fixed Checkmark
---
 .../runtime/admin/handler/HTTPClientHandler.java   | 58 ++++++++++++----------
 1 file changed, 32 insertions(+), 26 deletions(-)

diff --git 
a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/HTTPClientHandler.java
 
b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/HTTPClientHandler.java
index e21ac8e93..9a57f5c3d 100644
--- 
a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/HTTPClientHandler.java
+++ 
b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/HTTPClientHandler.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.request.DeleteHTTPClientRequest;
@@ -26,6 +27,7 @@ import 
org.apache.eventmesh.runtime.admin.response.GetClientResponse;
 import org.apache.eventmesh.runtime.admin.utils.HttpExchangeUtils;
 import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer;
 import org.apache.eventmesh.runtime.common.EventHttpHandler;
+import org.apache.eventmesh.runtime.constants.EventMeshConstants;
 import org.apache.eventmesh.runtime.core.protocol.http.processor.inf.Client;
 
 import java.io.IOException;
@@ -62,10 +64,10 @@ public class HTTPClientHandler 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();
@@ -78,13 +80,13 @@ public class HTTPClientHandler extends AbstractHttpHandler {
         try (OutputStream out = httpExchange.getResponseBody()) {
             String request = 
HttpExchangeUtils.streamToString(httpExchange.getRequestBody());
             DeleteHTTPClientRequest deleteHTTPClientRequest = 
JsonUtils.parseObject(request, DeleteHTTPClientRequest.class);
-            String url = deleteHTTPClientRequest.getUrl();
+            String url = 
Objects.requireNonNull(deleteHTTPClientRequest).getUrl();
 
             for (List<Client> clientList : 
eventMeshHTTPServer.getSubscriptionManager().getLocalClientInfoMapping().values())
 {
                 clientList.removeIf(client -> Objects.equals(client.getUrl(), 
url));
             }
 
-            
httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
+            
httpExchange.getResponseHeaders().add(EventMeshConstants.HANDLER_ORIGIN, "*");
             httpExchange.sendResponseHeaders(200, 0);
         } catch (Exception e) {
             StringWriter writer = new StringWriter();
@@ -105,8 +107,8 @@ public class HTTPClientHandler extends AbstractHttpHandler {
      */
     void list(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 {
             // Get the list of HTTP clients
@@ -115,17 +117,17 @@ public class HTTPClientHandler extends 
AbstractHttpHandler {
             for (List<Client> clientList : 
eventMeshHTTPServer.getSubscriptionManager().getLocalClientInfoMapping().values())
 {
                 for (Client client : clientList) {
                     GetClientResponse getClientResponse = new 
GetClientResponse(
-                        Optional.ofNullable(client.getEnv()).orElseGet(() -> 
""),
-                        Optional.ofNullable(client.getSys()).orElseGet(() -> 
""),
-                        Optional.ofNullable(client.getUrl()).orElseGet(() -> 
""),
+                        Optional.ofNullable(client.getEnv()).orElse(""),
+                        Optional.ofNullable(client.getSys()).orElse(""),
+                        Optional.ofNullable(client.getUrl()).orElse(""),
                         "0",
-                        Optional.ofNullable(client.getHostname()).orElseGet(() 
-> ""),
+                        Optional.ofNullable(client.getHostname()).orElse(""),
                         0,
-                        
Optional.ofNullable(client.getApiVersion()).orElseGet(() -> ""),
-                        Optional.ofNullable(client.getIdc()).orElseGet(() -> 
""),
-                        
Optional.ofNullable(client.getConsumerGroup()).orElseGet(() -> ""),
+                        Optional.ofNullable(client.getApiVersion()).orElse(""),
+                        Optional.ofNullable(client.getIdc()).orElse(""),
+                        
Optional.ofNullable(client.getConsumerGroup()).orElse(""),
                         "",
-                        "HTTP"
+                        EventMeshConstants.PROTOCOL_HTTP.toUpperCase()
 
                     );
                     getClientResponseList.add(getClientResponse);
@@ -140,7 +142,7 @@ public class HTTPClientHandler 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();
@@ -151,7 +153,7 @@ public class HTTPClientHandler 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) {
@@ -166,14 +168,18 @@ public class HTTPClientHandler extends 
AbstractHttpHandler {
 
     @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]

Reply via email to