diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/AbstractHttpHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/AbstractHttpHandler.java index af89c7fa5e..831f8e4e80 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/AbstractHttpHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/AbstractHttpHandler.java @@ -19,17 +19,10 @@ 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 java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -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.HttpHeaderNames; import io.netty.handler.codec.http.HttpHeaderValues; @@ -37,7 +30,6 @@ 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 lombok.Data; @@ -126,36 +118,5 @@ protected void put(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Ex protected void delete(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception { // Override this method in subclass } - - protected Map parseHttpRequestBody(final HttpRequest httpRequest) throws IOException { - return HttpRequestUtil.parseHttpRequestBody(httpRequest, null, null); - } - - /** - * Converts a query string to a map of key-value pairs. - *

- * 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 '='. - *

- * 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 queryToMap(String query) { - if (query == null) { - return new HashMap<>(); - } - Map 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; - } } diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/DeleteWebHookConfigHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/DeleteWebHookConfigHandler.java index 433143955e..390ed38bc4 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/DeleteWebHookConfigHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/DeleteWebHookConfigHandler.java @@ -20,6 +20,7 @@ import org.apache.eventmesh.common.utils.JsonUtils; import org.apache.eventmesh.runtime.admin.handler.AbstractHttpHandler; import org.apache.eventmesh.runtime.common.EventMeshHttpHandler; +import org.apache.eventmesh.runtime.util.HttpRequestUtil; import org.apache.eventmesh.webhook.api.WebHookConfig; import org.apache.eventmesh.webhook.api.WebHookConfigOperation; @@ -69,7 +70,7 @@ public DeleteWebHookConfigHandler(WebHookConfigOperation operation) { @Override public void handle(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception { - Map body = parseHttpRequestBody(httpRequest); + Map body = HttpRequestUtil.parseHttpRequestBody(httpRequest); Objects.requireNonNull(body, "body can not be null"); // Resolve to WebHookConfig WebHookConfig webHookConfig = JsonUtils.mapToObject(body, WebHookConfig.class); diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/EventHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/EventHandler.java index 40e6a551c9..f0341eee51 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/EventHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/EventHandler.java @@ -22,6 +22,7 @@ import org.apache.eventmesh.runtime.common.EventMeshHttpHandler; import org.apache.eventmesh.runtime.constants.EventMeshConstants; import org.apache.eventmesh.runtime.core.plugin.MQAdminWrapper; +import org.apache.eventmesh.runtime.util.HttpRequestUtil; import java.net.URI; import java.nio.charset.StandardCharsets; @@ -84,7 +85,7 @@ protected void get(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Ex writeUnauthorized(ctx, ""); return; } - Map queryMap = queryToMap(queryString); + Map queryMap = HttpRequestUtil.queryStringToMap(queryString); String topicName = queryMap.get("topicName"); int offset = Integer.parseInt(queryMap.get("offset")); int length = Integer.parseInt(queryMap.get("length")); @@ -104,7 +105,7 @@ protected void get(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Ex protected void post(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception { HttpHeaders responseHeaders = new DefaultHttpHeaders(); responseHeaders.add(EventMeshConstants.HANDLER_ORIGIN, "*"); - String request = JsonUtils.toJSONString(parseHttpRequestBody(httpRequest)); + String request = JsonUtils.toJSONString(HttpRequestUtil.parseHttpRequestBody(httpRequest)); byte[] rawRequest = request.getBytes(StandardCharsets.UTF_8); CloudEvent event = Objects.requireNonNull(EventFormatProvider .getInstance() diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/GrpcClientHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/GrpcClientHandler.java index d2ce7da9fd..c0a47f2567 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/GrpcClientHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/GrpcClientHandler.java @@ -25,6 +25,7 @@ import org.apache.eventmesh.runtime.common.EventMeshHttpHandler; import org.apache.eventmesh.runtime.core.protocol.grpc.consumer.ConsumerManager; import org.apache.eventmesh.runtime.core.protocol.grpc.consumer.consumergroup.ConsumerGroupClient; +import org.apache.eventmesh.runtime.util.HttpRequestUtil; import java.util.ArrayList; import java.util.List; @@ -65,7 +66,7 @@ public GrpcClientHandler( @Override protected void delete(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception { - Map body = parseHttpRequestBody(httpRequest); + Map body = HttpRequestUtil.parseHttpRequestBody(httpRequest); Objects.requireNonNull(body, "body can not be null"); DeleteGrpcClientRequest deleteGrpcClientRequest = JsonUtils.mapToObject(body, DeleteGrpcClientRequest.class); String url = Objects.requireNonNull(deleteGrpcClientRequest).getUrl(); diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/HTTPClientHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/HTTPClientHandler.java index ad0cfc8536..58edd57fa8 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/HTTPClientHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/HTTPClientHandler.java @@ -25,6 +25,7 @@ import org.apache.eventmesh.runtime.common.EventMeshHttpHandler; import org.apache.eventmesh.runtime.constants.EventMeshConstants; import org.apache.eventmesh.runtime.core.protocol.http.processor.inf.Client; +import org.apache.eventmesh.runtime.util.HttpRequestUtil; import java.util.ArrayList; import java.util.List; @@ -64,7 +65,7 @@ public HTTPClientHandler( } protected void delete(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception { - Map body = parseHttpRequestBody(httpRequest); + Map body = HttpRequestUtil.parseHttpRequestBody(httpRequest); if (!Objects.isNull(body)) { DeleteHTTPClientRequest deleteHTTPClientRequest = JsonUtils.mapToObject(body, DeleteHTTPClientRequest.class); String url = Objects.requireNonNull(deleteHTTPClientRequest).getUrl(); diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/InsertWebHookConfigHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/InsertWebHookConfigHandler.java index c9a8e03830..140decba92 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/InsertWebHookConfigHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/InsertWebHookConfigHandler.java @@ -20,6 +20,7 @@ import org.apache.eventmesh.common.utils.JsonUtils; import org.apache.eventmesh.runtime.admin.handler.AbstractHttpHandler; import org.apache.eventmesh.runtime.common.EventMeshHttpHandler; +import org.apache.eventmesh.runtime.util.HttpRequestUtil; import org.apache.eventmesh.webhook.api.WebHookConfig; import org.apache.eventmesh.webhook.api.WebHookConfigOperation; @@ -77,7 +78,7 @@ public InsertWebHookConfigHandler(WebHookConfigOperation operation) { */ @Override public void handle(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception { - Map body = parseHttpRequestBody(httpRequest); + Map body = HttpRequestUtil.parseHttpRequestBody(httpRequest); Objects.requireNonNull(body, "body can not be null"); WebHookConfig webHookConfig = JsonUtils.mapToObject(body, WebHookConfig.class); // Add the WebHookConfig if no existing duplicate configuration is found diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/QueryWebHookConfigByIdHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/QueryWebHookConfigByIdHandler.java index 8401c37a47..97e2c29e2a 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/QueryWebHookConfigByIdHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/QueryWebHookConfigByIdHandler.java @@ -20,8 +20,7 @@ import org.apache.eventmesh.common.utils.JsonUtils; import org.apache.eventmesh.runtime.admin.handler.AbstractHttpHandler; import org.apache.eventmesh.runtime.common.EventMeshHttpHandler; -import org.apache.eventmesh.runtime.constants.EventMeshConstants; -import org.apache.eventmesh.runtime.util.HttpResponseUtils; +import org.apache.eventmesh.runtime.util.HttpRequestUtil; import org.apache.eventmesh.webhook.api.WebHookConfig; import org.apache.eventmesh.webhook.api.WebHookConfigOperation; @@ -29,11 +28,7 @@ import java.util.Objects; import io.netty.channel.ChannelHandlerContext; -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 lombok.extern.slf4j.Slf4j; @@ -86,7 +81,7 @@ public QueryWebHookConfigByIdHandler(WebHookConfigOperation operation) { @Override public void handle(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception { // Resolve to WebHookConfig - Map body = parseHttpRequestBody(httpRequest); + Map body = HttpRequestUtil.parseHttpRequestBody(httpRequest); if (!Objects.isNull(body)) { WebHookConfig webHookConfig = JsonUtils.mapToObject(body, WebHookConfig.class); // Retrieve the WebHookConfig by callback path diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/QueryWebHookConfigByManufacturerHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/QueryWebHookConfigByManufacturerHandler.java index 0f6d9e1ee4..063cd6fd05 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/QueryWebHookConfigByManufacturerHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/QueryWebHookConfigByManufacturerHandler.java @@ -20,8 +20,7 @@ import org.apache.eventmesh.common.utils.JsonUtils; import org.apache.eventmesh.runtime.admin.handler.AbstractHttpHandler; import org.apache.eventmesh.runtime.common.EventMeshHttpHandler; -import org.apache.eventmesh.runtime.constants.EventMeshConstants; -import org.apache.eventmesh.runtime.util.HttpResponseUtils; +import org.apache.eventmesh.runtime.util.HttpRequestUtil; import org.apache.eventmesh.webhook.api.WebHookConfig; import org.apache.eventmesh.webhook.api.WebHookConfigOperation; @@ -30,11 +29,7 @@ import java.util.Objects; import io.netty.channel.ChannelHandlerContext; -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 lombok.extern.slf4j.Slf4j; @@ -83,7 +78,7 @@ public QueryWebHookConfigByManufacturerHandler(WebHookConfigOperation operation) @Override public void handle(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception { // Resolve to WebHookConfig - Map body = parseHttpRequestBody(httpRequest); + Map body = HttpRequestUtil.parseHttpRequestBody(httpRequest); Objects.requireNonNull(body, "body can not be null"); WebHookConfig webHookConfig = JsonUtils.mapToObject(body, WebHookConfig.class); Integer pageNum = Integer.valueOf(body.get("pageNum").toString()); diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/TCPClientHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/TCPClientHandler.java index 7ee3eff3cf..d2bffc15d1 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/TCPClientHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/TCPClientHandler.java @@ -27,6 +27,7 @@ import org.apache.eventmesh.runtime.core.protocol.tcp.client.EventMeshTcp2Client; import org.apache.eventmesh.runtime.core.protocol.tcp.client.group.ClientSessionGroupMapping; import org.apache.eventmesh.runtime.core.protocol.tcp.client.session.Session; +import org.apache.eventmesh.runtime.util.HttpRequestUtil; import java.net.InetSocketAddress; import java.util.ArrayList; @@ -70,7 +71,7 @@ public TCPClientHandler( @Override protected void delete(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception { // Parse the request body string into a DeleteTCPClientRequest object - Map body = parseHttpRequestBody(httpRequest); + Map body = HttpRequestUtil.parseHttpRequestBody(httpRequest); Objects.requireNonNull(body, "body can not be null"); DeleteTCPClientRequest deleteTCPClientRequest = JsonUtils.mapToObject(body, DeleteTCPClientRequest.class); String host = Objects.requireNonNull(deleteTCPClientRequest).getHost(); diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/TopicHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/TopicHandler.java index 18461bf38b..fc567ca69b 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/TopicHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/TopicHandler.java @@ -25,7 +25,7 @@ import org.apache.eventmesh.runtime.common.EventMeshHttpHandler; import org.apache.eventmesh.runtime.constants.EventMeshConstants; import org.apache.eventmesh.runtime.core.plugin.MQAdminWrapper; -import org.apache.eventmesh.runtime.util.HttpResponseUtils; +import org.apache.eventmesh.runtime.util.HttpRequestUtil; import java.util.List; import java.util.Map; @@ -35,8 +35,6 @@ 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 lombok.extern.slf4j.Slf4j; @@ -84,7 +82,7 @@ protected void get(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Ex @Override protected void post(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception { - Map body = parseHttpRequestBody(httpRequest); + Map body = HttpRequestUtil.parseHttpRequestBody(httpRequest); Objects.requireNonNull(body, "body can not be null"); HttpHeaders responseHeaders = new DefaultHttpHeaders(); responseHeaders.add(EventMeshConstants.CONTENT_TYPE, EventMeshConstants.APPLICATION_JSON); @@ -97,7 +95,7 @@ protected void post(HttpRequest httpRequest, ChannelHandlerContext ctx) throws E @Override protected void delete(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception { - Map body = parseHttpRequestBody(httpRequest); + Map body = HttpRequestUtil.parseHttpRequestBody(httpRequest); Objects.requireNonNull(body, "body can not be null"); HttpHeaders responseHeaders = new DefaultHttpHeaders(); responseHeaders.add(EventMeshConstants.CONTENT_TYPE, EventMeshConstants.APPLICATION_JSON); diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/UpdateWebHookConfigHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/UpdateWebHookConfigHandler.java index d1cef3c551..ab0c1192d1 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/UpdateWebHookConfigHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v1/UpdateWebHookConfigHandler.java @@ -20,6 +20,7 @@ import org.apache.eventmesh.common.utils.JsonUtils; import org.apache.eventmesh.runtime.admin.handler.AbstractHttpHandler; import org.apache.eventmesh.runtime.common.EventMeshHttpHandler; +import org.apache.eventmesh.runtime.util.HttpRequestUtil; import org.apache.eventmesh.webhook.api.WebHookConfig; import org.apache.eventmesh.webhook.api.WebHookConfigOperation; @@ -73,7 +74,7 @@ public UpdateWebHookConfigHandler(WebHookConfigOperation operation) { @Override public void handle(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception { - Map body = parseHttpRequestBody(httpRequest); + Map body = HttpRequestUtil.parseHttpRequestBody(httpRequest); Objects.requireNonNull(body, "body can not be null"); // Resolve to WebHookConfig WebHookConfig webHookConfig = JsonUtils.mapToObject(body, WebHookConfig.class); diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v2/ConfigurationHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v2/ConfigurationHandler.java index 3b3d7c626e..1e6d653a9f 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v2/ConfigurationHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/v2/ConfigurationHandler.java @@ -26,6 +26,7 @@ import org.apache.eventmesh.runtime.configuration.EventMeshGrpcConfiguration; import org.apache.eventmesh.runtime.configuration.EventMeshHTTPConfiguration; import org.apache.eventmesh.runtime.configuration.EventMeshTCPConfiguration; +import org.apache.eventmesh.runtime.util.HttpRequestUtil; import java.lang.reflect.Field; import java.util.ArrayList; @@ -75,14 +76,36 @@ public ConfigurationHandler( this.eventMeshGrpcConfiguration = eventMeshGrpcConfiguration; } + /** + * Parameters: + *

+ */ @Override - protected void get(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception { + protected void get(HttpRequest httpRequest, ChannelHandlerContext ctx) { + String format = HttpRequestUtil.getQueryParam(httpRequest, "format", "properties"); + + Filter[] filters; + if (format.equals("properties")) { + filters = new Filter[] {new ConfigFieldFilter(), new IPAddressToStringFilter()}; + } else if (format.equals("bean")) { + filters = new Filter[] {new IPAddressToStringFilter()}; + } else { + log.warn("Invalid format param: {}", format); + writeJson(ctx, "Invalid format param: " + format); + return; + } + GetConfigurationResponse getConfigurationResponse = new GetConfigurationResponse( eventMeshTCPConfiguration, eventMeshHTTPConfiguration, eventMeshGrpcConfiguration ); - Filter[] filters = new Filter[] {new ConfigFieldFilter(), new IPAddressToStringFilter()}; String result = JSON.toJSONString(getConfigurationResponse, filters); writeJson(ctx, result); } @@ -131,6 +154,10 @@ private Field findFieldInClassHierarchy(Class clazz, String fieldName) throws } } + /** + * {@link IPAddress} can't be serialized directly by FastJSON, + * so this filter converts {@link IPAddress} objects to their string representation. + */ static class IPAddressToStringFilter implements ValueFilter { @Override public Object apply(Object object, String name, Object value) { diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/utils/HttpExchangeUtils.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/utils/HttpExchangeUtils.java deleted file mode 100644 index 67479c8c2f..0000000000 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/utils/HttpExchangeUtils.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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.utils; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; - -public class HttpExchangeUtils { - - public static String streamToString(InputStream stream) throws IOException { - try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) { - - StringBuilder buffer = new StringBuilder(); - int b; - - while ((b = bufferedReader.read()) != -1) { - buffer.append((char) b); - } - - return buffer.toString(); - } - } -} diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/HttpRequestUtil.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/HttpRequestUtil.java index ecfcd0e198..b363e9679b 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/HttpRequestUtil.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/util/HttpRequestUtil.java @@ -19,6 +19,7 @@ import java.io.IOException; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Objects; import java.util.function.Consumer; @@ -57,6 +58,10 @@ public static Map parseHttpRequestBody(final HttpRequest htt return httpRequestBody; } + public static Map parseHttpRequestBody(final HttpRequest httpRequest) throws IOException { + return parseHttpRequestBody(httpRequest, null, null); + } + private static void decodeHttpRequestBody(HttpRequest httpRequest, Map httpRequestBody) throws IOException { final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(DEFAULT_HTTP_DATA_FACTORY, httpRequest); for (final InterfaceHttpData param : decoder.getBodyHttpDatas()) { @@ -68,4 +73,41 @@ private static void decodeHttpRequestBody(HttpRequest httpRequest, Map + * 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 '='. + *

+ * If the query string is null, an empty map is returned. + * + * @param queryString the query string to convert to a map + * @return a map containing the key-value pairs from the query string + */ + public static Map queryStringToMap(String queryString) { + if (queryString == null) { + return new HashMap<>(); + } + Map result = new HashMap<>(); + for (String param : queryString.split("&")) { + String[] entry = param.split("="); + if (entry.length > 1) { + result.put(entry[0], entry[1]); + } else { + result.put(entry[0], ""); + } + } + return result; + } + + public static String getQueryParam(HttpRequest httpRequest, String key, String defaultValue) { + List values = new QueryStringDecoder(httpRequest.uri()).parameters().get(key); + return values != null ? values.get(0) : defaultValue; + } + + public static String getPostParam(HttpRequest httpRequest, String key) throws IOException { + HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(httpRequest); + Attribute attribute = (Attribute) decoder.getBodyHttpData(key); + return attribute.getValue(); + } }