Search in sources :

Example 16 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project dhis2-core by dhis2.

the class SchemaController method validateSchema.

@RequestMapping(value = "/{type}", method = { RequestMethod.POST, RequestMethod.PUT }, consumes = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
public void validateSchema(@PathVariable String type, HttpServletRequest request, HttpServletResponse response) throws IOException {
    Schema schema = getSchemaFromType(type);
    if (schema == null) {
        throw new HttpClientErrorException(HttpStatus.NOT_FOUND, "Type " + type + " does not exist.");
    }
    Object object = renderService.fromJson(request.getInputStream(), schema.getKlass());
    List<ErrorReport> validationViolations = schemaValidator.validate(object);
    WebMessage webMessage = WebMessageUtils.errorReports(validationViolations);
    webMessageService.send(webMessage, response, request);
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) Schema(org.hisp.dhis.schema.Schema) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 17 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project dhis2-core by dhis2.

the class DefaultSynchronizationManager method isRemoteServerAvailable.

// -------------------------------------------------------------------------
// SynchronizatonManager implementation
// -------------------------------------------------------------------------
@Override
public AvailabilityStatus isRemoteServerAvailable() {
    Configuration config = configurationService.getConfiguration();
    if (!isRemoteServerConfigured(config)) {
        return new AvailabilityStatus(false, "Remote server is not configured", HttpStatus.BAD_GATEWAY);
    }
    String url = systemSettingManager.getSystemSetting(SettingKey.REMOTE_INSTANCE_URL) + PING_PATH;
    String username = (String) systemSettingManager.getSystemSetting(SettingKey.REMOTE_INSTANCE_USERNAME);
    String password = (String) systemSettingManager.getSystemSetting(SettingKey.REMOTE_INSTANCE_PASSWORD);
    log.debug(String.format("Remote server ping URL: %s, username: %s", url, username));
    HttpEntity<String> request = getBasicAuthRequestEntity(username, password);
    ResponseEntity<String> response = null;
    HttpStatus sc = null;
    String st = null;
    AvailabilityStatus status = null;
    try {
        response = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
        sc = response.getStatusCode();
    } catch (HttpClientErrorException ex) {
        sc = ex.getStatusCode();
        st = ex.getStatusText();
    } catch (HttpServerErrorException ex) {
        sc = ex.getStatusCode();
        st = ex.getStatusText();
    } catch (ResourceAccessException ex) {
        return new AvailabilityStatus(false, "Network is unreachable", HttpStatus.BAD_GATEWAY);
    }
    log.debug("Response status code: " + sc);
    if (HttpStatus.FOUND.equals(sc)) {
        status = new AvailabilityStatus(false, "No authentication was provided", sc);
    } else if (HttpStatus.UNAUTHORIZED.equals(sc)) {
        status = new AvailabilityStatus(false, "Authentication failed", sc);
    } else if (HttpStatus.INTERNAL_SERVER_ERROR.equals(sc)) {
        status = new AvailabilityStatus(false, "Remote server experienced an internal error", sc);
    } else if (HttpStatus.OK.equals(sc)) {
        status = new AvailabilityStatus(true, "Authentication was successful", sc);
    } else {
        status = new AvailabilityStatus(false, "Server is not available: " + st, sc);
    }
    log.info("Status: " + status);
    return status;
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) Configuration(org.hisp.dhis.configuration.Configuration) HttpStatus(org.springframework.http.HttpStatus) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) ResourceAccessException(org.springframework.web.client.ResourceAccessException)

Example 18 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project dhis2-core by dhis2.

the class DefaultMonitoringService method pushMonitoringInfo.

@Override
public void pushMonitoringInfo() {
    String url = (String) systemSettingManager.getSystemSetting(SettingKey.SYSTEM_MONITORING_URL);
    String username = (String) systemSettingManager.getSystemSetting(SettingKey.SYSTEM_MONITORING_USERNAME);
    String password = (String) systemSettingManager.getSystemSetting(SettingKey.SYSTEM_MONITORING_PASSWORD);
    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("Monitoring request failed, status code: " + sc, ex);
        return;
    } catch (ResourceAccessException ex) {
        log.info("Monitoring request failed, network is unreachable");
        return;
    }
    if (response != null && sc != null && sc.is2xxSuccessful()) {
        log.debug("Monitoring request successfully sent");
    } else {
        log.warn("Monitoring request was unsuccessful, status code: " + 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) ResourceAccessException(org.springframework.web.client.ResourceAccessException)

Aggregations

HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)18 HttpServerErrorException (org.springframework.web.client.HttpServerErrorException)8 RestTemplate (org.springframework.web.client.RestTemplate)6 HashMap (java.util.HashMap)4 HttpStatus (org.springframework.http.HttpStatus)4 ResourceAccessException (org.springframework.web.client.ResourceAccessException)4 URI (java.net.URI)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 Configuration (org.hisp.dhis.configuration.Configuration)2 ImportSummariesResponseExtractor (org.hisp.dhis.dxf2.common.ImportSummariesResponseExtractor)2 Events (org.hisp.dhis.dxf2.events.event.Events)2 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)2 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)2 Test (org.junit.Test)2 HttpEntity (org.springframework.http.HttpEntity)2 HttpHeaders (org.springframework.http.HttpHeaders)2 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)2 RequestCallback (org.springframework.web.client.RequestCallback)2 Account (android.accounts.Account)1