use of org.hisp.dhis.dxf2.webmessage.WebMessageParseException in project dhis2-core by dhis2.
the class DefaultSynchronizationManager method executeDataPush.
/**
* Executes a push of data values to the given remote instance.
*
* @param instance the remote system instance.
* @return an ImportSummary.
*/
private ImportSummary executeDataPush(SystemInstance instance) throws WebMessageParseException {
// ---------------------------------------------------------------------
// Set time for last success to start of process to make data saved
// subsequently part of next synch process without being ignored
// ---------------------------------------------------------------------
final Date startTime = new Date();
final Date lastSuccessTime = getLastDataSynchSuccessFallback();
final int lastUpdatedCount = dataValueService.getDataValueCountLastUpdatedAfter(lastSuccessTime, true);
log.info("Values: " + lastUpdatedCount + " since last synch success: " + lastSuccessTime);
if (lastUpdatedCount == 0) {
log.debug("Skipping synch, no new or updated data values");
return null;
}
log.info("Values: " + lastUpdatedCount + " since last synch success: " + lastSuccessTime);
log.info("Remote server POST URL: " + instance.getUrl());
final RequestCallback requestCallback = request -> {
request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
request.getHeaders().add(HEADER_AUTHORIZATION, CodecUtils.getBasicAuthString(instance.getUsername(), instance.getPassword()));
dataValueSetService.writeDataValueSetJson(lastSuccessTime, request.getBody(), new IdSchemes());
};
ResponseExtractor<ImportSummary> responseExtractor = new ImportSummaryResponseExtractor();
ImportSummary summary = null;
try {
summary = restTemplate.execute(instance.getUrl(), HttpMethod.POST, requestCallback, responseExtractor);
} catch (HttpClientErrorException ex) {
String responseBody = ex.getResponseBodyAsString();
summary = WebMessageParseUtils.fromWebMessageResponse(responseBody, ImportSummary.class);
} catch (HttpServerErrorException ex) {
String responseBody = ex.getResponseBodyAsString();
log.error("Internal error happened during event data push: " + responseBody, ex);
throw ex;
} catch (ResourceAccessException ex) {
log.error("Exception during event data push: " + ex.getMessage(), ex);
throw ex;
}
log.info("Synch summary: " + summary);
if (summary != null && ImportStatus.SUCCESS.equals(summary.getStatus())) {
setLastDataSynchSuccess(startTime);
log.info("Synch successful, setting last success time: " + startTime);
} else {
log.warn("Sync failed: " + summary);
}
return summary;
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageParseException in project dhis2-core by dhis2.
the class DefaultSynchronizationManager method executeEventPush.
@Override
public ImportSummaries executeEventPush() throws WebMessageParseException {
AvailabilityStatus availability = isRemoteServerAvailable();
if (!availability.isAvailable()) {
log.info("Aborting synch, server not available");
return null;
}
// ---------------------------------------------------------------------
// Set time for last success to start of process to make data saved
// subsequently part of next synch process without being ignored
// ---------------------------------------------------------------------
final Date startTime = new Date();
final Date lastSuccessTime = getLastEventSynchSuccessFallback();
int lastUpdatedEventsCount = eventService.getAnonymousEventValuesCountLastUpdatedAfter(lastSuccessTime);
log.info("Events: " + lastUpdatedEventsCount + " since last synch success: " + lastSuccessTime);
if (lastUpdatedEventsCount == 0) {
log.info("Skipping synch, no new or updated data values for events");
return null;
}
String url = systemSettingManager.getSystemSetting(SettingKey.REMOTE_INSTANCE_URL) + "/api/events";
log.info("Remote server events POST URL: " + url);
final String username = (String) systemSettingManager.getSystemSetting(SettingKey.REMOTE_INSTANCE_USERNAME);
final String password = (String) systemSettingManager.getSystemSetting(SettingKey.REMOTE_INSTANCE_PASSWORD);
final RequestCallback requestCallback = new RequestCallback() {
@Override
public void doWithRequest(ClientHttpRequest request) throws IOException {
request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
request.getHeaders().add(HEADER_AUTHORIZATION, CodecUtils.getBasicAuthString(username, password));
Events result = eventService.getAnonymousEventValuesLastUpdatedAfter(lastSuccessTime);
renderService.toJson(request.getBody(), result);
}
};
ResponseExtractor<ImportSummaries> responseExtractor = new ImportSummariesResponseExtractor();
ImportSummaries summaries = null;
try {
summaries = restTemplate.execute(url, HttpMethod.POST, requestCallback, responseExtractor);
} catch (HttpClientErrorException ex) {
String responseBody = ex.getResponseBodyAsString();
summaries = WebMessageParseUtils.fromWebMessageResponse(responseBody, ImportSummaries.class);
} catch (HttpServerErrorException ex) {
String responseBody = ex.getResponseBodyAsString();
log.error("Internal error happened during event data push: " + responseBody, ex);
throw ex;
} catch (ResourceAccessException ex) {
log.error("Exception during event data push: " + ex.getMessage(), ex);
throw ex;
}
log.info("Event synch summary: " + summaries);
boolean isError = false;
if (summaries != null) {
for (ImportSummary summary : summaries.getImportSummaries()) {
if (ImportStatus.ERROR.equals(summary.getStatus()) || ImportStatus.WARNING.equals(summary.getStatus())) {
isError = true;
log.debug("Sync failed: " + summaries);
break;
}
}
}
if (!isError) {
setLastEventSynchSuccess(startTime);
log.info("Synch successful, setting last success time: " + startTime);
}
return summaries;
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageParseException in project dhis2-core by dhis2.
the class WebMessageParseUtils method parseJson.
private static <T> T parseJson(String input, Class<T> klass) throws WebMessageParseException {
JsonNode objectNode = null;
try {
objectNode = JSON_MAPPER.readTree(input);
} catch (IOException e) {
throw new WebMessageParseException("Invalid JSON String. " + e.getMessage(), e);
}
JsonNode responseNode = null;
if (objectNode != null) {
responseNode = objectNode.get("response");
} else {
throw new WebMessageParseException("The object node is null. Could not parse the JSON.");
}
try {
return JSON_MAPPER.readValue(responseNode.toString(), klass);
} catch (IOException e) {
throw new WebMessageParseException("Could not parse the JSON." + e.getMessage(), e);
}
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageParseException in project dhis2-core by dhis2.
the class SyncUtils method runSyncRequest.
public static Optional<AbstractWebMessageResponse> runSyncRequest(RestTemplate restTemplate, RequestCallback requestCallback, Class<? extends AbstractWebMessageResponse> klass, String syncUrl, int maxSyncAttempts) {
boolean networkErrorOccurred = true;
int syncAttemptsDone = 0;
ResponseExtractor<? extends AbstractWebMessageResponse> responseExtractor = getResponseExtractor(klass);
AbstractWebMessageResponse responseSummary = null;
while (networkErrorOccurred) {
networkErrorOccurred = false;
syncAttemptsDone++;
try {
responseSummary = restTemplate.execute(syncUrl, HttpMethod.POST, requestCallback, responseExtractor);
} catch (HttpClientErrorException ex) {
String responseBody = ex.getResponseBodyAsString();
try {
responseSummary = WebMessageParseUtils.fromWebMessageResponse(responseBody, klass);
} catch (WebMessageParseException e) {
log.error("Parsing WebMessageResponse failed.", e);
return Optional.empty();
}
} catch (HttpServerErrorException ex) {
String responseBody = ex.getResponseBodyAsString();
log.error("Internal error happened during event data push: " + responseBody, ex);
if (syncAttemptsDone <= maxSyncAttempts) {
networkErrorOccurred = true;
} else {
throw ex;
}
} catch (ResourceAccessException ex) {
log.error("Exception during event data push: " + ex.getMessage(), ex);
throw ex;
}
}
log.info("Sync summary: " + responseSummary);
return Optional.ofNullable(responseSummary);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageParseException in project dhis2-core by dhis2.
the class DefaultSynchronizationManager method executeDataValuePush.
/**
* Executes a push of data values to the given remote instance.
*
* @param instance the remote system instance.
* @return an ImportSummary.
*/
private ImportConflicts executeDataValuePush(SystemInstance instance) throws WebMessageParseException {
// ---------------------------------------------------------------------
// Set time for last success to start of process to make data saved
// subsequently part of next synch process without being ignored
// ---------------------------------------------------------------------
final Date startTime = new Date();
final Date lastSuccessTime = SyncUtils.getLastSyncSuccess(systemSettingManager, SettingKey.LAST_SUCCESSFUL_DATA_VALUE_SYNC);
final Date skipChangedBefore = systemSettingManager.getDateSetting(SettingKey.SKIP_SYNCHRONIZATION_FOR_DATA_CHANGED_BEFORE);
final Date lastUpdatedAfter = lastSuccessTime.after(skipChangedBefore) ? lastSuccessTime : skipChangedBefore;
final int objectsToSynchronize = dataValueService.getDataValueCountLastUpdatedAfter(lastUpdatedAfter, true);
log.info("DataValues last changed before " + skipChangedBefore + " will not be synchronized.");
if (objectsToSynchronize == 0) {
SyncUtils.setLastSyncSuccess(systemSettingManager, SettingKey.LAST_SUCCESSFUL_DATA_VALUE_SYNC, startTime);
log.debug("Skipping data values push, no new or updated data values");
ImportCount importCount = new ImportCount(0, 0, 0, 0);
return new ImportSummary(ImportStatus.SUCCESS, "No new or updated data values to push.", importCount);
}
log.info("Data Values: " + objectsToSynchronize + " to push since last synchronization success: " + lastSuccessTime);
log.info("Remote server POST URL: " + instance.getUrl());
final RequestCallback requestCallback = request -> {
request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
request.getHeaders().add(HEADER_AUTHORIZATION, CodecUtils.getBasicAuthString(instance.getUsername(), instance.getPassword()));
dataValueSetService.exportDataValueSetJson(lastUpdatedAfter, request.getBody(), new IdSchemes());
};
final int maxSyncAttempts = systemSettingManager.getIntSetting(SettingKey.MAX_SYNC_ATTEMPTS);
Optional<AbstractWebMessageResponse> responseSummary = SyncUtils.runSyncRequest(restTemplate, requestCallback, SyncEndpoint.DATA_VALUE_SETS.getKlass(), instance.getUrl(), maxSyncAttempts);
ImportSummary summary = null;
if (responseSummary.isPresent()) {
summary = (ImportSummary) responseSummary.get();
if (ImportStatus.SUCCESS.equals(summary.getStatus())) {
log.info("Push successful: " + summary);
} else {
log.warn("Push failed: " + summary);
}
}
return summary;
}
Aggregations