Skip to content

Commit

Permalink
add admin http handlermanager
Browse files Browse the repository at this point in the history
  • Loading branch information
karsonto committed Feb 1, 2024
1 parent 5d70e4c commit 7b36bdb
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 89 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.eventmesh.runtime.admin.handler;

import org.apache.eventmesh.runtime.boot.EventMeshGrpcServer;
import org.apache.eventmesh.runtime.boot.EventMeshHTTPServer;
import org.apache.eventmesh.runtime.boot.EventMeshServer;
import org.apache.eventmesh.runtime.boot.EventMeshTCPServer;
import org.apache.eventmesh.runtime.common.EventHttpHandler;
import org.apache.eventmesh.runtime.meta.MetaStorage;
import org.apache.eventmesh.webhook.admin.AdminWebHookConfigOperationManager;
import org.apache.eventmesh.webhook.api.WebHookConfigOperation;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

import com.sun.net.httpserver.HttpHandler;

public class AdminHandlerManager {

private EventMeshTCPServer eventMeshTCPServer;

private EventMeshHTTPServer eventMeshHTTPServer;

private EventMeshGrpcServer eventMeshGrpcServer;

private MetaStorage eventMeshMetaStorage;

private AdminWebHookConfigOperationManager adminWebHookConfigOperationManage;

private final Map<String, HttpHandler> httpHandlerMap = new ConcurrentHashMap<>();

public AdminHandlerManager(EventMeshServer eventMeshServer) {
this.eventMeshGrpcServer = eventMeshServer.getEventMeshGrpcServer();
this.eventMeshHTTPServer = eventMeshServer.getEventMeshHTTPServer();
this.eventMeshTCPServer = eventMeshServer.getEventMeshTCPServer();
this.eventMeshMetaStorage = eventMeshServer.getMetaStorage();
this.adminWebHookConfigOperationManage = eventMeshTCPServer.getAdminWebHookConfigOperationManage();
}

public void registerHttpHandler() {
initHandler(new ShowClientHandler(eventMeshTCPServer));
initHandler(new ShowClientBySystemHandler(eventMeshTCPServer));
initHandler(new RejectAllClientHandler(eventMeshTCPServer));
initHandler(new RejectClientByIpPortHandler(eventMeshTCPServer));
initHandler(new RejectClientBySubSystemHandler(eventMeshTCPServer));
initHandler(new RedirectClientBySubSystemHandler(eventMeshTCPServer));
initHandler(new RedirectClientByPathHandler(eventMeshTCPServer));
initHandler(new RedirectClientByIpPortHandler(eventMeshTCPServer));
initHandler(new ShowListenClientByTopicHandler(eventMeshTCPServer));
initHandler(new QueryRecommendEventMeshHandler(eventMeshTCPServer));
initHandler(new TCPClientHandler(eventMeshTCPServer));
initHandler(new HTTPClientHandler(eventMeshHTTPServer));
initHandler(new GrpcClientHandler(eventMeshGrpcServer));
initHandler(new ConfigurationHandler(
eventMeshTCPServer.getEventMeshTCPConfiguration(),
eventMeshHTTPServer.getEventMeshHttpConfiguration(),
eventMeshGrpcServer.getEventMeshGrpcConfiguration()));
initHandler(new MetricsHandler(eventMeshHTTPServer, eventMeshTCPServer));
initHandler(new TopicHandler(eventMeshTCPServer.getEventMeshTCPConfiguration().getEventMeshStoragePluginType()));
initHandler(new EventHandler(eventMeshTCPServer.getEventMeshTCPConfiguration().getEventMeshStoragePluginType()));
initHandler(new MetaHandler(eventMeshMetaStorage));
if (Objects.nonNull(adminWebHookConfigOperationManage.getWebHookConfigOperation())) {
WebHookConfigOperation webHookConfigOperation = adminWebHookConfigOperationManage.getWebHookConfigOperation();
initHandler(new InsertWebHookConfigHandler(webHookConfigOperation));
initHandler(new UpdateWebHookConfigHandler(webHookConfigOperation));
initHandler(new DeleteWebHookConfigHandler(webHookConfigOperation));
initHandler(new QueryWebHookConfigByIdHandler(webHookConfigOperation));
initHandler(new QueryWebHookConfigByManufacturerHandler(webHookConfigOperation));
}
}

private void initHandler(HttpHandler httpHandler) {
EventHttpHandler eventHttpHandler = httpHandler.getClass().getAnnotation(EventHttpHandler.class);
httpHandlerMap.putIfAbsent(eventHttpHandler.path(), httpHandler);
}

public Optional<HttpHandler> getHttpHandler(String path) {
return Optional.ofNullable(httpHandlerMap.get(path));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,9 @@

package org.apache.eventmesh.runtime.boot;

import org.apache.eventmesh.runtime.admin.handler.ConfigurationHandler;
import org.apache.eventmesh.runtime.admin.handler.DeleteWebHookConfigHandler;
import org.apache.eventmesh.runtime.admin.handler.EventHandler;
import org.apache.eventmesh.runtime.admin.handler.GrpcClientHandler;
import org.apache.eventmesh.runtime.admin.handler.HTTPClientHandler;
import org.apache.eventmesh.runtime.admin.handler.InsertWebHookConfigHandler;
import org.apache.eventmesh.runtime.admin.handler.MetaHandler;
import org.apache.eventmesh.runtime.admin.handler.MetricsHandler;
import org.apache.eventmesh.runtime.admin.handler.QueryRecommendEventMeshHandler;
import org.apache.eventmesh.runtime.admin.handler.QueryWebHookConfigByIdHandler;
import org.apache.eventmesh.runtime.admin.handler.QueryWebHookConfigByManufacturerHandler;
import org.apache.eventmesh.runtime.admin.handler.RedirectClientByIpPortHandler;
import org.apache.eventmesh.runtime.admin.handler.RedirectClientByPathHandler;
import org.apache.eventmesh.runtime.admin.handler.RedirectClientBySubSystemHandler;
import org.apache.eventmesh.runtime.admin.handler.RejectAllClientHandler;
import org.apache.eventmesh.runtime.admin.handler.RejectClientByIpPortHandler;
import org.apache.eventmesh.runtime.admin.handler.RejectClientBySubSystemHandler;
import org.apache.eventmesh.runtime.admin.handler.ShowClientBySystemHandler;
import org.apache.eventmesh.runtime.admin.handler.ShowClientHandler;
import org.apache.eventmesh.runtime.admin.handler.ShowListenClientByTopicHandler;
import org.apache.eventmesh.runtime.admin.handler.TCPClientHandler;
import org.apache.eventmesh.runtime.admin.handler.TopicHandler;
import org.apache.eventmesh.runtime.admin.handler.UpdateWebHookConfigHandler;
import org.apache.eventmesh.runtime.common.EventHttpHandler;
import org.apache.eventmesh.runtime.meta.MetaStorage;
import org.apache.eventmesh.runtime.admin.handler.AdminHandlerManager;
import org.apache.eventmesh.runtime.util.HttpResponseUtils;
import org.apache.eventmesh.runtime.util.Utils;
import org.apache.eventmesh.webhook.admin.AdminWebHookConfigOperationManager;
import org.apache.eventmesh.webhook.api.WebHookConfigOperation;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand All @@ -57,10 +31,8 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
Expand Down Expand Up @@ -96,71 +68,19 @@ public class EventMeshAdminServer extends AbstractHTTPServer {

private HttpConnectionHandler httpConnectionHandler = new HttpConnectionHandler();

private final Map<String, HttpHandler> httpHandlerMap = new ConcurrentHashMap<>();

private EventMeshTCPServer eventMeshTCPServer;

private EventMeshHTTPServer eventMeshHTTPServer;

private EventMeshGrpcServer eventMeshGrpcServer;

private MetaStorage eventMeshMetaStorage;

private AdminWebHookConfigOperationManager adminWebHookConfigOperationManage;
private AdminHandlerManager adminHandlerManager;

public EventMeshAdminServer(EventMeshServer eventMeshServer) {
super(eventMeshServer.getEventMeshTCPServer().getEventMeshTCPConfiguration().getEventMeshServerAdminPort(), false,
eventMeshServer.getEventMeshHTTPServer().getEventMeshHttpConfiguration());
this.eventMeshGrpcServer = eventMeshServer.getEventMeshGrpcServer();
this.eventMeshHTTPServer = eventMeshServer.getEventMeshHTTPServer();
this.eventMeshTCPServer = eventMeshServer.getEventMeshTCPServer();
this.eventMeshMetaStorage = eventMeshServer.getMetaStorage();
this.adminWebHookConfigOperationManage = eventMeshTCPServer.getAdminWebHookConfigOperationManage();

adminHandlerManager = new AdminHandlerManager(eventMeshServer);
}


@Override
public void init() throws Exception {
super.init("eventMesh-admin-http");
registerHttpHandler();
}

private void registerHttpHandler() {
initHandler(new ShowClientHandler(eventMeshTCPServer));
initHandler(new ShowClientBySystemHandler(eventMeshTCPServer));
initHandler(new RejectAllClientHandler(eventMeshTCPServer));
initHandler(new RejectClientByIpPortHandler(eventMeshTCPServer));
initHandler(new RejectClientBySubSystemHandler(eventMeshTCPServer));
initHandler(new RedirectClientBySubSystemHandler(eventMeshTCPServer));
initHandler(new RedirectClientByPathHandler(eventMeshTCPServer));
initHandler(new RedirectClientByIpPortHandler(eventMeshTCPServer));
initHandler(new ShowListenClientByTopicHandler(eventMeshTCPServer));
initHandler(new QueryRecommendEventMeshHandler(eventMeshTCPServer));
initHandler(new TCPClientHandler(eventMeshTCPServer));
initHandler(new HTTPClientHandler(eventMeshHTTPServer));
initHandler(new GrpcClientHandler(eventMeshGrpcServer));
initHandler(new ConfigurationHandler(
eventMeshTCPServer.getEventMeshTCPConfiguration(),
eventMeshHTTPServer.getEventMeshHttpConfiguration(),
eventMeshGrpcServer.getEventMeshGrpcConfiguration()));
initHandler(new MetricsHandler(eventMeshHTTPServer, eventMeshTCPServer));
initHandler(new TopicHandler(eventMeshTCPServer.getEventMeshTCPConfiguration().getEventMeshStoragePluginType()));
initHandler(new EventHandler(eventMeshTCPServer.getEventMeshTCPConfiguration().getEventMeshStoragePluginType()));
initHandler(new MetaHandler(eventMeshMetaStorage));
if (Objects.nonNull(adminWebHookConfigOperationManage.getWebHookConfigOperation())) {
WebHookConfigOperation webHookConfigOperation = adminWebHookConfigOperationManage.getWebHookConfigOperation();
initHandler(new InsertWebHookConfigHandler(webHookConfigOperation));
initHandler(new UpdateWebHookConfigHandler(webHookConfigOperation));
initHandler(new DeleteWebHookConfigHandler(webHookConfigOperation));
initHandler(new QueryWebHookConfigByIdHandler(webHookConfigOperation));
initHandler(new QueryWebHookConfigByManufacturerHandler(webHookConfigOperation));
}
}

private void initHandler(HttpHandler httpHandler) {
EventHttpHandler eventHttpHandler = httpHandler.getClass().getAnnotation(EventHttpHandler.class);
httpHandlerMap.putIfAbsent(eventHttpHandler.path(), httpHandler);
adminHandlerManager.registerHttpHandler();
}

@Override
Expand Down Expand Up @@ -194,14 +114,10 @@ public void start() throws Exception {
started.compareAndSet(false, true);
}

public Optional<HttpHandler> getHttpHandler(String path) {
return Optional.ofNullable(httpHandlerMap.get(path));
}

public void parseHttpRequest(ChannelHandlerContext ctx, HttpRequest httpRequest) {
String uriStr = httpRequest.uri();
URI uri = URI.create(uriStr);
Optional<HttpHandler> httpHandlerOpt = getHttpHandler(uri.getPath());
Optional<HttpHandler> httpHandlerOpt = adminHandlerManager.getHttpHandler(uri.getPath());
if (httpHandlerOpt.isPresent()) {
try {
AdminHttpExchange adminHttpExchange = new AdminHttpExchange(ctx, httpRequest);
Expand Down

0 comments on commit 7b36bdb

Please sign in to comment.