|
@@ -0,0 +1,150 @@
|
|
|
+package com.hrsk.cloud.eg.domain.api.resolver.result;
|
|
|
+
|
|
|
+import com.alibaba.cola.exception.BizException;
|
|
|
+import com.google.gson.*;
|
|
|
+import com.hrsk.cloud.eg.domain.api.ApiContext;
|
|
|
+import com.hrsk.cloud.eg.domain.api.ApiResult;
|
|
|
+import com.hrsk.cloud.eg.domain.api.ValueObject;
|
|
|
+import com.hrsk.cloud.eg.domain.common.utils.GsonUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author: bianlanzhou
|
|
|
+ * @create: 2024-08-01 15:38
|
|
|
+ * @description: json类型payload
|
|
|
+ **/
|
|
|
+public class ApiJsonResultResolver implements ApiResultResolver {
|
|
|
+ /**
|
|
|
+ * JSON配置最大深度
|
|
|
+ */
|
|
|
+ private static final Integer MAX_DEEP = 10;
|
|
|
+ /**
|
|
|
+ * 配置前缀标记
|
|
|
+ */
|
|
|
+ private static final String CONFIG_PREFIX_SIGN = "#{";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 配置后缀标记
|
|
|
+ */
|
|
|
+ private static final String CONFIG_END_SIGN = "}";
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object resolve(String jsonTemplate, ApiResult context) {
|
|
|
+ JsonElement jsonElement = GsonUtils.fromJson(jsonTemplate, JsonElement.class);
|
|
|
+ traverseJson(jsonElement,jsonElement,null,context,0);
|
|
|
+ return jsonElement.toString();
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 遍历json
|
|
|
+ * @param jsonElement json元素
|
|
|
+ * @param context 上下文
|
|
|
+ * @param deep 深度
|
|
|
+ */
|
|
|
+ public void traverseJson(JsonElement jsonElement, JsonElement currentElement,String currentKey,ApiResult context,Integer deep) {
|
|
|
+ deep++;
|
|
|
+ if(deep >= MAX_DEEP){
|
|
|
+ throw new BizException(String.format("请求payload的json配置深度达到允许的最大深度[%s]!",MAX_DEEP));
|
|
|
+ }
|
|
|
+ if (jsonElement.isJsonObject()) {
|
|
|
+ JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
|
+ for (String key : jsonObject.keySet()) {
|
|
|
+ traverseJson(jsonObject.get(key),jsonObject,key,context,deep);
|
|
|
+ }
|
|
|
+ } else if (jsonElement.isJsonArray()) {
|
|
|
+ JsonArray jsonArray = jsonElement.getAsJsonArray();
|
|
|
+ for (JsonElement elem : jsonArray) {
|
|
|
+ traverseJson(elem,currentElement,null,context,deep);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if(jsonElement.isJsonPrimitive() && jsonElement.getAsJsonPrimitive().isString()){
|
|
|
+ String value = jsonElement.getAsString();
|
|
|
+ if(value.startsWith(CONFIG_PREFIX_SIGN)&&value.endsWith(CONFIG_END_SIGN)){
|
|
|
+ JsonPrimitive newElement = parseSpelConfigToRealValue(jsonElement,context);
|
|
|
+ if(StringUtils.isNotBlank(currentKey)){
|
|
|
+ currentElement.getAsJsonObject().add(currentKey,newElement);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 遍历json
|
|
|
+ * @param jsonElement json元素
|
|
|
+ * @param context 上下文
|
|
|
+ * @param deep 深度
|
|
|
+ */
|
|
|
+ public void traverseJson(JsonElement jsonElement, JsonElement currentElement,String currentKey,ApiContext context,Integer deep) {
|
|
|
+ deep++;
|
|
|
+ if(deep >= MAX_DEEP){
|
|
|
+ throw new BizException(String.format("请求payload的json配置深度达到允许的最大深度[%s]!",MAX_DEEP));
|
|
|
+ }
|
|
|
+ if (jsonElement.isJsonObject()) {
|
|
|
+ JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
|
+ for (String key : jsonObject.keySet()) {
|
|
|
+ traverseJson(jsonObject.get(key),jsonObject,key,context,deep);
|
|
|
+ }
|
|
|
+ } else if (jsonElement.isJsonArray()) {
|
|
|
+ JsonArray jsonArray = jsonElement.getAsJsonArray();
|
|
|
+ for (JsonElement elem : jsonArray) {
|
|
|
+ traverseJson(elem,currentElement,null,context,deep);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if(jsonElement.isJsonPrimitive() && jsonElement.getAsJsonPrimitive().isString()){
|
|
|
+ String value = jsonElement.getAsString();
|
|
|
+ if(value.startsWith(CONFIG_PREFIX_SIGN)&&value.endsWith(CONFIG_END_SIGN)){
|
|
|
+ JsonPrimitive newElement = parseSpelConfigToRealValue(jsonElement,context);
|
|
|
+ if(StringUtils.isNotBlank(currentKey)){
|
|
|
+ currentElement.getAsJsonObject().add(currentKey,newElement);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将spel表达式转换成真实值
|
|
|
+ * @param jsonElement json元素
|
|
|
+ */
|
|
|
+ private JsonPrimitive parseSpelConfigToRealValue(JsonElement jsonElement, ApiResult context){
|
|
|
+ String value = jsonElement.getAsString();
|
|
|
+ value = StringUtils.replace(value,CONFIG_PREFIX_SIGN,"{",3);
|
|
|
+ ValueObject valueObject = new Gson().fromJson(value,ValueObject.class);
|
|
|
+ Object realValue = valueObject.compile(context).getRealValue();
|
|
|
+ if(realValue instanceof Number){
|
|
|
+ return new JsonPrimitive((Number)realValue);
|
|
|
+ }else if(realValue instanceof String){
|
|
|
+ return new JsonPrimitive((String)realValue);
|
|
|
+ }else if(realValue instanceof Boolean){
|
|
|
+ return new JsonPrimitive((Boolean)realValue);
|
|
|
+ }else if(realValue instanceof Character){
|
|
|
+ return new JsonPrimitive((Character)realValue);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将spel表达式转换成真实值
|
|
|
+ * @param jsonElement json元素
|
|
|
+ */
|
|
|
+ private JsonPrimitive parseSpelConfigToRealValue(JsonElement jsonElement, ApiContext context){
|
|
|
+ String value = jsonElement.getAsString();
|
|
|
+ value = StringUtils.replace(value,CONFIG_PREFIX_SIGN,"{",3);
|
|
|
+ ValueObject valueObject = new Gson().fromJson(value,ValueObject.class);
|
|
|
+ Object realValue = valueObject.compile(context).getRealValue();
|
|
|
+ if(realValue instanceof Number){
|
|
|
+ return new JsonPrimitive((Number)realValue);
|
|
|
+ }else if(realValue instanceof String){
|
|
|
+ return new JsonPrimitive((String)realValue);
|
|
|
+ }else if(realValue instanceof Boolean){
|
|
|
+ return new JsonPrimitive((Boolean)realValue);
|
|
|
+ }else if(realValue instanceof Character){
|
|
|
+ return new JsonPrimitive((Character)realValue);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|