Search in sources :

Example 1 with TargetExceptionHandler

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

use of com.adobe.target.edge.client.exception.TargetExceptionHandler 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)2 TargetExceptionHandler (com.adobe.target.edge.client.exception.TargetExceptionHandler)2 OnDeviceDecisioningHandler (com.adobe.target.edge.client.model.ondevice.OnDeviceDecisioningHandler)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