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