Search in sources :

Example 1 with PlaceholderResolver

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;
    }
}
Also used : ThingEventToThingConverter(org.eclipse.ditto.things.model.signals.events.ThingEventToThingConverter) Command(org.eclipse.ditto.base.model.signals.commands.Command) PlaceholderResolver(org.eclipse.ditto.placeholders.PlaceholderResolver) Event(org.eclipse.ditto.base.model.signals.events.Event) Signal(org.eclipse.ditto.base.model.signals.Signal) AuthorizationContext(org.eclipse.ditto.base.model.auth.AuthorizationContext) ConnectionMonitor(org.eclipse.ditto.connectivity.service.messaging.monitoring.ConnectionMonitor) RqlPredicateParser(org.eclipse.ditto.rql.parser.RqlPredicateParser) PlaceholderFactory(org.eclipse.ditto.placeholders.PlaceholderFactory) Target(org.eclipse.ditto.connectivity.model.Target) Criteria(org.eclipse.ditto.rql.query.criteria.Criteria) WithEntityId(org.eclipse.ditto.base.model.entity.id.WithEntityId) DittoHeaders(org.eclipse.ditto.base.model.headers.DittoHeaders) Topic(org.eclipse.ditto.connectivity.model.Topic) COMMANDS(org.eclipse.ditto.protocol.TopicPath.Criterion.COMMANDS) CommandResponse(org.eclipse.ditto.base.model.signals.commands.CommandResponse) PolicyAnnouncement(org.eclipse.ditto.policies.model.signals.announcements.PolicyAnnouncement) ThingEvent(org.eclipse.ditto.things.model.signals.events.ThingEvent) JsonPointer(org.eclipse.ditto.json.JsonPointer) Nullable(javax.annotation.Nullable) NamespaceReader(org.eclipse.ditto.base.model.namespaces.NamespaceReader) ConnectivityAnnouncement(org.eclipse.ditto.connectivity.model.signals.announcements.ConnectivityAnnouncement) Thing(org.eclipse.ditto.things.model.Thing) DittoProtocolAdapter(org.eclipse.ditto.protocol.adapter.DittoProtocolAdapter) TopicPathPlaceholder(org.eclipse.ditto.protocol.placeholders.TopicPathPlaceholder) MessageCommand(org.eclipse.ditto.messages.model.signals.commands.MessageCommand) QueryFilterCriteriaFactory(org.eclipse.ditto.rql.query.filter.QueryFilterCriteriaFactory) Predicate(java.util.function.Predicate) FilteredTopic(org.eclipse.ditto.connectivity.model.FilteredTopic) Set(java.util.Set) TopicPath(org.eclipse.ditto.protocol.TopicPath) ThingId(org.eclipse.ditto.things.model.ThingId) Connection(org.eclipse.ditto.connectivity.model.Connection) Collectors(java.util.stream.Collectors) ConnectionMonitorRegistry(org.eclipse.ditto.connectivity.service.messaging.monitoring.ConnectionMonitorRegistry) TimePlaceholder(org.eclipse.ditto.placeholders.TimePlaceholder) List(java.util.List) MessageCommandResponse(org.eclipse.ditto.messages.model.signals.commands.MessageCommandResponse) EVENTS(org.eclipse.ditto.protocol.TopicPath.Criterion.EVENTS) JsonFieldSelector(org.eclipse.ditto.json.JsonFieldSelector) ResourcePlaceholder(org.eclipse.ditto.protocol.placeholders.ResourcePlaceholder) Optional(java.util.Optional) WithResource(org.eclipse.ditto.base.model.signals.WithResource) Collections(java.util.Collections) TopicPath(org.eclipse.ditto.protocol.TopicPath) Criteria(org.eclipse.ditto.rql.query.criteria.Criteria) JsonPointer(org.eclipse.ditto.json.JsonPointer) WithResource(org.eclipse.ditto.base.model.signals.WithResource) ThingEvent(org.eclipse.ditto.things.model.signals.events.ThingEvent) Thing(org.eclipse.ditto.things.model.Thing)

