rsanchez
2014-12-12 cb60d030f49ec48fb2021480919cb364d5ff442d
#2140 feature - fix - Added load of config file from ENV variable and
changes on Http client connections
3 files modified
changed files
src/main/java/net/curisit/securis/ConnectionManager.java patch | view | blame | history
src/main/java/net/curisit/securis/LicenseManager.java patch | view | blame | history
src/main/java/net/curisit/securis/utils/Params.java patch | view | blame | history
src/main/java/net/curisit/securis/ConnectionManager.java
....@@ -19,7 +19,6 @@
1919 import org.apache.http.client.methods.HttpPost;
2020 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
2121 import org.apache.http.entity.StringEntity;
22
-import org.apache.http.impl.client.CloseableHttpClient;
2322 import org.apache.http.impl.client.HttpClientBuilder;
2423 import org.apache.http.ssl.SSLContextBuilder;
2524 import org.apache.http.ssl.TrustStrategy;
....@@ -42,7 +41,7 @@
4241 private static ConnectionManager singleton;
4342
4443 private final String serverUrl;
45
- private final CloseableHttpClient httpClient;
44
+ private final HttpClientBuilder httpClientBuilder;
4645
4746 private ConnectionManager() throws SeCurisException {
4847 String aux = Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL);
....@@ -51,10 +50,10 @@
5150 } else {
5251 serverUrl = aux;
5352 }
54
- httpClient = createHttpClient();
53
+ httpClientBuilder = createHttpClientBuilder();
5554 }
5655
57
- private CloseableHttpClient createHttpClient() throws SeCurisException {
56
+ private HttpClientBuilder createHttpClientBuilder() throws SeCurisException {
5857 SSLContextBuilder builder = new SSLContextBuilder();
5958 SSLConnectionSocketFactory sslsf = null;
6059 try {
....@@ -69,7 +68,7 @@
6968 LOG.error(e1);
7069 throw new SeCurisException("Error creating SSL socket factory");
7170 }
72
- return HttpClientBuilder.create().setSSLSocketFactory(sslsf).build();
71
+ return HttpClientBuilder.create().setSSLSocketFactory(sslsf);
7372 }
7473
7574 public synchronized static ConnectionManager getInstance() throws SeCurisException {
....@@ -91,7 +90,7 @@
9190 }
9291 HttpResponse response;
9392 try {
94
- response = httpClient.execute(postRequest);
93
+ response = httpClientBuilder.build().execute(postRequest);
9594
9695 checkErrors(command, response);
9796
....@@ -103,8 +102,8 @@
103102
104103 return responseBean;
105104 } catch (IOException e) {
106
- LOG.error("Error accessing SeCuris server", e);
107
- throw new SeCurisException("Error accessing SeCuris server", e);
105
+ LOG.error("Error accessing SeCuris server with command: " + command, e);
106
+ throw new SeCurisException("Error accessing SeCuris server with command: " + command, e);
108107 }
109108 }
110109
....@@ -116,7 +115,8 @@
116115 throw new SeCurisException(String.format("[%s] - %s", errorCode, errorMsg));
117116 }
118117 LOG.error("Unexpected error executing {}, Reason: {}", command, response.getStatusLine().getReasonPhrase());
119
- throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
118
+ throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode(),
119
+ new IOException("Unexpected server error"));
120120 }
121121
122122 }
....@@ -127,7 +127,7 @@
127127
128128 HttpResponse response;
129129 try {
130
- response = httpClient.execute(getRequest);
130
+ response = httpClientBuilder.build().execute(getRequest);
131131 if (response.getStatusLine().getStatusCode() != 200) {
132132 throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
133133 }
src/main/java/net/curisit/securis/LicenseManager.java
....@@ -210,9 +210,8 @@
210210 */
211211 public void assertLicenseIsValid(File licenseFile) throws SeCurisException, IOException {
212212 LicenseBean lic = validateLicense(licenseFile);
213
-
214
- ConnectionManager.getInstance().executePost(Command.VALIDATE, LicenseBean.class, lic);
215
-
213
+ // We need to snd the signed version to validate signature on server
214
+ ConnectionManager.getInstance().executePost(Command.VALIDATE, LicenseBean.class, new SignedLicenseBean(lic));
216215 }
217216
218217 public void testServer() throws SeCurisException {
src/main/java/net/curisit/securis/utils/Params.java
....@@ -1,5 +1,7 @@
11 package net.curisit.securis.utils;
22
3
+import java.io.File;
4
+import java.io.FileInputStream;
35 import java.io.IOException;
46 import java.io.InputStream;
57 import java.text.MessageFormat;
....@@ -17,193 +19,208 @@
1719 */
1820 public class Params {
1921
20
- private static final Logger LOG = LogManager.getLogger(Params.class);
22
+ private static final Logger LOG = LogManager.getLogger(Params.class);
2123
22
- /**
23
- * Key used to store config file resource location. In a web application,
24
- * can be set as initial parameter in a servlet loaded on startup
25
- */
26
- public static final String DEFAUT_SERVER_URL = "https://securis.curistec.com/api";
27
- public static final String KEY_CONFIG_FILE = "/securis-client.properties";
24
+ /**
25
+ * Key used to store config file resource location. In a web application,
26
+ * can be set as initial parameter in a servlet loaded on startup
27
+ */
28
+ public static final String DEFAUT_SERVER_URL = "https://securis.curistec.com/api";
29
+ public static final String CONFIG_FILE_RESOURCE = "/securis-client.properties";
30
+ public static final String ENV_PARAM_CONFIGFILE = "SECURIS_CLIENT_CONFIG_FILE";
2831
29
- private static Properties params = null;
32
+ private static Properties params = null;
3033
31
- static {
32
- try {
33
- loadParameters(KEY_CONFIG_FILE);
34
- } catch (IOException e) {
35
- LOG.error("Config file {} was not found in classpath", KEY_CONFIG_FILE);
36
- System.exit(-2);
37
- }
38
- }
34
+ static {
35
+ try {
36
+ loadParameters();
37
+ } catch (IOException e) {
38
+ LOG.error("Config file {} was not found in classpath", CONFIG_FILE_RESOURCE);
39
+ System.exit(-2);
40
+ }
41
+ }
3942
40
- /**
41
- * Loads application global parameters from a classpath resource
42
- *
43
- * @param resource
44
- * : Resource location in classpath, i.e:
45
- * "/resource/cp-securis.conf"
46
- * @throws IOException
47
- */
48
- public static void loadParameters(String resource) throws IOException {
43
+ /**
44
+ * Loads application global parameters from a classpath resource
45
+ *
46
+ * @param resource
47
+ * : Resource location in classpath, i.e:
48
+ * "/resource/cp-securis.conf"
49
+ * @throws IOException
50
+ */
51
+ public static void loadParameters() throws IOException {
4952
50
- LOG.debug("Loading params from " + resource);
51
- InputStream fileis = Params.class.getResourceAsStream(resource);
53
+ String configPath = System.getenv(ENV_PARAM_CONFIGFILE);
54
+ if (configPath == null) {
55
+ configPath = System.getProperty(ENV_PARAM_CONFIGFILE);
56
+ }
57
+ InputStream fileis = null;
58
+ if (configPath != null) {
59
+ File configFile = new File(configPath);
60
+ if (configFile.exists()) {
61
+ LOG.info("Securis client config file read from: {}", configFile.getAbsolutePath());
62
+ fileis = new FileInputStream(configFile);
63
+ }
64
+ }
65
+ if (fileis == null) {
66
+ LOG.info("Securis client config file read from resource: {}", CONFIG_FILE_RESOURCE);
67
+ fileis = Params.class.getResourceAsStream(CONFIG_FILE_RESOURCE);
68
+ }
5269
53
- params = new Properties();
54
- try {
70
+ params = new Properties();
71
+ try {
5572
56
- params.load(fileis);
57
- LOG.debug("Params loaded OK from {}", resource);
58
- } catch (IOException e) {
59
- LOG.error("Error loading config file: " + e);
60
- params = null;
61
- throw e;
62
- }
73
+ params.load(fileis);
74
+ LOG.debug("Params loaded OK");
75
+ } catch (IOException e) {
76
+ LOG.error("Error loading config file", e);
77
+ params = null;
78
+ throw e;
79
+ }
6380
64
- }
81
+ }
6582
66
- public static String getByDomain(String domain, String paramname) {
67
- return getByDomain(domain, paramname, null);
68
- }
83
+ public static String getByDomain(String domain, String paramname) {
84
+ return getByDomain(domain, paramname, null);
85
+ }
6986
70
- public static String getByPrefix(String prefix, String paramname) {
71
- return get(prefix + "." + paramname, get(paramname));
72
- }
87
+ public static String getByPrefix(String prefix, String paramname) {
88
+ return get(prefix + "." + paramname, get(paramname));
89
+ }
7390
74
- public static String getByPrefix(String prefix, String paramname, String defaultVal) {
75
- return get(prefix + "." + paramname, get(paramname, defaultVal));
76
- }
91
+ public static String getByPrefix(String prefix, String paramname, String defaultVal) {
92
+ return get(prefix + "." + paramname, get(paramname, defaultVal));
93
+ }
7794
78
- public static String getByDomain(String domain, String paramname, String defaultval) {
79
- return get(paramname + "." + domain, defaultval);
80
- }
95
+ public static String getByDomain(String domain, String paramname, String defaultval) {
96
+ return get(paramname + "." + domain, defaultval);
97
+ }
8198
82
- public static int getIntByDomain(String domain, String paramname) {
83
- return getInt(paramname + "." + domain, getInt(paramname));
84
- }
99
+ public static int getIntByDomain(String domain, String paramname) {
100
+ return getInt(paramname + "." + domain, getInt(paramname));
101
+ }
85102
86
- public static int getIntByDomain(String domain, String paramname, int defaultval) {
87
- return getInt(paramname + "." + domain, defaultval);
88
- }
103
+ public static int getIntByDomain(String domain, String paramname, int defaultval) {
104
+ return getInt(paramname + "." + domain, defaultval);
105
+ }
89106
90
- /**
91
- * Gets a List with all values of properties that begins with
92
- * <code>prefix</code> It reads sequentially. For example:
93
- *
94
- * <pre>
95
- * securis.sort.comparator.0: net.cp.securis.comparators.ComparePttidVsPtn
96
- * securis.sort.comparator.1: net.cp.securis.comparators.CompareFrequency
97
- * securis.sort.comparator.2: net.cp.securis.comparators.CompareOutgoingVsIncomming
98
- * securis.sort.comparator.3: net.cp.securis.comparators.CompareDuration
99
- * securis.sort.comparator.4: net.cp.securis.comparators.CompareCallVsSms
100
- * </pre>
101
- *
102
- * That config (for prefix: "securis.sort.comparator" ) will return a
103
- * List<String> with values:
104
- *
105
- * <pre>
106
- * "net.cp.securis.comparators.ComparePttidVsPtn",
107
- * "net.cp.securis.comparators.CompareFrequency",
108
- * "net.cp.securis.comparators.CompareOutgoingVsIncomming",
109
- * "net.cp.securis.comparators.CompareDuration",
110
- * "net.cp.securis.comparators.CompareCallVsSms"
111
- * </pre>
112
- *
113
- * Note: If there is a gap between suffixes process will stop, that is, only
114
- * will be returned properties found before gap.
115
- *
116
- * @param prefix
117
- * @return
118
- */
119
- public static List<String> getListByPrefix(String prefix) {
120
- List<String> list = new ArrayList<String>();
107
+ /**
108
+ * Gets a List with all values of properties that begins with
109
+ * <code>prefix</code> It reads sequentially. For example:
110
+ *
111
+ * <pre>
112
+ * securis.sort.comparator.0: net.cp.securis.comparators.ComparePttidVsPtn
113
+ * securis.sort.comparator.1: net.cp.securis.comparators.CompareFrequency
114
+ * securis.sort.comparator.2: net.cp.securis.comparators.CompareOutgoingVsIncomming
115
+ * securis.sort.comparator.3: net.cp.securis.comparators.CompareDuration
116
+ * securis.sort.comparator.4: net.cp.securis.comparators.CompareCallVsSms
117
+ * </pre>
118
+ *
119
+ * That config (for prefix: "securis.sort.comparator" ) will return a
120
+ * List<String> with values:
121
+ *
122
+ * <pre>
123
+ * "net.cp.securis.comparators.ComparePttidVsPtn",
124
+ * "net.cp.securis.comparators.CompareFrequency",
125
+ * "net.cp.securis.comparators.CompareOutgoingVsIncomming",
126
+ * "net.cp.securis.comparators.CompareDuration",
127
+ * "net.cp.securis.comparators.CompareCallVsSms"
128
+ * </pre>
129
+ *
130
+ * Note: If there is a gap between suffixes process will stop, that is, only
131
+ * will be returned properties found before gap.
132
+ *
133
+ * @param prefix
134
+ * @return
135
+ */
136
+ public static List<String> getListByPrefix(String prefix) {
137
+ List<String> list = new ArrayList<String>();
121138
122
- String tpl = prefix + ".{0}";
139
+ String tpl = prefix + ".{0}";
123140
124
- int i = 0;
125
- String value = get(MessageFormat.format(tpl, i++));
126
- while (value != null) {
127
- list.add(value);
128
- value = get(MessageFormat.format(tpl, i++));
129
- }
141
+ int i = 0;
142
+ String value = get(MessageFormat.format(tpl, i++));
143
+ while (value != null) {
144
+ list.add(value);
145
+ value = get(MessageFormat.format(tpl, i++));
146
+ }
130147
131
- return list;
132
- }
148
+ return list;
149
+ }
133150
134
- /**
135
- * Gets param value in config file or environment variables
136
- *
137
- * @param paramname
138
- * Global parameter's name
139
- * @return Value of paramname or null if paramname is not found neither in
140
- * config file nor in environment variables
141
- */
142
- public static String get(String paramname) {
151
+ /**
152
+ * Gets param value in config file or environment variables
153
+ *
154
+ * @param paramname
155
+ * Global parameter's name
156
+ * @return Value of paramname or null if paramname is not found neither in
157
+ * config file nor in environment variables
158
+ */
159
+ public static String get(String paramname) {
143160
144
- assert (params != null) : "Parameters have not been loaded. Call method loadParameters(resource) before use Params.";
161
+ assert (params != null) : "Parameters have not been loaded. Call method loadParameters(resource) before use Params.";
145162
146
- String value = params.getProperty(paramname);
147
- if (value == null)
148
- value = System.getenv(paramname);
149
- return value;
150
- }
163
+ String value = params.getProperty(paramname);
164
+ if (value == null)
165
+ value = System.getenv(paramname);
166
+ return value;
167
+ }
151168
152
- /**
153
- * Gets param value from config file or environment variables
154
- *
155
- * @param paramname
156
- * Global parameter's name
157
- * @param defaultval
158
- * @return Value of paramname or defaultval if paramname is not found
159
- */
160
- public static String get(String paramname, String defaultval) {
161
- String value = get(paramname);
162
- return (value == null ? defaultval : value);
163
- }
169
+ /**
170
+ * Gets param value from config file or environment variables
171
+ *
172
+ * @param paramname
173
+ * Global parameter's name
174
+ * @param defaultval
175
+ * @return Value of paramname or defaultval if paramname is not found
176
+ */
177
+ public static String get(String paramname, String defaultval) {
178
+ String value = get(paramname);
179
+ return (value == null ? defaultval : value);
180
+ }
164181
165
- /**
166
- * Gets param value in config file or environment variables
167
- *
168
- * @param paramname
169
- * Global parameter's name
170
- * @return Integer value of paramname or -1 if paramname is not found
171
- * neither in config file nor in environment variables
172
- */
173
- public static int getInt(String paramname) {
174
- String value = get(paramname);
175
- return (value == null ? -1 : Integer.parseInt(value));
176
- }
182
+ /**
183
+ * Gets param value in config file or environment variables
184
+ *
185
+ * @param paramname
186
+ * Global parameter's name
187
+ * @return Integer value of paramname or -1 if paramname is not found
188
+ * neither in config file nor in environment variables
189
+ */
190
+ public static int getInt(String paramname) {
191
+ String value = get(paramname);
192
+ return (value == null ? -1 : Integer.parseInt(value));
193
+ }
177194
178
- /**
179
- * Gets param value from config file or environment variables
180
- *
181
- * @param paramname
182
- * Global parameter's name
183
- * @param defaultval
184
- * @return Integer value of paramname or defaultval if paramname is not
185
- * found
186
- */
187
- public static int getInt(String paramname, int defaultval) {
188
- String value = get(paramname);
189
- return (value == null ? defaultval : Integer.parseInt(value));
190
- }
195
+ /**
196
+ * Gets param value from config file or environment variables
197
+ *
198
+ * @param paramname
199
+ * Global parameter's name
200
+ * @param defaultval
201
+ * @return Integer value of paramname or defaultval if paramname is not
202
+ * found
203
+ */
204
+ public static int getInt(String paramname, int defaultval) {
205
+ String value = get(paramname);
206
+ return (value == null ? defaultval : Integer.parseInt(value));
207
+ }
191208
192
- public static class KEYS {
209
+ public static class KEYS {
193210
194
- /**
195
- * Public key file, Usually in "PEM" format
196
- */
197
- public static final String PUBLIC_KEY_FILE = "public.key.file";
211
+ /**
212
+ * Public key file, Usually in "PEM" format
213
+ */
214
+ public static final String PUBLIC_KEY_FILE = "public.key.file";
198215
199
- public static final String APPLICATION_CODE = "app.code";
216
+ public static final String APPLICATION_CODE = "app.code";
200217
201
- public static final String CUSTOMER_CODE = "customer.code";
218
+ public static final String CUSTOMER_CODE = "customer.code";
202219
203
- public static final String PACK_CODE = "pack.code";
220
+ public static final String PACK_CODE = "pack.code";
204221
205
- public static final String LICENSE_SERVER_URL = "license.server.url";
222
+ public static final String LICENSE_SERVER_URL = "license.server.url";
206223
207
- }
224
+ }
208225
209226 }