use of org.hisp.dhis.external.location.LocationManagerException in project dhis2-core by dhis2.
the class StaticContentController method getStaticContent.
/**
* Serves the PNG associated with the key. If custom logo is not used the
* request will redirect to the default.
*
* @param key key associated with the file.
*/
@RequestMapping(value = "/{key}", method = RequestMethod.GET)
public void getStaticContent(@PathVariable("key") String key, HttpServletRequest request, HttpServletResponse response) throws WebMessageException {
if (!KEY_WHITELIST_MAP.containsKey(key)) {
throw new WebMessageException(WebMessageUtils.notFound("Key does not exist."));
}
boolean useCustomFile = (boolean) systemSettingManager.getSystemSetting(KEY_WHITELIST_MAP.get(key));
if (// Serve default
!useCustomFile) {
try {
response.sendRedirect(getDefaultLogoUrl(request, key));
} catch (IOException e) {
throw new WebMessageException(WebMessageUtils.error("Can't read the file."));
}
} else // Serve custom
{
InputStream in = null;
try {
in = locationManager.getInputStream(key + ".png", "static");
response.setContentType("image/png");
IOUtils.copy(in, response.getOutputStream());
} catch (LocationManagerException e) {
throw new WebMessageException(WebMessageUtils.notFound("The requested file could not be found."));
} catch (IOException e) {
throw new WebMessageException(WebMessageUtils.error("Error occurred trying to serve file.", "An IOException was thrown, indicating a file I/O or networking error."));
} finally {
IOUtils.closeQuietly(in);
}
}
}
use of org.hisp.dhis.external.location.LocationManagerException 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."));
}
}
Aggregations