|
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; |
22 | 20 | import io.serverlessworkflow.api.types.EventProperties;
|
23 |
| -import io.serverlessworkflow.api.types.EventSource; |
24 |
| -import io.serverlessworkflow.api.types.EventTime; |
25 | 21 | import io.serverlessworkflow.impl.ExpressionHolder;
|
26 |
| -import io.serverlessworkflow.impl.StringFilter; |
27 | 22 | import io.serverlessworkflow.impl.TaskContext;
|
28 | 23 | import io.serverlessworkflow.impl.WorkflowContext;
|
29 | 24 | import io.serverlessworkflow.impl.WorkflowFilter;
|
30 |
| -import io.serverlessworkflow.impl.WorkflowUtils; |
31 | 25 | import io.serverlessworkflow.impl.expressions.ExpressionFactory;
|
32 | 26 | import io.serverlessworkflow.impl.json.JsonUtils;
|
33 |
| -import java.time.OffsetDateTime; |
34 |
| -import java.time.ZoneOffset; |
35 |
| -import java.util.Map; |
36 | 27 | import java.util.Optional;
|
37 | 28 |
|
38 | 29 | public class DefaultCloudEventPredicate implements CloudEventPredicate {
|
39 | 30 |
|
40 |
| - private final Optional<StringFilter> idFilter; |
41 |
| - private final Optional<StringFilter> sourceFilter; |
42 |
| - private final Optional<StringFilter> subjectFilter; |
43 |
| - private final Optional<StringFilter> contentTypeFilter; |
44 |
| - private final Optional<StringFilter> typeFilter; |
45 |
| - private final Optional<StringFilter> dataSchemaFilter; |
46 |
| - private final Optional<ExpressionHolder<OffsetDateTime>> timeFilter; |
47 |
| - private final Optional<WorkflowFilter> dataFilter; |
48 |
| - private final Optional<WorkflowFilter> additionalFilter; |
| 31 | + private final EventPropertiesFilter props; |
49 | 32 |
|
50 | 33 | public DefaultCloudEventPredicate(EventProperties properties, ExpressionFactory exprFactory) {
|
51 |
| - this.idFilter = buildFilter(exprFactory, properties.getId()); |
52 |
| - EventSource source = properties.getSource(); |
53 |
| - this.sourceFilter = |
54 |
| - source == null |
55 |
| - ? Optional.empty() |
56 |
| - : Optional.of( |
57 |
| - WorkflowUtils.buildStringFilter( |
58 |
| - exprFactory, |
59 |
| - source.getRuntimeExpression(), |
60 |
| - WorkflowUtils.toString(source.getUriTemplate()))); |
61 |
| - this.subjectFilter = buildFilter(exprFactory, properties.getSubject()); |
62 |
| - this.contentTypeFilter = buildFilter(exprFactory, properties.getDatacontenttype()); |
63 |
| - this.typeFilter = buildFilter(exprFactory, properties.getType()); |
64 |
| - EventDataschema dataSchema = properties.getDataschema(); |
65 |
| - this.dataSchemaFilter = |
66 |
| - dataSchema == null |
67 |
| - ? Optional.empty() |
68 |
| - : Optional.of( |
69 |
| - WorkflowUtils.buildStringFilter( |
70 |
| - exprFactory, |
71 |
| - dataSchema.getExpressionDataSchema(), |
72 |
| - WorkflowUtils.toString(dataSchema.getLiteralDataSchema()))); |
73 |
| - EventTime time = properties.getTime(); |
74 |
| - this.timeFilter = |
75 |
| - time == null |
76 |
| - ? Optional.empty() |
77 |
| - : Optional.of( |
78 |
| - WorkflowUtils.buildExpressionHolder( |
79 |
| - exprFactory, |
80 |
| - time.getRuntimeExpression(), |
81 |
| - time.getLiteralTime().toInstant().atOffset(ZoneOffset.UTC), |
82 |
| - JsonUtils::toOffsetDateTime)); |
83 |
| - |
84 |
| - EventData data = properties.getData(); |
85 |
| - this.dataFilter = |
86 |
| - properties.getData() == null |
87 |
| - ? Optional.empty() |
88 |
| - : Optional.of( |
89 |
| - WorkflowUtils.buildWorkflowFilter( |
90 |
| - exprFactory, data.getRuntimeExpression(), data.getObject())); |
91 |
| - Map<String, Object> ceAttrs = properties.getAdditionalProperties(); |
92 |
| - this.additionalFilter = |
93 |
| - ceAttrs == null || ceAttrs.isEmpty() |
94 |
| - ? Optional.empty() |
95 |
| - : Optional.of(WorkflowUtils.buildWorkflowFilter(exprFactory, null, ceAttrs)); |
96 |
| - } |
97 |
| - |
98 |
| - private Optional<StringFilter> buildFilter(ExpressionFactory exprFactory, String str) { |
99 |
| - return str == null |
100 |
| - ? Optional.empty() |
101 |
| - : Optional.of(WorkflowUtils.buildStringFilter(exprFactory, str)); |
| 34 | + this.props = EventPropertiesFilter.build(properties, exprFactory); |
102 | 35 | }
|
103 | 36 |
|
104 | 37 | @Override
|
105 | 38 | public boolean test(CloudEvent event, WorkflowContext workflow, TaskContext task) {
|
106 |
| - return test(idFilter, event.getId(), workflow, task) |
107 |
| - && test(sourceFilter, event.getSource().toString(), workflow, task) |
108 |
| - && test(subjectFilter, event.getSubject(), workflow, task) |
109 |
| - && test(contentTypeFilter, event.getDataContentType(), workflow, task) |
110 |
| - && test(typeFilter, event.getType(), workflow, task) |
111 |
| - && test(dataSchemaFilter, event.getDataSchema().toString(), workflow, task) |
112 |
| - && test(timeFilter, event.getTime(), workflow, task) |
113 |
| - && test(dataFilter, CloudEventUtils.toJsonNode(event.getData()), workflow, 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) |
114 | 47 | && test(
|
115 |
| - additionalFilter, |
| 48 | + props.additionalFilter(), |
116 | 49 | JsonUtils.fromValue(CloudEventUtils.extensions(event)),
|
117 | 50 | workflow,
|
118 | 51 | task);
|
|
0 commit comments