Search in sources :

Example 6 with FileResourceWebMessageResponse

use of org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse in project dhis2-core by dhis2.

the class DataValueController method saveFileDataValue.

@PreAuthorize("hasRole('ALL') or hasRole('F_DATAVALUE_ADD')")
@PostMapping(FILE_PATH)
@ResponseBody
public WebMessage saveFileDataValue(@RequestParam String de, @RequestParam(required = false) String co, @RequestParam(required = false) String cc, @RequestParam(required = false) String cp, @RequestParam String pe, @RequestParam String ou, @RequestParam(required = false) String ds, @RequestParam(required = false) String comment, @RequestParam(required = false) Boolean followUp, @RequestParam(required = false) boolean force, @RequestParam MultipartFile file, @CurrentUser User currentUser) throws WebMessageException, IOException {
    FileResource fileResource = fileResourceUtils.saveFileResource(file, FileResourceDomain.DATA_VALUE);
    saveDataValueInternal(de, co, cc, cp, pe, ou, ds, fileResource.getUid(), comment, followUp, force, currentUser);
    WebMessage webMessage = new WebMessage(Status.OK, HttpStatus.ACCEPTED);
    webMessage.setResponse(new FileResourceWebMessageResponse(fileResource));
    return webMessage;
}
Also used : FileResourceWebMessageResponse(org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse) FileResource(org.hisp.dhis.fileresource.FileResource) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) PostMapping(org.springframework.web.bind.annotation.PostMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 7 with FileResourceWebMessageResponse

use of org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse in project dhis2-core by dhis2.

the class DataValueController method getDataValueFile.

// ---------------------------------------------------------------------
// GET file
// ---------------------------------------------------------------------
@GetMapping("/files")
public void getDataValueFile(@RequestParam String de, @RequestParam(required = false) String co, @RequestParam(required = false) String cc, @RequestParam(required = false) String cp, @RequestParam String pe, @RequestParam String ou, @RequestParam(defaultValue = "original") String dimension, HttpServletResponse response, HttpServletRequest request) throws WebMessageException {
    // ---------------------------------------------------------------------
    // Input validation
    // ---------------------------------------------------------------------
    DataElement dataElement = dataValueValidation.getAndValidateDataElement(de);
    if (!dataElement.isFileType()) {
        throw new WebMessageException(conflict("DataElement must be of type file"));
    }
    CategoryOptionCombo categoryOptionCombo = dataValueValidation.getAndValidateCategoryOptionCombo(co, false);
    CategoryOptionCombo attributeOptionCombo = dataValueValidation.getAndValidateAttributeOptionCombo(cc, cp);
    Period period = dataValueValidation.getAndValidatePeriod(pe);
    OrganisationUnit organisationUnit = dataValueValidation.getAndValidateOrganisationUnit(ou);
    dataValueValidation.validateOrganisationUnitPeriod(organisationUnit, period);
    // ---------------------------------------------------------------------
    // Get data value
    // ---------------------------------------------------------------------
    DataValue dataValue = dataValueService.getDataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo);
    if (dataValue == null) {
        throw new WebMessageException(conflict("Data value does not exist"));
    }
    // ---------------------------------------------------------------------
    // Get file resource
    // ---------------------------------------------------------------------
    String uid = dataValue.getValue();
    FileResource fileResource = fileResourceService.getFileResource(uid);
    if (fileResource == null || fileResource.getDomain() != FileResourceDomain.DATA_VALUE) {
        throw new WebMessageException(notFound("A data value file resource with id " + uid + " does not exist."));
    }
    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.setHeader(HttpHeaders.CONTENT_DISPOSITION, "filename=" + fileResource.getName());
    response.setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(fileResourceService.getFileResourceContentLength(fileResource)));
    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 : DataElement(org.hisp.dhis.dataelement.DataElement) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) FileResourceWebMessageResponse(org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) DataValue(org.hisp.dhis.datavalue.DataValue) FileResourceStorageStatus(org.hisp.dhis.fileresource.FileResourceStorageStatus) FileResource(org.hisp.dhis.fileresource.FileResource) Period(org.hisp.dhis.period.Period) IOException(java.io.IOException) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 8 with FileResourceWebMessageResponse

use of org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse in project dhis2-core by dhis2.

the class EventController method getEventDataValueFile.

@GetMapping("/files")
public void getEventDataValueFile(@RequestParam String eventUid, @RequestParam String dataElementUid, @RequestParam(required = false) ImageFileDimension dimension, HttpServletResponse response, HttpServletRequest request) throws Exception {
    Event event = eventService.getEvent(programStageInstanceService.getProgramStageInstance(eventUid));
    if (event == null) {
        throw new WebMessageException(notFound("Event not found for ID " + eventUid));
    }
    DataElement dataElement = dataElementService.getDataElement(dataElementUid);
    if (dataElement == null) {
        throw new WebMessageException(notFound("DataElement not found for ID " + dataElementUid));
    }
    if (!dataElement.isFileType()) {
        throw new WebMessageException(conflict("DataElement must be of type file"));
    }
    // ---------------------------------------------------------------------
    // Get file resource
    // ---------------------------------------------------------------------
    String uid = null;
    for (DataValue value : event.getDataValues()) {
        if (value.getDataElement() != null && value.getDataElement().equals(dataElement.getUid())) {
            uid = value.getValue();
            break;
        }
    }
    if (uid == null) {
        throw new WebMessageException(conflict("DataElement must be of type file"));
    }
    FileResource fileResource = fileResourceService.getFileResource(uid);
    if (fileResource == null || fileResource.getDomain() != FileResourceDomain.DATA_VALUE) {
        throw new WebMessageException(notFound("A data value file resource with id " + uid + " does not exist."));
    }
    if (fileResource.getStorageStatus() != FileResourceStorageStatus.STORED) {
        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)));
    }
    FileResourceUtils.setImageFileDimensions(fileResource, MoreObjects.firstNonNull(dimension, ImageFileDimension.ORIGINAL));
    response.setContentType(fileResource.getContentType());
    response.setContentLength(new Long(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 : DataElement(org.hisp.dhis.dataelement.DataElement) FileResourceWebMessageResponse(org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) DataValue(org.hisp.dhis.dxf2.events.event.DataValue) FileResource(org.hisp.dhis.fileresource.FileResource) Event(org.hisp.dhis.dxf2.events.event.Event) IOException(java.io.IOException) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

FileResourceWebMessageResponse (org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse)8 FileResource (org.hisp.dhis.fileresource.FileResource)8 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)6 IOException (java.io.IOException)5 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)5 DataElement (org.hisp.dhis.dataelement.DataElement)4 ByteSource (com.google.common.io.ByteSource)3 FileResourceStorageStatus (org.hisp.dhis.fileresource.FileResourceStorageStatus)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 InputStream (java.io.InputStream)2 URI (java.net.URI)2 DataValue (org.hisp.dhis.datavalue.DataValue)2 DataValue (org.hisp.dhis.dxf2.events.event.DataValue)2 Event (org.hisp.dhis.dxf2.events.event.Event)2 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)2 Period (org.hisp.dhis.period.Period)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2