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 67041ac8e [ISSUE #3957]Code optimization.[GrpcClientHandler]  (#3992)
67041ac8e is described below

commit 67041ac8e1dbb07b5a77e9c848059aa6b94e292f
Author: Nitheesh Daram <[email protected]>
AuthorDate: Wed May 24 18:30:16 2023 +0530

    [ISSUE #3957]Code optimization.[GrpcClientHandler]  (#3992)
    
    * Resolves #3957
    
    * Updated EventMeshConstants.java
---
 .../runtime/admin/handler/GrpcClientHandler.java   | 42 ++++++++++++----------
 1 file changed, 24 insertions(+), 18 deletions(-)

diff --git 
a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/GrpcClientHandler.java
 
b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/GrpcClientHandler.java
index 5c59ae1dc..1c73b098d 100644
--- 
a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/GrpcClientHandler.java
+++ 
b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/GrpcClientHandler.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.DeleteGrpcClientRequest;
@@ -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.EventMeshGrpcServer;
 import org.apache.eventmesh.runtime.common.EventHttpHandler;
+import org.apache.eventmesh.runtime.constants.EventMeshConstants;
 import 
org.apache.eventmesh.runtime.core.protocol.grpc.consumer.ConsumerManager;
 import 
org.apache.eventmesh.runtime.core.protocol.grpc.consumer.consumergroup.ConsumerGroupClient;
 
@@ -64,10 +66,10 @@ public class GrpcClientHandler 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();
@@ -80,7 +82,7 @@ public class GrpcClientHandler extends AbstractHttpHandler {
         try (OutputStream out = httpExchange.getResponseBody()) {
             String request = 
HttpExchangeUtils.streamToString(httpExchange.getRequestBody());
             DeleteGrpcClientRequest deleteGrpcClientRequest = 
JsonUtils.parseObject(request, DeleteGrpcClientRequest.class);
-            String url = deleteGrpcClientRequest.getUrl();
+            String url = 
Objects.requireNonNull(deleteGrpcClientRequest).getUrl();
 
             ConsumerManager consumerManager = 
eventMeshGrpcServer.getConsumerManager();
             Map<String, List<ConsumerGroupClient>> clientTable = 
consumerManager.getClientTable();
@@ -92,7 +94,7 @@ public class GrpcClientHandler 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();
@@ -113,8 +115,8 @@ public class GrpcClientHandler 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 gRPC clients
@@ -149,7 +151,7 @@ public class GrpcClientHandler 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();
@@ -160,7 +162,7 @@ public class GrpcClientHandler 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) {
@@ -175,14 +177,18 @@ public class GrpcClientHandler 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