rsanchez
2016-06-29 a99c9ec291c889667a61240c46005ffa2164e40e
#0 fix - Fixed CLI to use activation code
3 files modified
changed files
pom.xml 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
pom.xml
....@@ -3,7 +3,7 @@
33 <modelVersion>4.0.0</modelVersion>
44 <groupId>net.curisit</groupId>
55 <artifactId>securis-client</artifactId>
6
- <version>1.1.2</version>
6
+ <version>1.2.0</version>
77 <build>
88 <plugins>
99 <plugin>
src/main/java/net/curisit/securis/License.java
....@@ -3,9 +3,6 @@
33 import java.io.File;
44 import java.net.URISyntaxException;
55
6
-import net.curisit.securis.beans.SignedLicenseBean;
7
-import net.curisit.securis.utils.Params;
8
-
96 import org.apache.commons.cli.CommandLine;
107 import org.apache.commons.cli.CommandLineParser;
118 import org.apache.commons.cli.HelpFormatter;
....@@ -15,6 +12,9 @@
1512 import org.apache.commons.cli.PosixParser;
1613 import org.apache.logging.log4j.LogManager;
1714 import org.apache.logging.log4j.Logger;
15
+
16
+import net.curisit.securis.beans.SignedLicenseBean;
17
+import net.curisit.securis.utils.Params;
1818
1919 /**
2020 * Main class when SeCuris client is used from command line.
....@@ -41,166 +41,177 @@
4141 */
4242 public class License {
4343
44
- private static final Logger LOG = LogManager.getLogger(License.class);
44
+ private static final Logger LOG = LogManager.getLogger(License.class);
4545
46
- /**
47
- * Aplication entry point when it used as CLI
48
- *
49
- * @param args
50
- * @throws URISyntaxException
51
- */
52
- public static void main(String[] args) throws URISyntaxException {
46
+ /**
47
+ * Aplication entry point when it used as CLI
48
+ *
49
+ * @param args
50
+ * @throws URISyntaxException
51
+ */
52
+ public static void main(String[] args) throws URISyntaxException {
5353
54
- LOG.debug("SeCuris client tool init ");
54
+ LOG.debug("SeCuris client tool init ");
5555
56
- checkConfigFile();
57
- CommandLine cmd = getCommandLine(args);
56
+ checkConfigFile();
57
+ CommandLine cmd = getCommandLine(args);
5858
59
- try {
60
- if (cmd.hasOption('g')) {
61
- String filename = cmd.getOptionValue("rfile");
62
- if (filename == null)
63
- filename = "./license.req";
64
- File file = new File(filename);
65
- LicenseManager.getInstance().createRequestFile(file);
66
- LOG.info("Request file {} generated OK", file.getAbsolutePath());
67
- System.exit(0);
68
- }
59
+ try {
60
+ if (cmd.hasOption('g')) {
61
+ String filename = cmd.getOptionValue("rfile");
62
+ if (filename == null)
63
+ filename = "./license.req";
64
+ File file = new File(filename);
65
+ String activationCode = cmd.getOptionValue("ac");
66
+ if (activationCode != null) {
67
+ String appcode = cmd.getOptionValue("appcode");
68
+ if (appcode == null) {
69
+ appcode = Params.get(Params.KEYS.APPLICATION_CODE);
70
+ if (appcode == null || appcode.startsWith("_")) {
71
+ checkMandatoryParameter(null, "appcode");
72
+ }
73
+ }
74
+ LicenseManager.getInstance().createRequestFile(file, activationCode, appcode);
75
+ } else {
76
+ LicenseManager.getInstance().createRequestFile(file);
77
+ }
78
+ LOG.info("Request file {} generated OK", file.getAbsolutePath());
79
+ System.exit(0);
80
+ }
6981
70
- if (cmd.hasOption('l')) {
71
- String filename = cmd.getOptionValue("validate");
72
- if (filename == null)
73
- filename = "./license.lic";
74
- File file = new File(filename);
75
- if (!file.exists()) {
76
- throw new SeCurisException("The license file doesn't exist: " + file.getAbsolutePath());
77
- }
78
- try {
79
- LicenseManager.getInstance().validateLicense(file);
80
- LOG.info("License file {} is valid", file.getAbsolutePath());
81
- } catch (SeCurisException e) {
82
- LOG.info("License file {} is NOT valid", file.getAbsolutePath());
83
- LOG.info("Reason: {}", e.toString());
84
- }
82
+ if (cmd.hasOption('l')) {
83
+ String filename = cmd.getOptionValue("validate");
84
+ if (filename == null)
85
+ filename = "./license.lic";
86
+ File file = new File(filename);
87
+ if (!file.exists()) {
88
+ throw new SeCurisException("The license file doesn't exist: " + file.getAbsolutePath());
89
+ }
90
+ try {
91
+ LicenseManager.getInstance().validateLicense(file);
92
+ LOG.info("License file {} is valid", file.getAbsolutePath());
93
+ } catch (SeCurisException e) {
94
+ LOG.info("License file {} is NOT valid", file.getAbsolutePath());
95
+ LOG.info("Reason: {}", e.toString());
96
+ }
8597
86
- System.exit(0);
87
- }
98
+ System.exit(0);
99
+ }
88100
89
- if (cmd.hasOption('c')) {
90
- // TODO: Change CLI to get name and email in license request
91
- SignedLicenseBean lic = LicenseManager.getInstance().requestLicense(null, null);
92
- String filename = cmd.getOptionValue("c");
93
- if (filename == null) {
94
- filename = "./license.lic";
95
- }
96
- File file = new File(filename);
97
- LicenseManager.getInstance().save(lic, file);
98
- LOG.info("License file sucessfully saved in file: {}", file.getAbsolutePath());
99
- System.exit(0);
100
- }
101
+ if (cmd.hasOption('c')) {
102
+ // TODO: Change CLI to get name and email in license request
103
+ SignedLicenseBean lic = LicenseManager.getInstance().requestLicense(null, null);
104
+ String filename = cmd.getOptionValue("c");
105
+ if (filename == null) {
106
+ filename = "./license.lic";
107
+ }
108
+ File file = new File(filename);
109
+ LicenseManager.getInstance().save(lic, file);
110
+ LOG.info("License file sucessfully saved in file: {}", file.getAbsolutePath());
111
+ System.exit(0);
112
+ }
101113
102
- if (cmd.hasOption('t')) {
103
- LicenseManager.getInstance().testServer();
104
- LOG.info("Server is OK, url: {}", Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL));
114
+ if (cmd.hasOption('t')) {
115
+ LicenseManager.getInstance().testServer();
116
+ LOG.info("Server is OK, url: {}", Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL));
105117
106
- System.exit(0);
107
- }
118
+ System.exit(0);
119
+ }
108120
109
- if (cmd.hasOption('r')) {
110
- String licFilename = cmd.getOptionValue("renew");
111
- checkMandatoryParameter(licFilename, "renew");
112
- File file = new File(licFilename);
113
- if (!file.exists()) {
114
- throw new SeCurisException("The license file doesn't exist: " + file.getAbsolutePath());
115
- }
116
- SignedLicenseBean newLic = LicenseManager.getInstance().renew(file);
117
- File oldLicFile = new File(file.getAbsoluteFile() + ".old");
118
- file.renameTo(oldLicFile);
119
- LOG.info("Old license file has been renamed to: {}", oldLicFile.getAbsolutePath());
120
- LicenseManager.getInstance().save(newLic, file);
121
- LOG.info("New license file saved as: {}", file.getAbsolutePath());
122
- System.exit(0);
123
- }
121
+ if (cmd.hasOption('r')) {
122
+ String licFilename = cmd.getOptionValue("renew");
123
+ checkMandatoryParameter(licFilename, "renew");
124
+ File file = new File(licFilename);
125
+ if (!file.exists()) {
126
+ throw new SeCurisException("The license file doesn't exist: " + file.getAbsolutePath());
127
+ }
128
+ SignedLicenseBean newLic = LicenseManager.getInstance().renew(file);
129
+ File oldLicFile = new File(file.getAbsoluteFile() + ".old");
130
+ file.renameTo(oldLicFile);
131
+ LOG.info("Old license file has been renamed to: {}", oldLicFile.getAbsolutePath());
132
+ LicenseManager.getInstance().save(newLic, file);
133
+ LOG.info("New license file saved as: {}", file.getAbsolutePath());
134
+ System.exit(0);
135
+ }
124136
125
- } catch (SeCurisException e) {
126
- LOG.error("The command generated an error: {}", e.toString());
127
- }
128
- }
137
+ } catch (SeCurisException e) {
138
+ LOG.error("The command generated an error: {}", e.toString());
139
+ }
140
+ }
129141
130
- private static void checkMandatoryParameter(String value, String param) {
131
- if (value == null) {
132
- LOG.error("Parameter {} is mandatory. Use --help to get information about parameters", param);
133
- System.exit(-5);
134
- }
135
- }
142
+ private static void checkMandatoryParameter(String value, String param) {
143
+ if (value == null) {
144
+ LOG.error("Parameter {} is mandatory. Use --help to get information about parameters", param);
145
+ System.exit(-5);
146
+ }
147
+ }
136148
137
- /**
138
- * Checks that config file exists and contains mandatory parameters
139
- */
140
- private static void checkConfigFile() {
141
- if (Params.get(Params.KEYS.APPLICATION_CODE) == null) {
149
+ /**
150
+ * Checks that config file exists and contains mandatory parameters
151
+ */
152
+ private static void checkConfigFile() {
153
+ if (Params.get(Params.KEYS.APPLICATION_CODE) == null) {
142154
143
- if (Params.get(Params.KEYS.LIC_TYPE_CODE) == null) {
144
- LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.LIC_TYPE_CODE);
145
- System.exit(-3);
146
- }
147
- if (Params.get(Params.KEYS.CUSTOMER_CODE) == null) {
148
- LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.CUSTOMER_CODE);
149
- System.exit(-4);
150
- }
151
- if (Params.get(Params.KEYS.PACK_CODE) == null) {
152
- LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.PACK_CODE);
153
- System.exit(-6);
154
- }
155
- } // else: The license will be got using activationCode
156
- }
155
+ if (Params.get(Params.KEYS.LIC_TYPE_CODE) == null) {
156
+ LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.LIC_TYPE_CODE);
157
+ System.exit(-3);
158
+ }
159
+ if (Params.get(Params.KEYS.CUSTOMER_CODE) == null) {
160
+ LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.CUSTOMER_CODE);
161
+ System.exit(-4);
162
+ }
163
+ if (Params.get(Params.KEYS.PACK_CODE) == null) {
164
+ LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.PACK_CODE);
165
+ System.exit(-6);
166
+ }
167
+ } // else: The license will be got using activationCode
168
+ }
157169
158
- private static CommandLine getCommandLine(String[] args) {
159
- Options ops = prepareOptionCLI();
160
- if (args.length == 0) {
161
- printHelp(ops);
162
- }
163
- CommandLineParser parser = new PosixParser();
164
- CommandLine cmd = null;
165
- try {
166
- cmd = parser.parse(ops, args);
167
- } catch (ParseException e) {
168
- printHelp(ops);
169
- }
170
+ private static CommandLine getCommandLine(String[] args) {
171
+ Options ops = prepareOptionCLI();
172
+ if (args.length == 0) {
173
+ printHelp(ops);
174
+ }
175
+ CommandLineParser parser = new PosixParser();
176
+ CommandLine cmd = null;
177
+ try {
178
+ cmd = parser.parse(ops, args);
179
+ } catch (ParseException e) {
180
+ printHelp(ops);
181
+ }
170182
171
- if (cmd.hasOption('h')) {
172
- printHelp(ops);
173
- }
183
+ if (cmd.hasOption('h')) {
184
+ printHelp(ops);
185
+ }
174186
175
- return cmd;
176
- }
187
+ return cmd;
188
+ }
177189
178
- private static void printHelp(Options ops) {
179
- HelpFormatter formatter = new HelpFormatter();
180
- formatter.printHelp("securis-client", ops, true);
181
- System.exit(-1);
182
- }
190
+ private static void printHelp(Options ops) {
191
+ HelpFormatter formatter = new HelpFormatter();
192
+ formatter.printHelp("securis-client", ops, true);
193
+ System.exit(-1);
194
+ }
183195
184
- @SuppressWarnings("static-access")
185
- private static Options prepareOptionCLI() {
186
- Options options = new Options();
196
+ @SuppressWarnings("static-access")
197
+ private static Options prepareOptionCLI() {
198
+ Options options = new Options();
187199
188
- options.addOption("h", "help", false, "Show help.");
189
- options.addOption(OptionBuilder.withArgName("req_file").withLongOpt("rfile")
190
- .withDescription("Set request file for its generation or for license requesting.").hasArg(true).create('r'));
191
- // options.addOption(OptionBuilder.withArgName("url_license_server").withLongOpt("server").withDescription("License server url.").hasArg(true).create('s'));
192
- options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("validate").withDescription("Validate lic file.").hasArg(true)
193
- .create('l'));
200
+ options.addOption("h", "help", false, "Show help.");
201
+ options.addOption(OptionBuilder.withArgName("req_file").withLongOpt("rfile").withDescription("Set request file for its generation or for license requesting.").hasArg(true)
202
+ .create('r'));
203
+ options.addOption(OptionBuilder.withArgName("app_code").withLongOpt("appcode").withDescription("Set the app code, ignoring the config value.").hasArg(true).create('a'));
204
+ options.addOption(OptionBuilder.withArgName("activation_code").withLongOpt("ac").withDescription("Set the activation code.").hasArg(true).create('o'));
205
+ options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("validate").withDescription("Validate lic file.").hasArg(true).create('l'));
194206
195
- options.addOption("g", "gen_request", false,
196
- "Generate request file. If --rfile parameter is missing then It is generated in current directory.");
197
- options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("create").withDescription("Request a license file to server.")
198
- .hasArg(true).hasOptionalArg().create('c'));
199
- options.addOption("t", "test_lc", false, "Test if License Server (LC) is available. ");
200
- options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("renew").withDescription("Synchronize/renew the current license file.")
201
- .hasArg(true).create('r'));
207
+ options.addOption(OptionBuilder.withLongOpt("gen_request")
208
+ .withDescription("Generate request file. If --rfile parameter is missing then It is generated in current directory.").hasArg(false).create('g'));
209
+ options.addOption(
210
+ OptionBuilder.withArgName("lic_file").withLongOpt("create").withDescription("Request a license file to server.").hasArg(true).hasOptionalArg().create('c'));
211
+ options.addOption("t", "test_lc", false, "Test if License Server (LC) is available. ");
212
+ options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("renew").withDescription("Synchronize/renew the current license file.").hasArg(true).create('r'));
202213
203
- return options;
204
- }
214
+ return options;
215
+ }
205216
206217 }
src/main/java/net/curisit/securis/LicenseManager.java
....@@ -10,6 +10,10 @@
1010 import java.util.HashMap;
1111 import java.util.Map;
1212
13
+import org.apache.commons.io.FileUtils;
14
+import org.apache.logging.log4j.LogManager;
15
+import org.apache.logging.log4j.Logger;
16
+
1317 import net.curisit.securis.ConnectionManager.Command;
1418 import net.curisit.securis.beans.LicenseBean;
1519 import net.curisit.securis.beans.RequestBean;
....@@ -19,10 +23,6 @@
1923 import net.curisit.securis.utils.Params;
2024 import net.curisit.securis.utils.SignatureHelper;
2125
22
-import org.apache.commons.io.FileUtils;
23
-import org.apache.logging.log4j.LogManager;
24
-import org.apache.logging.log4j.Logger;
25
-
2626 /**
2727 * Manage all licenses tasks, just like validation, renew, requesting, ...
2828 *
....@@ -30,257 +30,278 @@
3030 */
3131 public class LicenseManager {
3232
33
- private static final Logger LOG = LogManager.getLogger(LicenseManager.class);
33
+ private static final Logger LOG = LogManager.getLogger(LicenseManager.class);
3434
35
- private static LicenseManager singleton = new LicenseManager();
35
+ private static LicenseManager singleton = new LicenseManager();
3636
37
- public static final String PING_MESSAGE = "SeCuris API OK";
38
- public static final String HEADER_LICENSE_NAME_OR_REFERENCE = "X-SECURIS-LIC-NAMEREF";
39
- public static final String HEADER_LICENSE_EMAIL = "X-SECURIS-LIC-EMAIL";
37
+ public static final String PING_MESSAGE = "SeCuris API OK";
38
+ public static final String HEADER_LICENSE_NAME_OR_REFERENCE = "X-SECURIS-LIC-NAMEREF";
39
+ public static final String HEADER_LICENSE_EMAIL = "X-SECURIS-LIC-EMAIL";
4040
41
- private LicenseManager() {
42
- }
41
+ private LicenseManager() {
42
+ }
4343
44
- public static LicenseManager getInstance() {
45
- return singleton;
46
- }
44
+ public static LicenseManager getInstance() {
45
+ return singleton;
46
+ }
4747
48
- /**
49
- * Loads a license from file
50
- *
51
- * @param licFile
52
- * @return The license bean
53
- * @throws SeCurisException
54
- */
55
- public LicenseBean load(File licFile) throws SeCurisException {
56
- LicenseBean licBean;
57
- try {
58
- licBean = JsonUtils.json2object(FileUtils.readFileToString(licFile), LicenseBean.class);
59
- } catch (IOException e) {
60
- throw new SeCurisException("Error getting license data from file: " + licFile, e);
61
- }
62
- return licBean;
63
- }
48
+ /**
49
+ * Loads a license from file
50
+ *
51
+ * @param licFile
52
+ * @return The license bean
53
+ * @throws SeCurisException
54
+ */
55
+ public LicenseBean load(File licFile) throws SeCurisException {
56
+ LicenseBean licBean;
57
+ try {
58
+ licBean = JsonUtils.json2object(FileUtils.readFileToString(licFile), LicenseBean.class);
59
+ } catch (IOException e) {
60
+ throw new SeCurisException("Error getting license data from file: " + licFile, e);
61
+ }
62
+ return licBean;
63
+ }
6464
65
- /**
66
- * Validates the license stored in {@code licFile} and get the corresponding
67
- * LicenseBean
68
- * <p>
69
- * The validation includes:
70
- * <ul>
71
- * <li>Signature</li>
72
- * <li>HW data</li>
73
- * <li>Logo CRC</li>
74
- * </ul>
75
- * </p>
76
- *
77
- * @param licFile
78
- * @return The license bean stored in file
79
- * @throws SeCurisException
80
- */
81
- public LicenseBean validateLicense(File licFile) throws SeCurisException {
65
+ /**
66
+ * Validates the license stored in {@code licFile} and get the corresponding
67
+ * LicenseBean
68
+ * <p>
69
+ * The validation includes:
70
+ * <ul>
71
+ * <li>Signature</li>
72
+ * <li>HW data</li>
73
+ * <li>Logo CRC</li>
74
+ * </ul>
75
+ * </p>
76
+ *
77
+ * @param licFile
78
+ * @return The license bean stored in file
79
+ * @throws SeCurisException
80
+ */
81
+ public LicenseBean validateLicense(File licFile) throws SeCurisException {
8282
83
- return validateLicense(licFile, false);
84
- }
83
+ return validateLicense(licFile, false);
84
+ }
8585
86
- /**
87
- * Validates the license stored in {@code licFile} and get the corresponding
88
- * LicenseBean. The License date is not validated
89
- * <p>
90
- * The validation includes:
91
- * <ul>
92
- * <li>Signature</li>
93
- * <li>HW data</li>
94
- * <li>Logo CRC</li>
95
- * </ul>
96
- * </p>
97
- *
98
- * @param licFile
99
- * @return The license bean stored in file
100
- * @throws SeCurisException
101
- */
102
- public LicenseBean validateLicense(File licFile, boolean excludeDateValidation) throws SeCurisException {
103
- LicenseBean licBean = load(licFile);
104
- SignatureHelper.getInstance().validateSignature(licBean);
105
- if (licBean.getActivationCode() != null) {
106
- LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.APPLICATION_CODE), licBean.getActivationCode());
107
- } else {
108
- LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.LIC_TYPE_CODE, Params.get(Params.KEYS.APPLICATION_CODE)),
109
- Params.get(Params.KEYS.CUSTOMER_CODE), Params.get(Params.KEYS.PACK_CODE));
110
- }
111
- LicenseValidator.getInstance().validateLogo(licBean);
86
+ /**
87
+ * Validates the license stored in {@code licFile} and get the corresponding
88
+ * LicenseBean. The License date is not validated
89
+ * <p>
90
+ * The validation includes:
91
+ * <ul>
92
+ * <li>Signature</li>
93
+ * <li>HW data</li>
94
+ * <li>Logo CRC</li>
95
+ * </ul>
96
+ * </p>
97
+ *
98
+ * @param licFile
99
+ * @return The license bean stored in file
100
+ * @throws SeCurisException
101
+ */
102
+ public LicenseBean validateLicense(File licFile, boolean excludeDateValidation) throws SeCurisException {
103
+ LicenseBean licBean = load(licFile);
104
+ SignatureHelper.getInstance().validateSignature(licBean);
105
+ if (licBean.getActivationCode() != null) {
106
+ LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.APPLICATION_CODE), licBean.getActivationCode());
107
+ } else {
108
+ LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.LIC_TYPE_CODE, Params.get(Params.KEYS.APPLICATION_CODE)),
109
+ Params.get(Params.KEYS.CUSTOMER_CODE), Params.get(Params.KEYS.PACK_CODE));
110
+ }
111
+ LicenseValidator.getInstance().validateLogo(licBean);
112112
113
- if (!excludeDateValidation) {
114
- if (new Date().after(licBean.getExpirationDate())) {
115
- throw new ExpiredLicenseException();
116
- }
117
- }
113
+ if (!excludeDateValidation) {
114
+ if (new Date().after(licBean.getExpirationDate())) {
115
+ throw new ExpiredLicenseException();
116
+ }
117
+ }
118118
119
- return licBean;
120
- }
119
+ return licBean;
120
+ }
121121
122
- /**
123
- * Request to server for a valid license
124
- *
125
- * @return The license bean returned by the server
126
- * @throws SeCurisException
127
- */
128
- public SignedLicenseBean requestLicense(String nameOrReference, String email) throws SeCurisException {
129
- RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.LIC_TYPE_CODE, Params.get(Params.KEYS.APPLICATION_CODE)),
130
- Params.get(Params.KEYS.CUSTOMER_CODE), Params.get(Params.KEYS.PACK_CODE));
122
+ /**
123
+ * Request to server for a valid license
124
+ *
125
+ * @return The license bean returned by the server
126
+ * @throws SeCurisException
127
+ */
128
+ public SignedLicenseBean requestLicense(String nameOrReference, String email) throws SeCurisException {
129
+ RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.LIC_TYPE_CODE, Params.get(Params.KEYS.APPLICATION_CODE)),
130
+ Params.get(Params.KEYS.CUSTOMER_CODE), Params.get(Params.KEYS.PACK_CODE));
131131
132
- SignedLicenseBean lic = requestLicenseToServer(req, nameOrReference, email);
133
- return lic;
134
- }
132
+ SignedLicenseBean lic = requestLicenseToServer(req, nameOrReference, email);
133
+ return lic;
134
+ }
135135
136
- /**
137
- * Request to server for a valid license
138
- *
139
- * @return The license bean returned by the server
140
- * @throws SeCurisException
141
- */
142
- public SignedLicenseBean requestLicense(String nameOrReference, String email, String activationCode) throws SeCurisException {
143
- RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), activationCode);
136
+ /**
137
+ * Request to server for a valid license
138
+ *
139
+ * @return The license bean returned by the server
140
+ * @throws SeCurisException
141
+ */
142
+ public SignedLicenseBean requestLicense(String nameOrReference, String email, String activationCode) throws SeCurisException {
143
+ RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), activationCode);
144144
145
- SignedLicenseBean lic = requestLicenseToServer(req, nameOrReference, email);
146
- return lic;
147
- }
145
+ SignedLicenseBean lic = requestLicenseToServer(req, nameOrReference, email);
146
+ return lic;
147
+ }
148148
149
- /**
150
- * Request to server for a valid license
151
- *
152
- * @return The license bean returned by the server
153
- * @throws SeCurisException
154
- */
155
- public SignedLicenseBean requestLicense(String activationCode) throws SeCurisException {
156
- return requestLicense(null, null, activationCode);
157
- }
149
+ /**
150
+ * Request to server for a valid license
151
+ *
152
+ * @return The license bean returned by the server
153
+ * @throws SeCurisException
154
+ */
155
+ public SignedLicenseBean requestLicense(String activationCode) throws SeCurisException {
156
+ return requestLicense(null, null, activationCode);
157
+ }
158158
159
- /**
160
- * Generate a license file using a {@link LicenseBean}
161
- *
162
- * @param license
163
- * @param file
164
- * @throws SeCurisException
165
- */
166
- public void save(LicenseBean license, File file) throws SeCurisException {
167
- SignedLicenseBean signedLic = new SignedLicenseBean(license);
168
- save(signedLic, file);
169
- }
159
+ /**
160
+ * Generate a license file using a {@link LicenseBean}
161
+ *
162
+ * @param license
163
+ * @param file
164
+ * @throws SeCurisException
165
+ */
166
+ public void save(LicenseBean license, File file) throws SeCurisException {
167
+ SignedLicenseBean signedLic = new SignedLicenseBean(license);
168
+ save(signedLic, file);
169
+ }
170170
171
- /**
172
- * Generate a license file using a {@link LicenseBean}
173
- *
174
- * @param license
175
- * @param file
176
- * @throws SeCurisException
177
- */
178
- public void save(SignedLicenseBean signedLic, File file) throws SeCurisException {
179
- byte[] json;
180
- try {
181
- json = JsonUtils.toPrettyJSON(signedLic).getBytes("utf-8");
182
- Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
183
- } catch (UnsupportedEncodingException e) {
184
- LOG.error("Error creating json doc from license: " + signedLic, e);
185
- throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
186
- } catch (IOException e) {
187
- LOG.error("Error creating license file: " + file, e);
188
- throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
189
- }
171
+ /**
172
+ * Generate a license file using a {@link LicenseBean}
173
+ *
174
+ * @param license
175
+ * @param file
176
+ * @throws SeCurisException
177
+ */
178
+ public void save(SignedLicenseBean signedLic, File file) throws SeCurisException {
179
+ byte[] json;
180
+ try {
181
+ json = JsonUtils.toPrettyJSON(signedLic).getBytes("utf-8");
182
+ Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
183
+ } catch (UnsupportedEncodingException e) {
184
+ LOG.error("Error creating json doc from license: " + signedLic, e);
185
+ throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
186
+ } catch (IOException e) {
187
+ LOG.error("Error creating license file: " + file, e);
188
+ throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
189
+ }
190190
191
- LOG.debug("License saved in {}", file);
191
+ LOG.debug("License saved in {}", file);
192192
193
- }
193
+ }
194194
195
- private SignedLicenseBean requestLicenseToServer(RequestBean req, String nameOrReference, String email) throws SeCurisException {
196
- Map<String, String> headers = new HashMap<String, String>();
197
- headers.put(HEADER_LICENSE_NAME_OR_REFERENCE, nameOrReference);
198
- headers.put(HEADER_LICENSE_EMAIL, email);
199
- SignedLicenseBean lic = ConnectionManager.getInstance().executePost(Command.CREATE_LIC, SignedLicenseBean.class, req, headers);
195
+ private SignedLicenseBean requestLicenseToServer(RequestBean req, String nameOrReference, String email) throws SeCurisException {
196
+ Map<String, String> headers = new HashMap<String, String>();
197
+ headers.put(HEADER_LICENSE_NAME_OR_REFERENCE, nameOrReference);
198
+ headers.put(HEADER_LICENSE_EMAIL, email);
199
+ SignedLicenseBean lic = ConnectionManager.getInstance().executePost(Command.CREATE_LIC, SignedLicenseBean.class, req, headers);
200200
201
- return lic;
202
- }
201
+ return lic;
202
+ }
203203
204
- /**
205
- * Creates a new request file with current hardware in the File passed as
206
- * parameter
207
- *
208
- * @param outputRequestFile
209
- * File where the request data will be saved
210
- * @return The generated request bean
211
- * @throws SeCurisException
212
- */
213
- public RequestBean createRequestFile(File outputRequestFile) throws SeCurisException {
214
- RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.LIC_TYPE_CODE, Params.get(Params.KEYS.APPLICATION_CODE)),
215
- Params.get(Params.KEYS.CUSTOMER_CODE), Params.get(Params.KEYS.PACK_CODE));
204
+ /**
205
+ * Creates a new request file with current hardware in the File passed as
206
+ * parameter
207
+ *
208
+ * @param outputRequestFile
209
+ * File where the request data will be saved
210
+ * @return The generated request bean
211
+ * @throws SeCurisException
212
+ */
213
+ public RequestBean createRequestFile(File outputRequestFile) throws SeCurisException {
214
+ RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.LIC_TYPE_CODE, Params.get(Params.KEYS.APPLICATION_CODE)),
215
+ Params.get(Params.KEYS.CUSTOMER_CODE), Params.get(Params.KEYS.PACK_CODE));
216216
217
- ReqGenerator.getInstance().save(req, outputRequestFile);
217
+ ReqGenerator.getInstance().save(req, outputRequestFile);
218218
219
- return req;
220
- }
219
+ return req;
220
+ }
221221
222
- /**
223
- * Creates a new request file with current hardware in the File passed as
224
- * parameter
225
- *
226
- * @param outputRequestFile
227
- * File where the request data will be saved
228
- * @return The generated request bean
229
- * @throws SeCurisException
230
- */
231
- public RequestBean createRequestFile(File outputRequestFile, String activationCode) throws SeCurisException {
232
- RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), activationCode);
222
+ /**
223
+ * Creates a new request file with current hardware in the File passed as
224
+ * parameter
225
+ *
226
+ * @param outputRequestFile
227
+ * File where the request data will be saved
228
+ * @return The generated request bean
229
+ * @throws SeCurisException
230
+ */
231
+ public RequestBean createRequestFile(File outputRequestFile, String activationCode) throws SeCurisException {
232
+ return createRequestFile(outputRequestFile, activationCode, null);
233
+ }
233234
234
- ReqGenerator.getInstance().save(req, outputRequestFile);
235
+ /**
236
+ * Creates a new request file with current hardware in the File passed as
237
+ * parameter
238
+ *
239
+ * @param outputRequestFile
240
+ * File where the request data will be saved
241
+ *
242
+ * @param activationCode
243
+ * Activation code provided be SeCurisadministrator
244
+ * @param appCode
245
+ * Application code to use, ignoring the code set in config file
246
+ * @return The generated request bean
247
+ * @throws SeCurisException
248
+ */
249
+ public RequestBean createRequestFile(File outputRequestFile, String activationCode, String appCode) throws SeCurisException {
250
+ if (appCode == null) {
251
+ Params.get(Params.KEYS.APPLICATION_CODE);
252
+ }
253
+ RequestBean req = ReqGenerator.getInstance().createRequest(appCode, activationCode);
235254
236
- return req;
237
- }
255
+ ReqGenerator.getInstance().save(req, outputRequestFile);
238256
239
- /**
240
- * Send the current license file to server, which is previously validated,
241
- * to get a renewed one if it is prepared in server side.
242
- *
243
- * @param licenseFile
244
- * Current and valid License file
245
- * @return New license bean if server creates a new one, otherwise the same
246
- * current License bean will be returned
247
- * @throws SeCurisException
248
- */
249
- public SignedLicenseBean renew(File licenseFile) throws SeCurisException {
250
- LicenseBean lic = validateLicense(licenseFile, true);
257
+ return req;
258
+ }
251259
252
- SignedLicenseBean newLic = ConnectionManager.getInstance().executePost(Command.RENEW_LIC, SignedLicenseBean.class, lic);
260
+ /**
261
+ * Send the current license file to server, which is previously validated,
262
+ * to get a renewed one if it is prepared in server side.
263
+ *
264
+ * @param licenseFile
265
+ * Current and valid License file
266
+ * @return New license bean if server creates a new one, otherwise the same
267
+ * current License bean will be returned
268
+ * @throws SeCurisException
269
+ */
270
+ public SignedLicenseBean renew(File licenseFile) throws SeCurisException {
271
+ LicenseBean lic = validateLicense(licenseFile, true);
253272
254
- return newLic;
255
- }
273
+ SignedLicenseBean newLic = ConnectionManager.getInstance().executePost(Command.RENEW_LIC, SignedLicenseBean.class, lic);
256274
257
- /**
258
- * Check on SeCuris server if current license is still valid in server DB.
259
- *
260
- * @param licenseFile
261
- * @throws SeCurisException
262
- */
263
- public void assertLicenseIsValid(File licenseFile) throws SeCurisException, IOException {
264
- LicenseBean lic = validateLicense(licenseFile);
265
- // We need to snd the signed version to validate signature on server
266
- ConnectionManager.getInstance().executePost(Command.VALIDATE, LicenseBean.class, new SignedLicenseBean(lic));
267
- }
275
+ return newLic;
276
+ }
268277
269
- public void testServer() throws SeCurisException {
270
- StatusBean status = ConnectionManager.getInstance().executeGet(Command.TEST, StatusBean.class);
271
- if (!PING_MESSAGE.equals(status.getMessage())) {
272
- throw new SeCurisException("SeCuris Server is not running in given URL");
273
- }
274
- }
278
+ /**
279
+ * Check on SeCuris server if current license is still valid in server DB.
280
+ *
281
+ * @param licenseFile
282
+ * @throws SeCurisException
283
+ */
284
+ public void assertLicenseIsValid(File licenseFile) throws SeCurisException, IOException {
285
+ LicenseBean lic = validateLicense(licenseFile);
286
+ // We need to snd the signed version to validate signature on server
287
+ ConnectionManager.getInstance().executePost(Command.VALIDATE, LicenseBean.class, new SignedLicenseBean(lic));
288
+ }
275289
276
- public static void main(String[] args) throws SeCurisException {
277
- System.out.println("APPLICATION_CODE: " + Params.get(Params.KEYS.APPLICATION_CODE));
278
- System.out.println("LICENSE_SERVER_URL: " + Params.get(Params.KEYS.LICENSE_SERVER_URL));
279
- LicenseManager lm = LicenseManager.getInstance();
280
- // LicenseBean lic =
281
- // lm.requestLicense("aaf88d6c-6622-492a-93ec-10f3d1dc7120");
282
- LicenseBean lic = lm.requestLicense("Rob", "rsanchez@curisit.net");
283
- System.out.println(lic.getLicenseCode() + " " + lic.getExpirationDate());
284
- // LicenseBean lic = lm.
285
- }
290
+ public void testServer() throws SeCurisException {
291
+ StatusBean status = ConnectionManager.getInstance().executeGet(Command.TEST, StatusBean.class);
292
+ if (!PING_MESSAGE.equals(status.getMessage())) {
293
+ throw new SeCurisException("SeCuris Server is not running in given URL");
294
+ }
295
+ }
296
+
297
+ public static void main(String[] args) throws SeCurisException {
298
+ System.out.println("APPLICATION_CODE: " + Params.get(Params.KEYS.APPLICATION_CODE));
299
+ System.out.println("LICENSE_SERVER_URL: " + Params.get(Params.KEYS.LICENSE_SERVER_URL));
300
+ LicenseManager lm = LicenseManager.getInstance();
301
+ // LicenseBean lic =
302
+ // lm.requestLicense("aaf88d6c-6622-492a-93ec-10f3d1dc7120");
303
+ LicenseBean lic = lm.requestLicense("Rob", "rsanchez@curisit.net");
304
+ System.out.println(lic.getLicenseCode() + " " + lic.getExpirationDate());
305
+ // LicenseBean lic = lm.
306
+ }
286307 }