forked from Nepxion/Discovery
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
196 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/util/SpelUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.nepxion.discovery.plugin.framework.util; | ||
|
||
/** | ||
* <p>Title: Nepxion Discovery</p> | ||
* <p>Description: Nepxion Discovery</p> | ||
* <p>Copyright: Copyright (c) 2017-2050</p> | ||
* <p>Company: Nepxion</p> | ||
* @author Haojun Ren | ||
* @version 1.0 | ||
*/ | ||
|
||
import java.util.Map; | ||
|
||
import org.springframework.expression.ExpressionParser; | ||
import org.springframework.expression.spel.standard.SpelExpressionParser; | ||
import org.springframework.expression.spel.support.StandardEvaluationContext; | ||
|
||
public class SpelUtil { | ||
private static final ExpressionParser EXPRESSION_PARSER = new SpelExpressionParser(); | ||
|
||
public static boolean eval(String expression, String mapKey, Map<String, String> map) { | ||
StandardEvaluationContext context = new StandardEvaluationContext(); | ||
context.setVariable(mapKey, map); | ||
|
||
return eval(expression, context); | ||
} | ||
|
||
public static boolean eval(String expression, Map<String, Map<String, String>> map) { | ||
StandardEvaluationContext context = new StandardEvaluationContext(); | ||
for (Map.Entry<String, Map<String, String>> entry : map.entrySet()) { | ||
context.setVariable(entry.getKey(), entry.getValue()); | ||
} | ||
|
||
return eval(expression, context); | ||
} | ||
|
||
public static boolean eval(String expression, StandardEvaluationContext context) { | ||
Boolean result = EXPRESSION_PARSER.parseExpression(expression).getValue(context, Boolean.class); | ||
|
||
return result != null ? result.booleanValue() : false; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...-framework/src/test/java/com/nepxion/discovery/plugin/framework/loadbalance/SpelTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.nepxion.discovery.plugin.framework.loadbalance; | ||
|
||
/** | ||
* <p>Title: Nepxion Discovery</p> | ||
* <p>Description: Nepxion Discovery</p> | ||
* <p>Copyright: Copyright (c) 2017-2050</p> | ||
* <p>Company: Nepxion</p> | ||
* @author Haojun Ren | ||
* @version 1.0 | ||
*/ | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import com.nepxion.discovery.common.util.StringUtil; | ||
import com.nepxion.discovery.plugin.framework.util.SpelUtil; | ||
|
||
public class SpelTest { | ||
public static void main(String[] args) { | ||
System.out.println(test1()); | ||
System.out.println(test2()); | ||
System.out.println(test3()); | ||
} | ||
|
||
private static boolean test1() { | ||
// String expression = "#H['a'] == '123' && #H['b'] == '456'"; | ||
// String expression = "#H['a'] == '123' || #H['b'] == '456'"; | ||
// String expression = "#H['a'] >= '123' && #H['b'] <= '456'"; | ||
// String expression = "#H['a'] >= '123' || #H['b'] <= '456'"; | ||
String expression = "#H['a'] != '123' || #H['b'] != '456'"; | ||
|
||
Map<String, String> headerMap = new HashMap<String, String>(); | ||
headerMap.put("a", "123"); | ||
headerMap.put("b", "456"); | ||
|
||
return SpelUtil.eval(expression, "H", headerMap); | ||
} | ||
|
||
private static List<String> test2() { | ||
String regex = "\\#H\\['\\S+'\\]"; | ||
Pattern pattern = Pattern.compile(regex); | ||
|
||
String expression = "#H['a-A'] != '123' || #H['b//SS'] != '456' && #H['C**44!!66'] = 123"; | ||
Matcher matcher = pattern.matcher(expression); | ||
|
||
List<String> list = new ArrayList<String>(); | ||
while (matcher.find()) { | ||
String group = matcher.group(); | ||
String value = StringUtils.substringBetween(group, "#H['", "']"); | ||
list.add(value); | ||
} | ||
|
||
return list; | ||
} | ||
|
||
private static int test3() { | ||
String expression = "#H['a-A'] != '123' || #H['b//SS'] != '456' && #H['C**44!!66'] = 123"; | ||
String key = "#H['"; | ||
|
||
return StringUtil.count(expression, key); | ||
} | ||
} |
37 changes: 0 additions & 37 deletions
37
...n/java/com/nepxion/discovery/plugin/strategy/condition/HeaderEqualsStrategyCondition.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.