rsanchez
2014-09-29 464f9fc6047ff994728acf69f6f0d3971984b492
#2021 fix - Changed "ping" command return to JSON, StatusBean
1 files added
4 files modified
changed files
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/StatusBean.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
....@@ -41,7 +41,7 @@
4141 private final CloseableHttpClient httpClient;
4242
4343 private ConnectionManager() throws SeCurisException {
44
- String aux = Params.get(Params.KEYS.LICENSE_SERVER_URL, "https://securis.curistec.com/api");
44
+ String aux = Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL);
4545 if (aux.endsWith("/")) {
4646 serverUrl = aux.substring(0, aux.length()-2);
4747 } else {
src/main/java/net/curisit/securis/License.java
....@@ -3,6 +3,7 @@
33 import java.io.File;
44 import java.net.URISyntaxException;
55
6
+import net.curisit.securis.beans.SignedLicenseBean;
67 import net.curisit.securis.utils.Params;
78
89 import org.apache.commons.cli.CommandLine;
....@@ -14,6 +15,7 @@
1415 import org.apache.commons.cli.PosixParser;
1516 import org.apache.logging.log4j.LogManager;
1617 import org.apache.logging.log4j.Logger;
18
+import org.codehaus.jackson.map.DeserializerFactory.Config;
1719
1820 /**
1921 * Main class when SeCuris client is used from command line.
....@@ -40,7 +42,7 @@
4042 */
4143 public class License {
4244
43
- private static final Logger LOG = LogManager.getLogger(License.class);
45
+ private static final Logger LOG = LogManager.getLogger(License.class);
4446
4547 /**
4648 * Aplication entry point when it used as CLI
....@@ -83,12 +85,20 @@
8385 }
8486
8587 if (cmd.hasOption('c')) {
86
- LicenseManager.getInstance().requestLicense();
88
+ SignedLicenseBean lic = LicenseManager.getInstance().requestLicense();
89
+ String filename = cmd.getOptionValue("lic_file");
90
+ if (filename == null)
91
+ filename = "./license.lic";
92
+ File file = new File(filename);
93
+ LicenseManager.getInstance().save(lic, file);
94
+ LOG.info("License file sucessfully saved in file: {}", file.getAbsolutePath());
8795 System.exit(0);
8896 }
8997
9098 if (cmd.hasOption('t')) {
9199 LicenseManager.getInstance().testServer();
100
+ LOG.info("Server is OK, url: {}", Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL));
101
+
92102 System.exit(0);
93103 }
94104
....@@ -163,7 +173,7 @@
163173 options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("validate").withDescription("Validate lic file.").hasArg(true).create('l'));
164174
165175 options.addOption("g", "gen_request", false, "Generate request file. If --rfile parameter is missing then It is generated in current directory.");
166
- options.addOption(OptionBuilder.withLongOpt("create").withDescription("Request a license file to server.").hasArg(false).create('c'));
176
+ options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("create").withDescription("Request a license file to server.").hasArg(true).hasOptionalArg().create('c'));
167177 options.addOption("t", "test_lc", false, "Test if License Server (LC) is available. ");
168178 options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("renew").withDescription("Synchronize/renew the current license file.").hasArg(true).create('r'));
169179
src/main/java/net/curisit/securis/LicenseManager.java
....@@ -11,6 +11,7 @@
1111 import net.curisit.securis.beans.LicenseBean;
1212 import net.curisit.securis.beans.RequestBean;
1313 import net.curisit.securis.beans.SignedLicenseBean;
14
+import net.curisit.securis.beans.StatusBean;
1415 import net.curisit.securis.utils.JsonUtils;
1516 import net.curisit.securis.utils.Params;
1617 import net.curisit.securis.utils.SignatureHelper;
....@@ -86,37 +87,48 @@
8687 * @return The license bean returned by the server
8788 * @throws SeCurisException
8889 */
89
- public LicenseBean requestLicense() throws SeCurisException {
90
+ public SignedLicenseBean requestLicense() throws SeCurisException {
9091 RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE));
9192
92
- LicenseBean lic = requestLicenseToServer(req);
93
+ SignedLicenseBean lic = requestLicenseToServer(req);
9394 return lic;
9495 }
9596
96
- /**
97
- * Generate a license file using a {@link LicenseBean}
98
- *
99
- * @param license
100
- * @param file
101
- * @throws SeCurisException
102
- */
103
- public void save(LicenseBean license, File file) throws SeCurisException {
104
- SignedLicenseBean signedLic = new SignedLicenseBean(license);
105
- byte[] json;
106
- try {
107
- json = JsonUtils.toJSON(signedLic, true).getBytes("utf-8");
108
- Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
109
- } catch (UnsupportedEncodingException e) {
110
- LOG.error("Error creating json doc from license: " + license, e);
111
- throw new SeCurisException("Error creating json doc from license: " + license, e);
112
- } catch (IOException e) {
113
- LOG.error("Error creating license file: " + file, e);
114
- throw new SeCurisException("Error creating json doc from license: " + license, e);
115
- }
97
+ /**
98
+ * Generate a license file using a {@link LicenseBean}
99
+ *
100
+ * @param license
101
+ * @param file
102
+ * @throws SeCurisException
103
+ */
104
+ public void save(LicenseBean license, File file) throws SeCurisException {
105
+ SignedLicenseBean signedLic = new SignedLicenseBean(license);
106
+ save(signedLic, file);
107
+ }
116108
117
- LOG.debug("License saved in {}", file);
109
+ /**
110
+ * Generate a license file using a {@link LicenseBean}
111
+ *
112
+ * @param license
113
+ * @param file
114
+ * @throws SeCurisException
115
+ */
116
+ public void save(SignedLicenseBean signedLic, File file) throws SeCurisException {
117
+ byte[] json;
118
+ try {
119
+ json = JsonUtils.toJSON(signedLic, true).getBytes("utf-8");
120
+ Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
121
+ } catch (UnsupportedEncodingException e) {
122
+ LOG.error("Error creating json doc from license: " + signedLic, e);
123
+ throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
124
+ } catch (IOException e) {
125
+ LOG.error("Error creating license file: " + file, e);
126
+ throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
127
+ }
118128
119
- }
129
+ LOG.debug("License saved in {}", file);
130
+
131
+ }
120132
121133 private SignedLicenseBean requestLicenseToServer(RequestBean req) throws SeCurisException {
122134 SignedLicenseBean lic = ConnectionManager.getInstance().executePost(Command.CREATE_LIC, SignedLicenseBean.class, req);
....@@ -157,8 +169,8 @@
157169 }
158170
159171 public void testServer() throws SeCurisException {
160
- String pingMsg = ConnectionManager.getInstance().executeGet(Command.RENEW_LIC, String.class);
161
- if (!PING_MESSAGE.equals(pingMsg)) {
172
+ StatusBean status = ConnectionManager.getInstance().executeGet(Command.TEST, StatusBean.class);
173
+ if (!PING_MESSAGE.equals(status.getMessage())) {
162174 throw new SeCurisException("SeCuris Server is not running in given URL");
163175 }
164176 }
src/main/java/net/curisit/securis/beans/StatusBean.java
....@@ -0,0 +1,32 @@
1
+package net.curisit.securis.beans;
2
+
3
+import java.util.Date;
4
+
5
+import org.codehaus.jackson.annotate.JsonAutoDetect;
6
+
7
+@JsonAutoDetect
8
+public class StatusBean extends RequestBean {
9
+ private Date date;
10
+ private String message;
11
+
12
+ public StatusBean() {
13
+ }
14
+
15
+ public Date getDate() {
16
+ return date;
17
+ }
18
+
19
+ public void setDate(Date date) {
20
+ this.date = date;
21
+ }
22
+
23
+ public String getMessage() {
24
+ return message;
25
+ }
26
+
27
+ public void setMessage(String message) {
28
+ this.message = message;
29
+ }
30
+
31
+
32
+}
src/main/java/net/curisit/securis/utils/Params.java
....@@ -22,7 +22,8 @@
2222 /**
2323 * Key used to store config file resource location. In a web application, can be set as initial parameter in a servlet loaded on startup
2424 */
25
- public static final String KEY_CONFIG_FILE = "/securis-client.properties";
25
+ public static final String DEFAUT_SERVER_URL = "https://securis.curistec.com/api";
26
+ public static final String KEY_CONFIG_FILE = "/securis-client.properties";
2627
2728 private static Properties params = null;
2829