rsanchez
2014-09-29 4be65eae04c0ed497bf404d43f9a11a4993a239d
#2021 fix - Added HTTP client to connect to SeCuris server
1 files added
6 files modified
changed files
pom.xml patch | view | blame | history
src/main/java/net/curisit/securis/ConnectionManager.java patch | view | blame | history
src/main/java/net/curisit/securis/License.java patch | view | blame | history
src/main/java/net/curisit/securis/LicenseManager.java patch | view | blame | history
src/main/java/net/curisit/securis/beans/SignedLicenseBean.java patch | view | blame | history
src/main/java/net/curisit/securis/utils/SignatureHelper.java patch | view | blame | history
src/main/resources/securis-client.properties patch | view | blame | history
pom.xml
....@@ -3,7 +3,7 @@
33 <modelVersion>4.0.0</modelVersion>
44 <groupId>net.curisit</groupId>
55 <artifactId>securis-client</artifactId>
6
- <version>0.9.6-SNAPSHOT</version>
6
+ <version>0.9.7-SNAPSHOT</version>
77 <build>
88 <plugins>
99 <plugin>
....@@ -47,11 +47,6 @@
4747 <version>1.2</version>
4848 </dependency>
4949 <dependency>
50
- <groupId>commons-net</groupId>
51
- <artifactId>commons-net</artifactId>
52
- <version>3.3</version>
53
- </dependency>
54
- <dependency>
5550 <groupId>org.codehaus.jackson</groupId>
5651 <artifactId>jackson-mapper-asl</artifactId>
5752 <version>1.9.13</version>
....@@ -61,6 +56,11 @@
6156 <artifactId>log4j-core</artifactId>
6257 <version>2.0.2</version>
6358 </dependency>
59
+ <dependency>
60
+ <groupId>org.apache.httpcomponents</groupId>
61
+ <artifactId>httpclient</artifactId>
62
+ <version>4.4-beta1</version>
63
+ </dependency>
6464 </dependencies>
6565
6666 <distributionManagement>
src/main/java/net/curisit/securis/ConnectionManager.java
....@@ -0,0 +1,140 @@
1
+package net.curisit.securis;
2
+
3
+import java.io.IOException;
4
+import java.io.UnsupportedEncodingException;
5
+import java.security.KeyManagementException;
6
+import java.security.KeyStore;
7
+import java.security.KeyStoreException;
8
+import java.security.NoSuchAlgorithmException;
9
+import java.security.cert.CertificateException;
10
+import java.security.cert.X509Certificate;
11
+
12
+import net.curisit.securis.beans.RequestBean;
13
+import net.curisit.securis.utils.JsonUtils;
14
+import net.curisit.securis.utils.Params;
15
+
16
+import org.apache.commons.io.IOUtils;
17
+import org.apache.http.HttpResponse;
18
+import org.apache.http.client.methods.HttpGet;
19
+import org.apache.http.client.methods.HttpPost;
20
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
21
+import org.apache.http.entity.StringEntity;
22
+import org.apache.http.impl.client.CloseableHttpClient;
23
+import org.apache.http.impl.client.HttpClientBuilder;
24
+import org.apache.http.ssl.SSLContextBuilder;
25
+import org.apache.http.ssl.TrustStrategy;
26
+import org.apache.logging.log4j.LogManager;
27
+import org.apache.logging.log4j.Logger;
28
+
29
+/**
30
+ * Manage all server connections
31
+ *
32
+ * @author roberto <roberto.sanchez@curisit.net>
33
+ */
34
+public class ConnectionManager {
35
+
36
+ private static final Logger LOG = LogManager.getLogger(ConnectionManager.class);
37
+
38
+ private static ConnectionManager singleton;
39
+
40
+ private final String serverUrl;
41
+ private final CloseableHttpClient httpClient;
42
+
43
+ private ConnectionManager() throws SeCurisException {
44
+ String aux = Params.get(Params.KEYS.LICENSE_SERVER_URL, "https://securis.curistec.com/api");
45
+ if (aux.endsWith("/")) {
46
+ serverUrl = aux.substring(0, aux.length()-2);
47
+ } else {
48
+ serverUrl = aux;
49
+ }
50
+ httpClient = createHttpClient();
51
+ }
52
+
53
+ private CloseableHttpClient createHttpClient() throws SeCurisException {
54
+ SSLContextBuilder builder = new SSLContextBuilder();
55
+ SSLConnectionSocketFactory sslsf = null;
56
+ try {
57
+ builder.loadTrustMaterial((KeyStore)null, new TrustStrategy() {
58
+ @Override
59
+ public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
60
+ return true;
61
+ }
62
+ });
63
+ sslsf = new SSLConnectionSocketFactory(builder.build());
64
+ } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e1) {
65
+ LOG.error(e1);
66
+ throw new SeCurisException("Error creating SSL socket factory");
67
+ }
68
+ return HttpClientBuilder.create().setSSLSocketFactory(sslsf).build();
69
+ }
70
+
71
+ public synchronized static ConnectionManager getInstance() throws SeCurisException {
72
+ if (singleton == null) {
73
+ singleton = new ConnectionManager();
74
+ }
75
+ return singleton;
76
+ }
77
+
78
+
79
+ public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException {
80
+ HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command));
81
+ postRequest.addHeader("accept", "application/json");
82
+ postRequest.addHeader("content-type", "application/json");
83
+ try {
84
+ postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req)));
85
+ } catch (UnsupportedEncodingException | SeCurisException e1) {
86
+ throw new SeCurisException("Error preparing POST command", e1);
87
+ }
88
+ HttpResponse response;
89
+ try {
90
+ response = httpClient.execute(postRequest);
91
+ if (response.getStatusLine().getStatusCode() != 200) {
92
+ throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
93
+ }
94
+ String jsonLic = IOUtils.toString(response.getEntity().getContent());
95
+ LOG.info("License read OK: {}", jsonLic);
96
+ T responseBean = JsonUtils.json2object(jsonLic, returnType);
97
+
98
+ LOG.info("Response bean read OK: {}", responseBean);
99
+ LOG.info("JSON to write in file: {}", JsonUtils.toJSON(responseBean));
100
+
101
+ return responseBean;
102
+ } catch (IOException e) {
103
+ LOG.error("Error acessing SeCuris server", e);
104
+ throw new SeCurisException("Error accessing SeCuris server");
105
+ }
106
+ }
107
+
108
+
109
+ public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException {
110
+ HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command));
111
+ getRequest.addHeader("accept", "application/json");
112
+
113
+ HttpResponse response;
114
+ try {
115
+ response = httpClient.execute(getRequest);
116
+ if (response.getStatusLine().getStatusCode() != 200) {
117
+ throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
118
+ }
119
+ String jsonLic = IOUtils.toString(response.getEntity().getContent());
120
+ LOG.info("License read OK: {}", jsonLic);
121
+ T responseBean = JsonUtils.json2object(jsonLic, returnType);
122
+
123
+ LOG.info("Response bean read OK: {}", responseBean);
124
+ LOG.info("JSON to write in file: {}", JsonUtils.toJSON(responseBean));
125
+
126
+ return responseBean;
127
+ } catch (IOException e) {
128
+ LOG.error("Error acessing SeCuris server", e);
129
+ throw new SeCurisException("Error accessing SeCuris server");
130
+ }
131
+ }
132
+
133
+ public static class Command {
134
+ public static final String TEST = "ping";
135
+ public static final String CREATE_LIC = "request";
136
+ public static final String RENEW_LIC = "renew";
137
+ }
138
+
139
+
140
+}
src/main/java/net/curisit/securis/License.java
....@@ -33,8 +33,6 @@
3333 * file. --server parameter is mandatory.
3434 * -s,--server <url_license_server> License server url.
3535 * -t,--test_lc Test if License Server (LC) is
36
- * available. --server parameter is
37
- * mandatory.
3836 * </pre>
3937 *
4038 * @author roberto <roberto.sanchez@curisit.net>
....@@ -85,16 +83,13 @@
8583 }
8684
8785 if (cmd.hasOption('c')) {
88
- String reqFilename = cmd.getOptionValue("rfile");
89
- checkMandatoryParameter(reqFilename, "rfile");
90
-
91
- LOG.warn("This command is not yet implemented");
92
- System.exit(0);
86
+ LicenseManager.getInstance().requestLicense();
87
+ System.exit(0);
9388 }
9489
9590 if (cmd.hasOption('t')) {
96
- LOG.warn("This command is not yet implemented");
97
- System.exit(0);
91
+ LicenseManager.getInstance().testServer();
92
+ System.exit(0);
9893 }
9994
10095 if (cmd.hasOption('r')) {
....@@ -168,8 +163,8 @@
168163 options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("validate").withDescription("Validate lic file.").hasArg(true).create('l'));
169164
170165 options.addOption("g", "gen_request", false, "Generate request file. If --rfile parameter is missing then It is generated in current directory.");
171
- options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("create").withDescription("Request a license file from server.").hasArg(true).create('c'));
172
- options.addOption("t", "test_lc", false, "Test if License Server (LC) is available. --server parameter is mandatory.");
166
+ options.addOption(OptionBuilder.withLongOpt("create").withDescription("Request a license file to server.").hasArg(false).create('c'));
167
+ options.addOption("t", "test_lc", false, "Test if License Server (LC) is available. ");
173168 options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("renew").withDescription("Synchronize/renew the current license file.").hasArg(true).create('r'));
174169
175170 return options;
src/main/java/net/curisit/securis/LicenseManager.java
....@@ -7,6 +7,7 @@
77 import java.nio.file.Paths;
88 import java.nio.file.StandardOpenOption;
99
10
+import net.curisit.securis.ConnectionManager.Command;
1011 import net.curisit.securis.beans.LicenseBean;
1112 import net.curisit.securis.beans.RequestBean;
1213 import net.curisit.securis.beans.SignedLicenseBean;
....@@ -29,10 +30,9 @@
2930
3031 private static LicenseManager singleton = new LicenseManager();
3132
32
- String serverUrl = null;
33
-
33
+ public static final String PING_MESSAGE = "SeCuris API OK";
34
+
3435 private LicenseManager() {
35
- serverUrl = Params.get(Params.KEYS.LICENSE_SERVER_URL);
3636 }
3737
3838 public static LicenseManager getInstance() {
....@@ -88,9 +88,7 @@
8888 */
8989 public LicenseBean requestLicense() throws SeCurisException {
9090 RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE));
91
- if (true) {
92
- throw new SeCurisException("Action not implemented yet");
93
- }
91
+
9492 LicenseBean lic = requestLicenseToServer(req);
9593 return lic;
9694 }
....@@ -120,9 +118,10 @@
120118
121119 }
122120
123
- private LicenseBean requestLicenseToServer(RequestBean req) {
124
- // TODO Prepare call to server sending the request bean to get a valid license
125
- return null;
121
+ private SignedLicenseBean requestLicenseToServer(RequestBean req) throws SeCurisException {
122
+ SignedLicenseBean lic = ConnectionManager.getInstance().executePost(Command.CREATE_LIC, SignedLicenseBean.class, req);
123
+
124
+ return lic;
126125 }
127126
128127 /**
....@@ -149,13 +148,19 @@
149148 * @return New license bean if server creates a new one, otherwise the same current License bean will be returned
150149 * @throws SeCurisException
151150 */
152
- public LicenseBean renew(File licenseFile) throws SeCurisException {
151
+ public SignedLicenseBean renew(File licenseFile) throws SeCurisException {
153152 LicenseBean lic = validateLicense(licenseFile);
154
- if (true) {
155
- throw new SeCurisException("Action not implemented yet");
156
- }
157
- // TODO: Send the current LicenseBean to server to check if a new one is prepared.
158
- return lic;
153
+
154
+ SignedLicenseBean newLic = ConnectionManager.getInstance().executePost(Command.RENEW_LIC, SignedLicenseBean.class, lic);
155
+
156
+ return newLic;
159157 }
160158
159
+ public void testServer() throws SeCurisException {
160
+ String pingMsg = ConnectionManager.getInstance().executeGet(Command.RENEW_LIC, String.class);
161
+ if (!PING_MESSAGE.equals(pingMsg)) {
162
+ throw new SeCurisException("SeCuris Server is not running in given URL");
163
+ }
164
+ }
165
+
161166 }
src/main/java/net/curisit/securis/beans/SignedLicenseBean.java
....@@ -10,6 +10,9 @@
1010 public String getCurrentSignature() {
1111 return super.getSignature();
1212 }
13
+
14
+ public SignedLicenseBean() {
15
+ }
1316
1417 public SignedLicenseBean(LicenseBean lb) {
1518 super(lb);
src/main/java/net/curisit/securis/utils/SignatureHelper.java
....@@ -18,9 +18,9 @@
1818 import net.curisit.securis.SeCurisException;
1919 import net.curisit.securis.beans.LicenseBean;
2020
21
+import org.apache.commons.codec.binary.Base64;
2122 import org.apache.commons.io.FileUtils;
2223 import org.apache.commons.io.IOUtils;
23
-import org.apache.commons.net.util.Base64;
2424 import org.apache.logging.log4j.LogManager;
2525 import org.apache.logging.log4j.Logger;
2626
src/main/resources/securis-client.properties
....@@ -1,3 +1,3 @@
1
-license.server.url = https://securis.curistec.com/securis/api
2
-app.code = XXXX
3
-customer.code = XX01
1
+license.server.url = https://securis.curistec.com/api
2
+app.code = CI01
3
+customer.code = CT01