com.networknt.rule.encoder.ResponseBodyUtf8EncodingTransformAction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of body-encoder Show documentation
Show all versions of body-encoder Show documentation
A yaml rule action implementation to change the encoding from one to another for request body and response body.
The newest version!
package com.networknt.rule.encoder;
import com.networknt.rule.ResponseTransformAction;
import com.networknt.rule.RuleActionValue;
import com.networknt.rule.RuleConstants;
import com.networknt.utility.ModuleRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.Map;
/**
* This action is used to transform the response body encoding from one encoding to UTF-8 encoding.
* As the ResponseTransformInterceptor has already converted the body stream into UTF-8 encoding, here
* we just need to change the declaration in the body string if it is an XML string.
*
* @author Steve Hu
*/
public class ResponseBodyUtf8EncodingTransformAction implements ResponseTransformAction {
private static final Logger logger = LoggerFactory.getLogger(ResponseBodyUtf8EncodingTransformAction.class);
public static final String RESPONSE_BODY = "responseBody";
public static final String UTF8 = "UTF-8";
public ResponseBodyUtf8EncodingTransformAction() {
if(logger.isInfoEnabled()) logger.info("ResponseBodyUtf8EncodingTransformAction is constructed");
ModuleRegistry.registerPlugin(
ResponseBodyUtf8EncodingTransformAction.class.getPackage().getImplementationTitle(),
ResponseBodyUtf8EncodingTransformAction.class.getPackage().getImplementationVersion(),
null,
ResponseBodyUtf8EncodingTransformAction.class.getName(),
null,
null);
}
@Override
public void performAction(String ruleId, String actionId, Map objMap, Map resultMap, Collection actionValues) {
// get the body from the objMap and create a new body in the resultMap. Both in string format.
String responseBody = (String)objMap.get(RESPONSE_BODY);
if(logger.isTraceEnabled()) logger.debug("ruleId: {} actionId {} original response body = {}", ruleId, actionId, responseBody);
// replace XML encoding declaration if it is for XML strings.
if(responseBody.startsWith(" © 2015 - 2025 Weber Informatics LLC | Privacy Policy