/*
 *     Copyright 2016-2026 TinyZ
 *
 * Licensed 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.ogcs.log.serlvet;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
import static io.netty.handler.codec.http.HttpResponseStatus.FORBIDDEN;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;

/**
 * @author TinyZ
 */
@Sharable
public class ApiHandler extends SimpleChannelInboundHandler<FullHttpRequest> {

    private static final Logger LOG = LogManager.getLogger(ApiHandler.class);

    private static final Map<String /*api path*/, ApiServlet> SERVLETS = new HashMap<>();

    public ApiHandler() {
    }

    public static void register(ApiServlet servlet) {
        SERVLETS.put(servlet.path(), servlet);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
        QueryStringDecoder decoder = new QueryStringDecoder(msg.uri());
        ApiServlet servlet = SERVLETS.get(decoder.path());
        if (servlet == null) {
            response(ctx, FORBIDDEN);
            return;
        }
        HttpResponse response;
        if (msg.method() == HttpMethod.POST) {
            response = servlet.doPost(msg);
        } else {
            response = servlet.doGet(msg);
        }
        if (response == null) {
            response(ctx, BAD_REQUEST);
            return;
        }
        response(ctx, response);
    }

    private void response(ChannelHandlerContext ctx, HttpResponseStatus status) {
        ChannelFuture future = ctx.writeAndFlush(new DefaultFullHttpResponse(HTTP_1_1, status));
        future.addListener(ChannelFutureListener.CLOSE);
    }

    private void response(ChannelHandlerContext ctx, HttpResponse response) {
        ChannelFuture future = ctx.writeAndFlush(response);
        future.addListener(ChannelFutureListener.CLOSE);
    }

    private void response(ChannelHandlerContext ctx, String msg) {
        HttpResponse response = null;
        if (msg != null) {
            ByteBuf byteBuf = Unpooled.wrappedBuffer(msg.getBytes());
            response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK, byteBuf);
        } else {
            response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK);
        }
        ChannelFuture channelFuture = ctx.channel().writeAndFlush(response);
        channelFuture.addListener(ChannelFutureListener.CLOSE);
    }
}

@@ -1241,16 +1241,88 @@
 BIDDEN;%0A
+import static io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND;%0A
 import s
@@ -1377,16 +1377,16 @@
 TP_1_1;%0A
-
 %0A/**%0A * 
@@ -1985,24 +1985,187 @@
 msg.uri());%0A
+        switch (decoder.path()) %7B%0A            case %22/favicon.ico%22:%0A                response(ctx, NOT_FOUND);%0A                break;%0A            default: %7B%0A        
         ApiS
@@ -2215,24 +2215,32 @@
 ));%0A        
+  
+      
 if (servlet 
@@ -2254,32 +2254,40 @@
 ) %7B%0A            
+     
+   
 response(ctx, FO
@@ -2300,32 +2300,40 @@
 N);%0A            
+       
+ 
 return;%0A        
@@ -2332,26 +2332,36 @@
         
-%7D%0A
+        %7D%0A  
         
 HttpResp
@@ -2344,32 +2344,38 @@
     %7D%0A          
+      
 HttpResponse res
@@ -2385,24 +2385,32 @@
 se;%0A        
+    
+    
 if (msg.meth
@@ -2440,32 +2440,40 @@
 ) %7B%0A            
+       
+ 
 response = servl
@@ -2480,32 +2480,40 @@
 et.doPost(msg);%0A
+        
         %7D else %7B
@@ -2517,32 +2517,40 @@
 e %7B%0A            
+ 
+       
 response = servl
@@ -2572,26 +2572,39 @@
         
-%7D%0A
+        %7D%0A     
         
 if (resp
@@ -2591,24 +2591,27 @@
             
+   
 if (response
@@ -2614,32 +2614,40 @@
 onse == null) %7B%0A
+        
             resp
@@ -2674,32 +2674,40 @@
 T);%0A            
+        
 return;%0A        
@@ -2698,34 +2698,50 @@
 return;%0A        
+  
-%7D%0A
+      %7D%0A        
         response
@@ -2753,24 +2753,71 @@
  response);%0A
+                break;%0A            %7D%0A        %7D%0A
     %7D%0A%0A    p

Modify : ApiHandler