Search in sources :

Example 1 with OnDeviceDecisioningRuleSet

use of com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningRuleSet in project target-java-sdk by adobe.

the class OnDeviceDecisioningService method executeRequest.

public TargetDeliveryResponse executeRequest(TargetDeliveryRequest deliveryRequest) {
    TimingTool timer = new TimingTool();
    timer.timeStart(TIMING_EXECUTE_REQUEST);
    DeliveryRequest delivRequest = deliveryRequest.getDeliveryRequest();
    String requestId = delivRequest.getRequestId();
    if (requestId == null) {
        requestId = UUID.randomUUID().toString();
    }
    OnDeviceDecisioningRuleSet ruleSet = this.ruleLoader.getLatestRules();
    if (ruleSet == null) {
        DeliveryResponse deliveryResponse = new DeliveryResponse().client(clientConfig.getClient()).requestId(requestId).id(delivRequest.getId()).status(HttpStatus.SC_SERVICE_UNAVAILABLE);
        return new TargetDeliveryResponse(deliveryRequest, deliveryResponse, HttpStatus.SC_SERVICE_UNAVAILABLE, "Local-decisioning rules not available");
    }
    Map<String, Object> requestContext = new HashMap<>(timeParamsCollator.collateParams(deliveryRequest, null));
    geoLookupIfNeeded(deliveryRequest, ruleSet.isGeoTargetingEnabled());
    collateParams(requestContext, REQUEST_PARAMS_COLLATORS, deliveryRequest, null);
    TraceHandler traceHandler = null;
    if (delivRequest.getTrace() != null) {
        traceHandler = new TraceHandler(this.clientConfig, this.ruleLoader, this.mapper, ruleSet, deliveryRequest);
    }
    Set<String> responseTokens = new HashSet<>(ruleSet.getResponseTokens());
    TargetDeliveryResponse targetResponse = buildDeliveryResponse(deliveryRequest, requestId);
    String visitorId = getOrCreateVisitorId(deliveryRequest, targetResponse);
    List<RequestDetails> prefetchRequests = detailsFromPrefetch(delivRequest);
    handleDetails(prefetchRequests, requestContext, deliveryRequest, visitorId, responseTokens, traceHandler, ruleSet, targetResponse.getResponse().getPrefetch(), null, null);
    List<RequestDetails> executeRequests = detailsFromExecute(delivRequest);
    List<Notification> notifications = new ArrayList<>();
    handleDetails(executeRequests, requestContext, deliveryRequest, visitorId, responseTokens, traceHandler, ruleSet, null, targetResponse.getResponse().getExecute(), notifications);
    telemetryService.addTelemetry(deliveryRequest, timer, targetResponse);
    notificationService.buildNotifications(deliveryRequest, targetResponse, notifications);
    if (this.clientConfig.isLogRequests()) {
        logger.debug(targetResponse.toString());
    }
    return targetResponse;
}
Also used : TimingTool(com.adobe.target.edge.client.utils.TimingTool) TargetDeliveryRequest(com.adobe.target.edge.client.model.TargetDeliveryRequest) DeliveryRequest(com.adobe.target.delivery.v1.model.DeliveryRequest) TargetDeliveryResponse(com.adobe.target.edge.client.model.TargetDeliveryResponse) DeliveryResponse(com.adobe.target.delivery.v1.model.DeliveryResponse) RequestDetails(com.adobe.target.delivery.v1.model.RequestDetails) Notification(com.adobe.target.delivery.v1.model.Notification) OnDeviceDecisioningRuleSet(com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningRuleSet) TargetDeliveryResponse(com.adobe.target.edge.client.model.TargetDeliveryResponse)

Example 2 with OnDeviceDecisioningRuleSet

use of com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningRuleSet in project target-java-sdk by adobe.

the class DefaultRuleLoader method loadRules.

