Search in sources :

Example 1 with DhisHttpResponse

use of org.hisp.dhis.system.util.DhisHttpResponse in project dhis2-core by dhis2.

the class MetadataVersionDelegate method getMetaDataDifference.

public List<MetadataVersion> getMetaDataDifference(MetadataVersion metadataVersion) {
    String url;
    List<MetadataVersion> metadataVersions = new ArrayList<>();
    if (metadataVersion == null) {
        url = metadataSystemSettingService.getEntireVersionHistory();
    } else {
        url = metadataSystemSettingService.getMetaDataDifferenceURL(metadataVersion.getName());
    }
    DhisHttpResponse dhisHttpResponse = getDhisHttpResponse(url, VERSION_TIMEOUT);
    if (isValidDhisHttpResponse(dhisHttpResponse)) {
        try {
            metadataVersions = renderService.fromMetadataVersion(new ByteArrayInputStream(dhisHttpResponse.getResponse().getBytes()), RenderFormat.JSON);
            return metadataVersions;
        } catch (IOException io) {
            String message = "Exception occurred while trying to do JSON conversion. Caused by:  " + io.getMessage();
            log.error(message, io);
            throw new MetadataVersionServiceException(message, io);
        }
    }
    log.warn("Returning empty for the metadata versions difference");
    return metadataVersions;
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 2 with DhisHttpResponse

use of org.hisp.dhis.system.util.DhisHttpResponse in project dhis2-core by dhis2.

the class MetadataVersionDelegate method getDhisHttpResponse.

//----------------------------------------------------------------------------------------
// Private Methods
//----------------------------------------------------------------------------------------
private DhisHttpResponse getDhisHttpResponse(String url, int timeout) {
    AvailabilityStatus remoteServerAvailable = synchronizationManager.isRemoteServerAvailable();
    if (!(remoteServerAvailable.isAvailable())) {
        String message = remoteServerAvailable.getMessage();
        log.error(message);
        throw new RemoteServerUnavailableException(message);
    }
    String username = metadataSystemSettingService.getRemoteInstanceUserName();
    String password = metadataSystemSettingService.getRemoteInstancePassword();
    log.info("Remote server metadata version  URL: " + url + ", username: " + username);
    DhisHttpResponse dhisHttpResponse = null;
    try {
        dhisHttpResponse = HttpUtils.httpGET(url, true, username, password, null, timeout, true);
    } catch (Exception e) {
        String message = "Exception occurred while trying to make the GET call to URL: " + url;
        log.error(message, e);
        throw new MetadataVersionServiceException(message, e);
    }
    return dhisHttpResponse;
}
Also used : MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) RemoteServerUnavailableException(org.hisp.dhis.exception.RemoteServerUnavailableException) IOException(java.io.IOException) RemoteServerUnavailableException(org.hisp.dhis.exception.RemoteServerUnavailableException) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException)

Example 3 with DhisHttpResponse

use of org.hisp.dhis.system.util.DhisHttpResponse in project dhis2-core by dhis2.

the class MetadataVersionDelegateTest method testShouldThrowExceptionWhenRenderServiceThrowsException.

@Test
public void testShouldThrowExceptionWhenRenderServiceThrowsException() throws Exception {
    AvailabilityStatus availabilityStatus = new AvailabilityStatus(true, "testMessage", null);
    DhisHttpResponse dhisHttpResponse = new DhisHttpResponse(httpResponse, response, HttpStatus.OK.value());
    when(metadataSystemSettingService.getVersionDetailsUrl("testVersion")).thenReturn(versionUrl);
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
    PowerMockito.when(HttpUtils.httpGET(versionUrl, true, username, password, null, VERSION_TIMEOUT, true)).thenReturn(dhisHttpResponse);
    when(renderService.fromJson(response, MetadataVersion.class)).thenThrow(new MetadataVersionServiceException(""));
    expectedException.expect(MetadataVersionServiceException.class);
    expectedException.expectMessage("Exception occurred while trying to do JSON conversion for metadata version");
    metadataVersionDelegate.getRemoteMetadataVersion("testVersion");
}
Also used : MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) IntegrationTest(org.hisp.dhis.IntegrationTest) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 4 with DhisHttpResponse

use of org.hisp.dhis.system.util.DhisHttpResponse in project dhis2-core by dhis2.

the class MetadataVersionDelegateTest method testShouldGetRemoteMetadataVersionWithStatusOk.

@Test
public void testShouldGetRemoteMetadataVersionWithStatusOk() throws Exception {
    AvailabilityStatus availabilityStatus = new AvailabilityStatus(true, "testMessage", null);
    DhisHttpResponse dhisHttpResponse = new DhisHttpResponse(httpResponse, response, HttpStatus.OK.value());
    when(metadataSystemSettingService.getVersionDetailsUrl("testVersion")).thenReturn(versionUrl);
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
    PowerMockito.when(HttpUtils.httpGET(versionUrl, true, username, password, null, VERSION_TIMEOUT, true)).thenReturn(dhisHttpResponse);
    when(renderService.fromJson(response, MetadataVersion.class)).thenReturn(metadataVersion);
    MetadataVersion remoteMetadataVersion = metadataVersionDelegate.getRemoteMetadataVersion("testVersion");
    assertEquals(metadataVersion.getType(), remoteMetadataVersion.getType());
    assertEquals(metadataVersion.getHashCode(), remoteMetadataVersion.getHashCode());
    assertEquals(metadataVersion.getName(), remoteMetadataVersion.getName());
    assertEquals(metadataVersion, remoteMetadataVersion);
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) IntegrationTest(org.hisp.dhis.IntegrationTest) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 5 with DhisHttpResponse

use of org.hisp.dhis.system.util.DhisHttpResponse in project dhis2-core by dhis2.

the class MetadataVersionDelegate method downloadMetadataVersionSnapshot.

public String downloadMetadataVersionSnapshot(MetadataVersion version) throws MetadataVersionServiceException {
    String downloadVersionSnapshotURL = metadataSystemSettingService.getDownloadVersionSnapshotURL(version.getName());
    DhisHttpResponse dhisHttpResponse = getDhisHttpResponse(downloadVersionSnapshotURL, DOWNLOAD_TIMEOUT);
    if (isValidDhisHttpResponse(dhisHttpResponse)) {
        return dhisHttpResponse.getResponse();
    }
    return null;
}
Also used : DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse)

Aggregations

DhisHttpResponse (org.hisp.dhis.system.util.DhisHttpResponse)12 AvailabilityStatus (org.hisp.dhis.dxf2.synch.AvailabilityStatus)9 MetadataVersion (org.hisp.dhis.metadata.version.MetadataVersion)9 DhisSpringTest (org.hisp.dhis.DhisSpringTest)8 IntegrationTest (org.hisp.dhis.IntegrationTest)8 Test (org.junit.Test)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 IOException (java.io.IOException)5 MetadataVersionServiceException (org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException)4 HttpResponse (org.apache.http.HttpResponse)3 InputStream (org.omg.CORBA.portable.InputStream)3 ArrayList (java.util.ArrayList)2 RemoteServerUnavailableException (org.hisp.dhis.exception.RemoteServerUnavailableException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1