Search in sources :

Example 6 with LocationManagerException

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);
        }
    }
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) InputStream(java.io.InputStream) IOException(java.io.IOException) LocationManagerException(org.hisp.dhis.external.location.LocationManagerException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with LocationManagerException

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."));
    }
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) MimeType(org.springframework.util.MimeType) LocationManagerException(org.hisp.dhis.external.location.LocationManagerException) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

LocationManagerException (org.hisp.dhis.external.location.LocationManagerException)7 IOException (java.io.IOException)5 File (java.io.File)4 InputStream (java.io.InputStream)4 Properties (java.util.Properties)3 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 PostConstruct (javax.annotation.PostConstruct)1 Configuration (org.hibernate.cfg.Configuration)1 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)1 DateTime (org.joda.time.DateTime)1 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 FileSystemResource (org.springframework.core.io.FileSystemResource)1 InputStreamResource (org.springframework.core.io.InputStreamResource)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1