Pil0tXia commented on code in PR #4768: URL: https://github.com/apache/eventmesh/pull/4768#discussion_r1549257520
########## eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/AbstractHttpHandler.java: ########## @@ -17,18 +17,132 @@ package org.apache.eventmesh.runtime.admin.handler; -import com.sun.net.httpserver.HttpHandler; +import org.apache.eventmesh.common.enums.HttpMethod; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.util.HttpRequestUtil; +import org.apache.eventmesh.runtime.util.HttpResponseUtils; -import lombok.Data; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; -/** - * An abstract class that implements the {@link HttpHandler} interface - * and provides basic functionality for HTTP request handling. - * <p> - * Subclasses should extend this class to implement specific HTTP request handling logic. - */ +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.DefaultHttpHeaders; +import io.netty.handler.codec.http.HttpHeaders; +import io.netty.handler.codec.http.HttpRequest; +import io.netty.handler.codec.http.HttpResponse; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.netty.handler.codec.http.HttpVersion; +import io.netty.util.AsciiString; + +import lombok.Data; @Data public abstract class AbstractHttpHandler implements HttpHandler { + protected void write(ChannelHandlerContext ctx, byte[] result, AsciiString headerValue) { + ctx.writeAndFlush(HttpResponseUtils.getHttpResponse(result, ctx, headerValue)).addListener(ChannelFutureListener.CLOSE); + } + + protected void write(ChannelHandlerContext ctx, HttpResponse response) { + ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); + } + + protected void write(ChannelHandlerContext ctx, byte[] result, AsciiString headerValue, HttpResponseStatus httpResponseStatus) { + ctx.writeAndFlush(HttpResponseUtils.getHttpResponse(result, ctx, headerValue, httpResponseStatus)).addListener(ChannelFutureListener.CLOSE); + } + + protected void write401(ChannelHandlerContext ctx) { + DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED); + ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); + } + + protected void writeSuccess(ChannelHandlerContext ctx) { + HttpHeaders responseHeaders = new DefaultHttpHeaders(); + responseHeaders.add(EventMeshConstants.HANDLER_ORIGIN, "*"); + DefaultFullHttpResponse response = + new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.EMPTY_BUFFER, responseHeaders, responseHeaders); + ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); + } + + protected void writeSuccess(ChannelHandlerContext ctx, DefaultHttpHeaders responseHeaders) { + DefaultFullHttpResponse response = + new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.EMPTY_BUFFER, responseHeaders, responseHeaders); + ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); + } + + protected void preflight(ChannelHandlerContext ctx) { + HttpHeaders responseHeaders = new DefaultHttpHeaders(); + responseHeaders.add(EventMeshConstants.HANDLER_ORIGIN, "*"); + responseHeaders.add(EventMeshConstants.HANDLER_METHODS, "*"); + responseHeaders.add(EventMeshConstants.HANDLER_HEADERS, "*"); + responseHeaders.add(EventMeshConstants.HANDLER_AGE, EventMeshConstants.MAX_AGE); + DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.EMPTY_BUFFER, + responseHeaders, responseHeaders); + write(ctx, response); + } + + /** + * Converts a query string to a map of key-value pairs. + * <p> + * This method takes a query string and parses it to create a map of key-value pairs, where each key and value are extracted from the query string + * separated by '='. + * <p> + * If the query string is null, an empty map is returned. + * + * @param query the query string to convert to a map + * @return a map containing the key-value pairs from the query string + */ + protected Map<String, String> queryToMap(String query) { + if (query == null) { + return new HashMap<>(); + } + Map<String, String> result = new HashMap<>(); + for (String param : query.split("&")) { + String[] entry = param.split("="); + if (entry.length > 1) { + result.put(entry[0], entry[1]); + } else { + result.put(entry[0], ""); + } + } + return result; + } + + @Override + public void handle(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception { + switch (HttpMethod.valueOf(httpRequest.method().name())) { + case OPTIONS: + preflight(ctx); + break; + case GET: + get(httpRequest, ctx); + break; + case POST: + post(httpRequest, ctx); + break; + case DELETE: + delete(httpRequest, ctx); + break; + default: // do nothing + break; + } + } + + protected void post(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception{ + } + + protected void delete(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception{ + } + + protected void get(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception{ Review Comment: Need a space before `{`. ########## eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/AbstractHttpHandler.java: ########## @@ -17,18 +17,132 @@ package org.apache.eventmesh.runtime.admin.handler; -import com.sun.net.httpserver.HttpHandler; +import org.apache.eventmesh.common.enums.HttpMethod; +import org.apache.eventmesh.runtime.constants.EventMeshConstants; +import org.apache.eventmesh.runtime.util.HttpRequestUtil; +import org.apache.eventmesh.runtime.util.HttpResponseUtils; -import lombok.Data; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; -/** - * An abstract class that implements the {@link HttpHandler} interface - * and provides basic functionality for HTTP request handling. - * <p> - * Subclasses should extend this class to implement specific HTTP request handling logic. - */ +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.DefaultHttpHeaders; +import io.netty.handler.codec.http.HttpHeaders; +import io.netty.handler.codec.http.HttpRequest; +import io.netty.handler.codec.http.HttpResponse; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.netty.handler.codec.http.HttpVersion; +import io.netty.util.AsciiString; + +import lombok.Data; @Data public abstract class AbstractHttpHandler implements HttpHandler { + protected void write(ChannelHandlerContext ctx, byte[] result, AsciiString headerValue) { + ctx.writeAndFlush(HttpResponseUtils.getHttpResponse(result, ctx, headerValue)).addListener(ChannelFutureListener.CLOSE); + } + + protected void write(ChannelHandlerContext ctx, HttpResponse response) { + ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); + } + + protected void write(ChannelHandlerContext ctx, byte[] result, AsciiString headerValue, HttpResponseStatus httpResponseStatus) { + ctx.writeAndFlush(HttpResponseUtils.getHttpResponse(result, ctx, headerValue, httpResponseStatus)).addListener(ChannelFutureListener.CLOSE); + } + + protected void write401(ChannelHandlerContext ctx) { + DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED); + ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); + } + + protected void writeSuccess(ChannelHandlerContext ctx) { + HttpHeaders responseHeaders = new DefaultHttpHeaders(); + responseHeaders.add(EventMeshConstants.HANDLER_ORIGIN, "*"); + DefaultFullHttpResponse response = + new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.EMPTY_BUFFER, responseHeaders, responseHeaders); + ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); + } + + protected void writeSuccess(ChannelHandlerContext ctx, DefaultHttpHeaders responseHeaders) { + DefaultFullHttpResponse response = + new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.EMPTY_BUFFER, responseHeaders, responseHeaders); + ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); + } + + protected void preflight(ChannelHandlerContext ctx) { + HttpHeaders responseHeaders = new DefaultHttpHeaders(); + responseHeaders.add(EventMeshConstants.HANDLER_ORIGIN, "*"); + responseHeaders.add(EventMeshConstants.HANDLER_METHODS, "*"); + responseHeaders.add(EventMeshConstants.HANDLER_HEADERS, "*"); + responseHeaders.add(EventMeshConstants.HANDLER_AGE, EventMeshConstants.MAX_AGE); + DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.EMPTY_BUFFER, + responseHeaders, responseHeaders); + write(ctx, response); + } + + /** + * Converts a query string to a map of key-value pairs. + * <p> + * This method takes a query string and parses it to create a map of key-value pairs, where each key and value are extracted from the query string + * separated by '='. + * <p> + * If the query string is null, an empty map is returned. + * + * @param query the query string to convert to a map + * @return a map containing the key-value pairs from the query string + */ + protected Map<String, String> queryToMap(String query) { + if (query == null) { + return new HashMap<>(); + } + Map<String, String> result = new HashMap<>(); + for (String param : query.split("&")) { + String[] entry = param.split("="); + if (entry.length > 1) { + result.put(entry[0], entry[1]); + } else { + result.put(entry[0], ""); + } + } + return result; + } + + @Override + public void handle(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception { + switch (HttpMethod.valueOf(httpRequest.method().name())) { + case OPTIONS: + preflight(ctx); + break; + case GET: + get(httpRequest, ctx); + break; + case POST: + post(httpRequest, ctx); + break; + case DELETE: + delete(httpRequest, ctx); + break; + default: // do nothing + break; + } + } + + protected void post(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception{ + } + + protected void delete(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception{ + } + + protected void get(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception{ + } + + protected Map<String, Object> parseHttpRequestBody(final HttpRequest httpRequest) throws IOException { + return HttpRequestUtil.parseHttpRequestBody(httpRequest, null, null); Review Comment: There are two `space` after `return`. -- 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: issues-unsubscr...@eventmesh.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@eventmesh.apache.org For additional commands, e-mail: issues-h...@eventmesh.apache.org