Search in sources :

Example 1 with AbstractWebMessageResponse

use of org.hisp.dhis.dxf2.webmessage.AbstractWebMessageResponse 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);
}
Also used : WebMessageParseException(org.hisp.dhis.dxf2.webmessage.WebMessageParseException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) AbstractWebMessageResponse(org.hisp.dhis.dxf2.webmessage.AbstractWebMessageResponse) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) ResourceAccessException(org.springframework.web.client.ResourceAccessException)

Example 2 with AbstractWebMessageResponse

use of org.hisp.dhis.dxf2.webmessage.AbstractWebMessageResponse 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;
}
Also used : AtomicMode(org.hisp.dhis.dxf2.metadata.AtomicMode) WebMessageParseException(org.hisp.dhis.dxf2.webmessage.WebMessageParseException) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) Date(java.util.Date) ImportCount(org.hisp.dhis.dxf2.importsummary.ImportCount) SyncEndpoint(org.hisp.dhis.dxf2.sync.SyncEndpoint) DataValueService(org.hisp.dhis.datavalue.DataValueService) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Metadata(org.hisp.dhis.dxf2.metadata.Metadata) ImportConflicts(org.hisp.dhis.dxf2.importsummary.ImportConflicts) MetadataImportService(org.hisp.dhis.dxf2.metadata.MetadataImportService) AbstractWebMessageResponse(org.hisp.dhis.dxf2.webmessage.AbstractWebMessageResponse) User(org.hisp.dhis.user.User) SyncUtils(org.hisp.dhis.dxf2.sync.SyncUtils) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) SystemSettingManager(org.hisp.dhis.setting.SystemSettingManager) RestTemplate(org.springframework.web.client.RestTemplate) IdSchemes(org.hisp.dhis.common.IdSchemes) DataValueSetService(org.hisp.dhis.dxf2.datavalueset.DataValueSetService) MediaType(org.springframework.http.MediaType) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) SchemaService(org.hisp.dhis.schema.SchemaService) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) RequestCallback(org.springframework.web.client.RequestCallback) CodecUtils(org.hisp.dhis.system.util.CodecUtils) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) CurrentUserService(org.hisp.dhis.user.CurrentUserService) Optional(java.util.Optional) SettingKey(org.hisp.dhis.setting.SettingKey) RequestCallback(org.springframework.web.client.RequestCallback) IdSchemes(org.hisp.dhis.common.IdSchemes) AbstractWebMessageResponse(org.hisp.dhis.dxf2.webmessage.AbstractWebMessageResponse) ImportCount(org.hisp.dhis.dxf2.importsummary.ImportCount) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Date(java.util.Date) SyncEndpoint(org.hisp.dhis.dxf2.sync.SyncEndpoint)

Aggregations

AbstractWebMessageResponse (org.hisp.dhis.dxf2.webmessage.AbstractWebMessageResponse)2 WebMessageParseException (org.hisp.dhis.dxf2.webmessage.WebMessageParseException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 IOException (java.io.IOException)1 Date (java.util.Date)1 Optional (java.util.Optional)1 Slf4j (lombok.extern.slf4j.Slf4j)1 IdSchemes (org.hisp.dhis.common.IdSchemes)1 DataValueService (org.hisp.dhis.datavalue.DataValueService)1 DataValueSetService (org.hisp.dhis.dxf2.datavalueset.DataValueSetService)1 ImportConflicts (org.hisp.dhis.dxf2.importsummary.ImportConflicts)1 ImportCount (org.hisp.dhis.dxf2.importsummary.ImportCount)1 ImportStatus (org.hisp.dhis.dxf2.importsummary.ImportStatus)1 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)1 AtomicMode (org.hisp.dhis.dxf2.metadata.AtomicMode)1 Metadata (org.hisp.dhis.dxf2.metadata.Metadata)1 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)1 MetadataImportService (org.hisp.dhis.dxf2.metadata.MetadataImportService)1 ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)1