support URL path variables #103
This commit is contained in:
@@ -212,6 +212,28 @@ public class PathMapping {
|
||||
return val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns value of path, return default value if no value matched by path
|
||||
*
|
||||
* @param ctxNode
|
||||
* @param path e.g: step1.request1.headers.abc or
|
||||
* step1.request1.headers.abc|123 (default value seperate by "|")
|
||||
* @return
|
||||
*/
|
||||
public static Object getValueByPath(ONode ctxNode, String path) {
|
||||
String p = path;
|
||||
String defaultValue = null;
|
||||
if (path.indexOf("|") != -1) {
|
||||
p = path.substring(0, path.indexOf("|"));
|
||||
defaultValue = path.substring(path.indexOf("|") + 1);
|
||||
}
|
||||
ONode val = select(ctxNode, handlePath(p));
|
||||
if (val != null && !val.isNull()) {
|
||||
return val.toData();
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public static Map<String, Object> getScriptRules(Map<String, Object> rules) {
|
||||
if (rules.isEmpty()) {
|
||||
return new HashMap<>();
|
||||
|
||||
@@ -17,8 +17,13 @@
|
||||
|
||||
package we.fizz.input.extension.request;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -82,6 +87,10 @@ public class RequestInput extends RPCInput implements IInput{
|
||||
|
||||
private String[] xmlArrPaths;
|
||||
|
||||
private static Pattern PATH_VAR_PATTERN = Pattern.compile("(\\{)([^/]*)(\\})");
|
||||
|
||||
private static String DEFAULT_VALUE_SEPERATOR = "|";
|
||||
|
||||
public InputType getType() {
|
||||
return type;
|
||||
}
|
||||
@@ -114,15 +123,16 @@ public class RequestInput extends RPCInput implements IInput{
|
||||
params.putAll(MapUtil.toHashMap(config.getQueryParams()));
|
||||
request.put("params", params);
|
||||
|
||||
ONode ctxNode = null;
|
||||
// 数据转换
|
||||
if (inputContext != null && inputContext.getStepContext() != null) {
|
||||
StepContext<String, Object> stepContext = inputContext.getStepContext();
|
||||
ctxNode = PathMapping.toONode(stepContext);
|
||||
Map<String, Object> dataMapping = this.getConfig().getDataMapping();
|
||||
if (dataMapping != null) {
|
||||
Map<String, Object> requestMapping = (Map<String, Object>) dataMapping.get("request");
|
||||
if (!CollectionUtils.isEmpty(requestMapping)) {
|
||||
reqContentType = (String) requestMapping.get("contentType");
|
||||
ONode ctxNode = PathMapping.toONode(stepContext);
|
||||
|
||||
// headers
|
||||
Map<String, Object> headers = PathMapping.transform(ctxNode, stepContext,
|
||||
@@ -185,19 +195,37 @@ public class RequestInput extends RPCInput implements IInput{
|
||||
}
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append(config.getProtocol()).append("://").append(host)
|
||||
.append(config.getPath().startsWith("/") ? "" : "/").append(config.getPath());
|
||||
.append(config.getPath().startsWith("/") ? "" : "/").append(setPathVariable(ctxNode, config.getPath()));
|
||||
|
||||
UriComponents uriComponents = UriComponentsBuilder.fromUriString(sb.toString())
|
||||
.queryParams(MapUtil.toMultiValueMap(params)).build();
|
||||
|
||||
request.put("url", uriComponents.toUriString());
|
||||
} else {
|
||||
UriComponents uriComponents = UriComponentsBuilder.fromUriString(config.getBaseUrl() + config.getPath())
|
||||
UriComponents uriComponents = UriComponentsBuilder.fromUriString(config.getBaseUrl() + setPathVariable(ctxNode, config.getPath()))
|
||||
.queryParams(MapUtil.toMultiValueMap(params)).build();
|
||||
request.put("url", uriComponents.toUriString());
|
||||
}
|
||||
}
|
||||
|
||||
private String setPathVariable(ONode ctxNode, String path) {
|
||||
if (ctxNode == null || StringUtils.isBlank(path)) {
|
||||
return path;
|
||||
}
|
||||
String[] paths = path.split("/");
|
||||
for (int i = 0; i < paths.length; i++) {
|
||||
Matcher matcher = PATH_VAR_PATTERN.matcher(paths[i]);
|
||||
if (matcher.find()) {
|
||||
String jsonPath = matcher.group(2);
|
||||
Object val = PathMapping.getValueByPath(ctxNode, jsonPath);
|
||||
if (val != null && !(val instanceof Map) && !(val instanceof List)) {
|
||||
paths[i] = matcher.replaceAll(String.valueOf(val));
|
||||
}
|
||||
}
|
||||
}
|
||||
return String.join("/", paths);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doResponseMapping(InputConfig aConfig, InputContext inputContext, Object responseBody) {
|
||||
|
||||
|
||||
@@ -19,12 +19,17 @@ package we.fizz.input.extension.request;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.noear.snack.ONode;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
import we.fizz.input.InputConfig;
|
||||
import we.fizz.input.PathMapping;
|
||||
|
||||
|
||||
|
||||
@@ -124,14 +129,14 @@ public class RequestInputConfig extends InputConfig {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public RequestInputConfig() {
|
||||
super(null);
|
||||
}
|
||||
|
||||
public String getMethod() {
|
||||
return method;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setMethod(String method) {
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
@@ -153,6 +153,13 @@ class PathMappingTests {
|
||||
assertEquals("Ken", (String)name2.toData());
|
||||
assertEquals("Ken", (String)inputAbcName2.toData());
|
||||
|
||||
PathMapping.setByPath(ctxNode, "step1.requests.request1.request.headers.TEST", "1", true);
|
||||
Object abcVal1 = PathMapping.getValueByPath(ctxNode, "step1.requests.request1.request.headers.test[0]|123");
|
||||
Object abcVal2 = PathMapping.getValueByPath(ctxNode, "step1.requests.request1.request.headers.test[3]|123456");
|
||||
assertEquals("1", (String)abcVal1);
|
||||
assertEquals("123456", (String)abcVal2);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user