Example 2 with PlaceholderResolver

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);
    }
}
Also used : ThingEventToThingConverter(org.eclipse.ditto.things.model.signals.events.ThingEventToThingConverter) PFBuilder(akka.japi.pf.PFBuilder) PlaceholderResolver(org.eclipse.ditto.placeholders.PlaceholderResolver) StreamingType(org.eclipse.ditto.internal.utils.pubsub.StreamingType) Acknowledgement(org.eclipse.ditto.base.model.signals.acks.Acknowledgement) Signal(org.eclipse.ditto.base.model.signals.Signal) DittoHeaderDefinition(org.eclipse.ditto.base.model.headers.DittoHeaderDefinition) RqlPredicateParser(org.eclipse.ditto.rql.parser.RqlPredicateParser) PlaceholderFactory(org.eclipse.ditto.placeholders.PlaceholderFactory) DittoRuntimeException(org.eclipse.ditto.base.model.exceptions.DittoRuntimeException) Criteria(org.eclipse.ditto.rql.query.criteria.Criteria) AcknowledgementRequest(org.eclipse.ditto.base.model.acks.AcknowledgementRequest) DittoHeaders(org.eclipse.ditto.base.model.headers.DittoHeaders) ConnectionValidator(org.eclipse.ditto.connectivity.service.messaging.validation.ConnectionValidator) ActorRef(akka.actor.ActorRef) CommandResponse(org.eclipse.ditto.base.model.signals.commands.CommandResponse) Mapped(org.eclipse.ditto.connectivity.api.OutboundSignal.Mapped) OutboundSignalFactory(org.eclipse.ditto.connectivity.api.OutboundSignalFactory) PrintWriter(java.io.PrintWriter) JsonValue(org.eclipse.ditto.json.JsonValue) DittoProtocolAdapter(org.eclipse.ditto.protocol.adapter.DittoProtocolAdapter) LimitsConfig(org.eclipse.ditto.base.service.config.limits.LimitsConfig) LogCategory(org.eclipse.ditto.connectivity.model.LogCategory) PartialFunction(scala.PartialFunction) QueryFilterCriteriaFactory(org.eclipse.ditto.rql.query.filter.QueryFilterCriteriaFactory) Done(akka.Done) Predicate(java.util.function.Predicate) FilteredTopic(org.eclipse.ditto.connectivity.model.FilteredTopic) Collection(java.util.Collection) MappingConfig(org.eclipse.ditto.connectivity.service.config.mapping.MappingConfig) Set(java.util.Set) ConnectivityMdcEntryKey(org.eclipse.ditto.connectivity.service.util.ConnectivityMdcEntryKey) Sink(akka.stream.javadsl.Sink) MetricDirection(org.eclipse.ditto.connectivity.model.MetricDirection) ThingId(org.eclipse.ditto.things.model.ThingId) Collectors(java.util.stream.Collectors) DROPPED(org.eclipse.ditto.connectivity.model.MetricType.DROPPED) Objects(java.util.Objects) ConnectionFailure(org.eclipse.ditto.connectivity.service.messaging.internal.ConnectionFailure) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) ConnectivitySignalEnrichmentProvider(org.eclipse.ditto.connectivity.service.mapping.ConnectivitySignalEnrichmentProvider) InfoProviderFactory(org.eclipse.ditto.connectivity.service.messaging.monitoring.logs.InfoProviderFactory) NotUsed(akka.NotUsed) ThingPredicateVisitor(org.eclipse.ditto.rql.query.things.ThingPredicateVisitor) JsonFieldSelector(org.eclipse.ditto.json.JsonFieldSelector) ResourcePlaceholder(org.eclipse.ditto.protocol.placeholders.ResourcePlaceholder) Optional(java.util.Optional) WithResource(org.eclipse.ditto.base.model.signals.WithResource) MonitoringConfig(org.eclipse.ditto.connectivity.service.config.MonitoringConfig) SignalEnrichmentFacade(org.eclipse.ditto.internal.models.signalenrichment.SignalEnrichmentFacade) Props(akka.actor.Props) DittoAcknowledgementLabel(org.eclipse.ditto.base.model.acks.DittoAcknowledgementLabel) ThingNotAccessibleException(org.eclipse.ditto.things.model.signals.commands.exceptions.ThingNotAccessibleException) AbstractGraphActor(org.eclipse.ditto.internal.utils.akka.controlflow.AbstractGraphActor) ConnectivityConfig(org.eclipse.ditto.connectivity.service.config.ConnectivityConfig) DefaultConnectionMonitorRegistry(org.eclipse.ditto.connectivity.service.messaging.monitoring.DefaultConnectionMonitorRegistry) Flow(akka.stream.javadsl.Flow) Source(akka.stream.javadsl.Source) AcknowledgementLabel(org.eclipse.ditto.base.model.acks.AcknowledgementLabel) ConnectionMonitor(org.eclipse.ditto.connectivity.service.messaging.monitoring.ConnectionMonitor) CompletableFuture(java.util.concurrent.CompletableFuture) OutboundSignal(org.eclipse.ditto.connectivity.api.OutboundSignal) Target(org.eclipse.ditto.connectivity.model.Target) ArrayList(java.util.ArrayList) ErrorResponse(org.eclipse.ditto.base.model.signals.commands.ErrorResponse) JsonObject(org.eclipse.ditto.json.JsonObject) WithEntityId(org.eclipse.ditto.base.model.entity.id.WithEntityId) SignalEnrichmentFailedException(org.eclipse.ditto.base.model.exceptions.SignalEnrichmentFailedException) ThreadSafeDittoLoggingAdapter(org.eclipse.ditto.internal.utils.akka.logging.ThreadSafeDittoLoggingAdapter) EntityId(org.eclipse.ditto.base.model.entity.id.EntityId) Nullable(javax.annotation.Nullable) TopicPathPlaceholder(org.eclipse.ditto.protocol.placeholders.TopicPathPlaceholder) JsonSchemaVersion(org.eclipse.ditto.base.model.json.JsonSchemaVersion) StringWriter(java.io.StringWriter) BoxedUnit(scala.runtime.BoxedUnit) TopicPath(org.eclipse.ditto.protocol.TopicPath) Connection(org.eclipse.ditto.connectivity.model.Connection) Pair(akka.japi.Pair) Acknowledgements(org.eclipse.ditto.base.model.signals.acks.Acknowledgements) TimePlaceholder(org.eclipse.ditto.placeholders.TimePlaceholder) MetricType(org.eclipse.ditto.connectivity.model.MetricType) LogType(org.eclipse.ditto.connectivity.model.LogType) Status(akka.actor.Status) ConditionChecker.checkNotEmpty(org.eclipse.ditto.base.model.common.ConditionChecker.checkNotEmpty) MappingOutcome(org.eclipse.ditto.connectivity.service.messaging.mappingoutcome.MappingOutcome) JsonField(org.eclipse.ditto.json.JsonField) Collections(java.util.Collections) MAPPED(org.eclipse.ditto.connectivity.model.MetricType.MAPPED) JsonFieldSelector(org.eclipse.ditto.json.JsonFieldSelector) TopicPath(org.eclipse.ditto.protocol.TopicPath) Criteria(org.eclipse.ditto.rql.query.criteria.Criteria) WithResource(org.eclipse.ditto.base.model.signals.WithResource) DittoHeaders(org.eclipse.ditto.base.model.headers.DittoHeaders) JsonObject(org.eclipse.ditto.json.JsonObject)