// For unit test mocking
protected boolean loadRules(ClientConfig clientConfig) {
    try {
        TargetExceptionHandler handler = clientConfig.getExceptionHandler();
        GetRequest request = generateRequest(clientConfig);
        TimingTool timer = new TimingTool();
        timer.timeStart(TIMING_EXECUTE_REQUEST);
        HttpResponse<OnDeviceDecisioningRuleSet> response = executeRequest(request);
        double artifactDownloadTime = timer.timeEnd(TIMING_EXECUTE_REQUEST);
        double artifactDownloadTimeRounded = MathUtils.roundDouble(artifactDownloadTime, 2);
        this.telemetryService.addTelemetry(artifactDownloadTimeRounded);
        if (response.getStatus() != 200) {
            if (response.getStatus() == 304) {
                // Not updated, skip
                return true;
            }
            String message = "Received invalid HTTP response while getting local-decisioning rule set: " + response.getStatus() + " : " + response.getStatusText() + " from " + getLocalDecisioningUrl(clientConfig);
            logger.warn(message);
            if (handler != null) {
                handler.handleException(new TargetClientException(message));
            }
            return false;
        }
        OnDeviceDecisioningRuleSet ruleSet = response.getBody();
        String invalidMessage = invalidRuleSetMessage(ruleSet, response);
        if (invalidMessage == null) {
            setLatestETag(response.getHeaders().getFirst("ETag"));
            setLatestRules(ruleSet);
            OnDeviceDecisioningHandler localHandler = clientConfig.getOnDeviceDecisioningHandler();
            if (localHandler != null) {
                localHandler.artifactDownloadSucceeded(request == null ? null : request.asBytes().getBody());
            }
            logger.trace("rulesList={}", latestRules);
            return true;
        } else {
            logger.warn(invalidMessage);
            if (handler != null) {
                handler.handleException(new TargetClientException(invalidMessage));
            }
            return false;
        }
    } catch (Throwable t) {
        String message = "Hit exception while getting local-decisioning rule set from: " + getLocalDecisioningUrl(clientConfig);
        logger.warn(message, t);
        TargetExceptionHandler handler = clientConfig.getExceptionHandler();
        if (handler != null) {
            handler.handleException(new TargetClientException(message, t));
        }
        return false;
    }
}
Also used : TargetClientException(com.adobe.target.edge.client.exception.TargetClientException) TimingTool(com.adobe.target.edge.client.utils.TimingTool) TargetExceptionHandler(com.adobe.target.edge.client.exception.TargetExceptionHandler) OnDeviceDecisioningHandler(com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningHandler) OnDeviceDecisioningRuleSet(com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningRuleSet)

Example 3 with OnDeviceDecisioningRuleSet

use of com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningRuleSet in project target-java-sdk by adobe.

the class OnDeviceDecisioningEvaluatorTest method testUnknownMbox.

@Test
public void testUnknownMbox() throws JsonProcessingException, NoSuchFieldException {
    OnDeviceDecisioningRuleSet ruleSet = new OnDeviceDecisioningRuleSet();
    List<String> mboxes = new ArrayList<>();
    mboxes.add("test");
    FieldSetter.setField(ruleSet, ruleSet.getClass().getDeclaredField("localMboxes"), mboxes);
    FieldSetter.setField(ruleSet, ruleSet.getClass().getDeclaredField("remoteMboxes"), new ArrayList<>());
    String serializedRuleSet = objectMapper.writeValueAsString(ruleSet);
    RuleLoader testRuleLoader = TargetTestDeliveryRequestUtils.getTestRuleLoader(serializedRuleSet);
    evaluator = new OnDeviceDecisioningEvaluator(testRuleLoader);
    TargetDeliveryRequest request = TargetDeliveryRequest.builder().execute(new ExecuteRequest().addMboxesItem(new MboxRequest().name("test")).addMboxesItem(new MboxRequest().name("test2"))).build();
    OnDeviceDecisioningEvaluation evaluation = evaluator.evaluateLocalExecution(request);
    assertFalse(evaluation.isAllLocal());
    List<String> remoteMboxes = new ArrayList<>();
    remoteMboxes.add("test2");
    assertEquals(remoteMboxes, evaluation.getRemoteMBoxes());
    assertNull(evaluation.getRemoteViews());
}
Also used : ExecuteRequest(com.adobe.target.delivery.v1.model.ExecuteRequest) MboxRequest(com.adobe.target.delivery.v1.model.MboxRequest) ArrayList(java.util.ArrayList) TargetDeliveryRequest(com.adobe.target.edge.client.model.TargetDeliveryRequest) OnDeviceDecisioningRuleSet(com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningRuleSet) OnDeviceDecisioningEvaluation(com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningEvaluation) Test(org.junit.jupiter.api.Test)

Example 4 with OnDeviceDecisioningRuleSet

use of com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningRuleSet in project target-java-sdk by adobe.

the class OnDeviceDecisioningEvaluatorTest method testLocalAndRemoteView.

