Search in sources :

Example 51 with ERROR

use of org.hisp.dhis.dxf2.importsummary.ImportStatus.ERROR in project dhis2-core by dhis2.

the class MetadataVersionDelegateTest method testShouldThrowExceptionWhenGettingRemoteMetadataVersionWithServerError.

@Test
void testShouldThrowExceptionWhenGettingRemoteMetadataVersionWithServerError() {
    when(metadataSystemSettingService.getRemoteInstanceUserName()).thenReturn(username);
    when(metadataSystemSettingService.getRemoteInstancePassword()).thenReturn(password);
    String response = "{\"name\":\"testVersion\",\"created\":\"2016-05-26T11:43:59.787+0000\",\"type\":\"BEST_EFFORT\",\"id\":\"ktwh8PHNwtB\",\"hashCode\":\"12wa32d4f2et3tyt5yu6i\"}";
    MetadataVersion metadataVersion = new MetadataVersion("testVersion", VersionType.BEST_EFFORT);
    metadataVersion.setHashCode("12wa32d4f2et3tyt5yu6i");
    AvailabilityStatus availabilityStatus = new AvailabilityStatus(true, "test_message", null);
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
    HttpResponse httpResponse = mock(HttpResponse.class);
    DhisHttpResponse dhisHttpResponse = new DhisHttpResponse(httpResponse, response, HttpStatus.GATEWAY_TIMEOUT.value());
    when(metadataSystemSettingService.getVersionDetailsUrl("testVersion")).thenReturn(versionUrl);
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
    try (MockedStatic<HttpUtils> mocked = mockStatic(HttpUtils.class)) {
        mocked.when(() -> HttpUtils.httpGET(versionUrl, true, username, password, null, VERSION_TIMEOUT, true)).thenReturn(dhisHttpResponse);
        assertThrows(MetadataVersionServiceException.class, () -> target.getRemoteMetadataVersion("testVersion"), "Server Error. Http call failed with status code: " + HttpStatus.GATEWAY_TIMEOUT.value() + " Caused by: " + response);
    }
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) HttpUtils(org.hisp.dhis.system.util.HttpUtils) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 52 with ERROR

use of org.hisp.dhis.dxf2.importsummary.ImportStatus.ERROR in project dhis2-core by dhis2.

the class MetadataVersionDelegateTest method testShouldThrowExceptionWhenGettingRemoteMetadataVersionWithClientError.

@Test
void testShouldThrowExceptionWhenGettingRemoteMetadataVersionWithClientError() {
    when(metadataSystemSettingService.getRemoteInstanceUserName()).thenReturn(username);
    when(metadataSystemSettingService.getRemoteInstancePassword()).thenReturn(password);
    String response = "{\"name\":\"testVersion\",\"created\":\"2016-05-26T11:43:59.787+0000\",\"type\":\"BEST_EFFORT\",\"id\":\"ktwh8PHNwtB\",\"hashCode\":\"12wa32d4f2et3tyt5yu6i\"}";
    MetadataVersion metadataVersion = new MetadataVersion("testVersion", VersionType.BEST_EFFORT);
    metadataVersion.setHashCode("12wa32d4f2et3tyt5yu6i");
    AvailabilityStatus availabilityStatus = new AvailabilityStatus(true, "test_message", null);
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
    HttpResponse httpResponse = mock(HttpResponse.class);
    DhisHttpResponse dhisHttpResponse = new DhisHttpResponse(httpResponse, response, HttpStatus.CONFLICT.value());
    when(metadataSystemSettingService.getVersionDetailsUrl("testVersion")).thenReturn(versionUrl);
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
    try (MockedStatic<HttpUtils> mocked = mockStatic(HttpUtils.class)) {
        mocked.when(() -> HttpUtils.httpGET(versionUrl, true, username, password, null, VERSION_TIMEOUT, true)).thenReturn(dhisHttpResponse);
        assertThrows(MetadataVersionServiceException.class, () -> target.getRemoteMetadataVersion("testVersion"), "Client Error. Http call failed with status code: " + HttpStatus.CONFLICT.value() + " Caused by: " + response);
    }
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) HttpUtils(org.hisp.dhis.system.util.HttpUtils) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 53 with ERROR

use of org.hisp.dhis.dxf2.importsummary.ImportStatus.ERROR in project dhis2-core by dhis2.

the class AuditController method getFileAudit.

/**
 * Returns the file with the given uid
 *
 * @param uid the unique id of the file resource
 */
