support generating XML response according to response header content-type #114
This commit is contained in:
@@ -49,6 +49,7 @@ import we.schema.util.PropertiesSupportUtils;
|
|||||||
import we.util.JacksonUtils;
|
import we.util.JacksonUtils;
|
||||||
import we.util.JsonSchemaUtils;
|
import we.util.JsonSchemaUtils;
|
||||||
import we.util.MapUtil;
|
import we.util.MapUtil;
|
||||||
|
import we.xml.JsonToXml;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -58,6 +59,7 @@ import we.util.MapUtil;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class Pipeline {
|
public class Pipeline {
|
||||||
|
private static final String CONTENT_TYPE_XML = "application/xml";
|
||||||
private ConfigurableApplicationContext applicationContext;
|
private ConfigurableApplicationContext applicationContext;
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(Pipeline.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(Pipeline.class);
|
||||||
private LinkedList<Step> steps = new LinkedList<Step>();
|
private LinkedList<Step> steps = new LinkedList<Step>();
|
||||||
@@ -230,19 +232,24 @@ public class Pipeline {
|
|||||||
group.put("response", response);
|
group.put("response", response);
|
||||||
}
|
}
|
||||||
response = group.get("response");
|
response = group.get("response");
|
||||||
|
String respContentType = null;
|
||||||
if (input != null && input.getConfig() != null && input.getConfig().getDataMapping() != null) {
|
if (input != null && input.getConfig() != null && input.getConfig().getDataMapping() != null) {
|
||||||
Map<String, Object> responseMapping = (Map<String, Object>) input.getConfig().getDataMapping()
|
Map<String, Object> responseMapping = (Map<String, Object>) input.getConfig().getDataMapping()
|
||||||
.get("response");
|
.get("response");
|
||||||
if (validateResponse != null) {
|
if (validateResponse != null) {
|
||||||
responseMapping = validateResponse;
|
responseMapping = validateResponse;
|
||||||
}
|
}
|
||||||
if (responseMapping != null && !StringUtils.isEmpty(responseMapping)) {
|
if (!CollectionUtils.isEmpty(responseMapping)) {
|
||||||
|
respContentType = (String) responseMapping.get("contentType");
|
||||||
ONode ctxNode = PathMapping.toONode(stepContext);
|
ONode ctxNode = PathMapping.toONode(stepContext);
|
||||||
|
|
||||||
// headers
|
// headers
|
||||||
Map<String, Object> headers = PathMapping.transform(ctxNode, stepContext,
|
Map<String, Object> headers = PathMapping.transform(ctxNode, stepContext,
|
||||||
MapUtil.upperCaseKey((Map<String, Object>) responseMapping.get("fixedHeaders")),
|
MapUtil.upperCaseKey((Map<String, Object>) responseMapping.get("fixedHeaders")),
|
||||||
MapUtil.upperCaseKey((Map<String, Object>) responseMapping.get("headers")), false);
|
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)
|
if (headers.containsKey(CommonConstants.WILDCARD_TILDE)
|
||||||
&& headers.get(CommonConstants.WILDCARD_TILDE) instanceof Map) {
|
&& headers.get(CommonConstants.WILDCARD_TILDE) instanceof Map) {
|
||||||
response.put("headers", headers.get(CommonConstants.WILDCARD_TILDE));
|
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");
|
Object respBody = response.get("body");
|
||||||
// 测试模式返回StepContext
|
// 测试模式返回StepContext
|
||||||
if (stepContext.returnContext() && respBody instanceof Map) {
|
if (stepContext.returnContext() && respBody instanceof Map) {
|
||||||
|
|||||||
@@ -78,6 +78,7 @@ public class RequestInput extends RPCInput implements IInput{
|
|||||||
private static final Integer SERVICE_TYPE_HTTP = 2;
|
private static final Integer SERVICE_TYPE_HTTP = 2;
|
||||||
|
|
||||||
private String respContentType;
|
private String respContentType;
|
||||||
|
private String reqContentType;
|
||||||
|
|
||||||
private String[] xmlArrPaths;
|
private String[] xmlArrPaths;
|
||||||
|
|
||||||
@@ -120,6 +121,7 @@ public class RequestInput extends RPCInput implements IInput{
|
|||||||
if (dataMapping != null) {
|
if (dataMapping != null) {
|
||||||
Map<String, Object> requestMapping = (Map<String, Object>) dataMapping.get("request");
|
Map<String, Object> requestMapping = (Map<String, Object>) dataMapping.get("request");
|
||||||
if (!CollectionUtils.isEmpty(requestMapping)) {
|
if (!CollectionUtils.isEmpty(requestMapping)) {
|
||||||
|
reqContentType = (String) requestMapping.get("contentType");
|
||||||
ONode ctxNode = PathMapping.toONode(stepContext);
|
ONode ctxNode = PathMapping.toONode(stepContext);
|
||||||
|
|
||||||
// headers
|
// headers
|
||||||
@@ -309,8 +311,8 @@ public class RequestInput extends RPCInput implements IInput{
|
|||||||
headers.remove(CommonConstants.HEADER_CONTENT_LENGTH);
|
headers.remove(CommonConstants.HEADER_CONTENT_LENGTH);
|
||||||
headers.add(CommonConstants.HEADER_TRACE_ID, inputContext.getStepContext().getTraceId());
|
headers.add(CommonConstants.HEADER_TRACE_ID, inputContext.getStepContext().getTraceId());
|
||||||
|
|
||||||
// convert JSON to XML if it has XML content-type header
|
// convert JSON to XML if it is XML content type
|
||||||
if (CONTENT_TYPE_XML.equals(headers.getFirst(CommonConstants.HEADER_CONTENT_TYPE))) {
|
if (CONTENT_TYPE_XML.equals(reqContentType)) {
|
||||||
request.put("jsonBody", request.get("body"));
|
request.put("jsonBody", request.get("body"));
|
||||||
JsonToXml jsonToXml = new JsonToXml.Builder(body).build();
|
JsonToXml jsonToXml = new JsonToXml.Builder(body).build();
|
||||||
body = jsonToXml.toString();
|
body = jsonToXml.toString();
|
||||||
|
|||||||
Reference in New Issue
Block a user