Use the user-configured Content-Type preferentially #203

This commit is contained in:
Francis Dong
2021-05-19 17:27:55 +08:00
committed by dxfeng10
parent 0b9c22d82b
commit b993945364
2 changed files with 20 additions and 6 deletions

View File

@@ -27,6 +27,8 @@ import javax.script.ScriptException;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.util.Pair; import org.springframework.data.util.Pair;
import org.springframework.http.HttpHeaders;
import we.schema.util.I18nUtils; import we.schema.util.I18nUtils;
import org.noear.snack.ONode; import org.noear.snack.ONode;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -282,9 +284,6 @@ public class Pipeline {
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));
@@ -317,6 +316,12 @@ public class Pipeline {
} }
} }
HttpHeaders httpHeaders = MapUtil.toHttpHeaders((Map<String, Object>) response.get("headers"));
if (CONTENT_TYPE_XML.equals(respContentType) && !httpHeaders.containsKey(CommonConstants.HEADER_CONTENT_TYPE)) {
httpHeaders.add(CommonConstants.HEADER_CONTENT_TYPE.toUpperCase(), CONTENT_TYPE_XML);
response.put(CommonConstants.HEADER_CONTENT_TYPE.toUpperCase(), CONTENT_TYPE_XML);
}
// convert JSON to XML if it is XML content type // convert JSON to XML if it is XML content type
if(CONTENT_TYPE_XML.equals(respContentType)) { if(CONTENT_TYPE_XML.equals(respContentType)) {
Object respBody = response.get("body"); Object respBody = response.get("body");
@@ -333,7 +338,7 @@ public class Pipeline {
} }
aggResult.setBody(response.get("body")); aggResult.setBody(response.get("body"));
aggResult.setHeaders(MapUtil.toMultiValueMap((Map<String, Object>) response.get("headers"))); aggResult.setHeaders(httpHeaders);
return aggResult; return aggResult;
} }

View File

@@ -73,6 +73,7 @@ public class RequestInput extends RPCInput implements IInput{
private static final String CONTENT_TYPE_JSON = "application/json"; private static final String CONTENT_TYPE_JSON = "application/json";
private static final String CONTENT_TYPE_XML = "application/xml"; private static final String CONTENT_TYPE_XML = "application/xml";
private static final String CONTENT_TYPE_TEXT_XML = "text/xml";
private static final String CONTENT_TYPE_JS = "application/javascript"; private static final String CONTENT_TYPE_JS = "application/javascript";
private static final String CONTENT_TYPE_HTML = "text/html"; private static final String CONTENT_TYPE_HTML = "text/html";
private static final String CONTENT_TYPE_TEXT = "text/plain"; private static final String CONTENT_TYPE_TEXT = "text/plain";
@@ -325,7 +326,11 @@ public class RequestInput extends RPCInput implements IInput{
if (!headers.containsKey(CommonConstants.HEADER_CONTENT_TYPE)) { if (!headers.containsKey(CommonConstants.HEADER_CONTENT_TYPE)) {
// default content-type // default content-type
headers.add(CommonConstants.HEADER_CONTENT_TYPE, CommonConstants.CONTENT_TYPE_JSON); if (CONTENT_TYPE_XML.equals(reqContentType) || CONTENT_TYPE_TEXT_XML.equals(reqContentType)) {
headers.add(CommonConstants.HEADER_CONTENT_TYPE, CONTENT_TYPE_XML);
} else {
headers.add(CommonConstants.HEADER_CONTENT_TYPE, CommonConstants.CONTENT_TYPE_JSON);
}
} }
// add default headers // add default headers
@@ -340,11 +345,14 @@ public class RequestInput extends RPCInput implements IInput{
headers.add(CommonConstants.HEADER_TRACE_ID, inputContext.getStepContext().getTraceId()); headers.add(CommonConstants.HEADER_TRACE_ID, inputContext.getStepContext().getTraceId());
// convert JSON to XML if it is XML content type // convert JSON to XML if it is XML content type
if (CONTENT_TYPE_XML.equals(reqContentType)) { if (CONTENT_TYPE_XML.equals(reqContentType) || CONTENT_TYPE_TEXT_XML.equals(reqContentType)) {
request.put("jsonBody", request.get("body")); request.put("jsonBody", request.get("body"));
LOGGER.info("jsonBody={}", JSON.toJSONString(request.get("body")));
JsonToXml jsonToXml = new JsonToXml.Builder(body).build(); JsonToXml jsonToXml = new JsonToXml.Builder(body).build();
body = jsonToXml.toString(); body = jsonToXml.toString();
request.put("body", body); request.put("body", body);
LOGGER.info("body={}", body);
LOGGER.info("headers={}", JSON.toJSONString(headers));
} }
HttpMethod aggrMethod = HttpMethod.valueOf(inputContext.getStepContext().getInputReqAttr("method").toString()); HttpMethod aggrMethod = HttpMethod.valueOf(inputContext.getStepContext().getInputReqAttr("method").toString());
@@ -423,6 +431,7 @@ public class RequestInput extends RPCInput implements IInput{
} }
break; break;
case CONTENT_TYPE_XML: case CONTENT_TYPE_XML:
case CONTENT_TYPE_TEXT_XML:
Builder builder = new XmlToJson.Builder(responseBody); Builder builder = new XmlToJson.Builder(responseBody);
if (this.xmlArrPaths != null && this.xmlArrPaths.length > 0) { if (this.xmlArrPaths != null && this.xmlArrPaths.length > 0) {
for (int j = 0; j < this.xmlArrPaths.length; j++) { for (int j = 0; j < this.xmlArrPaths.length; j++) {