support XML input in gateway aggregation #115

This commit is contained in:
Francis Dong
2021-04-19 11:52:55 +08:00
committed by dxfeng10
parent 5e103ed574
commit e71be5f9c8
4 changed files with 71 additions and 13 deletions

View File

@@ -135,14 +135,14 @@ public class AggregateFilter implements WebFilter {
clientInput.put("method", method);
clientInput.put("headers", headers);
clientInput.put("params", MapUtil.toHashMap(request.getQueryParams()));
clientInput.put("contentType", request.getHeaders().getFirst(CommonConstants.HEADER_CONTENT_TYPE));
Mono<AggregateResult> result = null;
if (HttpMethod.POST.name().equalsIgnoreCase(method)) {
result = DataBufferUtils.join(request.getBody()).defaultIfEmpty(emptyBody).flatMap(buf -> {
if(buf != null && buf != emptyBody) {
try {
clientInput.put("body", JSON.parse(buf.toString(StandardCharsets.UTF_8)));
clientInput.put("body", buf.toString(StandardCharsets.UTF_8));
} finally {
DataBufferUtils.release(buf);
}

View File

@@ -135,6 +135,8 @@ public class ConfigLoader {
clientInputConfig.setParamsDef(cfgNode.select("$.paramsDef").toObject(Map.class));
clientInputConfig.setScriptValidate(cfgNode.select("$.scriptValidate").toObject(Map.class));
clientInputConfig.setValidateResponse(cfgNode.select("$.validateResponse").toObject(Map.class));
clientInputConfig.setContentType(cfgNode.select("$.contentType").getString());
clientInputConfig.setXmlArrPaths(cfgNode.select("$.xmlArrPaths").getString());
input.setConfig(clientInputConfig);
return input;
}

View File

@@ -50,6 +50,8 @@ import we.util.JacksonUtils;
import we.util.JsonSchemaUtils;
import we.util.MapUtil;
import we.xml.JsonToXml;
import we.xml.XmlToJson;
import we.xml.XmlToJson.Builder;
/**
*
@@ -78,7 +80,7 @@ public class Pipeline {
public Mono<AggregateResult> run(Input input, Map<String, Object> clientInput, String traceId) {
ClientInputConfig config = (ClientInputConfig)input.getConfig();
this.initialStepContext(clientInput);
this.initialStepContext(clientInput, config);
this.stepContext.setDebug(config.isDebug());
this.stepContext.setApplicationContext(applicationContext);
@@ -141,7 +143,7 @@ public class Pipeline {
return Mono.just(aggResult);
}
@SuppressWarnings("unchecked")
@SuppressWarnings({ "unchecked", "rawtypes" })
public Mono<StepResponse> createStep(Step step) {
long start = System.currentTimeMillis();
List<Mono> monos = step.run();
@@ -168,7 +170,7 @@ public class Pipeline {
* 初始化上下文
* @param clientInput 客户端提交上来的信息
*/
public void initialStepContext(Map<String,Object> clientInput) {
public void initialStepContext(Map<String,Object> clientInput, ClientInputConfig config) {
Map<String,Object> input = new HashMap<>();
Map<String,Object> inputRequest = new HashMap<>();
Map<String,Object> inputResponse = new HashMap<>();
@@ -179,12 +181,41 @@ public class Pipeline {
inputRequest.put("method", clientInput.get("method"));
inputRequest.put("headers", clientInput.get("headers"));
inputRequest.put("params", clientInput.get("params"));
inputRequest.put("body", clientInput.get("body"));
if (CONTENT_TYPE_XML.equals(config.getContentType()) || (StringUtils.isEmpty(config.getContentType())
&& isXmlContentType((String) clientInput.get("contentType")))) {
String[] paths = null;
if (!StringUtils.isEmpty(config.getXmlArrPaths())) {
paths = config.getXmlArrPaths().split(",");
}
Builder builder = new XmlToJson.Builder((String) clientInput.get("body"));
if (paths != null && paths.length > 0) {
for (int j = 0; j < paths.length; j++) {
String p = paths[j];
builder = builder.forceList(p);
}
}
inputRequest.put("body", builder.build().toJson().toMap());
} else {
inputRequest.put("body", JSON.parse((String) clientInput.get("body")));
}
}
stepContext.put("input", input);
}
private boolean isXmlContentType(String contentType) {
if (contentType != null) {
String[] cts = contentType.split(";");
for (int i = 0; i < cts.length; i++) {
if (CONTENT_TYPE_XML.equals(cts[i])) {
return true;
}
}
}
return false;
}
@SuppressWarnings("unchecked")
private StepResponse doStepDataMapping(Step step) {
StepResponse stepResponse = (StepResponse)stepContext.get(step.getName());
if (step.getDataMapping() != null) {

View File

@@ -38,8 +38,10 @@ public class ClientInputConfig extends InputConfig {
private Map<String, Object> paramsDef;
private Map<String, Object> scriptValidate;
private Map<String, Object> validateResponse;
private String contentType;
private String xmlArrPaths;
@SuppressWarnings("unchecked")
@SuppressWarnings({ "unchecked", "rawtypes" })
public ClientInputConfig(Map configBody) {
super(configBody);
if(configBody.get("debug") != null) {
@@ -56,22 +58,28 @@ public class ClientInputConfig extends InputConfig {
}
if (configBody.get("langDef") != null) {
langDef = ((Map) configBody.get("langDef"));
langDef = (Map) configBody.get("langDef");
}
if (configBody.get("bodyDef") != null) {
bodyDef = ((Map) configBody.get("bodyDef"));
bodyDef = (Map) configBody.get("bodyDef");
}
if (configBody.get("paramsDef") != null) {
paramsDef = ((Map) configBody.get("paramsDef"));
paramsDef = (Map) configBody.get("paramsDef");
}
if (configBody.get("headersDef") != null) {
headersDef = ((Map) configBody.get("headersDef"));
headersDef = (Map) configBody.get("headersDef");
}
if (configBody.get("scriptValidate") != null) {
scriptValidate = ((Map) configBody.get("scriptValidate"));
scriptValidate = (Map) configBody.get("scriptValidate");
}
if (configBody.get("validateResponse") != null) {
validateResponse = ((Map) configBody.get("validateResponse"));
validateResponse = (Map) configBody.get("validateResponse");
}
if (configBody.get("contentType") != null) {
contentType = (String) configBody.get("contentType");
}
if (configBody.get("xmlArrPaths") != null) {
xmlArrPaths = (String) configBody.get("xmlArrPaths");
}
}
@@ -159,4 +167,21 @@ public class ClientInputConfig extends InputConfig {
public void setValidateResponse(Map<String, Object> validateResponse) {
this.validateResponse = validateResponse;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getXmlArrPaths() {
return xmlArrPaths;
}
public void setXmlArrPaths(String xmlArrPaths) {
this.xmlArrPaths = xmlArrPaths;
}
}