|
17 | 17 |
|
18 | 18 | import com.fasterxml.jackson.databind.JsonNode;
|
19 | 19 | import io.cloudevents.CloudEvent;
|
| 20 | +import io.serverlessworkflow.api.types.EventData; |
| 21 | +import io.serverlessworkflow.api.types.EventDataschema; |
20 | 22 | import io.serverlessworkflow.api.types.EventProperties;
|
21 |
| -import io.serverlessworkflow.impl.ExpressionHolder; |
22 |
| -import io.serverlessworkflow.impl.TaskContext; |
23 |
| -import io.serverlessworkflow.impl.WorkflowContext; |
| 23 | +import io.serverlessworkflow.api.types.EventSource; |
| 24 | +import io.serverlessworkflow.api.types.EventTime; |
| 25 | +import io.serverlessworkflow.api.types.UriTemplate; |
24 | 26 | import io.serverlessworkflow.impl.WorkflowFilter;
|
| 27 | +import io.serverlessworkflow.impl.WorkflowUtils; |
| 28 | +import io.serverlessworkflow.impl.expressions.Expression; |
25 | 29 | import io.serverlessworkflow.impl.expressions.ExpressionFactory;
|
26 | 30 | import io.serverlessworkflow.impl.json.JsonUtils;
|
27 |
| -import java.util.Optional; |
| 31 | +import java.net.URI; |
| 32 | +import java.time.OffsetDateTime; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.Objects; |
28 | 35 |
|
29 | 36 | public class DefaultCloudEventPredicate implements CloudEventPredicate {
|
30 | 37 |
|
31 |
| - private final EventPropertiesFilter props; |
| 38 | + private final CloudEventAttrPredicate<String> idFilter; |
| 39 | + private final CloudEventAttrPredicate<URI> sourceFilter; |
| 40 | + private final CloudEventAttrPredicate<String> subjectFilter; |
| 41 | + private final CloudEventAttrPredicate<String> contentTypeFilter; |
| 42 | + private final CloudEventAttrPredicate<String> typeFilter; |
| 43 | + private final CloudEventAttrPredicate<URI> dataSchemaFilter; |
| 44 | + private final CloudEventAttrPredicate<OffsetDateTime> timeFilter; |
| 45 | + private final CloudEventAttrPredicate<JsonNode> dataFilter; |
| 46 | + private final CloudEventAttrPredicate<JsonNode> additionalFilter; |
| 47 | + |
| 48 | + private static final <T> CloudEventAttrPredicate<T> isTrue() { |
| 49 | + return x -> true; |
| 50 | + } |
32 | 51 |
|
33 | 52 | public DefaultCloudEventPredicate(EventProperties properties, ExpressionFactory exprFactory) {
|
34 |
| - this.props = EventPropertiesFilter.build(properties, exprFactory); |
| 53 | + idFilter = stringFilter(properties.getId()); |
| 54 | + subjectFilter = stringFilter(properties.getSubject()); |
| 55 | + typeFilter = stringFilter(properties.getType()); |
| 56 | + contentTypeFilter = stringFilter(properties.getDatacontenttype()); |
| 57 | + sourceFilter = sourceFilter(properties.getSource(), exprFactory); |
| 58 | + dataSchemaFilter = dataSchemaFilter(properties.getDataschema(), exprFactory); |
| 59 | + timeFilter = offsetTimeFilter(properties.getTime(), exprFactory); |
| 60 | + dataFilter = dataFilter(properties.getData(), exprFactory); |
| 61 | + additionalFilter = additionalFilter(properties.getAdditionalProperties(), exprFactory); |
35 | 62 | }
|
36 | 63 |
|
37 |
| - @Override |
38 |
| - public boolean test(CloudEvent event, WorkflowContext workflow, TaskContext task) { |
39 |
| - return test(props.idFilter(), event.getId(), workflow, task) |
40 |
| - && test(props.sourceFilter(), event.getSource().toString(), workflow, task) |
41 |
| - && test(props.subjectFilter(), event.getSubject(), workflow, task) |
42 |
| - && test(props.contentTypeFilter(), event.getDataContentType(), workflow, task) |
43 |
| - && test(props.typeFilter(), event.getType(), workflow, task) |
44 |
| - && test(props.dataSchemaFilter(), event.getDataSchema().toString(), workflow, task) |
45 |
| - && test(props.timeFilter(), event.getTime(), workflow, task) |
46 |
| - && test(props.dataFilter(), CloudEventUtils.toJsonNode(event.getData()), workflow, task) |
47 |
| - && test( |
48 |
| - props.additionalFilter(), |
49 |
| - JsonUtils.fromValue(CloudEventUtils.extensions(event)), |
50 |
| - workflow, |
51 |
| - task); |
| 64 | + private CloudEventAttrPredicate<JsonNode> additionalFilter( |
| 65 | + Map<String, Object> additionalProperties, ExpressionFactory exprFactory) { |
| 66 | + return additionalProperties != null && !additionalProperties.isEmpty() |
| 67 | + ? from(WorkflowUtils.buildWorkflowFilter(exprFactory, null, additionalProperties)) |
| 68 | + : isTrue(); |
| 69 | + } |
| 70 | + |
| 71 | + private CloudEventAttrPredicate<JsonNode> from(WorkflowFilter filter) { |
| 72 | + return d -> filter.apply(null, null, d).asBoolean(); |
| 73 | + } |
| 74 | + |
| 75 | + private CloudEventAttrPredicate<JsonNode> dataFilter( |
| 76 | + EventData data, ExpressionFactory exprFactory) { |
| 77 | + return data != null |
| 78 | + ? from( |
| 79 | + WorkflowUtils.buildWorkflowFilter( |
| 80 | + exprFactory, data.getRuntimeExpression(), data.getObject())) |
| 81 | + : isTrue(); |
| 82 | + } |
| 83 | + |
| 84 | + private CloudEventAttrPredicate<OffsetDateTime> offsetTimeFilter( |
| 85 | + EventTime time, ExpressionFactory exprFactory) { |
| 86 | + if (time != null) { |
| 87 | + if (time.getRuntimeExpression() != null) { |
| 88 | + final Expression expr = exprFactory.getExpression(time.getRuntimeExpression()); |
| 89 | + return s -> evalExpr(expr, toString(s)); |
| 90 | + } else if (time.getLiteralTime() != null) { |
| 91 | + return s -> Objects.equals(s, CloudEventUtils.toOffset(time.getLiteralTime())); |
| 92 | + } |
| 93 | + } |
| 94 | + return isTrue(); |
| 95 | + } |
| 96 | + |
| 97 | + private CloudEventAttrPredicate<URI> dataSchemaFilter( |
| 98 | + EventDataschema dataSchema, ExpressionFactory exprFactory) { |
| 99 | + if (dataSchema != null) { |
| 100 | + if (dataSchema.getExpressionDataSchema() != null) { |
| 101 | + final Expression expr = exprFactory.getExpression(dataSchema.getExpressionDataSchema()); |
| 102 | + return s -> evalExpr(expr, toString(s)); |
| 103 | + } else if (dataSchema.getLiteralDataSchema() != null) { |
| 104 | + return templateFilter(dataSchema.getLiteralDataSchema()); |
| 105 | + } |
| 106 | + } |
| 107 | + return isTrue(); |
| 108 | + } |
| 109 | + |
| 110 | + private CloudEventAttrPredicate<String> stringFilter(String str) { |
| 111 | + return str == null ? isTrue() : x -> x.equals(str); |
| 112 | + } |
| 113 | + |
| 114 | + private CloudEventAttrPredicate<URI> sourceFilter( |
| 115 | + EventSource source, ExpressionFactory exprFactory) { |
| 116 | + if (source != null) { |
| 117 | + if (source.getRuntimeExpression() != null) { |
| 118 | + final Expression expr = exprFactory.getExpression(source.getRuntimeExpression()); |
| 119 | + return s -> evalExpr(expr, toString(s)); |
| 120 | + } else if (source.getUriTemplate() != null) { |
| 121 | + return templateFilter(source.getUriTemplate()); |
| 122 | + } |
| 123 | + } |
| 124 | + return isTrue(); |
52 | 125 | }
|
53 | 126 |
|
54 |
| - private <T, V extends ExpressionHolder<T>> boolean test( |
55 |
| - Optional<V> optFilter, T value, WorkflowContext workflow, TaskContext task) { |
56 |
| - return optFilter.map(filter -> filter.apply(workflow, task).equals(value)).orElse(true); |
| 127 | + private CloudEventAttrPredicate<URI> templateFilter(UriTemplate template) { |
| 128 | + if (template.getLiteralUri() != null) { |
| 129 | + return u -> Objects.equals(u, template.getLiteralUri()); |
| 130 | + } |
| 131 | + throw new UnsupportedOperationException("Template not supporte here yet"); |
57 | 132 | }
|
58 | 133 |
|
59 |
| - private boolean test( |
60 |
| - Optional<WorkflowFilter> optFilter, |
61 |
| - JsonNode value, |
62 |
| - WorkflowContext workflow, |
63 |
| - TaskContext task) { |
64 |
| - return optFilter |
65 |
| - .map(filter -> filter.apply(workflow, task, task.input()).equals(value)) |
66 |
| - .orElse(true); |
| 134 | + private <T> String toString(T uri) { |
| 135 | + return uri != null ? uri.toString() : null; |
| 136 | + } |
| 137 | + |
| 138 | + private <T> boolean evalExpr(Expression expr, T value) { |
| 139 | + return expr.eval(null, null, JsonUtils.fromValue(value)).asBoolean(); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public boolean test(CloudEvent event) { |
| 144 | + return idFilter.test(event.getId()) |
| 145 | + && sourceFilter.test(event.getSource()) |
| 146 | + && subjectFilter.test(event.getSubject()) |
| 147 | + && contentTypeFilter.test(event.getDataContentType()) |
| 148 | + && typeFilter.test(event.getType()) |
| 149 | + && dataSchemaFilter.test(event.getDataSchema()) |
| 150 | + && timeFilter.test(event.getTime()) |
| 151 | + && dataFilter.test(CloudEventUtils.toJsonNode(event.getData())) |
| 152 | + && additionalFilter.test(JsonUtils.fromValue(CloudEventUtils.extensions(event))); |
67 | 153 | }
|
68 | 154 | }
|
0 commit comments