use of org.eclipse.ditto.placeholders.PlaceholderResolver in project ditto by eclipse.
the class SignalFilter method matchesFilterBeforeEnrichment.
private static boolean matchesFilterBeforeEnrichment(final FilteredTopic filteredTopic, final Signal<?> signal) {
final Optional<String> filterOptional = filteredTopic.getFilter();
if (filterOptional.isPresent()) {
// match filter ignoring "extraFields"
final TopicPath topicPath = DITTO_PROTOCOL_ADAPTER.toTopicPath(signal);
final PlaceholderResolver<TopicPath> topicPathPlaceholderResolver = PlaceholderFactory.newPlaceholderResolver(TOPIC_PATH_PLACEHOLDER, topicPath);
final PlaceholderResolver<WithResource> resourcePlaceholderResolver = PlaceholderFactory.newPlaceholderResolver(RESOURCE_PLACEHOLDER, signal);
final PlaceholderResolver<Object> timePlaceholderResolver = PlaceholderFactory.newPlaceholderResolver(TIME_PLACEHOLDER, new Object());
final Criteria criteria = parseCriteria(filterOptional.get(), signal.getDittoHeaders(), topicPathPlaceholderResolver, resourcePlaceholderResolver, timePlaceholderResolver);
final Set<JsonPointer> extraFields = filteredTopic.getExtraFields().map(JsonFieldSelector::getPointers).orElse(Collections.emptySet());
if (signal instanceof ThingEvent) {
return ThingEventToThingConverter.thingEventToThing((ThingEvent<?>) signal).filter(thing -> Thing3ValuePredicateVisitor.couldBeTrue(criteria, extraFields, thing, topicPathPlaceholderResolver, resourcePlaceholderResolver, timePlaceholderResolver)).isPresent();
} else {
final Thing emptyThing = Thing.newBuilder().build();
return Thing3ValuePredicateVisitor.couldBeTrue(criteria, extraFields, emptyThing, topicPathPlaceholderResolver, resourcePlaceholderResolver, timePlaceholderResolver);
}
} else {
return true;
}
}
use of org.eclipse.ditto.placeholders.PlaceholderResolver in project ditto by eclipse.
the class OutboundMappingProcessorActor method applyFilter.
private Collection<OutboundSignalWithSender> applyFilter(final OutboundSignalWithSender outboundSignalWithExtra, final FilteredTopic filteredTopic) {
final Optional<String> filter = filteredTopic.getFilter();
final Optional<JsonFieldSelector> extraFields = filteredTopic.getExtraFields();
if (filter.isPresent() && extraFields.isPresent()) {
// evaluate filter criteria again if signal enrichment is involved.
final Signal<?> signal = outboundSignalWithExtra.getSource();
final TopicPath topicPath = DITTO_PROTOCOL_ADAPTER.toTopicPath(signal);
final PlaceholderResolver<TopicPath> topicPathPlaceholderResolver = PlaceholderFactory.newPlaceholderResolver(TOPIC_PATH_PLACEHOLDER, topicPath);
final PlaceholderResolver<WithResource> resourcePlaceholderResolver = PlaceholderFactory.newPlaceholderResolver(RESOURCE_PLACEHOLDER, signal);
final PlaceholderResolver<Object> timePlaceholderResolver = PlaceholderFactory.newPlaceholderResolver(TIME_PLACEHOLDER, new Object());
final DittoHeaders dittoHeaders = signal.getDittoHeaders();
final Criteria criteria = QueryFilterCriteriaFactory.modelBased(RqlPredicateParser.getInstance(), topicPathPlaceholderResolver, resourcePlaceholderResolver, timePlaceholderResolver).filterCriteria(filter.get(), dittoHeaders);
return outboundSignalWithExtra.getExtra().flatMap(extra -> ThingEventToThingConverter.mergeThingWithExtraFields(signal, extraFields.get(), extra).filter(ThingPredicateVisitor.apply(criteria, topicPathPlaceholderResolver, resourcePlaceholderResolver, timePlaceholderResolver)).map(thing -> outboundSignalWithExtra)).map(Collections::singletonList).orElse(List.of());
} else {
// no signal enrichment: filtering is already done in SignalFilter since there is no ignored field
return Collections.singletonList(outboundSignalWithExtra);
}
}
use of org.eclipse.ditto.placeholders.PlaceholderResolver in project ditto by eclipse.
the class Resolvers method forOutbound.
/**
* Create an expression resolver for an outbound message.
*
* @param mappedOutboundSignal the mapped external message.
* @param sendingConnectionId the ID of the connection sending the message.
* @return the expression resolver.
*/
public static ExpressionResolver forOutbound(final OutboundSignal.Mapped mappedOutboundSignal, final ConnectionId sendingConnectionId) {
final Signal<?> signal = mappedOutboundSignal.getSource();
final ExternalMessage externalMessage = mappedOutboundSignal.getExternalMessage();
final Adaptable adaptable = mappedOutboundSignal.getAdaptable();
return PlaceholderFactory.newExpressionResolver(RESOLVER_CREATORS.stream().map(creator -> creator.create(adaptable.getDittoHeaders(), signal, externalMessage.getTopicPath().orElse(null), signal.getDittoHeaders().getAuthorizationContext(), sendingConnectionId)).toArray(PlaceholderResolver[]::new));
}
Aggregations