Example 3 with PlaceholderResolver

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));
}
Also used : ExternalMessage(org.eclipse.ditto.connectivity.api.ExternalMessage) Adaptable(org.eclipse.ditto.protocol.Adaptable) PlaceholderResolver(org.eclipse.ditto.placeholders.PlaceholderResolver)

Aggregations

PlaceholderResolver (org.eclipse.ditto.placeholders.PlaceholderResolver)3 Collections (java.util.Collections)2 List (java.util.List)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Predicate (java.util.function.Predicate)2 Collectors (java.util.stream.Collectors)2 Nullable (javax.annotation.Nullable)2 WithEntityId (org.eclipse.ditto.base.model.entity.id.WithEntityId)2 DittoHeaders (org.eclipse.ditto.base.model.headers.DittoHeaders)2 Signal (org.eclipse.ditto.base.model.signals.Signal)2 WithResource (org.eclipse.ditto.base.model.signals.WithResource)2 CommandResponse (org.eclipse.ditto.base.model.signals.commands.CommandResponse)2 Connection (org.eclipse.ditto.connectivity.model.Connection)2 FilteredTopic (org.eclipse.ditto.connectivity.model.FilteredTopic)2 Target (org.eclipse.ditto.connectivity.model.Target)2 ConnectionMonitor (org.eclipse.ditto.connectivity.service.messaging.monitoring.ConnectionMonitor)2 JsonFieldSelector (org.eclipse.ditto.json.JsonFieldSelector)2 PlaceholderFactory (org.eclipse.ditto.placeholders.PlaceholderFactory)2 TimePlaceholder (org.eclipse.ditto.placeholders.TimePlaceholder)2