support generating XML response according to response header content-type #114

This commit is contained in:
Francis Dong
2021-04-16 17:30:41 +08:00
committed by dxfeng10
parent 9c72aa0787
commit 5e103ed574
2 changed files with 20 additions and 3 deletions

View File

@@ -49,6 +49,7 @@ import we.schema.util.PropertiesSupportUtils;
import we.util.JacksonUtils;
import we.util.JsonSchemaUtils;
import we.util.MapUtil;
import we.xml.JsonToXml;
/**
*
@@ -58,6 +59,7 @@ import we.util.MapUtil;
*
*/
public class Pipeline {
private static final String CONTENT_TYPE_XML = "application/xml";
private ConfigurableApplicationContext applicationContext;
private static final Logger LOGGER = LoggerFactory.getLogger(Pipeline.class);
private LinkedList<Step> steps = new LinkedList<Step>();
@@ -230,19 +232,24 @@ public class Pipeline {
group.put("response", response);
}
response = group.get("response");
String respContentType = null;
if (input != null && input.getConfig() != null && input.getConfig().getDataMapping() != null) {
Map<String, Object> responseMapping = (Map<String, Object>) input.getConfig().getDataMapping()
.get("response");
if (validateResponse != null) {
responseMapping = validateResponse;
}
if (responseMapping != null && !StringUtils.isEmpty(responseMapping)) {
if (!CollectionUtils.isEmpty(responseMapping)) {
respContentType = (String) responseMapping.get("contentType");
ONode ctxNode = PathMapping.toONode(stepContext);
// headers
Map<String, Object> headers = PathMapping.transform(ctxNode, stepContext,
MapUtil.upperCaseKey((Map<String, Object>) responseMapping.get("fixedHeaders")),
MapUtil.upperCaseKey((Map<String, Object>) responseMapping.get("headers")), false);
if(CONTENT_TYPE_XML.equals(respContentType)) {
headers.put(CommonConstants.HEADER_CONTENT_TYPE.toUpperCase(), CONTENT_TYPE_XML);
}
if (headers.containsKey(CommonConstants.WILDCARD_TILDE)
&& headers.get(CommonConstants.WILDCARD_TILDE) instanceof Map) {
response.put("headers", headers.get(CommonConstants.WILDCARD_TILDE));
@@ -275,6 +282,14 @@ public class Pipeline {
}
}
// convert JSON to XML if it is XML content type
if(CONTENT_TYPE_XML.equals(respContentType)) {
Object respBody = response.get("body");
response.put("jsonBody", respBody);
JsonToXml jsonToXml = new JsonToXml.Builder(JSON.toJSONString(respBody)).build();
response.put("body", jsonToXml.toString());
}
Object respBody = response.get("body");
// 测试模式返回StepContext
if (stepContext.returnContext() && respBody instanceof Map) {

View File

@@ -78,6 +78,7 @@ public class RequestInput extends RPCInput implements IInput{
private static final Integer SERVICE_TYPE_HTTP = 2;
private String respContentType;
private String reqContentType;
private String[] xmlArrPaths;
@@ -120,6 +121,7 @@ public class RequestInput extends RPCInput implements IInput{
if (dataMapping != null) {
Map<String, Object> requestMapping = (Map<String, Object>) dataMapping.get("request");
if (!CollectionUtils.isEmpty(requestMapping)) {
reqContentType = (String) requestMapping.get("contentType");
ONode ctxNode = PathMapping.toONode(stepContext);
// headers
@@ -309,8 +311,8 @@ public class RequestInput extends RPCInput implements IInput{
headers.remove(CommonConstants.HEADER_CONTENT_LENGTH);
headers.add(CommonConstants.HEADER_TRACE_ID, inputContext.getStepContext().getTraceId());
// convert JSON to XML if it has XML content-type header
if (CONTENT_TYPE_XML.equals(headers.getFirst(CommonConstants.HEADER_CONTENT_TYPE))) {
// convert JSON to XML if it is XML content type
if (CONTENT_TYPE_XML.equals(reqContentType)) {
request.put("jsonBody", request.get("body"));
JsonToXml jsonToXml = new JsonToXml.Builder(body).build();
body = jsonToXml.toString();