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