package fr.insee.rmes.config;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Configuration {
    private static Logger logger = LogManager.getLogger(Configuration.class);

    private static String sparqlEndPoint = "";

    //Base for all uri in our database
    private static String baseHost = "";
    
    //folder where documents are stored
    private static String fileStorage = "";
    //Server where documents are stored
    private static String fileStorageLocation = "";

    //API Server
    private static String swaggerHost = "";
    //API name
    private static String swaggerBasepath = "";
    //Build with host and basepath
    private static String swaggerUrl = "";
    //Https or Http
    private static Boolean requiresSsl = false;

    private Properties getProperties() throws IOException {
        Properties props = new Properties();
        props.load(this.getClass().getClassLoader().getResourceAsStream("rmes-api.properties"));
        this.loadIfExists(props, "rmes-api.properties");
        this.loadIfExists(props, "rmeswnci.properties");
        this.loadIfExists(props, "rmeswncz.properties");
        return props;
    }

    /*
     * load properties on catalina base
     */
    private void loadIfExists(Properties props, String filename) throws IOException {
        File f;
        f = new File(String.format("%s/webapps/%s", System.getProperty("catalina.base"), filename));
        if (f.exists() && ! f.isDirectory()) {
            FileReader r = new FileReader(f);
            props.load(r);
            r.close();
        }
    }

    public Configuration() {
        Properties props = null;
        try {
            props = this.getProperties();

            sparqlEndPoint = props.getProperty("fr.insee.rmes.api.sparqlEndpoint");
            baseHost = props.getProperty("fr.insee.rmes.api.baseHost");
            fileStorage = props.getProperty("fr.insee.rmes.api.fileStorage");
            fileStorageLocation = props.getProperty("fr.insee.rmes.storage.document");

            swaggerHost = props.getProperty("fr.insee.rmes.api.host");
            swaggerBasepath = props.getProperty("fr.insee.rmes.api.basepath");
            requiresSsl = Boolean.valueOf(props.getProperty("fr.insee.rmes.api.force.ssl"));
            swaggerUrl = (requiresSsl ? "https" : "http") + "://" + swaggerHost + "/" + swaggerBasepath;

        }
        catch (IOException e) {
            logger.error("Configuration error, can't read properties", e);
        }

    }

    public static String getSparqlEndPoint() {
        return sparqlEndPoint;
    }

    public static void setSparqlEndPoint(String sparqlEndPoint) {
        Configuration.sparqlEndPoint = sparqlEndPoint;
    }

    public static String getBaseHost() {
        return baseHost;
    }

    public static void setBaseHost(String baseHost) {
        Configuration.baseHost = baseHost;
    }

    public static String getFileStorage() {
        return fileStorage;
    }

    public static void setFileStorage(String fileStorage) {
        Configuration.fileStorage = fileStorage;
    }

    public static String getFileStorageLocation() {
        return fileStorageLocation;
    }

    public static void setFileStorageLocation(String fileStorageLocation) {
        Configuration.fileStorageLocation = fileStorageLocation;
    }

    public static String getSwaggerHost() {
        return swaggerHost;
    }

    public static void setSwaggerHost(String swaggerHost) {
        Configuration.swaggerHost = swaggerHost;
    }

    public static String getSwaggerBasepath() {
        return swaggerBasepath;
    }

    public static void setSwaggerBasepath(String swaggerBasepath) {
        Configuration.swaggerBasepath = swaggerBasepath;
    }

    public static String getSwaggerUrl() {
        return swaggerUrl;
    }

    public static void setSwaggerUrl(String swaggerUrl) {
        Configuration.swaggerUrl = swaggerUrl;
    }

    public static Boolean getRequiresSsl() {
        return requiresSsl;
    }

    public static void setRequiresSsl(Boolean requiresSsl) {
        Configuration.requiresSsl = requiresSsl;
    }

}

@@ -1269,32 +1269,98 @@
 z.properties%22);%0A
+        this.loadIfExists(props, %22rmes-metadata-api.properties%22);%0A
         return p

Add another properties name (cei)