@GetMapping("/files/{uid}")
public void getFileAudit(@PathVariable String uid, HttpServletResponse response) throws WebMessageException {
    FileResource fileResource = fileResourceService.getFileResource(uid);
    if (fileResource == null || fileResource.getDomain() != FileResourceDomain.DATA_VALUE) {
        throw new WebMessageException(notFound("No file found with uid '" + uid + "'"));
    }
    FileResourceStorageStatus storageStatus = fileResource.getStorageStatus();
    if (storageStatus != FileResourceStorageStatus.STORED) {
        // HTTP 409, for lack of a more suitable status code
        throw new WebMessageException(conflict("The content is being processed and is not available yet. Try again later.", "The content requested is in transit to the file store and will be available at a later time.").setResponse(new FileResourceWebMessageResponse(fileResource)));
    }
    response.setContentType(fileResource.getContentType());
    response.setContentLength(Long.valueOf(fileResource.getContentLength()).intValue());
    response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "filename=" + fileResource.getName());
    try {
        fileResourceService.copyFileResourceContent(fileResource, response.getOutputStream());
    } catch (IOException e) {
        throw new WebMessageException(error("Failed fetching the file from storage", "There was an exception when trying to fetch the file from the storage backend, could be network or filesystem related"));
    }
}
Also used : FileResourceWebMessageResponse(org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) FileResourceStorageStatus(org.hisp.dhis.fileresource.FileResourceStorageStatus) FileResource(org.hisp.dhis.fileresource.FileResource) IOException(java.io.IOException) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 54 with ERROR

use of org.hisp.dhis.dxf2.importsummary.ImportStatus.ERROR in project dhis2-core by dhis2.

the class ExternalFileResourceController method getExternalFileResource.

/**
 * Returns a file associated with the externalFileResource resolved from the
 * accessToken.
 * <p>
 * Only files contained in externalFileResources with a valid accessToken,
 * expiration date null or in the future are files allowed to be served
 * trough this endpoint.
 *
 * @param accessToken a unique string that resolves to a given
 *        externalFileResource
 * @param response
 * @throws WebMessageException
 */
@GetMapping("/{accessToken}")
public void getExternalFileResource(@PathVariable String accessToken, HttpServletResponse response) throws WebMessageException {
    ExternalFileResource externalFileResource = externalFileResourceService.getExternalFileResourceByAccessToken(accessToken);
    if (externalFileResource == null) {
        throw new WebMessageException(notFound("No file found with key '" + accessToken + "'"));
    }
    if (externalFileResource.getExpires() != null && externalFileResource.getExpires().before(new Date())) {
        throw new WebMessageException(WebMessageUtils.createWebMessage("The key you requested has expired", Status.WARNING, HttpStatus.GONE));
    }
    FileResource fileResource = externalFileResource.getFileResource();
    response.setContentType(fileResource.getContentType());
    response.setContentLength(new Long(fileResource.getContentLength()).intValue());
    response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "filename=" + fileResource.getName());
    setNoStore(response);
    try {
        fileResourceService.copyFileResourceContent(fileResource, response.getOutputStream());
    } catch (IOException e) {
        throw new WebMessageException(error("Failed fetching the file from storage", "There was an exception when trying to fetch the file from the storage backend, could be network or filesystem related"));
    }
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) FileResource(org.hisp.dhis.fileresource.FileResource) ExternalFileResource(org.hisp.dhis.fileresource.ExternalFileResource) IOException(java.io.IOException) Date(java.util.Date) ExternalFileResource(org.hisp.dhis.fileresource.ExternalFileResource) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 55 with ERROR

use of org.hisp.dhis.dxf2.importsummary.ImportStatus.ERROR in project dhis2-core by dhis2.

the class FileResourceController method getFileResourceData.

@GetMapping(value = "/{uid}/data")
public void getFileResourceData(@PathVariable String uid, HttpServletResponse response, @RequestParam(required = false) ImageFileDimension dimension, @CurrentUser User currentUser) throws WebMessageException {
    FileResource fileResource = fileResourceService.getFileResource(uid);
    if (fileResource == null) {
        throw new WebMessageException(notFound(FileResource.class, uid));
    }
    FileResourceUtils.setImageFileDimensions(fileResource, MoreObjects.firstNonNull(dimension, ImageFileDimension.ORIGINAL));
    if (!checkSharing(fileResource, currentUser)) {
        throw new WebMessageException(unauthorized("You don't have access to fileResource '" + uid + "' or this fileResource is not available from this endpoint"));
    }
    response.setContentType(fileResource.getContentType());
    response.setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(fileResourceService.getFileResourceContentLength(fileResource)));
    response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "filename=" + fileResource.getName());
    try {
        fileResourceService.copyFileResourceContent(fileResource, response.getOutputStream());
    } catch (IOException e) {
        log.error("Could not retrieve file.", e);
        throw new WebMessageException(error("Failed fetching the file from storage", "There was an exception when trying to fetch the file from the storage backend. " + "Depending on the provider the root cause could be network or file system related."));
    }
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) FileResource(org.hisp.dhis.fileresource.FileResource) IOException(java.io.IOException) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)33 Test (org.junit.jupiter.api.Test)25 User (org.hisp.dhis.user.User)21 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)20 IOException (java.io.IOException)15 Event (org.hisp.dhis.dxf2.events.event.Event)14 List (java.util.List)12 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)11 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)9 ImportStrategy (org.hisp.dhis.importexport.ImportStrategy)9 ClassPathResource (org.springframework.core.io.ClassPathResource)9 ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)8 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)7 GetMapping (org.springframework.web.bind.annotation.GetMapping)7 ArrayList (java.util.ArrayList)6 FileResource (org.hisp.dhis.fileresource.FileResource)6 SchemaService (org.hisp.dhis.schema.SchemaService)6 UserService (org.hisp.dhis.user.UserService)6 Autowired (org.springframework.beans.factory.annotation.Autowired)6 InputStream (java.io.InputStream)4