All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.target.edge.client.ondevice.OnDeviceDecisioningDetailsExecutor Maven / Gradle / Ivy

There is a newer version: 2.6.1
Show newest version
/*
 * Copyright 2021 Adobe. All rights reserved.
 * This file is licensed 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 REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package com.adobe.target.edge.client.ondevice;

import com.adobe.target.delivery.v1.model.*;
import com.adobe.target.edge.client.ClientConfig;
import com.adobe.target.edge.client.model.TargetDeliveryRequest;
import com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningRule;
import com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningRuleSet;
import com.adobe.target.edge.client.utils.StringUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.*;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OnDeviceDecisioningDetailsExecutor {

  private static final Logger logger =
      LoggerFactory.getLogger(OnDeviceDecisioningDetailsExecutor.class);

  private final ClientConfig clientConfig;
  private final ObjectMapper mapper;
  private final OnDeviceDecisioningRuleExecutor ruleExecutor;

  public OnDeviceDecisioningDetailsExecutor(ClientConfig clientConfig, ObjectMapper mapper) {
    this.clientConfig = clientConfig;
    this.mapper = mapper;
    this.ruleExecutor = new OnDeviceDecisioningRuleExecutor(clientConfig, mapper);
  }

  public void executeDetails(
      TargetDeliveryRequest deliveryRequest,
      Set onDeviceAllMatchingRulesMboxes,
      Map localContext,
      String visitorId,
      Set responseTokens,
      TraceHandler traceHandler,
      OnDeviceDecisioningRuleSet ruleSet,
      RequestDetails details,
      PrefetchResponse prefetchResponse,
      ExecuteResponse executeResponse,
      List notifications) {
    if (traceHandler != null) {
      traceHandler.updateRequest(deliveryRequest, details, executeResponse != null);
    }

    List rules = detailsRules(details, ruleSet);
    String propertyToken = requestPropertyToken(deliveryRequest);
    boolean handledAtLeastOnce = false;
    Set skipKeySet = new HashSet<>();
    if (rules != null) {
      for (OnDeviceDecisioningRule rule : rules) {
        if (propertyTokenMismatch(rule.getPropertyTokens(), propertyToken)) {
          continue;
        }
        String ruleKey = rule.getRuleKey();
        if (ruleKey != null && skipKeySet.contains(ruleKey)) {
          continue;
        }
        Map consequence =
            this.ruleExecutor.executeRule(
                localContext, details, visitorId, rule, responseTokens, traceHandler);
        boolean handled =
            handleResult(
                consequence,
                rule,
                details,
                prefetchResponse,
                executeResponse,
                notifications,
                traceHandler);
        if (handled) {
          handledAtLeastOnce = true;
          if (details instanceof MboxRequest) {
            if (!onDeviceAllMatchingRulesMboxes.contains(((MboxRequest) details).getName())) {
              break;
            }
          } else if (ruleKey != null) {
            skipKeySet.add(ruleKey);
          }
        }
      }
    }
    if (!handledAtLeastOnce) {
      unhandledResponse(details, prefetchResponse, executeResponse, traceHandler);
    }
  }

  private boolean handleResult(
      Map consequence,
      OnDeviceDecisioningRule rule,
      RequestDetails details,
      PrefetchResponse prefetchResponse,
      ExecuteResponse executeResponse,
      List notifications,
      TraceHandler traceHandler) {
    logger.trace("consequence={}", consequence);
    if (consequence == null || consequence.isEmpty()) {
      return false;
    }
    if (details instanceof ViewRequest) {
      View view = this.mapper.convertValue(consequence, new TypeReference() {});
      view.setTrace(currentTrace(traceHandler));
      if (prefetchResponse != null) {
        List views = prefetchResponse.getViews();
        if (views == null) {
          views = new ArrayList<>();
          prefetchResponse.setViews(views);
        }
        View foundView = null;
        for (View existing : views) {
          if (existing.getName().equals(view.getName())) {
            foundView = existing;
            break;
          }
        }
        if (foundView == null) {
          views.add(view);
        } else {
          if (view.getOptions() != null) {
            List




© 2015 - 2024 Weber Informatics LLC | Privacy Policy