Merge pull request #439 from wehotel/develop

This commit is contained in:
hongqiaowei
2022-07-05 15:19:01 +08:00
committed by GitHub
8 changed files with 152 additions and 130 deletions

View File

@@ -12,7 +12,7 @@
<groupId>com.fizzgate</groupId> <groupId>com.fizzgate</groupId>
<artifactId>fizz-bootstrap</artifactId> <artifactId>fizz-bootstrap</artifactId>
<version>2.6.5</version> <version>2.6.6-beta1</version>
<properties> <properties>
<java.version>1.8</java.version> <java.version>1.8</java.version>

View File

@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>fizz-gateway-community</artifactId> <artifactId>fizz-gateway-community</artifactId>
<groupId>com.fizzgate</groupId> <groupId>com.fizzgate</groupId>
<version>2.6.5</version> <version>2.6.6-beta1</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>fizz-gateway-community</artifactId> <artifactId>fizz-gateway-community</artifactId>
<groupId>com.fizzgate</groupId> <groupId>com.fizzgate</groupId>
<version>2.6.5</version> <version>2.6.6-beta1</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@@ -18,6 +18,7 @@
package we.fizz.input; package we.fizz.input;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.script.ScriptException; import javax.script.ScriptException;
@@ -42,8 +43,8 @@ public class RPCInput extends Input {
protected static final Logger LOGGER = LoggerFactory.getLogger(RPCInput.class.getName()); protected static final Logger LOGGER = LoggerFactory.getLogger(RPCInput.class.getName());
protected static final String FALLBACK_MODE_STOP = "stop"; protected static final String FALLBACK_MODE_STOP = "stop";
protected static final String FALLBACK_MODE_CONTINUE = "continue"; protected static final String FALLBACK_MODE_CONTINUE = "continue";
protected Map<String, Object> request = new HashMap<>(); protected Map<String, Object> request = new ConcurrentHashMap<>();
protected Map<String, Object> response = new HashMap<>(); protected Map<String, Object> response = new ConcurrentHashMap<>();
protected void doRequestMapping(InputConfig aConfig, InputContext inputContext) { protected void doRequestMapping(InputConfig aConfig, InputContext inputContext) {

View File

@@ -121,80 +121,84 @@ public class RequestInput extends RPCInput implements IInput{
protected void doRequestMapping(InputConfig aConfig, InputContext inputContext) { protected void doRequestMapping(InputConfig aConfig, InputContext inputContext) {
RequestInputConfig config = (RequestInputConfig) aConfig; RequestInputConfig config = (RequestInputConfig) aConfig;
// 把请求信息放入stepContext
Map<String, Object> group = new HashMap<>();
group.put("request", request);
group.put("response", response);
this.stepResponse.addRequest(name, group);
HttpMethod method = HttpMethod.valueOf(config.getMethod().toUpperCase());
request.put("method", method);
Map<String, Object> params = new HashMap<>(); Map<String, Object> params = new HashMap<>();
params.putAll(MapUtil.toHashMap(config.getQueryParams())); synchronized (inputContext.getStepContext()) {
request.put("params", params); // 把请求信息放入stepContext
Map<String, Object> group = new HashMap<>();
group.put("request", request);
group.put("response", response);
this.stepResponse.addRequest(name, group);
HttpMethod method = HttpMethod.valueOf(config.getMethod().toUpperCase());
request.put("method", method);
params.putAll(MapUtil.toHashMap(config.getQueryParams()));
request.put("params", params);
}
ONode ctxNode = null; ONode ctxNode = null;
// 数据转换 // 数据转换
if (inputContext != null && inputContext.getStepContext() != null) { if (inputContext != null && inputContext.getStepContext() != null) {
StepContext<String, Object> stepContext = inputContext.getStepContext(); StepContext<String, Object> stepContext = inputContext.getStepContext();
ctxNode = PathMapping.toONode(stepContext); ctxNode = PathMapping.toONode(stepContext);
Map<String, Object> dataMapping = this.getConfig().getDataMapping(); synchronized (stepContext) {
if (dataMapping != null) { Map<String, Object> dataMapping = this.getConfig().getDataMapping();
Map<String, Object> requestMapping = (Map<String, Object>) dataMapping.get("request"); if (dataMapping != null) {
if (!CollectionUtils.isEmpty(requestMapping)) { Map<String, Object> requestMapping = (Map<String, Object>) dataMapping.get("request");
reqContentType = (String) requestMapping.get("contentType"); if (!CollectionUtils.isEmpty(requestMapping)) {
reqContentType = (String) requestMapping.get("contentType");
// headers
Map<String, Object> headers = PathMapping.transform(ctxNode, stepContext, // headers
MapUtil.upperCaseKey(MapUtil.list2Map(requestMapping.get("fixedHeaders"))), Map<String, Object> headers = PathMapping.transform(ctxNode, stepContext,
MapUtil.upperCaseKey(MapUtil.list2Map(requestMapping.get("headers"))), false); MapUtil.upperCaseKey(MapUtil.list2Map(requestMapping.get("fixedHeaders"))),
if (headers.containsKey(CommonConstants.WILDCARD_TILDE) MapUtil.upperCaseKey(MapUtil.list2Map(requestMapping.get("headers"))), false);
&& headers.get(CommonConstants.WILDCARD_TILDE) instanceof Map) { if (headers.containsKey(CommonConstants.WILDCARD_TILDE)
request.put("headers", headers.get(CommonConstants.WILDCARD_TILDE)); && headers.get(CommonConstants.WILDCARD_TILDE) instanceof Map) {
} else { request.put("headers", headers.get(CommonConstants.WILDCARD_TILDE));
request.put("headers", headers); } else {
} request.put("headers", headers);
}
// params
params.putAll(PathMapping.transform(ctxNode, stepContext, // params
MapUtil.list2Map(requestMapping.get("fixedParams")), params.putAll(PathMapping.transform(ctxNode, stepContext,
MapUtil.list2Map(requestMapping.get("params")), false)); MapUtil.list2Map(requestMapping.get("fixedParams")),
if (params.containsKey(CommonConstants.WILDCARD_TILDE) MapUtil.list2Map(requestMapping.get("params")), false));
&& params.get(CommonConstants.WILDCARD_TILDE) instanceof Map) { if (params.containsKey(CommonConstants.WILDCARD_TILDE)
request.put("params", params.get(CommonConstants.WILDCARD_TILDE)); && params.get(CommonConstants.WILDCARD_TILDE) instanceof Map) {
} else { request.put("params", params.get(CommonConstants.WILDCARD_TILDE));
request.put("params", params); } else {
} request.put("params", params);
}
// body
boolean supportMultiLevels = true; // body
if (CONTENT_TYPE_MULTIPART_FORM_DATA.equals(reqContentType) || boolean supportMultiLevels = true;
CONTENT_TYPE_FORM_URLENCODED.equals(reqContentType)) { if (CONTENT_TYPE_MULTIPART_FORM_DATA.equals(reqContentType) ||
supportMultiLevels = false; CONTENT_TYPE_FORM_URLENCODED.equals(reqContentType)) {
} supportMultiLevels = false;
Map<String,Object> body = PathMapping.transform(ctxNode, stepContext, }
MapUtil.list2Map(requestMapping.get("fixedBody")), Map<String,Object> body = PathMapping.transform(ctxNode, stepContext,
MapUtil.list2Map(requestMapping.get("body")), supportMultiLevels); MapUtil.list2Map(requestMapping.get("fixedBody")),
if (body.containsKey(CommonConstants.WILDCARD_TILDE)) { MapUtil.list2Map(requestMapping.get("body")), supportMultiLevels);
request.put("body", body.get(CommonConstants.WILDCARD_TILDE)); if (body.containsKey(CommonConstants.WILDCARD_TILDE)) {
} else { request.put("body", body.get(CommonConstants.WILDCARD_TILDE));
// script } else {
if (requestMapping.get("script") != null) { // script
Map<String, Object> scriptCfg = (Map<String, Object>) requestMapping.get("script"); if (requestMapping.get("script") != null) {
try { Map<String, Object> scriptCfg = (Map<String, Object>) requestMapping.get("script");
Object reqBody = ScriptHelper.execute(scriptCfg, ctxNode, stepContext); try {
if (reqBody != null) { Object reqBody = ScriptHelper.execute(scriptCfg, ctxNode, stepContext);
body.putAll((Map<String, Object>) reqBody); if (reqBody != null) {
} body.putAll((Map<String, Object>) reqBody);
} catch (ScriptException e) { }
LogService.setBizId(inputContext.getStepContext().getTraceId()); } catch (ScriptException e) {
LOGGER.warn("execute script failed, {}", JacksonUtils.writeValueAsString(scriptCfg), e); LogService.setBizId(inputContext.getStepContext().getTraceId());
throw new ExecuteScriptException(e, stepContext, scriptCfg); LOGGER.warn("execute script failed, {}", JacksonUtils.writeValueAsString(scriptCfg), e);
} throw new ExecuteScriptException(e, stepContext, scriptCfg);
}
}
request.put("body", body);
} }
request.put("body", body);
} }
} }
} }
@@ -223,11 +227,15 @@ public class RequestInput extends RPCInput implements IInput{
UriComponents uriComponents = UriComponentsBuilder.fromUriString(sb.toString()) UriComponents uriComponents = UriComponentsBuilder.fromUriString(sb.toString())
.queryParams(MapUtil.toMultiValueMap(params)).build(); .queryParams(MapUtil.toMultiValueMap(params)).build();
request.put("url", uriComponents.toUriString()); synchronized (inputContext.getStepContext()) {
request.put("url", uriComponents.toUriString());
}
} else { } else {
UriComponents uriComponents = UriComponentsBuilder.fromUriString(config.getBaseUrl() + setPathVariable(ctxNode, config.getPath())) UriComponents uriComponents = UriComponentsBuilder.fromUriString(config.getBaseUrl() + setPathVariable(ctxNode, config.getPath()))
.queryParams(MapUtil.toMultiValueMap(params)).build(); .queryParams(MapUtil.toMultiValueMap(params)).build();
request.put("url", uriComponents.toUriString()); synchronized (inputContext.getStepContext()) {
request.put("url", uriComponents.toUriString());
}
} }
} }
@@ -277,56 +285,59 @@ public class RequestInput extends RPCInput implements IInput{
ct = CONTENT_TYPE_JSON; ct = CONTENT_TYPE_JSON;
} }
response.put("body", this.parseBody(ct, (String)responseBody)); synchronized (inputContext.getStepContext()) {
response.put("body", this.parseBody(ct, (String)responseBody));
}
// 数据转换 // 数据转换
if (inputContext != null && inputContext.getStepContext() != null) { if (inputContext != null && inputContext.getStepContext() != null) {
StepContext<String, Object> stepContext = inputContext.getStepContext(); StepContext<String, Object> stepContext = inputContext.getStepContext();
if (!CollectionUtils.isEmpty(responseMapping)) { if (!CollectionUtils.isEmpty(responseMapping)) {
ONode ctxNode = PathMapping.toONode(stepContext); ONode ctxNode = PathMapping.toONode(stepContext);
synchronized (stepContext) {
// headers // headers
Map<String, Object> fixedHeaders = MapUtil.upperCaseKey((Map<String, Object>) responseMapping.get("fixedHeaders")); Map<String, Object> fixedHeaders = MapUtil.upperCaseKey((Map<String, Object>) responseMapping.get("fixedHeaders"));
Map<String, Object> headerMapping = MapUtil.upperCaseKey((Map<String, Object>) responseMapping.get("headers")); Map<String, Object> headerMapping = MapUtil.upperCaseKey((Map<String, Object>) responseMapping.get("headers"));
if ((fixedHeaders != null && !fixedHeaders.isEmpty()) if ((fixedHeaders != null && !fixedHeaders.isEmpty())
|| (headerMapping != null && !headerMapping.isEmpty())) { || (headerMapping != null && !headerMapping.isEmpty())) {
Map<String, Object> headers = new HashMap<>(); Map<String, Object> headers = new HashMap<>();
headers.putAll(PathMapping.transform(ctxNode, stepContext, fixedHeaders, headerMapping, false)); headers.putAll(PathMapping.transform(ctxNode, stepContext, fixedHeaders, headerMapping, false));
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));
} else { } else {
response.put("headers", headers); response.put("headers", headers);
} }
} }
// body // body
Map<String, Object> fixedBody = (Map<String, Object>) responseMapping.get("fixedBody"); Map<String, Object> fixedBody = (Map<String, Object>) responseMapping.get("fixedBody");
Map<String, Object> bodyMapping = (Map<String, Object>) responseMapping.get("body"); Map<String, Object> bodyMapping = (Map<String, Object>) responseMapping.get("body");
Map<String, Object> scriptCfg = (Map<String, Object>) responseMapping.get("script"); Map<String, Object> scriptCfg = (Map<String, Object>) responseMapping.get("script");
if ((fixedBody != null && !fixedBody.isEmpty()) || (bodyMapping != null && !bodyMapping.isEmpty()) if ((fixedBody != null && !fixedBody.isEmpty()) || (bodyMapping != null && !bodyMapping.isEmpty())
|| (scriptCfg != null && scriptCfg.get("type") != null || (scriptCfg != null && scriptCfg.get("type") != null
&& scriptCfg.get("source") != null)) { && scriptCfg.get("source") != null)) {
// body // body
Map<String, Object> body = new HashMap<>(); Map<String, Object> body = new HashMap<>();
body.putAll(PathMapping.transform(ctxNode, stepContext, fixedBody, bodyMapping)); body.putAll(PathMapping.transform(ctxNode, stepContext, fixedBody, bodyMapping));
if (body.containsKey(CommonConstants.WILDCARD_TILDE)) { if (body.containsKey(CommonConstants.WILDCARD_TILDE)) {
response.put("body", body.get(CommonConstants.WILDCARD_TILDE)); response.put("body", body.get(CommonConstants.WILDCARD_TILDE));
} else { } else {
// script // script
if (scriptCfg != null && scriptCfg.get("type") != null && scriptCfg.get("source") != null) { if (scriptCfg != null && scriptCfg.get("type") != null && scriptCfg.get("source") != null) {
try { try {
Object respBody = ScriptHelper.execute(scriptCfg, ctxNode, stepContext); Object respBody = ScriptHelper.execute(scriptCfg, ctxNode, stepContext);
if (respBody != null) { if (respBody != null) {
body.putAll((Map<String, Object>) respBody); body.putAll((Map<String, Object>) respBody);
} }
} catch (ScriptException e) { } catch (ScriptException e) {
LogService.setBizId(inputContext.getStepContext().getTraceId()); LogService.setBizId(inputContext.getStepContext().getTraceId());
LOGGER.warn("execute script failed, {}", JacksonUtils.writeValueAsString(scriptCfg), e); LOGGER.warn("execute script failed, {}", JacksonUtils.writeValueAsString(scriptCfg), e);
throw new ExecuteScriptException(e, stepContext, scriptCfg); throw new ExecuteScriptException(e, stepContext, scriptCfg);
} }
}
response.put("body", body);
} }
response.put("body", body);
} }
} }
} }
@@ -385,12 +396,16 @@ public class RequestInput extends RPCInput implements IInput{
headers.remove(CommonConstants.HEADER_CONTENT_LENGTH); headers.remove(CommonConstants.HEADER_CONTENT_LENGTH);
headers.add(systemConfig.fizzTraceIdHeader(), inputContext.getStepContext().getTraceId()); headers.add(systemConfig.fizzTraceIdHeader(), inputContext.getStepContext().getTraceId());
request.put("headers", MapUtil.headerToHashMap(headers)); synchronized (inputContext.getStepContext()) {
request.put("headers", MapUtil.headerToHashMap(headers));
}
Object body = null; Object body = null;
if (CONTENT_TYPE_XML.equals(reqContentType) || CONTENT_TYPE_TEXT_XML.equals(reqContentType)) { if (CONTENT_TYPE_XML.equals(reqContentType) || CONTENT_TYPE_TEXT_XML.equals(reqContentType)) {
// convert JSON to XML if it is XML content type // convert JSON to XML if it is XML content type
request.put("jsonBody", request.get("body")); synchronized (inputContext.getStepContext()) {
request.put("jsonBody", request.get("body"));
}
String jsonStr = null; String jsonStr = null;
if (TypeUtils.isBasicType(request.get("body"))) { if (TypeUtils.isBasicType(request.get("body"))) {
jsonStr = request.get("body").toString(); jsonStr = request.get("body").toString();
@@ -404,7 +419,9 @@ public class RequestInput extends RPCInput implements IInput{
} else { } else {
body = jsonStr; body = jsonStr;
} }
request.put("body", body); synchronized (inputContext.getStepContext()) {
request.put("body", body);
}
LOGGER.info("body={}", body); LOGGER.info("body={}", body);
LOGGER.info("headers={}", JSON.toJSONString(headers)); LOGGER.info("headers={}", JSON.toJSONString(headers));
} else if (CONTENT_TYPE_MULTIPART_FORM_DATA.equals(reqContentType)) { } else if (CONTENT_TYPE_MULTIPART_FORM_DATA.equals(reqContentType)) {
@@ -460,13 +477,15 @@ public class RequestInput extends RPCInput implements IInput{
headers.put("ELAPSEDTIME", elapsedMillis + "ms"); headers.put("ELAPSEDTIME", elapsedMillis + "ms");
RequestRPCResponse reqCr = (RequestRPCResponse) cr; RequestRPCResponse reqCr = (RequestRPCResponse) cr;
if (reqCr.getStatusCode() != null) { synchronized (inputContext.getStepContext()) {
this.response.put("httpStatus", reqCr.getStatusCode().value()); if (reqCr.getStatusCode() != null) {
this.response.put("httpStatus", reqCr.getStatusCode().value());
}
this.response.put("headers", headers);
this.respContentType = httpHeaders.getFirst(CONTENT_TYPE);
inputContext.getStepContext().addElapsedTime(prefix + request.get("url"),
elapsedMillis);
} }
this.response.put("headers", headers);
this.respContentType = httpHeaders.getFirst(CONTENT_TYPE);
inputContext.getStepContext().addElapsedTime(prefix + request.get("url"),
elapsedMillis);
} }
protected Mono<Object> bodyToMono(ClientResponse cr){ protected Mono<Object> bodyToMono(ClientResponse cr){
return cr.bodyToMono(String.class); return cr.bodyToMono(String.class);
@@ -475,8 +494,10 @@ public class RequestInput extends RPCInput implements IInput{
protected void doOnBodyError(Throwable ex, long elapsedMillis) { protected void doOnBodyError(Throwable ex, long elapsedMillis) {
LogService.setBizId(inputContext.getStepContext().getTraceId()); LogService.setBizId(inputContext.getStepContext().getTraceId());
LOGGER.warn("failed to call {}", request.get("url"), ex); LOGGER.warn("failed to call {}", request.get("url"), ex);
inputContext.getStepContext().addElapsedTime( synchronized (inputContext.getStepContext()) {
stepResponse.getStepName() + "-" + "调用接口 failed " + request.get("url"), elapsedMillis); inputContext.getStepContext().addElapsedTime(
stepResponse.getStepName() + "-" + "调用接口 failed " + request.get("url"), elapsedMillis);
}
} }
// Parse response body according to content-type header // Parse response body according to content-type header

View File

@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>fizz-gateway-community</artifactId> <artifactId>fizz-gateway-community</artifactId>
<groupId>com.fizzgate</groupId> <groupId>com.fizzgate</groupId>
<version>2.6.5</version> <version>2.6.6-beta1</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>fizz-gateway-community</artifactId> <artifactId>fizz-gateway-community</artifactId>
<groupId>com.fizzgate</groupId> <groupId>com.fizzgate</groupId>
<version>2.6.5</version> <version>2.6.6-beta1</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@@ -37,7 +37,7 @@
<artifactId>fizz-gateway-community</artifactId> <artifactId>fizz-gateway-community</artifactId>
<name>${project.artifactId}</name> <name>${project.artifactId}</name>
<description>fizz gateway community</description> <description>fizz gateway community</description>
<version>2.6.5</version> <version>2.6.6-beta1</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<modules> <modules>
<module>fizz-common</module> <module>fizz-common</module>