Support configuring multiple values in http header/query parameter/form-data #246

This commit is contained in:
Francis Dong
2021-07-16 17:13:06 +08:00
committed by dxfeng10
parent 5aa53fec9b
commit 60f2122b14
5 changed files with 326 additions and 132 deletions

View File

@@ -17,6 +17,7 @@
package we.util; package we.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -36,6 +37,10 @@ import org.springframework.util.MultiValueMap;
*/ */
public class MapUtil { public class MapUtil {
private static final String KEY = "key";
private static final String VALUE = "value";
public static HttpHeaders toHttpHeaders(Map<String, Object> params) { public static HttpHeaders toHttpHeaders(Map<String, Object> params) {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
@@ -65,7 +70,6 @@ public class MapUtil {
return headers; return headers;
} }
public static MultiValueMap<String, String> toMultiValueMap(Map<String, Object> params) { public static MultiValueMap<String, String> toMultiValueMap(Map<String, Object> params) {
MultiValueMap<String, String> mvmap = new LinkedMultiValueMap<>(); MultiValueMap<String, String> mvmap = new LinkedMultiValueMap<>();
@@ -96,7 +100,7 @@ public class MapUtil {
return mvmap; return mvmap;
} }
public static MultiValueMap<String, Object> toMultipartDataMap(Map<String, Object> params) { public static MultiValueMap<String, Object> toMultipartDataMap(Map<String, Object> params) {
MultiValueMap<String, Object> mvmap = new LinkedMultiValueMap<>(); MultiValueMap<String, Object> mvmap = new LinkedMultiValueMap<>();
if (params == null || params.isEmpty()) { if (params == null || params.isEmpty()) {
@@ -124,15 +128,16 @@ public class MapUtil {
return mvmap; return mvmap;
} }
/** /**
* Extract form data from multipart map exclude file * Extract form data from multipart map exclude file
*
* @param params * @param params
* @param fileKeyPrefix * @param fileKeyPrefix
* @param filePartMap Map * @param filePartMap Map
* @return * @return
*/ */
public static Map<String, Object> extractFormData(MultiValueMap<String, Part> params, String fileKeyPrefix, Map<String, FilePart> filePartMap) { public static Map<String, Object> extractFormData(MultiValueMap<String, Part> params, String fileKeyPrefix,
Map<String, FilePart> filePartMap) {
HashMap<String, Object> m = new HashMap<>(); HashMap<String, Object> m = new HashMap<>();
if (params == null || params.isEmpty()) { if (params == null || params.isEmpty()) {
return m; return m;
@@ -148,7 +153,7 @@ public class MapUtil {
formFieldValues.add(p.value()); formFieldValues.add(p.value());
} else if (part instanceof FilePart) { } else if (part instanceof FilePart) {
FilePart fp = (FilePart) part; FilePart fp = (FilePart) part;
String k = fileKeyPrefix + UUIDUtil.getUUID() + "-" + fp.filename(); String k = fileKeyPrefix + UUIDUtil.getUUID() + "-" + fp.filename();
formFieldValues.add(k); formFieldValues.add(k);
filePartMap.put(k, fp); filePartMap.put(k, fp);
} }
@@ -171,26 +176,28 @@ public class MapUtil {
} }
return m; return m;
} }
/** /**
* Replace file field with FilePart object * Replace file field with FilePart object
*
* @param params * @param params
* @param fileKeyPrefix * @param fileKeyPrefix
* @param filePartMap * @param filePartMap
*/ */
public static void replaceWithFilePart(MultiValueMap<String, Object> params, String fileKeyPrefix, Map<String, FilePart> filePartMap) { public static void replaceWithFilePart(MultiValueMap<String, Object> params, String fileKeyPrefix,
Map<String, FilePart> filePartMap) {
if (params == null || params.isEmpty() || filePartMap == null || filePartMap.isEmpty()) { if (params == null || params.isEmpty() || filePartMap == null || filePartMap.isEmpty()) {
return; return;
} }
for (Entry<String, List<Object>> entry : params.entrySet()) { for (Entry<String, List<Object>> entry : params.entrySet()) {
List<Object> list = entry.getValue(); List<Object> list = entry.getValue();
if (list != null && list.size() > 0) { if (list != null && list.size() > 0) {
List<Object> newlist = new ArrayList<>(); List<Object> newlist = new ArrayList<>();
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
if (list.get(i).toString().startsWith(fileKeyPrefix)) { if (list.get(i).toString().startsWith(fileKeyPrefix)) {
newlist.add(filePartMap.get(list.get(i).toString())); newlist.add(filePartMap.get(list.get(i).toString()));
}else { } else {
newlist.add(list.get(i)); newlist.add(list.get(i));
} }
} }
@@ -198,7 +205,7 @@ public class MapUtil {
} }
} }
} }
public static Map<String, Object> toHashMap(MultiValueMap<String, String> params) { public static Map<String, Object> toHashMap(MultiValueMap<String, String> params) {
HashMap<String, Object> m = new HashMap<>(); HashMap<String, Object> m = new HashMap<>();
@@ -219,7 +226,7 @@ public class MapUtil {
return m; return m;
} }
public static Map<String, Object> headerToHashMap(HttpHeaders headers) { public static Map<String, Object> headerToHashMap(HttpHeaders headers) {
HashMap<String, Object> m = new HashMap<>(); HashMap<String, Object> m = new HashMap<>();
@@ -240,7 +247,7 @@ public class MapUtil {
return m; return m;
} }
public static Map<String, Object> upperCaseKey(Map<String, Object> m) { public static Map<String, Object> upperCaseKey(Map<String, Object> m) {
HashMap<String, Object> rs = new HashMap<>(); HashMap<String, Object> rs = new HashMap<>();
@@ -254,79 +261,96 @@ public class MapUtil {
return rs; return rs;
} }
public static MultiValueMap<String, Object> upperCaseKey(MultiValueMap<String, Object> m) {
MultiValueMap<String, Object> rs = new LinkedMultiValueMap<>();
if (m == null || m.isEmpty()) {
return rs;
}
for (Entry<String, List<Object>> entry : m.entrySet()) {
rs.put(entry.getKey().toUpperCase(), entry.getValue());
}
return rs;
}
/** /**
* Set value by pathsupport multiple levelsega.b.c <br> * Set value by pathsupport multiple levelsega.b.c <br>
* Do NOT use this method if field name contains a dot <br> * Do NOT use this method if field name contains a dot <br>
* @param data *
* @param data
* @param path * @param path
* @param value * @param value
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static void set(Map<String, Object> data, String path, Object value) { public static void set(Map<String, Object> data, String path, Object value) {
String[] fields = path.split("\\."); String[] fields = path.split("\\.");
if(fields.length < 2) { if (fields.length < 2) {
data.put(path, value); data.put(path, value);
}else { } else {
Map<String, Object> next = data; Map<String, Object> next = data;
for (int i = 0; i < fields.length - 1; i++) { for (int i = 0; i < fields.length - 1; i++) {
Map<String, Object> val = (Map<String, Object>) next.get(fields[i]); Map<String, Object> val = (Map<String, Object>) next.get(fields[i]);
if(val == null) { if (val == null) {
val = new HashMap<>(); val = new HashMap<>();
next.put(fields[i], val); next.put(fields[i], val);
} }
if(i == fields.length - 2) { if (i == fields.length - 2) {
val.put(fields[i+1], value); val.put(fields[i + 1], value);
break; break;
} }
next = val; next = val;
} }
} }
} }
/** /**
* Get value by path, support multiple levelsega.b.c <br> * Get value by path, support multiple levelsega.b.c <br>
* Do NOT use this method if field name contains a dot <br> * Do NOT use this method if field name contains a dot <br>
*
* @param data * @param data
* @param path * @param path
* @return * @return
*/ */
public static Object get(Map<String, Object> data, String path) { public static Object get(Map<String, Object> data, String path) {
String[] fields = path.split("\\."); String[] fields = path.split("\\.");
if(fields.length < 2) { if (fields.length < 2) {
return data.get(path); return data.get(path);
}else { } else {
Map<String, Object> next = data; Map<String, Object> next = data;
for (int i = 0; i < fields.length - 1; i++) { for (int i = 0; i < fields.length - 1; i++) {
if(!(next.get(fields[i]) instanceof Map)) { if (!(next.get(fields[i]) instanceof Map)) {
return null; return null;
} }
Map<String, Object> val = (Map<String, Object>) next.get(fields[i]); Map<String, Object> val = (Map<String, Object>) next.get(fields[i]);
if(val == null) { if (val == null) {
return null; return null;
} }
if(i == fields.length - 2) { if (i == fields.length - 2) {
return val.get(fields[i+1]); return val.get(fields[i + 1]);
} }
next = val; next = val;
} }
} }
return null; return null;
} }
/** /**
* Merge maps, merge src to target * Merge maps, merge src to target
*
* @param target * @param target
* @param src * @param src
* @return * @return
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static Map<String, Object> merge(Map<String, Object> target, Map<String, Object> src) { public static Map<String, Object> merge(Map<String, Object> target, Map<String, Object> src) {
if(src == null || src.isEmpty()) { if (src == null || src.isEmpty()) {
return target; return target;
} }
src.forEach((key, value) -> { src.forEach((key, value) -> {
if(value != null) { if (value != null) {
target.merge(key, value, (oldValue, newValue) -> { target.merge(key, value, (oldValue, newValue) -> {
if (oldValue instanceof Map && newValue instanceof Map) { if (oldValue instanceof Map && newValue instanceof Map) {
oldValue = merge((Map<String, Object>) oldValue, (Map<String, Object>) newValue); oldValue = merge((Map<String, Object>) oldValue, (Map<String, Object>) newValue);
@@ -338,5 +362,71 @@ public class MapUtil {
}); });
return target; return target;
} }
/**
* Convert list to map and merge multiple values<br>
* Example: <br>
* List as:<br>
* [{"key": "abc", "value": "aaa"},{"key": "abc", "value": "xyz"},{"key":
* "a123", "value": 666}]<br>
* Merge Result:<br>
* {"abc": ["aaa","xyz"], "a123": 666} <br>
*
* @param obj
* @return
*/
@SuppressWarnings("unchecked")
public static Map<String, Object> list2Map(Object obj) {
// Compatible with older version configuration
if (obj instanceof Map) {
return (Map<String, Object>) obj;
}
if (obj instanceof List) {
Map<String, Object> rs = new HashMap<>();
List<Map<String, Object>> list = (List<Map<String, Object>>) obj;
if (list == null || list.size() == 0) {
return rs;
}
for (Map<String, Object> m : list) {
String k = m.get(KEY).toString();
Object v = m.get(VALUE);
if (rs.containsKey(k)) {
List<Object> vals = null;
if (rs.get(k) instanceof List) {
vals = (List<Object>) rs.get(k);
} else {
vals = new ArrayList<>();
vals.add(rs.get(k));
}
vals.add(v);
rs.put(k, vals);
} else {
rs.put(k, v);
}
}
return rs;
}
return null;
}
/**
* Convert list to MultiValueMap<br>
* List format example:<br>
* [{"key": "abc", "value": "aaa"},{"key": "abc", "value": "xyz"},,{"key":
* "a123", "value": 666}]<br>
*
* @param list
* @return
*/
public static MultiValueMap<String, Object> listToMultiValueMap(List<Map<String, Object>> list) {
MultiValueMap<String, Object> mvmap = new LinkedMultiValueMap<>();
if (list == null || list.size() == 0) {
return mvmap;
}
for (Map<String, Object> m : list) {
mvmap.add(m.get(KEY).toString(), m.get(VALUE));
}
return mvmap;
}
} }

View File

@@ -32,6 +32,7 @@ import reactor.core.publisher.Mono;
import we.exception.ExecuteScriptException; import we.exception.ExecuteScriptException;
import we.exception.RedirectException; import we.exception.RedirectException;
import we.exception.StopAndResponseException; import we.exception.StopAndResponseException;
import we.fizz.exception.FizzRuntimeException;
import we.flume.clients.log4j2appender.LogService; import we.flume.clients.log4j2appender.LogService;
import we.legacy.RespEntity; import we.legacy.RespEntity;
import we.util.JacksonUtils; import we.util.JacksonUtils;
@@ -81,6 +82,19 @@ public class FilterExceptionHandlerConfig {
return resp.writeWith(Mono.just(resp.bufferFactory().wrap(rs.toString().getBytes()))); return resp.writeWith(Mono.just(resp.bufferFactory().wrap(rs.toString().getBytes())));
} }
} }
if (t instanceof FizzRuntimeException) {
FizzRuntimeException ex = (FizzRuntimeException) t;
resp.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
RespEntity rs = null;
String reqId = exchange.getRequest().getId();
if (ex.getStepContext() != null && ex.getStepContext().returnContext()) {
rs = new RespEntity(HttpStatus.INTERNAL_SERVER_ERROR.value(), t.getMessage(), reqId, ex.getStepContext());
return resp.writeWith(Mono.just(resp.bufferFactory().wrap(JacksonUtils.writeValueAsString(rs).getBytes())));
} else {
rs = new RespEntity(HttpStatus.INTERNAL_SERVER_ERROR.value(), t.getMessage(), reqId);
return resp.writeWith(Mono.just(resp.bufferFactory().wrap(rs.toString().getBytes())));
}
}
Mono<Void> vm; Mono<Void> vm;
Object fc = exchange.getAttributes().get(WebUtils.FILTER_CONTEXT); Object fc = exchange.getAttributes().get(WebUtils.FILTER_CONTEXT);
if (fc == null) { // t came from flow control filter if (fc == null) { // t came from flow control filter

View File

@@ -1,11 +1,37 @@
package we.fizz.exception; package we.fizz.exception;
import we.fizz.StepContext;
public class FizzRuntimeException extends RuntimeException { public class FizzRuntimeException extends RuntimeException {
private StepContext<String, Object> stepContext;
public FizzRuntimeException(String message) { public FizzRuntimeException(String message) {
super(message); super(message);
} }
public FizzRuntimeException(String message, Throwable cause) { public FizzRuntimeException(String message, Throwable cause) {
super(message, cause); super(message, cause);
this.setStackTrace(cause.getStackTrace());
} }
public FizzRuntimeException(String message, StepContext<String, Object> stepContext) {
super(message);
this.stepContext = stepContext;
}
public FizzRuntimeException(String message, Throwable cause, StepContext<String, Object> stepContext) {
super(message, cause);
this.setStackTrace(cause.getStackTrace());
this.stepContext = stepContext;
}
public StepContext<String, Object> getStepContext() {
return stepContext;
}
public void setStepContext(StepContext<String, Object> stepContext) {
this.stepContext = stepContext;
}
} }

View File

@@ -19,13 +19,13 @@ package we.fizz.input;
import java.util.*; import java.util.*;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.noear.snack.ONode; import org.noear.snack.ONode;
import we.constants.CommonConstants; import we.constants.CommonConstants;
import we.fizz.StepContext; import we.fizz.StepContext;
import we.fizz.exception.FizzRuntimeException;
import we.util.MapUtil; import we.util.MapUtil;
/** /**
@@ -105,14 +105,15 @@ public class PathMapping {
return target.toObject(Map.class); return target.toObject(Map.class);
} }
@SuppressWarnings("unchecked")
public static ONode transform(ONode ctxNode, Map<String, Object> rules, boolean supportMultiLevels) { public static ONode transform(ONode ctxNode, Map<String, Object> rules, boolean supportMultiLevels) {
ONode target = ONode.load(new HashMap()); ONode target = ONode.load(new HashMap());
if (rules.isEmpty()) { if (rules.isEmpty()) {
return target; return target;
} }
Map<String, String> rs = new HashMap<>(); Map<String, Object> rs = new HashMap<>();
Map<String, String> types = new HashMap<>(); Map<String, Object> types = new HashMap<>();
for (Entry<String, Object> entry : rules.entrySet()) { for (Entry<String, Object> entry : rules.entrySet()) {
if (entry.getValue() instanceof String) { if (entry.getValue() instanceof String) {
String val = (String) entry.getValue(); String val = (String) entry.getValue();
@@ -123,6 +124,25 @@ public class PathMapping {
} else { } else {
rs.put(entry.getKey(), val); rs.put(entry.getKey(), val);
} }
} else if (entry.getValue() instanceof List) {
List<Object> values = (List<Object>) entry.getValue();
List<String> vList = new ArrayList<>();
List<String> tList = new ArrayList<>();
for (Object v : values) {
if (v instanceof String) {
String val = (String) v;
Optional<String> optType = typeList.stream().filter(s -> val.startsWith(s + " ")).findFirst();
if (optType.isPresent()) {
vList.add(val.substring(optType.get().length() + 1));
tList.add(optType.get());
} else {
vList.add(val);
tList.add(null);
}
}
}
rs.put(entry.getKey(), vList);
types.put(entry.getKey(), tList);
} }
} }
@@ -134,8 +154,46 @@ public class PathMapping {
Object starValObj = null; Object starValObj = null;
String starEntryKey = null; String starEntryKey = null;
for (Entry<String, String> entry : rs.entrySet()) { for (Entry<String, Object> entry : rs.entrySet()) {
String path = entry.getValue(); if (entry.getValue() instanceof String) {
String path = (String) entry.getValue();
String type = (String) types.get(entry.getKey());
Object obj = getRefValue(ctxNode, type, path);
if (CommonConstants.WILDCARD_STAR.equals(entry.getKey())) {
starValObj = obj;
starEntryKey = entry.getKey();
} else {
setByPath(target, entry.getKey(), obj, supportMultiLevels);
}
} else if (entry.getValue() instanceof List) {
List<String> refs = (List<String>) entry.getValue();
List<String> tList = (List<String>) types.get(entry.getKey());
List<Object> refValList = new ArrayList<>();
for (int i = 0; i < refs.size(); i++) {
String path = refs.get(i);
String type = tList.get(i);
Object obj = getRefValue(ctxNode, type, path);
// Only header form-data and query Parameter support multiple values, merge result into
// one a list
if (obj instanceof List) {
refValList.addAll((List<Object>) obj);
} else {
refValList.add(obj);
}
}
setByPath(target, entry.getKey(), refValList, supportMultiLevels);
}
}
if(starEntryKey != null) {
setByPath(target, starEntryKey, starValObj, supportMultiLevels);
}
return target;
}
private static Object getRefValue(ONode ctxNode, String type, String path) {
Object obj = null;
try {
String p = path; String p = path;
String defaultValue = null; String defaultValue = null;
if (path.indexOf("|") != -1) { if (path.indexOf("|") != -1) {
@@ -143,82 +201,77 @@ public class PathMapping {
defaultValue = path.substring(path.indexOf("|") + 1); defaultValue = path.substring(path.indexOf("|") + 1);
} }
ONode val = select(ctxNode, handlePath(p)); ONode val = select(ctxNode, handlePath(p));
Object obj = null;
if (val != null && !val.isNull()) { if (val != null && !val.isNull()) {
obj = val; obj = val;
} else { } else {
obj = defaultValue; obj = defaultValue;
} }
if (obj != null && types.containsKey(entry.getKey())) { if (obj != null && type != null) {
switch (types.get(entry.getKey())) { obj = cast(obj, type);
case "Integer":
case "int": {
if (obj instanceof ONode) {
obj = ((ONode) obj).val().getInt();
} else {
obj = Integer.valueOf(obj.toString());
}
break;
}
case "Boolean":
case "boolean": {
if (obj instanceof ONode) {
obj = ((ONode) obj).val().getBoolean();
} else {
obj = Boolean.valueOf(obj.toString());
}
break;
}
case "Float":
case "float": {
if (obj instanceof ONode) {
obj = ((ONode) obj).val().getFloat();
} else {
obj = Float.valueOf(obj.toString());
}
break;
}
case "Double":
case "double": {
if (obj instanceof ONode) {
obj = ((ONode) obj).val().getDouble();
} else {
obj = Double.valueOf(obj.toString());
}
break;
}
case "String":
case "string": {
if (obj instanceof ONode) {
obj = ((ONode) obj).val().getString();
}
break;
}
case "Long":
case "long": {
if (obj instanceof ONode) {
obj = ((ONode) obj).val().getLong();
} else {
obj = Long.valueOf(obj.toString());
}
break;
}
}
} }
if (CommonConstants.WILDCARD_STAR.equals(entry.getKey())) { } catch (Exception e) {
starValObj = obj; e.printStackTrace();
starEntryKey = entry.getKey(); throw new FizzRuntimeException(String.format("path mapping errer: %s , path mapping data: %s %s", e.getMessage(), type, path), e);
}
return obj;
}
private static Object cast(Object obj, String type) {
switch (type) {
case "Integer":
case "int": {
if (obj instanceof ONode) {
obj = ((ONode) obj).val().getInt();
} else { } else {
setByPath(target, entry.getKey(), obj, supportMultiLevels); obj = Integer.valueOf(obj.toString());
} }
break;
} }
case "Boolean":
if(starEntryKey != null) { case "boolean": {
setByPath(target, starEntryKey, starValObj, supportMultiLevels); if (obj instanceof ONode) {
obj = ((ONode) obj).val().getBoolean();
} else {
obj = Boolean.valueOf(obj.toString());
}
break;
} }
case "Float":
return target; case "float": {
if (obj instanceof ONode) {
obj = ((ONode) obj).val().getFloat();
} else {
obj = Float.valueOf(obj.toString());
}
break;
}
case "Double":
case "double": {
if (obj instanceof ONode) {
obj = ((ONode) obj).val().getDouble();
} else {
obj = Double.valueOf(obj.toString());
}
break;
}
case "String":
case "string": {
if (obj instanceof ONode) {
obj = ((ONode) obj).val().getString();
}
break;
}
case "Long":
case "long": {
if (obj instanceof ONode) {
obj = ((ONode) obj).val().getLong();
} else {
obj = Long.valueOf(obj.toString());
}
break;
}
}
return obj;
} }
public static ONode select(ONode ctxNode, String path) { public static ONode select(ONode ctxNode, String path) {
@@ -270,7 +323,14 @@ public class PathMapping {
} }
Map<String, Object> rs = new HashMap<>(); Map<String, Object> rs = new HashMap<>();
for (Entry<String, Object> entry : rules.entrySet()) { for (Entry<String, Object> entry : rules.entrySet()) {
if (!(entry.getValue() instanceof String)) { if (entry.getValue() instanceof List) {
List<Object> values = (List<Object>) entry.getValue();
for (Object v : values) {
if (!(v instanceof String)) {
rs.put(entry.getKey(), v);
}
}
} else if (!(entry.getValue() instanceof String) && entry.getValue() instanceof Map) {
rs.put(entry.getKey(), entry.getValue()); rs.put(entry.getKey(), entry.getValue());
} }
} }
@@ -418,31 +478,35 @@ public class PathMapping {
*/ */
public static Map<String, Object> transform(ONode ctxNode, StepContext<String, Object> stepContext, public static Map<String, Object> transform(ONode ctxNode, StepContext<String, Object> stepContext,
Map<String, Object> fixed, Map<String, Object> mappingRules, boolean supportMultiLevels) { Map<String, Object> fixed, Map<String, Object> mappingRules, boolean supportMultiLevels) {
if (fixed != null && fixed.containsKey(CommonConstants.WILDCARD_TILDE)) { try {
Object val = fixed.get(CommonConstants.WILDCARD_TILDE); if (fixed != null && fixed.containsKey(CommonConstants.WILDCARD_TILDE)) {
fixed = new HashMap<>(); Object val = fixed.get(CommonConstants.WILDCARD_TILDE);
fixed.put(CommonConstants.WILDCARD_TILDE, val); fixed = new HashMap<>();
} fixed.put(CommonConstants.WILDCARD_TILDE, val);
if (mappingRules != null && mappingRules.containsKey(CommonConstants.WILDCARD_TILDE)) {
Object val = mappingRules.get(CommonConstants.WILDCARD_TILDE);
mappingRules = new HashMap<>();
mappingRules.put(CommonConstants.WILDCARD_TILDE, val);
}
Map<String, Object> result = new HashMap<>();
if (fixed != null) {
result.putAll((Map<String, Object>) convertPath(fixed, supportMultiLevels));
}
if (mappingRules != null) {
// 路径映射
ONode target = PathMapping.transform(ctxNode, mappingRules, supportMultiLevels);
// 脚本转换
Map<String, Object> scriptRules = PathMapping.getScriptRules(mappingRules);
Map<String, Object> scriptResult = ScriptHelper.executeScripts(target, scriptRules, ctxNode, stepContext, supportMultiLevels);
if (scriptResult != null && !scriptResult.isEmpty()) {
result = MapUtil.merge(result, scriptResult);
} }
if (mappingRules != null && mappingRules.containsKey(CommonConstants.WILDCARD_TILDE)) {
Object val = mappingRules.get(CommonConstants.WILDCARD_TILDE);
mappingRules = new HashMap<>();
mappingRules.put(CommonConstants.WILDCARD_TILDE, val);
}
Map<String, Object> result = new HashMap<>();
if (fixed != null) {
result.putAll((Map<String, Object>) convertPath(fixed, supportMultiLevels));
}
if (mappingRules != null) {
// 路径映射
ONode target = transform(ctxNode, mappingRules, supportMultiLevels);
// 脚本转换
Map<String, Object> scriptRules = getScriptRules(mappingRules);
Map<String, Object> scriptResult = ScriptHelper.executeScripts(target, scriptRules, ctxNode, stepContext, supportMultiLevels);
if (scriptResult != null && !scriptResult.isEmpty()) {
result = MapUtil.merge(result, scriptResult);
}
}
return result;
}catch(FizzRuntimeException e) {
throw new FizzRuntimeException(e.getMessage(), e, stepContext);
} }
return result;
} }
public static Map<String, Object> convertPath(Map<String, Object> fixed, boolean supportMultiLevels) { public static Map<String, Object> convertPath(Map<String, Object> fixed, boolean supportMultiLevels) {

View File

@@ -143,8 +143,8 @@ public class RequestInput extends RPCInput implements IInput{
// headers // headers
Map<String, Object> headers = PathMapping.transform(ctxNode, stepContext, Map<String, Object> headers = PathMapping.transform(ctxNode, stepContext,
MapUtil.upperCaseKey((Map<String, Object>) requestMapping.get("fixedHeaders")), MapUtil.upperCaseKey(MapUtil.list2Map(requestMapping.get("fixedHeaders"))),
MapUtil.upperCaseKey((Map<String, Object>) requestMapping.get("headers")), false); MapUtil.upperCaseKey(MapUtil.list2Map(requestMapping.get("headers"))), 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) {
request.put("headers", headers.get(CommonConstants.WILDCARD_TILDE)); request.put("headers", headers.get(CommonConstants.WILDCARD_TILDE));
@@ -154,8 +154,8 @@ public class RequestInput extends RPCInput implements IInput{
// params // params
params.putAll(PathMapping.transform(ctxNode, stepContext, params.putAll(PathMapping.transform(ctxNode, stepContext,
(Map<String, Object>) requestMapping.get("fixedParams"), MapUtil.list2Map(requestMapping.get("fixedParams")),
(Map<String, Object>) requestMapping.get("params"), false)); MapUtil.list2Map(requestMapping.get("params")), false));
if (params.containsKey(CommonConstants.WILDCARD_TILDE) if (params.containsKey(CommonConstants.WILDCARD_TILDE)
&& params.get(CommonConstants.WILDCARD_TILDE) instanceof Map) { && params.get(CommonConstants.WILDCARD_TILDE) instanceof Map) {
request.put("params", params.get(CommonConstants.WILDCARD_TILDE)); request.put("params", params.get(CommonConstants.WILDCARD_TILDE));
@@ -170,8 +170,8 @@ public class RequestInput extends RPCInput implements IInput{
supportMultiLevels = false; supportMultiLevels = false;
} }
Map<String,Object> body = PathMapping.transform(ctxNode, stepContext, Map<String,Object> body = PathMapping.transform(ctxNode, stepContext,
(Map<String, Object>) requestMapping.get("fixedBody"), MapUtil.list2Map(requestMapping.get("fixedBody")),
(Map<String, Object>) requestMapping.get("body"), supportMultiLevels); MapUtil.list2Map(requestMapping.get("body")), supportMultiLevels);
if (body.containsKey(CommonConstants.WILDCARD_TILDE)) { if (body.containsKey(CommonConstants.WILDCARD_TILDE)) {
request.put("body", body.get(CommonConstants.WILDCARD_TILDE)); request.put("body", body.get(CommonConstants.WILDCARD_TILDE));
} else { } else {