Pil0tXia commented on code in PR #4739:
URL: https://github.com/apache/eventmesh/pull/4739#discussion_r1457224036


##########
eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/controller/HttpHandlerManager.java:
##########
@@ -18,48 +18,231 @@
 package org.apache.eventmesh.runtime.admin.controller;
 
 import org.apache.eventmesh.runtime.common.EventHttpHandler;
+import org.apache.eventmesh.runtime.util.HttpResponseUtils;
+import org.apache.eventmesh.runtime.util.Utils;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.InetSocketAddress;
+import java.net.URI;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.ChannelFutureListener;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.http.DefaultFullHttpResponse;
+import io.netty.handler.codec.http.FullHttpRequest;
+import io.netty.handler.codec.http.HttpRequest;
+import io.netty.handler.codec.http.HttpResponseStatus;
+import io.netty.handler.codec.http.HttpVersion;
+
+import com.sun.net.httpserver.Headers;
+import com.sun.net.httpserver.HttpContext;
+import com.sun.net.httpserver.HttpExchange;
 import com.sun.net.httpserver.HttpHandler;
-import com.sun.net.httpserver.HttpServer;
+import com.sun.net.httpserver.HttpPrincipal;
+
+import lombok.extern.slf4j.Slf4j;
 
 /**
- * This class manages the registration of {@linkplain 
com.sun.net.httpserver.HttpHandler HttpHandler}
- * for an {@linkplain com.sun.net.httpserver.HttpServer HttpServer}.
+ * This class manages the registration of {@linkplain 
com.sun.net.httpserver.HttpHandler HttpHandler} for an {@linkplain
+ * com.sun.net.httpserver.HttpServer HttpServer}.
  */
 
+@Slf4j
 public class HttpHandlerManager {
 
     private final List<HttpHandler> httpHandlers = new ArrayList<>();
 
+    private final Map<String, HttpHandler> httpHandlerMap = new 
ConcurrentHashMap<>();
+
     /**
      * Registers an HTTP handler.
      *
-     * @param httpHandler The {@link HttpHandler} to be registered.
-     *                    A handler which is invoked to process HTTP exchanges.
-     *                    Each HTTP exchange is handled by one of these 
handlers.
+     * @param httpHandler The {@link HttpHandler} to be registered. A handler 
which is invoked to process HTTP exchanges. Each HTTP exchange is
+     *                    handled by one of these handlers.
      */
     public void register(HttpHandler httpHandler) {
         this.httpHandlers.add(httpHandler);
     }
 
-    /**
-     * Registers multiple HTTP handlers to a given HttpServer.
-     * <p>
-     * Each HTTP handler is annotated with the {@link EventHttpHandler} 
annotation,
-     * which specifies the path where the handler should be registered.
-     *
-     * @param server A HttpServer object that is bound to an IP address and 
port number
-     *               and listens for incoming TCP connections from clients on 
this address.
-     *               The registered HTTP handlers will be associated with this 
server.
-     */
-    public void registerHttpHandler(HttpServer server) {
-        httpHandlers.forEach(httpHandler -> {
-            EventHttpHandler eventHttpHandler = 
httpHandler.getClass().getAnnotation(EventHttpHandler.class);
-            server.createContext(eventHttpHandler.path(), httpHandler);
+    public void register() {
+        httpHandlers.forEach(e -> {
+            EventHttpHandler eventHttpHandler = 
e.getClass().getAnnotation(EventHttpHandler.class);
+            httpHandlerMap.putIfAbsent(eventHttpHandler.path(), e);
         });
+    }
 
+    public void exec(ChannelHandlerContext ctx, HttpRequest httpRequest) {
+        String uriStr = httpRequest.uri();
+        URI uri = URI.create(uriStr);
+        HttpHandler httpHandler = httpHandlerMap.get(uri.getPath());
+        if (httpHandler != null) {
+            try {
+                HttpHandlerManager.AdminHttpExchange adminHttpExchange = new 
HttpHandlerManager.AdminHttpExchange(ctx, httpRequest);
+                httpHandler.handle(adminHttpExchange);
+                adminHttpExchange.writeAndFlash();
+                return;
+            } catch (Exception e) {
+                log.error(e.getMessage(), e);
+                
ctx.writeAndFlush(HttpResponseUtils.createInternalServerError()).addListener(ChannelFutureListener.CLOSE);
+            }
+        } else {
+            
ctx.writeAndFlush(HttpResponseUtils.createNotFound()).addListener(ChannelFutureListener.CLOSE);
+        }

Review Comment:
   How about integrating `EventMeshAdminServer` with `HandlerService` instead 
of implementing a group of http processing logic seperately for admin?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to