Search in sources :

Example 1 with LocationManagerException

use of org.hisp.dhis.external.location.LocationManagerException in project dhis2-core by dhis2.

the class DefaultDhisConfigurationProvider method loadDhisConf.

// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
private Properties loadDhisConf() throws IllegalStateException {
    try (InputStream in = locationManager.getInputStream(CONF_FILENAME)) {
        Properties conf = PropertiesLoaderUtils.loadProperties(new InputStreamResource(in));
        substituteEnvironmentVariables(conf);
        return conf;
    } catch (LocationManagerException | IOException | SecurityException ex) {
        log.debug(String.format("Could not load %s", CONF_FILENAME), ex);
        throw new IllegalStateException("Properties could not be loaded", ex);
    }
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties) LocationManagerException(org.hisp.dhis.external.location.LocationManagerException) InputStreamResource(org.springframework.core.io.InputStreamResource)

Example 2 with LocationManagerException

use of org.hisp.dhis.external.location.LocationManagerException in project dhis2-core by dhis2.

the class DefaultDocumentService method deleteDocument.

@Override
public void deleteDocument(Document document) {
    // Remove files is !external
    if (!document.isExternal()) {
        if (document.getFileResource() != null) {
            deleteFileFromDocument(document);
            log.info("Document " + document.getUrl() + " successfully deleted");
        } else {
            try {
                File file = locationManager.getFileForReading(document.getUrl(), DocumentService.DIR);
                if (file.delete()) {
                    log.info("Document " + document.getUrl() + " successfully deleted");
                } else {
                    log.warn("Document " + document.getUrl() + " could not be deleted");
                }
            } catch (LocationManagerException ex) {
                log.warn("An error occured while deleting " + document.getUrl());
            }
        }
    }
    documentStore.delete(document);
}
Also used : File(java.io.File) LocationManagerException(org.hisp.dhis.external.location.LocationManagerException)

Example 3 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)

Example 4 with LocationManagerException

use of org.hisp.dhis.external.location.LocationManagerException in project dhis2-core by dhis2.

the class DefaultSystemService method getFixedSystemInfo.

private SystemInfo getFixedSystemInfo() {
    Configuration config = configurationService.getConfiguration();
    // ---------------------------------------------------------------------
    // Version
    // ---------------------------------------------------------------------
    SystemInfo info = loadBuildProperties();
    // ---------------------------------------------------------------------
    // External directory
    // ---------------------------------------------------------------------
    info.setEnvironmentVariable(locationManager.getEnvironmentVariable());
    try {
        File directory = locationManager.getExternalDirectory();
        info.setExternalDirectory(directory.getAbsolutePath());
    } catch (LocationManagerException ex) {
        info.setExternalDirectory("Not set");
    }
    info.setFileStoreProvider(dhisConfig.getProperty(ConfigurationKey.FILESTORE_PROVIDER));
    info.setReadOnlyMode(dhisConfig.getProperty(ConfigurationKey.SYSTEM_READ_ONLY_MODE));
    info.setNodeId(dhisConfig.getProperty(ConfigurationKey.NODE_ID));
    info.setSystemMonitoringUrl(dhisConfig.getProperty(ConfigurationKey.SYSTEM_MONITORING_URL));
    info.setSystemId(config.getSystemId());
    info.setClusterHostname(dhisConfig.getProperty(ConfigurationKey.CLUSTER_HOSTNAME));
    info.setRedisEnabled(dhisConfig.isEnabled(ConfigurationKey.REDIS_ENABLED));
    if (info.isRedisEnabled()) {
        info.setRedisHostname(dhisConfig.getProperty(ConfigurationKey.REDIS_HOST));
    }
    // ---------------------------------------------------------------------
    // Database
    // ---------------------------------------------------------------------
    info.setDatabaseInfo(databaseInfo.instance());
    info.setReadReplicaCount(Integer.valueOf(dhisConfig.getProperty(ConfigurationKey.ACTIVE_READ_REPLICAS)));
    try {
        info.setJavaOpts(System.getenv("JAVA_OPTS"));
    } catch (SecurityException ex) {
        info.setJavaOpts("Unknown");
    }
    Properties props = System.getProperties();
    info.setJavaVersion(props.getProperty("java.version"));
    info.setJavaVendor(props.getProperty("java.vendor"));
    info.setOsName(props.getProperty("os.name"));
    info.setOsArchitecture(props.getProperty("os.arch"));
    info.setOsVersion(props.getProperty("os.version"));
    info.setMemoryInfo(SystemUtils.getMemoryString());
    info.setCpuCores(SystemUtils.getCpuCores());
    info.setEncryption(dhisConfig.getEncryptionStatus().isOk());
    return info;
}
Also used : Configuration(org.hisp.dhis.configuration.Configuration) Properties(java.util.Properties) File(java.io.File) LocationManagerException(org.hisp.dhis.external.location.LocationManagerException)

Example 5 with LocationManagerException

use of org.hisp.dhis.external.location.LocationManagerException in project dhis2-core by dhis2.

the class JCloudsAppStorageService method getAppResource.

@Override
public Resource getAppResource(App app, String pageName) throws IOException {
    if (app == null || !app.getAppStorageSource().equals(AppStorageSource.JCLOUDS)) {
        log.warn("Can't look up resource " + pageName + ". The specified app was not found in JClouds storage.");
        return null;
    }
    String key = (app.getFolderName() + ("/" + pageName)).replaceAll("//", "/");
    URI uri = getSignedGetContentUri(key);
    if (uri == null) {
        String filepath = configurationProvider.getProperty(ConfigurationKey.FILESTORE_CONTAINER) + "/" + key;
        filepath = filepath.replaceAll("//", "/");
        File res;
        try {
            res = locationManager.getFileForReading(filepath);
        } catch (LocationManagerException e) {
            return null;
        }
        if (res.isDirectory()) {
            String indexPath = pageName.replaceAll("/+$", "") + "/index.html";
            log.info("Resource " + pageName + " (" + filepath + " is a directory, serving " + indexPath);
            return getAppResource(app, indexPath);
        } else if (res.exists()) {
            return new FileSystemResource(res);
        } else {
            return null;
        }
    }
    return new UrlResource(uri);
}
Also used : UrlResource(org.springframework.core.io.UrlResource) FileSystemResource(org.springframework.core.io.FileSystemResource) URI(java.net.URI) ZipFile(java.util.zip.ZipFile) File(java.io.File) LocationManagerException(org.hisp.dhis.external.location.LocationManagerException)

Aggregations

LocationManagerException (org.hisp.dhis.external.location.LocationManagerException)7 File (java.io.File)5 IOException (java.io.IOException)3 Properties (java.util.Properties)3 InputStream (java.io.InputStream)2 PostConstruct (javax.annotation.PostConstruct)2 FileSystemResource (org.springframework.core.io.FileSystemResource)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)1 URI (java.net.URI)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 ZipFile (java.util.zip.ZipFile)1 Configuration (org.hibernate.cfg.Configuration)1 Configuration (org.hisp.dhis.configuration.Configuration)1 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)1 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)1 InputStreamResource (org.springframework.core.io.InputStreamResource)1 UrlResource (org.springframework.core.io.UrlResource)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1