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;
}
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;
}
}
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());
}
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());
}
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());
}
Aggregations