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