|
17 | 17 |
|
18 | 18 | package org.apache.eventmesh.runtime.admin.handler;
|
19 | 19 |
|
20 |
| -import com.sun.net.httpserver.HttpHandler; |
| 20 | +import org.apache.eventmesh.common.enums.HttpMethod; |
| 21 | +import org.apache.eventmesh.common.protocol.http.HttpCommand; |
| 22 | +import org.apache.eventmesh.runtime.constants.EventMeshConstants; |
| 23 | +import org.apache.eventmesh.runtime.util.HttpResponseUtils; |
21 | 24 |
|
22 |
| -import lombok.Data; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.Map; |
23 | 27 |
|
24 |
| -/** |
25 |
| - * An abstract class that implements the {@link HttpHandler} interface |
26 |
| - * and provides basic functionality for HTTP request handling. |
27 |
| - * <p> |
28 |
| - * Subclasses should extend this class to implement specific HTTP request handling logic. |
29 |
| - */ |
| 28 | +import io.netty.buffer.Unpooled; |
| 29 | +import io.netty.channel.ChannelFutureListener; |
| 30 | +import io.netty.channel.ChannelHandlerContext; |
| 31 | +import io.netty.handler.codec.http.DefaultFullHttpResponse; |
| 32 | +import io.netty.handler.codec.http.DefaultHttpHeaders; |
| 33 | +import io.netty.handler.codec.http.HttpHeaders; |
| 34 | +import io.netty.handler.codec.http.HttpResponse; |
| 35 | +import io.netty.handler.codec.http.HttpResponseStatus; |
| 36 | +import io.netty.handler.codec.http.HttpVersion; |
| 37 | +import io.netty.util.AsciiString; |
| 38 | + |
| 39 | +import lombok.Data; |
30 | 40 |
|
31 | 41 | @Data
|
32 |
| -public abstract class AbstractHttpHandler implements HttpHandler { |
| 42 | +public abstract class AbstractHttpHandler implements org.apache.eventmesh.runtime.admin.handler.HttpHandler { |
| 43 | + |
| 44 | + protected void write(ChannelHandlerContext ctx, byte[] result, AsciiString headerValue) { |
| 45 | + ctx.writeAndFlush(HttpResponseUtils.getHttpResponse(result, ctx, headerValue)).addListener(ChannelFutureListener.CLOSE); |
| 46 | + } |
| 47 | + |
| 48 | + protected void write(ChannelHandlerContext ctx, HttpResponse response) { |
| 49 | + ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); |
| 50 | + } |
| 51 | + |
| 52 | + protected void write(ChannelHandlerContext ctx, byte[] result, AsciiString headerValue, HttpResponseStatus httpResponseStatus) { |
| 53 | + ctx.writeAndFlush(HttpResponseUtils.getHttpResponse(result, ctx, headerValue, httpResponseStatus)).addListener(ChannelFutureListener.CLOSE); |
| 54 | + } |
| 55 | + |
| 56 | + protected void write401(ChannelHandlerContext ctx) { |
| 57 | + DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED); |
| 58 | + ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); |
| 59 | + } |
| 60 | + |
| 61 | + protected void writeSuccess(ChannelHandlerContext ctx) { |
| 62 | + HttpHeaders responseHeaders = new DefaultHttpHeaders(); |
| 63 | + responseHeaders.add(EventMeshConstants.HANDLER_ORIGIN, "*"); |
| 64 | + DefaultFullHttpResponse response = |
| 65 | + new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.EMPTY_BUFFER, responseHeaders, responseHeaders); |
| 66 | + ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); |
| 67 | + } |
| 68 | + |
| 69 | + protected void writeSuccess(ChannelHandlerContext ctx, DefaultHttpHeaders responseHeaders) { |
| 70 | + DefaultFullHttpResponse response = |
| 71 | + new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.EMPTY_BUFFER, responseHeaders, responseHeaders); |
| 72 | + ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); |
| 73 | + } |
| 74 | + |
| 75 | + protected void preflight(ChannelHandlerContext ctx) { |
| 76 | + HttpHeaders responseHeaders = new DefaultHttpHeaders(); |
| 77 | + responseHeaders.add(EventMeshConstants.HANDLER_ORIGIN, "*"); |
| 78 | + responseHeaders.add(EventMeshConstants.HANDLER_METHODS, "*"); |
| 79 | + responseHeaders.add(EventMeshConstants.HANDLER_HEADERS, "*"); |
| 80 | + responseHeaders.add(EventMeshConstants.HANDLER_AGE, EventMeshConstants.MAX_AGE); |
| 81 | + DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.EMPTY_BUFFER, |
| 82 | + responseHeaders, responseHeaders); |
| 83 | + write(ctx, response); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Converts a query string to a map of key-value pairs. |
| 88 | + * <p> |
| 89 | + * 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 |
| 90 | + * separated by '='. |
| 91 | + * <p> |
| 92 | + * If the query string is null, an empty map is returned. |
| 93 | + * |
| 94 | + * @param query the query string to convert to a map |
| 95 | + * @return a map containing the key-value pairs from the query string |
| 96 | + */ |
| 97 | + protected Map<String, String> queryToMap(String query) { |
| 98 | + if (query == null) { |
| 99 | + return new HashMap<>(); |
| 100 | + } |
| 101 | + Map<String, String> result = new HashMap<>(); |
| 102 | + for (String param : query.split("&")) { |
| 103 | + String[] entry = param.split("="); |
| 104 | + if (entry.length > 1) { |
| 105 | + result.put(entry[0], entry[1]); |
| 106 | + } else { |
| 107 | + result.put(entry[0], ""); |
| 108 | + } |
| 109 | + } |
| 110 | + return result; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void handle(HttpCommand httpCommand, ChannelHandlerContext ctx) throws Exception { |
| 115 | + switch (HttpMethod.valueOf(httpCommand.getHttpMethod())) { |
| 116 | + case OPTIONS: |
| 117 | + preflight(ctx); |
| 118 | + break; |
| 119 | + case GET: |
| 120 | + get(httpCommand, ctx); |
| 121 | + break; |
| 122 | + case POST: |
| 123 | + post(httpCommand, ctx); |
| 124 | + break; |
| 125 | + case DELETE: |
| 126 | + delete(httpCommand, ctx); |
| 127 | + break; |
| 128 | + default: // do nothing |
| 129 | + break; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + protected void post(HttpCommand httpCommand, ChannelHandlerContext ctx) throws Exception{ |
| 134 | + } |
| 135 | + |
| 136 | + protected void delete(HttpCommand httpCommand, ChannelHandlerContext ctx) throws Exception{ |
| 137 | + } |
| 138 | + |
| 139 | + protected void get(HttpCommand httpCommand, ChannelHandlerContext ctx) throws Exception{ |
| 140 | + } |
| 141 | + |
33 | 142 |
|
34 | 143 | }
|
| 144 | + |
0 commit comments