From cb60d030f49ec48fb2021480919cb364d5ff442d Mon Sep 17 00:00:00 2001
From: rsanchez <rsanchez@curisit.net>
Date: Fri, 12 Dec 2014 11:34:08 +0000
Subject: [PATCH] #2140 feature - fix - Added load of config file from ENV variable and changes on Http client connections

---
 src/main/java/net/curisit/securis/ConnectionManager.java |   20 +-
 src/main/java/net/curisit/securis/utils/Params.java      |  331 +++++++++++++++++++++++++++++--------------------------
 src/main/java/net/curisit/securis/LicenseManager.java    |    5 
 3 files changed, 186 insertions(+), 170 deletions(-)

diff --git a/src/main/java/net/curisit/securis/ConnectionManager.java b/src/main/java/net/curisit/securis/ConnectionManager.java
index c8140f1..c46ebe9 100644
--- a/src/main/java/net/curisit/securis/ConnectionManager.java
+++ b/src/main/java/net/curisit/securis/ConnectionManager.java
@@ -19,7 +19,6 @@
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
 import org.apache.http.entity.StringEntity;
-import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClientBuilder;
 import org.apache.http.ssl.SSLContextBuilder;
 import org.apache.http.ssl.TrustStrategy;
@@ -42,7 +41,7 @@
 	private static ConnectionManager singleton;
 
 	private final String serverUrl;
-	private final CloseableHttpClient httpClient;
+	private final HttpClientBuilder httpClientBuilder;
 
 	private ConnectionManager() throws SeCurisException {
 		String aux = Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL);
@@ -51,10 +50,10 @@
 		} else {
 			serverUrl = aux;
 		}
-		httpClient = createHttpClient();
+		httpClientBuilder = createHttpClientBuilder();
 	}
 
-	private CloseableHttpClient createHttpClient() throws SeCurisException {
+	private HttpClientBuilder createHttpClientBuilder() throws SeCurisException {
 		SSLContextBuilder builder = new SSLContextBuilder();
 		SSLConnectionSocketFactory sslsf = null;
 		try {
@@ -69,7 +68,7 @@
 			LOG.error(e1);
 			throw new SeCurisException("Error creating SSL socket factory");
 		}
-		return HttpClientBuilder.create().setSSLSocketFactory(sslsf).build();
+		return HttpClientBuilder.create().setSSLSocketFactory(sslsf);
 	}
 
 	public synchronized static ConnectionManager getInstance() throws SeCurisException {
@@ -91,7 +90,7 @@
 		}
 		HttpResponse response;
 		try {
-			response = httpClient.execute(postRequest);
+			response = httpClientBuilder.build().execute(postRequest);
 
 			checkErrors(command, response);
 
@@ -103,8 +102,8 @@
 
 			return responseBean;
 		} catch (IOException e) {
-			LOG.error("Error accessing SeCuris server", e);
-			throw new SeCurisException("Error accessing SeCuris server", e);
+			LOG.error("Error accessing SeCuris server with command: " + command, e);
+			throw new SeCurisException("Error accessing SeCuris server with command: " + command, e);
 		}
 	}
 
@@ -116,7 +115,8 @@
 				throw new SeCurisException(String.format("[%s] - %s", errorCode, errorMsg));
 			}
 			LOG.error("Unexpected error executing {}, Reason: {}", command, response.getStatusLine().getReasonPhrase());
-			throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
+			throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode(),
+					new IOException("Unexpected server error"));
 		}
 
 	}
