Skip to content

Commit

Permalink
fixes #2225 update request and response transformer to remove underto…
Browse files Browse the repository at this point in the history
…w objects in the yaml-rule-plugin input (#2226)
  • Loading branch information
stevehu authored Apr 23, 2024
1 parent ac1dddc commit 9c9b7c0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.networknt.http;

import io.undertow.util.HeaderMap;

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

public class UndertowConverter {
public static Map<String, String> convertHeadersToMap(HeaderMap headerMap) {
Map<String, String> headers = new HashMap<>();
headerMap.forEach(header -> {
headers.put(header.getHeaderName().toString(), header.getFirst());
});
return headers;
}

public static Map<String, String> convertParametersToMap(Map<String, Deque<String>> parameters) {
Map<String, String> paramMap = new HashMap<>();
// Iterate over the parameter names and values
for (Map.Entry<String, Deque<String>> entry : parameters.entrySet()) {
String paramName = entry.getKey();
Deque<String> paramValues = entry.getValue();
if (!paramValues.isEmpty()) {
// For simplicity, only consider the first value if there are multiple values
paramMap.put(paramName, paramValues.getFirst());
}
}
return paramMap;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.networknt.handler.BuffersUtils;
import com.networknt.handler.MiddlewareHandler;
import com.networknt.handler.RequestInterceptor;
import com.networknt.http.UndertowConverter;
import com.networknt.httpstring.AttachmentConstants;
import com.networknt.rule.RuleConstants;
import com.networknt.rule.RuleLoaderStartupHook;
Expand All @@ -15,17 +16,15 @@
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.protocol.http.HttpContinue;
import io.undertow.util.HeaderMap;
import io.undertow.util.Headers;
import io.undertow.util.HttpString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xnio.Buffers;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;

/**
* Transforms the request body of an active request being processed.
Expand Down Expand Up @@ -113,10 +112,10 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
// call the rule engine to transform the request metadata or body. The input contains all the request elements
Map<String, Object> objMap = new HashMap<>();
objMap.put("auditInfo", auditInfo);
objMap.put("requestHeaders", exchange.getRequestHeaders());
objMap.put("responseHeaders", exchange.getResponseHeaders());
objMap.put("queryParameters", exchange.getQueryParameters());
objMap.put("pathParameters", exchange.getPathParameters());
objMap.put("requestHeaders", UndertowConverter.convertHeadersToMap(exchange.getRequestHeaders()));
objMap.put("responseHeaders", UndertowConverter.convertHeadersToMap(exchange.getResponseHeaders()));
objMap.put("queryParameters", UndertowConverter.convertParametersToMap(exchange.getQueryParameters()));
objMap.put("pathParameters", UndertowConverter.convertParametersToMap(exchange.getPathParameters()));
objMap.put("method", method);
objMap.put("requestURL", exchange.getRequestURL());
objMap.put("requestURI", exchange.getRequestURI());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.networknt.handler.BuffersUtils;
import com.networknt.handler.MiddlewareHandler;
import com.networknt.handler.ResponseInterceptor;
import com.networknt.http.UndertowConverter;
import com.networknt.httpstring.AttachmentConstants;
import com.networknt.rule.RuleConstants;
import com.networknt.rule.RuleLoaderStartupHook;
Expand Down Expand Up @@ -211,10 +212,10 @@ private Map<String, Object> executeRules(Map<String, Object> objMap, Map<String,

private Map<String, Object> createExchangeInfoMap(HttpServerExchange exchange, HttpString method, String responseBody, Map<String, Object> auditInfo) {
Map<String, Object> objMap = new HashMap<>();
objMap.put(REQUEST_HEADERS, exchange.getRequestHeaders());
objMap.put(RESPONSE_HEADERS, exchange.getResponseHeaders());
objMap.put(QUERY_PARAMETERS, exchange.getQueryParameters());
objMap.put(PATH_PARAMETERS, exchange.getPathParameters());
objMap.put(REQUEST_HEADERS, UndertowConverter.convertHeadersToMap(exchange.getRequestHeaders()));
objMap.put(RESPONSE_HEADERS, UndertowConverter.convertHeadersToMap(exchange.getResponseHeaders()));
objMap.put(QUERY_PARAMETERS, UndertowConverter.convertParametersToMap(exchange.getQueryParameters()));
objMap.put(PATH_PARAMETERS, UndertowConverter.convertParametersToMap(exchange.getPathParameters()));
objMap.put(METHOD, method.toString());
objMap.put(REQUEST_URL, exchange.getRequestURL());
objMap.put(REQUEST_URI, exchange.getRequestURI());
Expand Down

0 comments on commit 9c9b7c0

Please sign in to comment.