use of org.hisp.dhis.dxf2.webmessage.WebMessage in project dhis2-core by dhis2.
the class MinMaxDataElementController method postJsonObject.
//--------------------------------------------------------------------------
// POST
//--------------------------------------------------------------------------
@RequestMapping(method = RequestMethod.POST, consumes = "application/json")
@PreAuthorize("hasRole('ALL') or hasRole('F_MINMAX_DATAELEMENT_ADD')")
public void postJsonObject(HttpServletRequest request, HttpServletResponse response) throws Exception {
MinMaxDataElement minMax = renderService.fromJson(request.getInputStream(), MinMaxDataElement.class);
validate(minMax);
minMax = getReferences(minMax);
MinMaxDataElement persisted = minMaxService.getMinMaxDataElement(minMax.getSource(), minMax.getDataElement(), minMax.getOptionCombo());
if (Objects.isNull(persisted)) {
minMaxService.addMinMaxDataElement(minMax);
} else {
persisted.mergeWith(minMax);
minMaxService.updateMinMaxDataElement(persisted);
}
WebMessage webMessage = new WebMessage();
webMessage.setHttpStatus(HttpStatus.CREATED);
webMessage.setStatus(Status.OK);
webMessageService.send(webMessage, response, request);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessage in project dhis2-core by dhis2.
the class EventController method getEventDataValueFile.
@RequestMapping(value = "/files", method = RequestMethod.GET)
@PreAuthorize("hasRole('ALL') or hasRole('F_TRACKED_ENTITY_DATAVALUE_ADD') or hasRole('F_TRACKED_ENTITY_DATAVALUE_READ')")
public void getEventDataValueFile(@RequestParam String eventUid, @RequestParam String dataElementUid, HttpServletResponse response, HttpServletRequest request) throws Exception {
Event event = eventService.getEvent(eventUid);
if (event == null) {
throw new WebMessageException(WebMessageUtils.notFound("Event not found for ID " + eventUid));
}
DataElement dataElement = dataElementService.getDataElement(dataElementUid);
if (dataElement == null) {
throw new WebMessageException(WebMessageUtils.notFound("DataElement not found for ID " + dataElementUid));
}
if (!dataElement.isFileType()) {
throw new WebMessageException(WebMessageUtils.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(WebMessageUtils.conflict("DataElement must be of type file"));
}
FileResource fileResource = fileResourceService.getFileResource(uid);
if (fileResource == null || fileResource.getDomain() != FileResourceDomain.DATA_VALUE) {
throw new WebMessageException(WebMessageUtils.notFound("A data value file resource with id " + uid + " does not exist."));
}
if (fileResource.getStorageStatus() != FileResourceStorageStatus.STORED) {
// -----------------------------------------------------------------
// The FileResource exists and is tied to DataValue, however the
// underlying file content still not stored to external file store
// -----------------------------------------------------------------
WebMessage webMessage = WebMessageUtils.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.");
webMessage.setResponse(new FileResourceWebMessageResponse(fileResource));
throw new WebMessageException(webMessage);
}
ByteSource content = fileResourceService.getFileResourceContent(fileResource);
if (content == null) {
throw new WebMessageException(WebMessageUtils.notFound("The referenced file could not be found"));
}
// ---------------------------------------------------------------------
// Attempt to build signed URL request for content and redirect
// ---------------------------------------------------------------------
URI signedGetUri = fileResourceService.getSignedGetFileResourceContentUri(uid);
if (signedGetUri != null) {
response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
response.setHeader(HttpHeaders.LOCATION, signedGetUri.toASCIIString());
return;
}
// ---------------------------------------------------------------------
// Build response and return
// ---------------------------------------------------------------------
response.setContentType(fileResource.getContentType());
response.setContentLength(new Long(fileResource.getContentLength()).intValue());
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "filename=" + fileResource.getName());
// ---------------------------------------------------------------------
// Request signing is not available, stream content back to client
// ---------------------------------------------------------------------
InputStream inputStream = null;
try {
inputStream = content.openStream();
IOUtils.copy(inputStream, response.getOutputStream());
} catch (IOException e) {
throw new WebMessageException(WebMessageUtils.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."));
} finally {
IOUtils.closeQuietly(inputStream);
}
}
use of org.hisp.dhis.dxf2.webmessage.WebMessage in project dhis2-core by dhis2.
the class StaticContentController method updateStaticContent.
/**
* Uploads PNG images based on a key. Only accepts PNG and white listed keys.
*
* @param key the key.
* @param file the image file.
*/
@PreAuthorize("hasRole('ALL') or hasRole('F_SYSTEM_SETTING')")
@ResponseStatus(HttpStatus.NO_CONTENT)
@RequestMapping(value = "/{key}", method = RequestMethod.POST)
public void updateStaticContent(@PathVariable("key") String key, @RequestParam(value = "file") MultipartFile file) throws WebMessageException, IOException {
if (file == null || file.isEmpty()) {
throw new WebMessageException(WebMessageUtils.badRequest("Missing parameter 'file'"));
}
// Only PNG is accepted at the current time
MimeType mimeType = MimeTypeUtils.parseMimeType(file.getContentType());
if (!mimeType.isCompatibleWith(MimeTypeUtils.IMAGE_PNG)) {
throw new WebMessageException(new WebMessage(Status.WARNING, HttpStatus.UNSUPPORTED_MEDIA_TYPE));
}
if (!KEY_WHITELIST_MAP.containsKey(key)) {
throw new WebMessageException(WebMessageUtils.badRequest("This key is not supported."));
}
File out = null;
try {
out = locationManager.getFileForWriting(key + ".png", "static");
} catch (LocationManagerException e) {
throw new WebMessageException(WebMessageUtils.error(e.getMessage()));
}
try {
file.transferTo(out);
} catch (IOException e) {
throw new WebMessageException(WebMessageUtils.error("Could not save file."));
}
}
use of org.hisp.dhis.dxf2.webmessage.WebMessage in project dhis2-core by dhis2.
the class WebMessageUtils method importReport.
public static WebMessage importReport(ImportReport importReport) {
WebMessage webMessage = new WebMessage();
webMessage.setResponse(new ImportReportWebMessageResponse(importReport));
webMessage.setStatus(importReport.getStatus());
if (webMessage.getStatus() != Status.OK) {
webMessage.setMessage("One more more errors occurred, please see full details in import report.");
webMessage.setStatus(Status.WARNING);
webMessage.setHttpStatus(HttpStatus.CONFLICT);
}
return webMessage;
}
use of org.hisp.dhis.dxf2.webmessage.WebMessage in project dhis2-core by dhis2.
the class WebMessageUtils method typeReport.
public static WebMessage typeReport(TypeReport typeReport) {
WebMessage webMessage = new WebMessage();
webMessage.setResponse(new TypeReportWebMessageResponse(typeReport));
if (typeReport.getErrorReports().isEmpty()) {
webMessage.setStatus(Status.OK);
webMessage.setHttpStatus(HttpStatus.OK);
} else {
webMessage.setMessage("One more more errors occurred, please see full details in import report.");
webMessage.setStatus(Status.ERROR);
webMessage.setHttpStatus(HttpStatus.CONFLICT);
}
return webMessage;
}
Aggregations