Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* initial implementation -informal * initial implementation -informal * fix code style problem * remove unnecessary code snippets. * add license header * add some dep * fix JsonPathUtils checkstyle problem * fix test class checkstyle problem * fix CI problem
- Loading branch information
Showing
25 changed files
with
1,564 additions
and
0 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
68 changes: 68 additions & 0 deletions
68
eventmesh-common/src/main/java/org/apache/eventmesh/common/filter/PatternEntry.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 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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 org.apache.eventmesh.common.filter; | ||
|
||
import org.apache.eventmesh.common.filter.condition.Condition; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
public class PatternEntry { | ||
|
||
private final String patternPath; | ||
|
||
private final List<Condition> conditionList = new ArrayList<>(); | ||
|
||
public PatternEntry(final String patternPath) { | ||
this.patternPath = patternPath; | ||
} | ||
|
||
public void addRuleCondition(Condition patternCondition) { | ||
this.conditionList.add(patternCondition); | ||
} | ||
|
||
public String getPatternName() { | ||
return "123"; | ||
} | ||
|
||
public String getPatternPath() { | ||
return patternPath; | ||
} | ||
|
||
public boolean match(JsonNode jsonElement) { | ||
for (final Condition patternCondition : conditionList) { | ||
if (patternCondition.match(jsonElement)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
|
||
} | ||
|
||
/** | ||
* Returns the condition list for test only | ||
* | ||
* @return the condition list | ||
*/ | ||
List<Condition> getConditionList() { | ||
return conditionList; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...mmon/src/main/java/org/apache/eventmesh/common/filter/condition/AnythingButCondition.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,71 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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 org.apache.eventmesh.common.filter.condition; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map.Entry; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
class AnythingButCondition implements Condition { | ||
|
||
private List<Condition> conditionList = new ArrayList<>(); | ||
|
||
AnythingButCondition(JsonNode condition) { | ||
|
||
if (condition.isValueNode()) { | ||
this.conditionList.add(new SpecifiedCondition(condition)); | ||
} | ||
// [] | ||
if (condition.isArray()) { | ||
for (JsonNode element : condition) { | ||
this.conditionList.add(new SpecifiedCondition(element)); | ||
} | ||
} | ||
|
||
// prefix,suffix | ||
if (condition.isObject()) { | ||
Iterator<Entry<String, JsonNode>> iterator = condition.fields(); | ||
while (iterator.hasNext()) { | ||
Entry<String, JsonNode> entry = iterator.next(); | ||
String key = entry.getKey(); | ||
JsonNode value = entry.getValue(); | ||
Condition condition1 = new ConditionsBuilder().withKey(key).withParams(value).build(); | ||
this.conditionList.add(condition1); | ||
} | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public boolean match(JsonNode inputEvent) { | ||
if (inputEvent == null) { | ||
return false; | ||
} | ||
|
||
for (Condition condition : conditionList) { | ||
if (condition.match(inputEvent)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
eventmesh-common/src/main/java/org/apache/eventmesh/common/filter/condition/Condition.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,29 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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 org.apache.eventmesh.common.filter.condition; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
/** | ||
* | ||
*/ | ||
public interface Condition { | ||
|
||
boolean match(JsonNode inputEvent); | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
...-common/src/main/java/org/apache/eventmesh/common/filter/condition/ConditionsBuilder.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,65 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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 org.apache.eventmesh.common.filter.condition; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
public class ConditionsBuilder { | ||
|
||
private String key; | ||
private JsonNode jsonNode; | ||
|
||
public ConditionsBuilder withKey(String key) { | ||
this.key = key; | ||
return this; | ||
} | ||
|
||
public ConditionsBuilder withParams(JsonNode jsonNode) { | ||
this.jsonNode = jsonNode; | ||
return this; | ||
} | ||
|
||
public Condition build() { | ||
Condition condition = null; | ||
switch (this.key) { | ||
case "prefix": | ||
condition = new PrefixxCondition(this.jsonNode); | ||
break; | ||
case "suffix": | ||
condition = new SuffixCondition(this.jsonNode); | ||
break; | ||
case "anything-but": | ||
condition = new AnythingButCondition(this.jsonNode); | ||
break; | ||
case "numeric": | ||
condition = new NumericCondition(this.jsonNode); | ||
break; | ||
case "exists": | ||
condition = new ExistsCondition(this.jsonNode); | ||
break; | ||
case "specified": | ||
condition = new SpecifiedCondition(this.jsonNode); | ||
break; | ||
default: | ||
throw new RuntimeException("INVALID CONDITION"); | ||
// Add cases for other keys and conditions | ||
} | ||
|
||
return condition; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...sh-common/src/main/java/org/apache/eventmesh/common/filter/condition/ExistsCondition.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,35 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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 org.apache.eventmesh.common.filter.condition; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
class ExistsCondition implements Condition { | ||
|
||
private JsonNode exists; | ||
|
||
ExistsCondition(JsonNode exists) { | ||
this.exists = exists; | ||
} | ||
|
||
@Override | ||
public boolean match(JsonNode inputEvent) { | ||
|
||
return this.exists.asBoolean() == (inputEvent != null); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...h-common/src/main/java/org/apache/eventmesh/common/filter/condition/NumericCondition.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,80 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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 org.apache.eventmesh.common.filter.condition; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
class NumericCondition implements Condition { | ||
|
||
List<String> operators = new ArrayList<>(); | ||
List<Double> nums = new ArrayList<>(); | ||
|
||
NumericCondition(JsonNode numeric) { | ||
if (numeric.isArray()) { | ||
if (numeric.size() % 2 != 0) { | ||
throw new RuntimeException("NUMERIC NO RIGHT FORMAT"); | ||
} | ||
for (int i = 0; i < numeric.size(); i += 2) { | ||
JsonNode opt = numeric.get(i); | ||
JsonNode number = numeric.get(i + 1); | ||
operators.add(opt.asText()); | ||
nums.add(number.asDouble()); | ||
} | ||
} else { | ||
throw new RuntimeException("NUMERIC MUST BE ARRAY"); | ||
} | ||
} | ||
|
||
private boolean compareNums(Double rule, Double target, String opt) { | ||
int res = Double.compare(target, rule); | ||
switch (opt) { | ||
case "=": | ||
return res == 0; | ||
case "!=": | ||
return res != 0; | ||
case ">": | ||
return res > 0; | ||
case ">=": | ||
return res >= 0; | ||
case "<": | ||
return res < 0; | ||
case "<=": | ||
return res <= 0; | ||
default: // Never be here | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean match(JsonNode inputEvent) { | ||
if (inputEvent.isNumber()) { | ||
for (int i = 0; i < operators.size(); ++i) { | ||
if (!compareNums(nums.get(i), inputEvent.asDouble(), operators.get(i))) { | ||
return false; | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
return true; | ||
} | ||
} |
Oops, something went wrong.