Search in sources :

Example 1 with TargetClientException

use of com.adobe.target.edge.client.exception.TargetClientException in project target-java-sdk by adobe.

the class DefaultRuleLoader method scheduleTimer.

private void scheduleTimer(long delay) {
    if (this.timer != null) {
        this.timer.cancel();
    }
    this.timer = new Timer(this.getClass().getCanonicalName());
    this.timer.schedule(new TimerTask() {

        @Override
        public void run() {
            boolean success = DefaultRuleLoader.this.loadRules(clientConfig);
            OnDeviceDecisioningHandler handler = clientConfig.getOnDeviceDecisioningHandler();
            if (!success && DefaultRuleLoader.this.latestRules == null) {
                // retry if initial rules file download fails
                String message;
                if (DefaultRuleLoader.this.retries++ < MAX_RETRIES) {
                    long retryDelay = DefaultRuleLoader.this.retries * 1000;
                    message = String.format("Download of local-decisioning rules failed, retying in %s ms", retryDelay);
                    logger.debug(message);
                    scheduleTimer(retryDelay);
                } else {
                    message = "Exhausted retries trying to download local-decisioning rules.";
                    logger.warn(message);
                }
                if (handler != null) {
                    handler.artifactDownloadFailed(new TargetClientException(message));
                }
            } else {
                if (handler != null && !succeeded) {
                    succeeded = true;
                    handler.onDeviceDecisioningReady();
                }
                DefaultRuleLoader.this.numFetches++;
                DefaultRuleLoader.this.lastFetch = new Date();
            }
        }
    }, delay, getPollingInterval());
}
Also used : TargetClientException(com.adobe.target.edge.client.exception.TargetClientException) Timer(java.util.Timer) TimerTask(java.util.TimerTask) OnDeviceDecisioningHandler(com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningHandler) Date(java.util.Date)

Example 2 with TargetClientException

use of com.adobe.target.edge.client.exception.TargetClientException 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 TargetClientException

use of com.adobe.target.edge.client.exception.TargetClientException in project target-java-sdk by adobe.

the class DefaultRuleLoader method start.

@Override
public synchronized void start(final ClientConfig clientConfig, TelemetryService telemetryService) {
    if (!clientConfig.isOnDeviceDecisioningEnabled()) {
        return;
    }
    if (started) {
        return;
    }
    ObjectMapper mapper = new JacksonObjectMapper();
    byte[] artifactPayload = clientConfig.getOnDeviceArtifactPayload();
    if (artifactPayload != null) {
        String payload = new String(artifactPayload, StandardCharsets.UTF_8);
        OnDeviceDecisioningRuleSet ruleSet = mapper.readValue(payload, new GenericType<OnDeviceDecisioningRuleSet>() {
        });
        String invalidMessage = invalidRuleSetMessage(ruleSet, null);
        if (invalidMessage == null) {
            setLatestRules(ruleSet);
            OnDeviceDecisioningHandler handler = clientConfig.getOnDeviceDecisioningHandler();
            if (handler != null && !succeeded) {
                succeeded = true;
                handler.onDeviceDecisioningReady();
            }
        } else {
            logger.warn(invalidMessage);
            TargetExceptionHandler handler = clientConfig.getExceptionHandler();
            if (handler != null) {
                handler.handleException(new TargetClientException(invalidMessage));
            }
        }
    }
    started = true;
    retries = 0;
    if (unirestInstance != null) {
        unirestInstance.config().socketTimeout(clientConfig.getSocketTimeout()).connectTimeout(clientConfig.getConnectTimeout()).concurrency(clientConfig.getMaxConnectionsTotal(), clientConfig.getMaxConnectionsPerHost()).automaticRetries(clientConfig.isEnabledRetries()).enableCookieManagement(false).setObjectMapper(mapper).setDefaultHeader("Accept", "application/json");
        if (clientConfig.isProxyEnabled()) {
            ClientProxyConfig proxyConfig = clientConfig.getProxyConfig();
            if (proxyConfig.isAuthProxy()) {
                unirestInstance.config().proxy(proxyConfig.getHost(), proxyConfig.getPort(), proxyConfig.getUsername(), proxyConfig.getPassword());
            } else {
                unirestInstance.config().proxy(proxyConfig.getHost(), proxyConfig.getPort());
            }
        }
    }
    this.clientConfig = clientConfig;
    this.telemetryService = telemetryService;
    this.scheduleTimer(0);
}
Also used : ClientProxyConfig(com.adobe.target.edge.client.ClientProxyConfig) TargetClientException(com.adobe.target.edge.client.exception.TargetClientException) JacksonObjectMapper(com.adobe.target.edge.client.http.JacksonObjectMapper) TargetExceptionHandler(com.adobe.target.edge.client.exception.TargetExceptionHandler) JacksonObjectMapper(com.adobe.target.edge.client.http.JacksonObjectMapper) OnDeviceDecisioningHandler(com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningHandler) OnDeviceDecisioningRuleSet(com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningRuleSet)

Aggregations

TargetClientException (com.adobe.target.edge.client.exception.TargetClientException)3 OnDeviceDecisioningHandler (com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningHandler)3 TargetExceptionHandler (com.adobe.target.edge.client.exception.TargetExceptionHandler)2 OnDeviceDecisioningRuleSet (com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningRuleSet)2 ClientProxyConfig (com.adobe.target.edge.client.ClientProxyConfig)1 JacksonObjectMapper (com.adobe.target.edge.client.http.JacksonObjectMapper)1 TimingTool (com.adobe.target.edge.client.utils.TimingTool)1 Date (java.util.Date)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1