@@ -127,7 +127,7 @@
 
 		HttpResponse response;
 		try {
-			response = httpClient.execute(getRequest);
+			response = httpClientBuilder.build().execute(getRequest);
 			if (response.getStatusLine().getStatusCode() != 200) {
 				throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
 			}
diff --git a/src/main/java/net/curisit/securis/LicenseManager.java b/src/main/java/net/curisit/securis/LicenseManager.java
index 350cd38..540119b 100644
--- a/src/main/java/net/curisit/securis/LicenseManager.java
+++ b/src/main/java/net/curisit/securis/LicenseManager.java
@@ -210,9 +210,8 @@
 	 */
 	public void assertLicenseIsValid(File licenseFile) throws SeCurisException, IOException {
 		LicenseBean lic = validateLicense(licenseFile);
-
-		ConnectionManager.getInstance().executePost(Command.VALIDATE, LicenseBean.class, lic);
-
+		// We need to snd the signed version to validate signature on server
+		ConnectionManager.getInstance().executePost(Command.VALIDATE, LicenseBean.class, new SignedLicenseBean(lic));
 	}
 
 	public void testServer() throws SeCurisException {
diff --git a/src/main/java/net/curisit/securis/utils/Params.java b/src/main/java/net/curisit/securis/utils/Params.java
index 622335b..af21626 100644
--- a/src/main/java/net/curisit/securis/utils/Params.java
+++ b/src/main/java/net/curisit/securis/utils/Params.java
@@ -1,5 +1,7 @@
 package net.curisit.securis.utils;
 
+import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.text.MessageFormat;
@@ -17,193 +19,208 @@
  */
 public class Params {
 
-    private static final Logger LOG = LogManager.getLogger(Params.class);
+	private static final Logger LOG = LogManager.getLogger(Params.class);
 
-    /**
-     * Key used to store config file resource location. In a web application,
-     * can be set as initial parameter in a servlet loaded on startup
-     */
-    public static final String DEFAUT_SERVER_URL = "https://securis.curistec.com/api";
-    public static final String KEY_CONFIG_FILE = "/securis-client.properties";
+	/**
+	 * Key used to store config file resource location. In a web application,
+	 * can be set as initial parameter in a servlet loaded on startup
+	 */
+	public static final String DEFAUT_SERVER_URL = "https://securis.curistec.com/api";
+	public static final String CONFIG_FILE_RESOURCE = "/securis-client.properties";
+	public static final String ENV_PARAM_CONFIGFILE = "SECURIS_CLIENT_CONFIG_FILE";
 
-    private static Properties params = null;
+	private static Properties params = null;
 
-    static {
-        try {
-            loadParameters(KEY_CONFIG_FILE);
-        } catch (IOException e) {
-            LOG.error("Config file {} was not found in classpath", KEY_CONFIG_FILE);
-            System.exit(-2);
-        }
-    }
+	static {
+		try {
+			loadParameters();
+		} catch (IOException e) {
+			LOG.error("Config file {} was not found in classpath", CONFIG_FILE_RESOURCE);
+			System.exit(-2);
+		}
+	}
 
-    /**
-     * Loads application global parameters from a classpath resource
-     * 
-     * @param resource
-     *            : Resource location in classpath, i.e:
-     *            "/resource/cp-securis.conf"
-     * @throws IOException
-     */
-    public static void loadParameters(String resource) throws IOException {
+	/**
+	 * Loads application global parameters from a classpath resource
+	 * 
+	 * @param resource
+	 *            : Resource location in classpath, i.e:
+	 *            "/resource/cp-securis.conf"
+	 * @throws IOException
+	 */
+	public static void loadParameters() throws IOException {
 
-        LOG.debug("Loading params from " + resource);
-        InputStream fileis = Params.class.getResourceAsStream(resource);
+		String configPath = System.getenv(ENV_PARAM_CONFIGFILE);
+		if (configPath == null) {
+			configPath = System.getProperty(ENV_PARAM_CONFIGFILE);
+		}
+		InputStream fileis = null;
+		if (configPath != null) {
+			File configFile = new File(configPath);
+			if (configFile.exists()) {
+				LOG.info("Securis client config file read from: {}", configFile.getAbsolutePath());
+				fileis = new FileInputStream(configFile);
+			}
+		}
+		if (fileis == null) {
+			LOG.info("Securis client config file read from resource: {}", CONFIG_FILE_RESOURCE);
+			fileis = Params.class.getResourceAsStream(CONFIG_FILE_RESOURCE);
+		}
 
-        params = new Properties();
-        try {
+		params = new Properties();
+		try {
 
-            params.load(fileis);
-            LOG.debug("Params loaded OK from {}", resource);
-        } catch (IOException e) {
-            LOG.error("Error loading config file: " + e);
-            params = null;
-            throw e;
-        }
+			params.load(fileis);
+			LOG.debug("Params loaded OK");
+		} catch (IOException e) {
+			LOG.error("Error loading config file", e);
+			params = null;
+			throw e;
+		}
 
-    }
+	}
 
-    public static String getByDomain(String domain, String paramname) {
-        return getByDomain(domain, paramname, null);
-    }
+	public static String getByDomain(String domain, String paramname) {
+		return getByDomain(domain, paramname, null);
+	}
 
-    public static String getByPrefix(String prefix, String paramname) {
-        return get(prefix + "." + paramname, get(paramname));
-    }
+	public static String getByPrefix(String prefix, String paramname) {
+		return get(prefix + "." + paramname, get(paramname));
+	}
 
-    public static String getByPrefix(String prefix, String paramname, String defaultVal) {
-        return get(prefix + "." + paramname, get(paramname, defaultVal));
-    }
+	public static String getByPrefix(String prefix, String paramname, String defaultVal) {
+		return get(prefix + "." + paramname, get(paramname, defaultVal));
+	}
 
-    public static String getByDomain(String domain, String paramname, String defaultval) {
-        return get(paramname + "." + domain, defaultval);
-    }
+	public static String getByDomain(String domain, String paramname, String defaultval) {
+		return get(paramname + "." + domain, defaultval);
+	}
 
-    public static int getIntByDomain(String domain, String paramname) {
-        return getInt(paramname + "." + domain, getInt(paramname));
-    }
+	public static int getIntByDomain(String domain, String paramname) {
+		return getInt(paramname + "." + domain, getInt(paramname));
+	}
 
-    public static int getIntByDomain(String domain, String paramname, int defaultval) {
-        return getInt(paramname + "." + domain, defaultval);
-    }
+	public static int getIntByDomain(String domain, String paramname, int defaultval) {
+		return getInt(paramname + "." + domain, defaultval);
+	}
 
-    /**
-     * Gets a List with all values of properties that begins with
-     * <code>prefix</code> It reads sequentially. For example:
-     * 
-     * <pre>
-     * 	securis.sort.comparator.0: net.cp.securis.comparators.ComparePttidVsPtn
-     * 	securis.sort.comparator.1: net.cp.securis.comparators.CompareFrequency
-     * 	securis.sort.comparator.2: net.cp.securis.comparators.CompareOutgoingVsIncomming
-     * 	securis.sort.comparator.3: net.cp.securis.comparators.CompareDuration 
-     * 	securis.sort.comparator.4: net.cp.securis.comparators.CompareCallVsSms
-     * </pre>
-     * 
-     * That config (for prefix: "securis.sort.comparator" ) will return a
-     * List<String> with values:
-     * 
-     * <pre>
-     * 	"net.cp.securis.comparators.ComparePttidVsPtn", 
-     * 	"net.cp.securis.comparators.CompareFrequency", 
-     * 	"net.cp.securis.comparators.CompareOutgoingVsIncomming", 
-     * 	"net.cp.securis.comparators.CompareDuration", 
-     * 	"net.cp.securis.comparators.CompareCallVsSms"
-     * </pre>
-     * 
-     * Note: If there is a gap between suffixes process will stop, that is, only
-     * will be returned properties found before gap.
-     * 
-     * @param prefix
-     * @return
-     */
-    public static List<String> getListByPrefix(String prefix) {
-        List<String> list = new ArrayList<String>();
+	/**
+	 * Gets a List with all values of properties that begins with
+	 * <code>prefix</code> It reads sequentially. For example:
+	 * 
+	 * <pre>
+	 * 	securis.sort.comparator.0: net.cp.securis.comparators.ComparePttidVsPtn
+	 * 	securis.sort.comparator.1: net.cp.securis.comparators.CompareFrequency
+	 * 	securis.sort.comparator.2: net.cp.securis.comparators.CompareOutgoingVsIncomming
+	 * 	securis.sort.comparator.3: net.cp.securis.comparators.CompareDuration 
+	 * 	securis.sort.comparator.4: net.cp.securis.comparators.CompareCallVsSms
+	 * </pre>
+	 * 
+	 * That config (for prefix: "securis.sort.comparator" ) will return a
+	 * List<String> with values:
+	 * 
+	 * <pre>
+	 * 	"net.cp.securis.comparators.ComparePttidVsPtn", 
+	 * 	"net.cp.securis.comparators.CompareFrequency", 
+	 * 	"net.cp.securis.comparators.CompareOutgoingVsIncomming", 
+	 * 	"net.cp.securis.comparators.CompareDuration", 
+	 * 	"net.cp.securis.comparators.CompareCallVsSms"
+	 * </pre>
+	 * 
+	 * Note: If there is a gap between suffixes process will stop, that is, only
+	 * will be returned properties found before gap.
+	 * 
+	 * @param prefix
+	 * @return
+	 */
+	public static List<String> getListByPrefix(String prefix) {
+		List<String> list = new ArrayList<String>();
 
-        String tpl = prefix + ".{0}";
+		String tpl = prefix + ".{0}";
 
-        int i = 0;
-        String value = get(MessageFormat.format(tpl, i++));
-        while (value != null) {
-            list.add(value);
-            value = get(MessageFormat.format(tpl, i++));
-        }
+		int i = 0;
+		String value = get(MessageFormat.format(tpl, i++));
+		while (value != null) {
+			list.add(value);
+			value = get(MessageFormat.format(tpl, i++));
+		}
 
-        return list;
-    }
+		return list;
+	}
 
-    /**
-     * Gets param value in config file or environment variables
-     * 
-     * @param paramname
-     *            Global parameter's name
-     * @return Value of paramname or null if paramname is not found neither in
-     *         config file nor in environment variables
-     */
-    public static String get(String paramname) {
+	/**
+	 * Gets param value in config file or environment variables
+	 * 
+	 * @param paramname
+	 *            Global parameter's name
+	 * @return Value of paramname or null if paramname is not found neither in
+	 *         config file nor in environment variables
+	 */
+	public static String get(String paramname) {
 
-        assert (params != null) : "Parameters have not been loaded. Call method loadParameters(resource) before use Params.";
+		assert (params != null) : "Parameters have not been loaded. Call method loadParameters(resource) before use Params.";
 
-        String value = params.getProperty(paramname);
-        if (value == null)
-            value = System.getenv(paramname);
-        return value;
-    }
+		String value = params.getProperty(paramname);
+		if (value == null)
+			value = System.getenv(paramname);
+		return value;
+	}
 
-    /**
-     * Gets param value from config file or environment variables
-     * 
-     * @param paramname
-     *            Global parameter's name
-     * @param defaultval
-     * @return Value of paramname or defaultval if paramname is not found
-     */
-    public static String get(String paramname, String defaultval) {
-        String value = get(paramname);
-        return (value == null ? defaultval : value);
-    }
+	/**
+	 * Gets param value from config file or environment variables
+	 * 
+	 * @param paramname
+	 *            Global parameter's name
+	 * @param defaultval
+	 * @return Value of paramname or defaultval if paramname is not found
+	 */
+	public static String get(String paramname, String defaultval) {
+		String value = get(paramname);
+		return (value == null ? defaultval : value);
+	}
 
-    /**
-     * Gets param value in config file or environment variables
-     * 
-     * @param paramname
-     *            Global parameter's name
-     * @return Integer value of paramname or -1 if paramname is not found
-     *         neither in config file nor in environment variables
-     */
-    public static int getInt(String paramname) {
-        String value = get(paramname);
-        return (value == null ? -1 : Integer.parseInt(value));
-    }
+	/**
+	 * Gets param value in config file or environment variables
+	 * 
+	 * @param paramname
+	 *            Global parameter's name
+	 * @return Integer value of paramname or -1 if paramname is not found
+	 *         neither in config file nor in environment variables
+	 */
+	public static int getInt(String paramname) {
+		String value = get(paramname);
+		return (value == null ? -1 : Integer.parseInt(value));
+	}
 
-    /**
-     * Gets param value from config file or environment variables
-     * 
-     * @param paramname
-     *            Global parameter's name
-     * @param defaultval
-     * @return Integer value of paramname or defaultval if paramname is not
-     *         found
-     */
-    public static int getInt(String paramname, int defaultval) {
-        String value = get(paramname);
-        return (value == null ? defaultval : Integer.parseInt(value));
-    }
+	/**
+	 * Gets param value from config file or environment variables
+	 * 
+	 * @param paramname
+	 *            Global parameter's name
+	 * @param defaultval
+	 * @return Integer value of paramname or defaultval if paramname is not
+	 *         found
+	 */
+	public static int getInt(String paramname, int defaultval) {
+		String value = get(paramname);
+		return (value == null ? defaultval : Integer.parseInt(value));
+	}
 
-    public static class KEYS {
+	public static class KEYS {
 
-        /**
-         * Public key file, Usually in "PEM" format
-         */
-        public static final String PUBLIC_KEY_FILE = "public.key.file";
+		/**
+		 * Public key file, Usually in "PEM" format
+		 */
+		public static final String PUBLIC_KEY_FILE = "public.key.file";
 
-        public static final String APPLICATION_CODE = "app.code";
+		public static final String APPLICATION_CODE = "app.code";
 
-        public static final String CUSTOMER_CODE = "customer.code";
+		public static final String CUSTOMER_CODE = "customer.code";
 
-        public static final String PACK_CODE = "pack.code";
+		public static final String PACK_CODE = "pack.code";
 
-        public static final String LICENSE_SERVER_URL = "license.server.url";
+		public static final String LICENSE_SERVER_URL = "license.server.url";
 
-    }
+	}
 
 }

--
Gitblit v1.3.2