Skip to content

Commit

Permalink
Add format option to query string
Browse files Browse the repository at this point in the history
  • Loading branch information
Pil0tXia committed Apr 4, 2024
1 parent da4eed8 commit 195cb9d
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,17 @@

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;
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 lombok.Data;

Expand Down Expand Up @@ -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<String, Object> parseHttpRequestBody(final HttpRequest httpRequest) throws IOException {
return HttpRequestUtil.parseHttpRequestBody(httpRequest, null, null);
}

/**
* 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;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -69,7 +70,7 @@ public DeleteWebHookConfigHandler(WebHookConfigOperation operation) {

@Override
public void handle(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception {
Map<String, Object> body = parseHttpRequestBody(httpRequest);
Map<String, Object> body = HttpRequestUtil.parseHttpRequestBody(httpRequest);
Objects.requireNonNull(body, "body can not be null");
// Resolve to WebHookConfig
WebHookConfig webHookConfig = JsonUtils.mapToObject(body, WebHookConfig.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -84,7 +85,7 @@ protected void get(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Ex
writeUnauthorized(ctx, "");
return;
}
Map<String, String> queryMap = queryToMap(queryString);
Map<String, String> queryMap = HttpRequestUtil.queryStringToMap(queryString);
String topicName = queryMap.get("topicName");
int offset = Integer.parseInt(queryMap.get("offset"));
int length = Integer.parseInt(queryMap.get("length"));
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,7 +66,7 @@ public GrpcClientHandler(

@Override
protected void delete(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception {
Map<String, Object> body = parseHttpRequestBody(httpRequest);
Map<String, Object> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,7 +65,7 @@ public HTTPClientHandler(
}

protected void delete(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception {
Map<String, Object> body = parseHttpRequestBody(httpRequest);
Map<String, Object> body = HttpRequestUtil.parseHttpRequestBody(httpRequest);
if (!Objects.isNull(body)) {
DeleteHTTPClientRequest deleteHTTPClientRequest = JsonUtils.mapToObject(body, DeleteHTTPClientRequest.class);
String url = Objects.requireNonNull(deleteHTTPClientRequest).getUrl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -77,7 +78,7 @@ public InsertWebHookConfigHandler(WebHookConfigOperation operation) {
*/
@Override
public void handle(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception {
Map<String, Object> body = parseHttpRequestBody(httpRequest);
Map<String, Object> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,15 @@
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;

import java.util.Map;
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;

Expand Down Expand Up @@ -86,7 +81,7 @@ public QueryWebHookConfigByIdHandler(WebHookConfigOperation operation) {
@Override
public void handle(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception {
// Resolve to WebHookConfig
Map<String, Object> body = parseHttpRequestBody(httpRequest);
Map<String, Object> body = HttpRequestUtil.parseHttpRequestBody(httpRequest);
if (!Objects.isNull(body)) {
WebHookConfig webHookConfig = JsonUtils.mapToObject(body, WebHookConfig.class);
// Retrieve the WebHookConfig by callback path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -83,7 +78,7 @@ public QueryWebHookConfigByManufacturerHandler(WebHookConfigOperation operation)
@Override
public void handle(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception {
// Resolve to WebHookConfig
Map<String, Object> body = parseHttpRequestBody(httpRequest);
Map<String, Object> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Object> body = parseHttpRequestBody(httpRequest);
Map<String, Object> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -84,7 +82,7 @@ protected void get(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Ex

@Override
protected void post(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception {
Map<String, Object> body = parseHttpRequestBody(httpRequest);
Map<String, Object> body = HttpRequestUtil.parseHttpRequestBody(httpRequest);
Objects.requireNonNull(body, "body can not be null");
HttpHeaders responseHeaders = new DefaultHttpHeaders();
responseHeaders.add(EventMeshConstants.CONTENT_TYPE, EventMeshConstants.APPLICATION_JSON);
Expand All @@ -97,7 +95,7 @@ protected void post(HttpRequest httpRequest, ChannelHandlerContext ctx) throws E

@Override
protected void delete(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception {
Map<String, Object> body = parseHttpRequestBody(httpRequest);
Map<String, Object> body = HttpRequestUtil.parseHttpRequestBody(httpRequest);
Objects.requireNonNull(body, "body can not be null");
HttpHeaders responseHeaders = new DefaultHttpHeaders();
responseHeaders.add(EventMeshConstants.CONTENT_TYPE, EventMeshConstants.APPLICATION_JSON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -73,7 +74,7 @@ public UpdateWebHookConfigHandler(WebHookConfigOperation operation) {

@Override
public void handle(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Exception {
Map<String, Object> body = parseHttpRequestBody(httpRequest);
Map<String, Object> body = HttpRequestUtil.parseHttpRequestBody(httpRequest);
Objects.requireNonNull(body, "body can not be null");
// Resolve to WebHookConfig
WebHookConfig webHookConfig = JsonUtils.mapToObject(body, WebHookConfig.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,14 +76,36 @@ public ConfigurationHandler(
this.eventMeshGrpcConfiguration = eventMeshGrpcConfiguration;
}

/**
* Parameters:
* <ul>
* <li>
* {@code format}: String; Optional, DefaultValue: {@code properties}, SelectableValue: {@code bean}.
* <p>When {@code properties}, the field names are returned in Properties format;
* <p>When {@code bean}, the field names themselves are used as json keys.
* </li>
* </ul>
*/
@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);
}
Expand Down Expand Up @@ -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) {
Expand Down

This file was deleted.

Loading

0 comments on commit 195cb9d

Please sign in to comment.