Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #2315 make the request response transformer body encoding confi… #2316

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions handler/src/main/java/com/networknt/handler/BuffersUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ public static String toString(final PooledByteBuffer[] srcs, Charset cs) throws
return new String(toByteArray(srcs), cs);
}

public static String toString(final PooledByteBuffer[] srcs, String charsetName) throws IOException {
return new String(toByteArray(srcs), charsetName);
}

public static String toString(final byte[] src, Charset cs) throws IOException {
return new String(src, cs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ public class RequestTransformerConfig {

private static final String ENABLED = "enabled";
private static final String REQUIRED_CONTENT = "requiredContent";
private static final String DEFAULT_BODY_ENCODING = "defaultBodyEncoding";
private static final String APPLIED_PATH_PREFIXES = "appliedPathPrefixes";

private Map<String, Object> mappedConfig;
private Config config;
private final Config config;
private boolean enabled;
private boolean requiredContent;
private String defaultBodyEncoding;
List<String> appliedPathPrefixes;

private RequestTransformerConfig() {
Expand Down Expand Up @@ -63,6 +65,11 @@ public boolean isEnabled() {
}

public boolean isRequiredContent() { return requiredContent; }

public String getDefaultBodyEncoding() {
return defaultBodyEncoding;
}

public List<String> getAppliedPathPrefixes() {
return appliedPathPrefixes;
}
Expand Down Expand Up @@ -92,6 +99,8 @@ private void setConfigData() {
throw new ConfigException("requiredContent must be a boolean value.");
}
}
object = mappedConfig.get(DEFAULT_BODY_ENCODING);
if (object != null) defaultBodyEncoding = (String) object;
}

private void setConfigList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
enabled: ${request-transformer.enabled:true}
# indicate if the transform interceptor needs to change the request body
requiredContent: ${request-transformer.requiredContent:true}
# default body encoding for the request body. The default value is UTF-8. Other options are ISO-8859-1 and US-ASCII.
defaultBodyEncoding: ${request-transformer.defaultBodyEncoding:UTF-8}
# A list of applied request path prefixes, other requests will skip this handler. The value can be a string
# if there is only one request path prefix needs this handler. or a list of strings if there are multiple.
appliedPathPrefixes: ${request-transformer.appliedPathPrefixes:}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public class RequestTransformerConfigTest {
public void testConfigLoad() {
RequestTransformerConfig config = RequestTransformerConfig.load();
Assert.assertTrue(config.getMappedConfig().size() > 0);
Assert.assertEquals(config.getDefaultBodyEncoding(), "UTF-8");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
if (shouldAttachBody(exchange.getRequestHeaders())) {
if(logger.isTraceEnabled()) logger.trace("shouldAttachBody is true");
PooledByteBuffer[] requestData = this.getBuffer(exchange);
String s = BuffersUtils.toString(requestData, StandardCharsets.UTF_8);
String s = BuffersUtils.toString(requestData, config.getDefaultBodyEncoding());
// Transform the request body with the rule engine.
if(logger.isDebugEnabled()) logger.debug("original request body = " + s);
objMap.put("requestBody", s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
if (logger.isDebugEnabled()) logger.trace("ResponseTransformerInterceptor.handleRequest starts.");
String requestPath = exchange.getRequestPath();
if (config.getAppliedPathPrefixes() != null && config.getAppliedPathPrefixes().stream().anyMatch(requestPath::startsWith)) {
String responseBody = BuffersUtils.toString(getBuffer(exchange), StandardCharsets.UTF_8);
String responseBody = BuffersUtils.toString(getBuffer(exchange), config.getDefaultBodyEncoding());
if (logger.isTraceEnabled())
logger.trace("original response body = " + responseBody);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ public class ResponseTransformerConfig {

private static final String ENABLED = "enabled";
private static final String REQUIRED_CONTENT = "requiredContent";
private static final String DEFAULT_BODY_ENCODING = "defaultBodyEncoding";
private static final String APPLIED_PATH_PREFIXES = "appliedPathPrefixes";

private Map<String, Object> mappedConfig;
private Config config;
private boolean enabled;
private boolean requiredContent;
private String defaultBodyEncoding;
List<String> appliedPathPrefixes;

private ResponseTransformerConfig() {
Expand Down Expand Up @@ -55,6 +57,7 @@ public boolean isEnabled() {
return enabled;
}
public boolean isRequiredContent() { return requiredContent; }
public String getDefaultBodyEncoding() { return defaultBodyEncoding; }
public List<String> getAppliedPathPrefixes() {
return appliedPathPrefixes;
}
Expand Down Expand Up @@ -84,6 +87,8 @@ private void setConfigData() {
throw new ConfigException("requiredContent must be a boolean value.");
}
}
object = mappedConfig.get(DEFAULT_BODY_ENCODING);
if (object != null) defaultBodyEncoding = (String) object;
}

private void setConfigList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
enabled: ${response-transformer.enabled:true}
# indicate if the transformer needs to modify the response body in the transform rules.
requiredContent: ${response-transformer.requiredContent:true}
# default body encoding for the response body. The default value is UTF-8. Other options are ISO-8859-1 and US-ASCII.
defaultBodyEncoding: ${response-transformer.defaultBodyEncoding:UTF-8}
# A list of applied request path prefixes, other requests will skip this handler. The value can be a string
# if there is only one request path prefix needs this handler. or a list of strings if there are multiple.
appliedPathPrefixes: ${response-transformer.appliedPathPrefixes:}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class ResponseTransformerConfigTest {
public void testConfigLoad() {
ResponseTransformerConfig config = ResponseTransformerConfig.load();
Assert.assertTrue(config.getMappedConfig().size() > 0);
Assert.assertEquals(config.getDefaultBodyEncoding(), "UTF-8");
}

}