@Test
public void testLocalAndRemoteView() throws JsonProcessingException, NoSuchFieldException {
    OnDeviceDecisioningRuleSet ruleSet = new OnDeviceDecisioningRuleSet();
    List<String> views = new ArrayList<>();
    views.add("test");
    views.add("test2");
    FieldSetter.setField(ruleSet, ruleSet.getClass().getDeclaredField("localViews"), views);
    FieldSetter.setField(ruleSet, ruleSet.getClass().getDeclaredField("remoteViews"), views);
    String serializedRuleSet = objectMapper.writeValueAsString(ruleSet);
    RuleLoader testRuleLoader = TargetTestDeliveryRequestUtils.getTestRuleLoader(serializedRuleSet);
    evaluator = new OnDeviceDecisioningEvaluator(testRuleLoader);
    TargetDeliveryRequest request = TargetDeliveryRequest.builder().prefetch(new PrefetchRequest().addViewsItem(new ViewRequest().name("test")).addViewsItem(new ViewRequest().name("test2"))).build();
    OnDeviceDecisioningEvaluation evaluation = evaluator.evaluateLocalExecution(request);
    assertFalse(evaluation.isAllLocal());
    List<String> remoteViews = evaluation.getRemoteViews();
    remoteViews.sort(Comparator.naturalOrder());
    assertEquals(views, remoteViews);
    assertNull(evaluation.getRemoteMBoxes());
}
Also used : PrefetchRequest(com.adobe.target.delivery.v1.model.PrefetchRequest) ArrayList(java.util.ArrayList) TargetDeliveryRequest(com.adobe.target.edge.client.model.TargetDeliveryRequest) ViewRequest(com.adobe.target.delivery.v1.model.ViewRequest) OnDeviceDecisioningRuleSet(com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningRuleSet) OnDeviceDecisioningEvaluation(com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningEvaluation) Test(org.junit.jupiter.api.Test)

Example 5 with OnDeviceDecisioningRuleSet

use of com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningRuleSet in project target-java-sdk by adobe.

the class OnDeviceDecisioningEvaluatorTest method testNullRequest.

@Test
public void testNullRequest() throws JsonProcessingException {
    OnDeviceDecisioningRuleSet ruleSet = new OnDeviceDecisioningRuleSet();
    String serializedRuleSet = objectMapper.writeValueAsString(ruleSet);
    RuleLoader testRuleLoader = TargetTestDeliveryRequestUtils.getTestRuleLoader(serializedRuleSet);
    evaluator = new OnDeviceDecisioningEvaluator(testRuleLoader);
    OnDeviceDecisioningEvaluation evaluation = evaluator.evaluateLocalExecution(null);
    assertFalse(evaluation.isAllLocal());
    assertNull(evaluation.getRemoteMBoxes());
    assertNull(evaluation.getRemoteViews());
}
Also used : OnDeviceDecisioningRuleSet(com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningRuleSet) OnDeviceDecisioningEvaluation(com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningEvaluation) Test(org.junit.jupiter.api.Test)

Aggregations

OnDeviceDecisioningRuleSet (com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningRuleSet)17 Test (org.junit.jupiter.api.Test)11 OnDeviceDecisioningEvaluation (com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningEvaluation)10 TargetDeliveryRequest (com.adobe.target.edge.client.model.TargetDeliveryRequest)9 ArrayList (java.util.ArrayList)9 PrefetchRequest (com.adobe.target.delivery.v1.model.PrefetchRequest)5 ViewRequest (com.adobe.target.delivery.v1.model.ViewRequest)5 TargetClientException (com.adobe.target.edge.client.exception.TargetClientException)4 ExecuteRequest (com.adobe.target.delivery.v1.model.ExecuteRequest)3 MboxRequest (com.adobe.target.delivery.v1.model.MboxRequest)3 ClientConfig (com.adobe.target.edge.client.ClientConfig)3 TargetExceptionHandler (com.adobe.target.edge.client.exception.TargetExceptionHandler)2 OnDeviceDecisioningHandler (com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningHandler)2 TimingTool (com.adobe.target.edge.client.utils.TimingTool)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 DeliveryRequest (com.adobe.target.delivery.v1.model.DeliveryRequest)1 DeliveryResponse (com.adobe.target.delivery.v1.model.DeliveryResponse)1 Notification (com.adobe.target.delivery.v1.model.Notification)1 RequestDetails (com.adobe.target.delivery.v1.model.RequestDetails)1 ClientProxyConfig (com.adobe.target.edge.client.ClientProxyConfig)1