/*
 * oxTrust is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

package org.gluu.oxauth.client.util;

import java.io.File;

import org.apache.commons.lang.StringUtils;
import org.xdi.util.properties.FileConfiguration;

/**
 * oAuth properties and constants
 * 
 * @author Yuriy Movchan
 * @version 0.1, 03/20/2013
 */
public final class Configuration {

	static {
        if ((System.getProperty("catalina.base") != null) && (System.getProperty("catalina.base.ignore") == null)) {
            BASE_DIR = System.getProperty("catalina.base");
        } else if (System.getProperty("catalina.home") != null) {
            BASE_DIR = System.getProperty("catalina.home");
        } else if (System.getProperty("jboss.home.dir") != null) {
            BASE_DIR = System.getProperty("jboss.home.dir");
        } else {
            BASE_DIR = null;
        }
    }

    private static final String BASE_DIR;
    private static final String DIR = BASE_DIR + File.separator + "conf" + File.separator;

    /**
     * Represents the constant for where the OAuth data will be located in memory
     */
	public static final String SESSION_OAUTH_DATA = "_oauth_data_";

	/**
	 * OAuth constants
	 */
	public static final String OAUTH_CLIENT_ID = "client_id";
	public static final String OAUTH_CLIENT_PASSWORD = "client_password";
	public static final String OAUTH_CLIENT_CREDENTIALS = "client_credentials";
	public static final String OAUTH_REDIRECT_URI = "redirect_uri";
	public static final String OAUTH_RESPONSE_TYPE = "response_type";
	public static final String OAUTH_SCOPE = "scope";
	public static final String OAUTH_STATE = "state";
	public static final String OAUTH_CODE = "code";
	public static final String OAUTH_ID_TOKEN = "id_token";
	public static final String OAUTH_ERROR = "error";
	public static final String OAUTH_NONCE = "nonce";
	public static final String OAUTH_ERROR_DESCRIPTION = "error_description";
	public static final String OAUTH_ACCESS_TOKEN = "access_token";
	public static final String OAUTH_AUTH_MODE = "acr_values";
    public static final String OAUTH_ID_TOKEN_HINT = "id_token_hint";
    public static final String OAUTH_POST_LOGOUT_REDIRECT_URI = "post_logout_redirect_uri";

	/**
	 * OAuth properties
	 */
	public static final String OAUTH_PROPERTY_AUTHORIZE_URL = "oxauth.authorize.url";
	public static final String OAUTH_PROPERTY_TOKEN_URL = "oxauth.token.url";
	public static final String OAUTH_PROPERTY_TOKEN_VALIDATION_URL = "oxauth.token.validation.url";
	public static final String OAUTH_PROPERTY_CHECKSESSION_URL = "oxauth.checksession.url";
	public static final String OAUTH_PROPERTY_USERINFO_URL = "oxauth.userinfo.url";
	public static final String OAUTH_PROPERTY_LOGOUT_URL = "oxauth.logout.url";
	public static final String OAUTH_PROPERTY_CLIENT_ID = "oxauth.client.id";
	public static final String OAUTH_PROPERTY_CLIENT_PASSWORD = "oxauth.client.password";
	public static final String OAUTH_PROPERTY_CLIENT_SCOPE = "oxauth.client.scope";

	/**
	 * Configuration files
	 */
	public static final String LDAP_PROPERTIES_FILE = DIR + "oxTrustLdap.properties";
	public static final String CONFIGURATION_FILE_APPLICATION_CONFIGURATION = "oxTrust.properties";
	private static final String CONFIGURATION_FILE_CRYPTO_PROPERTIES_FILE = "salt";
	

	private static class ConfigurationSingleton {
		static Configuration INSTANCE = new Configuration();
	}

	private FileConfiguration applicationConfiguration;

	private FileConfiguration cryptoConfiguration;

	private Configuration() {
		FileConfiguration ldapConfiguration = new FileConfiguration(LDAP_PROPERTIES_FILE);

	    String confDir = ldapConfiguration.getString("confDir");
	    if (StringUtils.isBlank(confDir)) {
	    	confDir = DIR;
	    }

		applicationConfiguration = new FileConfiguration(confDir + CONFIGURATION_FILE_APPLICATION_CONFIGURATION);
	    cryptoConfiguration = new FileConfiguration(confDir + CONFIGURATION_FILE_CRYPTO_PROPERTIES_FILE);
	}

	public static Configuration instance() {
		return ConfigurationSingleton.INSTANCE;
	}

	public String getPropertyValue(String propertyName) {
		return applicationConfiguration.getString(propertyName);
	}

	public String getCryptoPropertyValue(String propertyName) {
		return cryptoConfiguration.getString(propertyName);
	}
}

@@ -3672,20 +3672,17 @@
 ILE);%0A%0A%09
-    
+%09
 String c
@@ -3690,16 +3690,27 @@
 nfDir = 
+DIR;%0A%09%09if (
 ldapConf
@@ -3723,97 +3723,77 @@
 ion.
-getString(%22confDir%22);%0A%09    if (StringUtils.isBlank(confDir)) %7B%0A%09    %09confDir = DIR;%0A%09    
+isLoaded()) %7B%0A%09%09%09confDir = ldapConfiguration.getString(%22confDir%22);%0A%09%09
 %7D%0A%0A%09

Fix scenario when there is no oxTrustLdap.properties file