Search in sources :

Example 1 with HttpHeadersBuilder

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));
    }
}
Also used : SystemInfo(org.hisp.dhis.system.SystemInfo) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HttpEntity(org.springframework.http.HttpEntity) HttpStatus(org.springframework.http.HttpStatus) HttpHeadersBuilder(org.hisp.dhis.system.util.HttpHeadersBuilder) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) Date(java.util.Date) ResourceAccessException(org.springframework.web.client.ResourceAccessException)

Aggregations

Date (java.util.Date)1 SystemInfo (org.hisp.dhis.system.SystemInfo)1 HttpHeadersBuilder (org.hisp.dhis.system.util.HttpHeadersBuilder)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpStatus (org.springframework.http.HttpStatus)1 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)1 HttpServerErrorException (org.springframework.web.client.HttpServerErrorException)1 ResourceAccessException (org.springframework.web.client.ResourceAccessException)1