use of org.hisp.dhis.system.util.HttpHeadersBuilder in project dhis2-core by dhis2.
the class DefaultMonitoringService method pushMonitoringInfo.
@Override
public void pushMonitoringInfo() {
final Date startTime = new Date();
String url = config.getProperty(ConfigurationKey.SYSTEM_MONITORING_URL);
String username = config.getProperty(ConfigurationKey.SYSTEM_MONITORING_USERNAME);
String password = config.getProperty(ConfigurationKey.SYSTEM_MONITORING_URL);
if (StringUtils.isBlank(url)) {
log.debug("Monitoring service URL not configured, aborting monitoring request");
return;
}
SystemInfo systemInfo = systemService.getSystemInfo();
if (systemInfo == null) {
log.warn("System info not available, aborting monitoring request");
return;
}
systemInfo.clearSensitiveInfo();
HttpHeadersBuilder headersBuilder = new HttpHeadersBuilder().withContentTypeJson();
if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
headersBuilder.withBasicAuth(username, password);
}
HttpEntity<SystemInfo> requestEntity = new HttpEntity<>(systemInfo, headersBuilder.build());
ResponseEntity<String> response = null;
HttpStatus sc = null;
try {
response = restTemplate.postForEntity(url, requestEntity, String.class);
sc = response.getStatusCode();
} catch (HttpClientErrorException | HttpServerErrorException ex) {
log.warn(String.format("Monitoring request failed, status code: %s", sc), ex);
return;
} catch (ResourceAccessException ex) {
log.info("Monitoring request failed, network is unreachable");
return;
}
if (response != null && sc != null && sc.is2xxSuccessful()) {
systemSettingManager.saveSystemSetting(SettingKey.LAST_SUCCESSFUL_SYSTEM_MONITORING_PUSH, startTime);
log.debug(String.format("Monitoring request successfully sent, url: %s", url));
} else {
log.warn(String.format("Monitoring request was unsuccessful, status code: %s", sc));
}
}
Aggregations