support new label expression (#627)
parent
008551b15f
commit
e33f60bdaa
@ -0,0 +1,95 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed
|
||||||
|
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||||
|
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.tencent.cloud.common.util.expresstion;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Expression parser for rate limit rule and router rule.
|
||||||
|
* @author lepdou 2022-10-08
|
||||||
|
*/
|
||||||
|
public interface ExpressionParser {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* whether is valid expression.
|
||||||
|
* @param expression the expression
|
||||||
|
* @return true if is valid
|
||||||
|
*/
|
||||||
|
boolean isExpressionLabel(String expression);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* whether is header expression.
|
||||||
|
* @param expression the expression
|
||||||
|
* @return true if is header expression
|
||||||
|
*/
|
||||||
|
boolean isHeaderLabel(String expression);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* parse label from header expression.
|
||||||
|
* @param expression the expression
|
||||||
|
* @return parsed key from expression
|
||||||
|
*/
|
||||||
|
String parseHeaderKey(String expression);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* whether is query expression.
|
||||||
|
* @param expression the expression
|
||||||
|
* @return true if is query expression
|
||||||
|
*/
|
||||||
|
boolean isQueryLabel(String expression);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* parse label from query expression.
|
||||||
|
* @param expression the expression
|
||||||
|
* @return parsed key from expression
|
||||||
|
*/
|
||||||
|
String parseQueryKey(String expression);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* whether is cookie expression.
|
||||||
|
* @param expression the expression
|
||||||
|
* @return true if is cookie expression
|
||||||
|
*/
|
||||||
|
boolean isCookieLabel(String expression);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* parse label from cookie expression.
|
||||||
|
* @param expression the expression
|
||||||
|
* @return parsed cookie key from expression
|
||||||
|
*/
|
||||||
|
String parseCookieKey(String expression);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* whether is method expression.
|
||||||
|
* @param expression the expression
|
||||||
|
* @return true if is method expression
|
||||||
|
*/
|
||||||
|
boolean isMethodLabel(String expression);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* whether is uri/path expression.
|
||||||
|
* @param expression the expression
|
||||||
|
* @return true if is uri/path expression
|
||||||
|
*/
|
||||||
|
boolean isUriLabel(String expression);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* whether is caller ip expression.
|
||||||
|
* @param expression the expression
|
||||||
|
* @return true if is caller ip expression
|
||||||
|
*/
|
||||||
|
boolean isCallerIPLabel(String expression);
|
||||||
|
}
|
@ -0,0 +1,93 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed
|
||||||
|
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||||
|
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.tencent.cloud.common.util.expresstion;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Old custom expression resolver like ${http.query.key}、${http.header.key}.
|
||||||
|
* New expression like $query.key、$header.key
|
||||||
|
* @author lepdou 2022-10-08
|
||||||
|
*/
|
||||||
|
public class ExpressionParserV1 implements ExpressionParser {
|
||||||
|
|
||||||
|
private static final String LABEL_HEADER_PREFIX = "${http.header.";
|
||||||
|
private static final int LABEL_HEADER_PREFIX_LEN = LABEL_HEADER_PREFIX.length();
|
||||||
|
private static final String LABEL_QUERY_PREFIX = "${http.query.";
|
||||||
|
private static final int LABEL_QUERY_PREFIX_LEN = LABEL_QUERY_PREFIX.length();
|
||||||
|
private static final String LABEL_COOKIE_PREFIX = "${http.cookie.";
|
||||||
|
private static final int LABEL_COOKIE_PREFIX_LEN = LABEL_COOKIE_PREFIX.length();
|
||||||
|
private static final String LABEL_METHOD = "${http.method}";
|
||||||
|
private static final String LABEL_URI = "${http.uri}";
|
||||||
|
private static final String LABEL_CALLER_IP = "${http.caller.ip}";
|
||||||
|
private static final String LABEL_PREFIX = "${";
|
||||||
|
private static final String LABEL_SUFFIX = "}";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isExpressionLabel(String labelKey) {
|
||||||
|
if (StringUtils.isEmpty(labelKey)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return StringUtils.startsWith(labelKey, LABEL_PREFIX) && StringUtils.endsWith(labelKey, LABEL_SUFFIX);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isHeaderLabel(String expression) {
|
||||||
|
return StringUtils.startsWith(expression, LABEL_HEADER_PREFIX) && StringUtils.endsWith(expression, LABEL_SUFFIX);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String parseHeaderKey(String expression) {
|
||||||
|
return expression.substring(LABEL_HEADER_PREFIX_LEN, expression.length() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isQueryLabel(String expression) {
|
||||||
|
return StringUtils.startsWith(expression, LABEL_QUERY_PREFIX) && StringUtils.endsWith(expression, LABEL_SUFFIX);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String parseQueryKey(String expression) {
|
||||||
|
return expression.substring(LABEL_QUERY_PREFIX_LEN, expression.length() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCookieLabel(String expression) {
|
||||||
|
return StringUtils.startsWith(expression, LABEL_COOKIE_PREFIX) && StringUtils.endsWith(expression, LABEL_SUFFIX);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String parseCookieKey(String expression) {
|
||||||
|
return expression.substring(LABEL_COOKIE_PREFIX_LEN, expression.length() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isMethodLabel(String expression) {
|
||||||
|
return StringUtils.equalsIgnoreCase(expression, LABEL_METHOD);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isUriLabel(String expression) {
|
||||||
|
return StringUtils.equalsIgnoreCase(expression, LABEL_URI);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCallerIPLabel(String expression) {
|
||||||
|
return StringUtils.equalsIgnoreCase(expression, LABEL_CALLER_IP);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,88 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed
|
||||||
|
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||||
|
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.tencent.cloud.common.util.expresstion;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* New custom expression resolver like $query.key、$header.key.
|
||||||
|
* Old expression like ${http.query.key}、${http.header.key}
|
||||||
|
* @author lepdou 2022-10-08
|
||||||
|
*/
|
||||||
|
public class ExpressionParserV2 implements ExpressionParser {
|
||||||
|
|
||||||
|
private static final String LABEL_HEADER_PREFIX = "$header.";
|
||||||
|
private static final int LABEL_HEADER_PREFIX_LEN = LABEL_HEADER_PREFIX.length();
|
||||||
|
private static final String LABEL_QUERY_PREFIX = "$query.";
|
||||||
|
private static final int LABEL_QUERY_PREFIX_LEN = LABEL_QUERY_PREFIX.length();
|
||||||
|
private static final String LABEL_METHOD = "$method";
|
||||||
|
private static final String LABEL_PATH = "$path";
|
||||||
|
private static final String LABEL_CALLER_IP = "$caller_ip";
|
||||||
|
private static final String LABEL_PREFIX = "$";
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isExpressionLabel(String expression) {
|
||||||
|
return StringUtils.startsWith(expression, LABEL_PREFIX);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isHeaderLabel(String expression) {
|
||||||
|
return StringUtils.startsWith(expression, LABEL_HEADER_PREFIX);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String parseHeaderKey(String expression) {
|
||||||
|
return StringUtils.substring(expression, LABEL_HEADER_PREFIX_LEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isQueryLabel(String expression) {
|
||||||
|
return StringUtils.startsWith(expression, LABEL_QUERY_PREFIX);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String parseQueryKey(String expression) {
|
||||||
|
return StringUtils.substring(expression, LABEL_QUERY_PREFIX_LEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCookieLabel(String expression) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String parseCookieKey(String expression) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isMethodLabel(String expression) {
|
||||||
|
return StringUtils.equalsIgnoreCase(expression, LABEL_METHOD);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isUriLabel(String expression) {
|
||||||
|
return StringUtils.equalsIgnoreCase(expression, LABEL_PATH);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCallerIPLabel(String expression) {
|
||||||
|
return StringUtils.equalsIgnoreCase(expression, LABEL_CALLER_IP);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed
|
||||||
|
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||||
|
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.tencent.cloud.common.util;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.util.expresstion.ExpressionParserV1;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link ExpressionParserV1}
|
||||||
|
* @author lepdou 2022-10-08
|
||||||
|
*/
|
||||||
|
public class ExpressionParserV1Test {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testExpressionLabel() {
|
||||||
|
String validLabel1 = "${http.query.uid}";
|
||||||
|
String validLabel2 = "${http.header.uid}";
|
||||||
|
String validLabel3 = "${http.cookie.uid}";
|
||||||
|
String validLabel4 = "${http.method}";
|
||||||
|
String validLabel5 = "${http.uri}";
|
||||||
|
String invalidLabel1 = "${http.queryuid}";
|
||||||
|
String invalidLabel2 = "{http.query.uid}";
|
||||||
|
String invalidLabel3 = "${http.query.uid";
|
||||||
|
String invalidLabel4 = "$ {http.query.uid}";
|
||||||
|
String invalidLabel5 = "${ http.query.uid}";
|
||||||
|
String invalidLabel6 = "${query.uid}";
|
||||||
|
String invalidLabel7 = "http.query.uid";
|
||||||
|
String invalidLabel8 = "$${http.uri}";
|
||||||
|
String invalidLabel9 = "#{http.uri}";
|
||||||
|
|
||||||
|
ExpressionParserV1 parser = new ExpressionParserV1();
|
||||||
|
|
||||||
|
Assert.assertTrue(parser.isExpressionLabel(validLabel1));
|
||||||
|
Assert.assertTrue(parser.isExpressionLabel(validLabel2));
|
||||||
|
Assert.assertTrue(parser.isExpressionLabel(validLabel3));
|
||||||
|
Assert.assertTrue(parser.isExpressionLabel(validLabel4));
|
||||||
|
Assert.assertTrue(parser.isExpressionLabel(validLabel5));
|
||||||
|
Assert.assertTrue(parser.isExpressionLabel(invalidLabel1));
|
||||||
|
Assert.assertFalse(parser.isExpressionLabel(invalidLabel2));
|
||||||
|
Assert.assertFalse(parser.isExpressionLabel(invalidLabel3));
|
||||||
|
Assert.assertFalse(parser.isExpressionLabel(invalidLabel4));
|
||||||
|
Assert.assertTrue(parser.isExpressionLabel(invalidLabel5));
|
||||||
|
Assert.assertTrue(parser.isExpressionLabel(invalidLabel6));
|
||||||
|
Assert.assertFalse(parser.isExpressionLabel(invalidLabel7));
|
||||||
|
Assert.assertFalse(parser.isExpressionLabel(invalidLabel8));
|
||||||
|
Assert.assertFalse(parser.isExpressionLabel(invalidLabel9));
|
||||||
|
|
||||||
|
Assert.assertTrue(parser.isQueryLabel(validLabel1));
|
||||||
|
Assert.assertTrue(parser.isHeaderLabel(validLabel2));
|
||||||
|
Assert.assertTrue(parser.isCookieLabel(validLabel3));
|
||||||
|
Assert.assertTrue(parser.isMethodLabel(validLabel4));
|
||||||
|
Assert.assertTrue(parser.isUriLabel(validLabel5));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed
|
||||||
|
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||||
|
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.tencent.cloud.common.util;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.util.expresstion.ExpressionParserV2;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link ExpressionParserV2}
|
||||||
|
* @author lepdou 2022-10-08
|
||||||
|
*/
|
||||||
|
public class ExpressionParserV2Test {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testExpressionLabel() {
|
||||||
|
String validLabel1 = "${http.query.uid}";
|
||||||
|
String validLabel2 = "${http.header.uid}";
|
||||||
|
String validLabel3 = "${http.cookie.uid}";
|
||||||
|
String validLabel4 = "${http.method}";
|
||||||
|
String validLabel5 = "${http.uri}";
|
||||||
|
String invalidLabel1 = "${http.queryuid}";
|
||||||
|
String invalidLabel2 = "{http.query.uid}";
|
||||||
|
String invalidLabel3 = "${http.query.uid";
|
||||||
|
String invalidLabel4 = "$ {http.query.uid}";
|
||||||
|
String invalidLabel5 = "${ http.query.uid}";
|
||||||
|
String invalidLabel6 = "${query.uid}";
|
||||||
|
String invalidLabel7 = "http.query.uid";
|
||||||
|
String invalidLabel8 = "$${http.uri}";
|
||||||
|
String invalidLabel9 = "#{http.uri}";
|
||||||
|
|
||||||
|
ExpressionParserV2 parser = new ExpressionParserV2();
|
||||||
|
|
||||||
|
Assert.assertTrue(parser.isExpressionLabel(validLabel1));
|
||||||
|
Assert.assertTrue(parser.isExpressionLabel(validLabel2));
|
||||||
|
Assert.assertTrue(parser.isExpressionLabel(validLabel3));
|
||||||
|
Assert.assertTrue(parser.isExpressionLabel(validLabel4));
|
||||||
|
Assert.assertTrue(parser.isExpressionLabel(validLabel5));
|
||||||
|
Assert.assertTrue(parser.isExpressionLabel(invalidLabel1));
|
||||||
|
Assert.assertFalse(parser.isExpressionLabel(invalidLabel2));
|
||||||
|
Assert.assertTrue(parser.isExpressionLabel(invalidLabel3));
|
||||||
|
Assert.assertTrue(parser.isExpressionLabel(invalidLabel4));
|
||||||
|
Assert.assertTrue(parser.isExpressionLabel(invalidLabel5));
|
||||||
|
Assert.assertTrue(parser.isExpressionLabel(invalidLabel6));
|
||||||
|
Assert.assertFalse(parser.isExpressionLabel(invalidLabel7));
|
||||||
|
Assert.assertTrue(parser.isExpressionLabel(invalidLabel8));
|
||||||
|
Assert.assertFalse(parser.isExpressionLabel(invalidLabel9));
|
||||||
|
|
||||||
|
Assert.assertFalse(parser.isQueryLabel(validLabel1));
|
||||||
|
Assert.assertFalse(parser.isHeaderLabel(validLabel2));
|
||||||
|
Assert.assertFalse(parser.isCookieLabel(validLabel3));
|
||||||
|
Assert.assertFalse(parser.isMethodLabel(validLabel4));
|
||||||
|
Assert.assertFalse(parser.isUriLabel(validLabel5));
|
||||||
|
|
||||||
|
Assert.assertTrue(parser.isHeaderLabel("$header.userId"));
|
||||||
|
Assert.assertTrue(parser.isMethodLabel("$method"));
|
||||||
|
Assert.assertTrue(parser.isQueryLabel("$query.userId"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue