Skip to content

Commit

Permalink
refactor(come|particle): Key template support extract value with `$pa…
Browse files Browse the repository at this point in the history
…rams.name`, `$header.name`, `$cookie.name`.
  • Loading branch information
yizzuide committed Apr 21, 2020
1 parent fa61dca commit f599fc2
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 31 deletions.
4 changes: 2 additions & 2 deletions Milkomeda/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<properties>
<java.version>1.8</java.version>
<project.release.version>3.0.10-SNAPSHOT</project.release.version>
<project.release.version>3.1.0-SNAPSHOT</project.release.version>
<spring-boot.version>2.2.4</spring-boot.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
<mybatis.starter.version>2.1.1</mybatis.starter.version>
Expand Down Expand Up @@ -65,7 +65,7 @@
<profile>
<id>sonatype-oss-release</id>
<properties>
<project.release.version>3.0.10</project.release.version>
<project.release.version>3.1.0</project.release.version>
</properties>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @author yizzuide
* @since 3.0.0
* @since 3.0.3
* @since 3.1.0
* Create at 2020/04/05 18:47
*/
@Data
Expand Down Expand Up @@ -46,9 +46,9 @@ public static class Strategy {
*/
private List<String> paths = Collections.singletonList("/**");
/**
* 策略模板,默认能识别的占位符:uri、method、params(参数可配置占位符,请求头固定使用[]
* 策略模板(固定占位符:uri、method、params;请求参数域:$params.name;请求头域:$header.name;cookie域:$cookie.name
*/
private String tpl = "{\"uri\":\"{uri}\", \"method\": \"{method}\", \"params\": \"{params}\", \"token\": \"(token)\"}";
private String tpl = "{\"uri\":\"{uri}\", \"method\": \"{method}\", \"params\": \"{params}\", \"token\": \"{$header.token}\"}";
/**
* 缓存占位符(模块内部使用)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @author yizzuide
* @since 3.0.0
* @version 3.0.4
* @version 3.1.0
* Create at 2020/04/08 11:12
*/
@Data
Expand Down Expand Up @@ -63,9 +63,9 @@ public static class Limiter {
private Map<String, Object> props;

/**
* 分布式key模板
* 分布式key模板(固定占位符:uri、method、params;请求参数域:$params.name;请求头域:$header.name;cookie域:$cookie.name)
*/
private String keyTpl = "limit_{method}_{uri}_(token)";
private String keyTpl = "limit_{method}_{uri}_{$header.token}";

/**
* 分布式key过期时间
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,48 @@
import com.github.yizzuide.milkomeda.util.JSONUtil;
import com.github.yizzuide.milkomeda.util.PlaceholderResolver;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* URLPlaceholderParser
* URL占位符解析
*
* @author yizzuide
* @since 3.0.0
* @version 3.0.3
* @version 3.1.0
* Create at 2020/04/09 15:07
*/
@Data
public class URLPlaceholderParser {

// 请求参数解析
private PlaceholderResolver paramsPlaceholderResolver;
// 请求头解析
private static PlaceholderResolver headerPlaceholderResolver = PlaceholderResolver.getResolver("(", ")");
// 忽略的固定参数
private static List<String> ignorePlaceHolders = Arrays.asList("uri", "method", "params");
private PlaceholderResolver placeholderResolver;
private String paramsPrefix;
private String paramsSuffix;

/**
* 自定义请求参数解析器
*/
private URLPlaceholderResolver customURLPlaceholderResolver;

// 固定参数
private static List<String> ignorePlaceHolders = Arrays.asList("uri", "method", "params");

// 请求数据域
private static final String headerStartToken = "$header.";
private static final String cookieStartToken = "$cookie.";
private static final String paramsStartToken = "$params.";

public static final String KEY_HEAD = "header";
public static final String KEY_COOKIE = "cookie";
public static final String KEY_PARAMS = "params";

public URLPlaceholderParser() {
Expand All @@ -50,7 +59,9 @@ public URLPlaceholderParser() {
* @param paramsSuffix 参数占位符后缀
*/
public URLPlaceholderParser(String paramsPrefix, String paramsSuffix) {
paramsPlaceholderResolver = PlaceholderResolver.getResolver(paramsPrefix, paramsSuffix);
this.paramsPrefix = paramsPrefix;
this.paramsSuffix = paramsSuffix;
placeholderResolver = PlaceholderResolver.getResolver(paramsPrefix, paramsSuffix);
}

/**
Expand All @@ -59,9 +70,11 @@ public URLPlaceholderParser(String paramsPrefix, String paramsSuffix) {
* @return Map
*/
public Map<String, List<String>> grabPlaceHolders(String tpl) {
Map<String, List<String>> keyMap = new HashMap<>(4);
keyMap.put(KEY_HEAD, headerPlaceholderResolver.getPlaceHolders(tpl));
keyMap.put(KEY_PARAMS, paramsPlaceholderResolver.getPlaceHolders(tpl));
List<String> placeHolders = placeholderResolver.getPlaceHolders(tpl);
Map<String, List<String>> keyMap = new HashMap<>(6);
keyMap.put(KEY_HEAD, placeHolders.stream().filter(s -> s.startsWith(headerStartToken)).collect(Collectors.toList()));
keyMap.put(KEY_COOKIE, placeHolders.stream().filter(s -> s.startsWith(cookieStartToken)).collect(Collectors.toList()));
keyMap.put(KEY_PARAMS, placeHolders.stream().filter(s -> s.startsWith(paramsStartToken)).collect(Collectors.toList()));
return keyMap;
}

Expand Down Expand Up @@ -90,22 +103,35 @@ public String parse(String tpl, HttpServletRequest request, String params, Map<S
// 参数占位替换
for (String placeHolder : placeHolders.get(KEY_PARAMS)) {
if (ignorePlaceHolders.contains(placeHolder)) continue;
Object paramValue = paramsMap == null ? request.getParameter(placeHolder) :
DataTypeConvertUtil.extractPath(placeHolder, paramsMap, "");
String actualPlaceHolder = placeHolder.substring(paramsStartToken.length());
Object paramValue = paramsMap == null ? request.getParameter(actualPlaceHolder) :
DataTypeConvertUtil.extractPath(actualPlaceHolder, paramsMap, "");
placeholderResultMap.put(placeHolder, paramValue == null ? (this.customURLPlaceholderResolver == null ? "" :
String.valueOf(this.customURLPlaceholderResolver.resolver(placeHolder, request))) : paramValue);
}

if (CollectionUtils.isEmpty(placeHolders.get(KEY_HEAD))) {
return paramsPlaceholderResolver.resolveByMap(tpl, placeholderResultMap);
String.valueOf(this.customURLPlaceholderResolver.resolver(actualPlaceHolder, request))) : paramValue);
}

// 请求头占位替换
for (String placeHolder : placeHolders.get(KEY_HEAD)) {
String headerValue = request.getHeader(placeHolder);
String actualPlaceHolder = placeHolder.substring(headerStartToken.length());
String headerValue = request.getHeader(actualPlaceHolder);
placeholderResultMap.put(placeHolder, headerValue == null ? (this.customURLPlaceholderResolver == null ? "" :
String.valueOf(this.customURLPlaceholderResolver.resolver(placeHolder, request))) : headerValue);
String.valueOf(this.customURLPlaceholderResolver.resolver(actualPlaceHolder, request))) : headerValue);
}

// cookie占位替换
for (String placeHolder : placeHolders.get(KEY_COOKIE)) {
String actualPlaceHolder = placeHolder.substring(cookieStartToken.length());
String cookieValue = null;
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
if (cookie.getName().equals(actualPlaceHolder)) {
cookieValue = cookie.getValue();
break;
}
}
placeholderResultMap.put(placeHolder, cookieValue == null ? (this.customURLPlaceholderResolver == null ? "" :
String.valueOf(this.customURLPlaceholderResolver.resolver(actualPlaceHolder, request))) : cookieValue);
}
return headerPlaceholderResolver.resolveByMap(paramsPlaceholderResolver.resolveByMap(tpl, placeholderResultMap), placeholderResultMap);
return placeholderResolver.resolveByMap(tpl, placeholderResultMap);
}
}
2 changes: 1 addition & 1 deletion MilkomedaDemo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
<milkomeda.version>3.0.10-SNAPSHOT</milkomeda.version>
<milkomeda.version>3.1.0-SNAPSHOT</milkomeda.version>
<mybatis.starter>2.1.1</mybatis.starter>
</properties>

Expand Down
2 changes: 1 addition & 1 deletion MilkomedaDemo/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ milkomeda:
# 根据路径匹配策略
strategy:
- paths: ["/**"]
tpl: url->{uri},userId:{uid},token:(token),method->{method},parameters->{params},dataId->{data.id}
tpl: url->{uri},userId:{uid},token:{$header.token},method->{method},parameters->{params},dataId->{$params.data.id}
- paths: ["/error/**"]
tpl: url->{uri},method->{method}
# 日志收集器
Expand Down

0 comments on commit f599fc2

Please sign in to comment.