rsanchez
2015-09-22 7d8713e88c7872b195f7e4d02ac4812536ae196b
#2756 feature - Added lic activation using activationCode
8 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
src/main/java/net/curisit/securis/LicenseValidator.java patch | view | blame | history
src/main/java/net/curisit/securis/ReqGenerator.java patch | view | blame | history
src/main/java/net/curisit/securis/beans/LicenseBean.java patch | view | blame | history
src/main/java/net/curisit/securis/beans/RequestBean.java patch | view | blame | history
src/main/java/net/curisit/securis/utils/JsonUtils.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.0.6-SNAPSHOT</version>
6
+ <version>1.1.0-SNAPSHOT</version>
77 <build>
88 <plugins>
99 <plugin>
src/main/java/net/curisit/securis/License.java
....@@ -41,163 +41,166 @@
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
+ LicenseManager.getInstance().createRequestFile(file);
66
+ LOG.info("Request file {} generated OK", file.getAbsolutePath());
67
+ System.exit(0);
68
+ }
6969
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
- }
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
+ }
8585
86
- System.exit(0);
87
- }
86
+ System.exit(0);
87
+ }
8888
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
- }
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
+ }
101101
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));
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));
105105
106
- System.exit(0);
107
- }
106
+ System.exit(0);
107
+ }
108108
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
- }
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
+ }
124124
125
- } catch (SeCurisException e) {
126
- LOG.error("The command generated an error: {}", e.toString());
127
- }
128
- }
125
+ } catch (SeCurisException e) {
126
+ LOG.error("The command generated an error: {}", e.toString());
127
+ }
128
+ }
129129
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
- }
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
+ }
136136
137
- /**
138
- * Checks that config file exists and contains mandatory parameters
139
- */
140
- private static void checkConfigFile() {
141
- if (Params.get(Params.KEYS.LIC_TYPE_CODE) == null && Params.get(Params.KEYS.APPLICATION_CODE) == null) {
142
- LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.LIC_TYPE_CODE);
143
- System.exit(-3);
144
- }
145
- if (Params.get(Params.KEYS.CUSTOMER_CODE) == null) {
146
- LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.CUSTOMER_CODE);
147
- System.exit(-4);
148
- }
149
- if (Params.get(Params.KEYS.PACK_CODE) == null) {
150
- LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.PACK_CODE);
151
- System.exit(-6);
152
- }
153
- }
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) {
154142
155
- private static CommandLine getCommandLine(String[] args) {
156
- Options ops = prepareOptionCLI();
157
- if (args.length == 0) {
158
- printHelp(ops);
159
- }
160
- CommandLineParser parser = new PosixParser();
161
- CommandLine cmd = null;
162
- try {
163
- cmd = parser.parse(ops, args);
164
- } catch (ParseException e) {
165
- printHelp(ops);
166
- }
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
+ }
167157
168
- if (cmd.hasOption('h')) {
169
- printHelp(ops);
170
- }
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
+ }
171170
172
- return cmd;
173
- }
171
+ if (cmd.hasOption('h')) {
172
+ printHelp(ops);
173
+ }
174174
175
- private static void printHelp(Options ops) {
176
- HelpFormatter formatter = new HelpFormatter();
177
- formatter.printHelp("securis-client", ops, true);
178
- System.exit(-1);
179
- }
175
+ return cmd;
176
+ }
180177
181
- @SuppressWarnings("static-access")
182
- private static Options prepareOptionCLI() {
183
- Options options = new Options();
178
+ private static void printHelp(Options ops) {
179
+ HelpFormatter formatter = new HelpFormatter();
180
+ formatter.printHelp("securis-client", ops, true);
181
+ System.exit(-1);
182
+ }
184183
185
- options.addOption("h", "help", false, "Show help.");
186
- options.addOption(OptionBuilder.withArgName("req_file").withLongOpt("rfile")
187
- .withDescription("Set request file for its generation or for license requesting.").hasArg(true).create('r'));
188
- // options.addOption(OptionBuilder.withArgName("url_license_server").withLongOpt("server").withDescription("License server url.").hasArg(true).create('s'));
189
- options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("validate").withDescription("Validate lic file.").hasArg(true)
190
- .create('l'));
184
+ @SuppressWarnings("static-access")
185
+ private static Options prepareOptionCLI() {
186
+ Options options = new Options();
191187
192
- options.addOption("g", "gen_request", false,
193
- "Generate request file. If --rfile parameter is missing then It is generated in current directory.");
194
- options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("create").withDescription("Request a license file to server.")
195
- .hasArg(true).hasOptionalArg().create('c'));
196
- options.addOption("t", "test_lc", false, "Test if License Server (LC) is available. ");
197
- options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("renew").withDescription("Synchronize/renew the current license file.")
198
- .hasArg(true).create('r'));
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'));
199194
200
- return options;
201
- }
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'));
202
+
203
+ return options;
204
+ }
202205
203206 }
src/main/java/net/curisit/securis/LicenseManager.java
....@@ -38,7 +38,8 @@
3838 public static final String HEADER_LICENSE_NAME_OR_REFERENCE = "X-SECURIS-LIC-NAMEREF";
3939 public static final String HEADER_LICENSE_EMAIL = "X-SECURIS-LIC-EMAIL";
4040
41
- private LicenseManager() {}
41
+ private LicenseManager() {
42
+ }
4243
4344 public static LicenseManager getInstance() {
4445 return singleton;
....@@ -62,7 +63,8 @@
6263 }
6364
6465 /**
65
- * Validates the license stored in {@code licFile} and get the corresponding LicenseBean
66
+ * Validates the license stored in {@code licFile} and get the corresponding
67
+ * LicenseBean
6668 * <p>
6769 * The validation includes:
6870 * <ul>
....@@ -82,7 +84,8 @@
8284 }
8385
8486 /**
85
- * Validates the license stored in {@code licFile} and get the corresponding LicenseBean. The License date is not validated
87
+ * Validates the license stored in {@code licFile} and get the corresponding
88
+ * LicenseBean. The License date is not validated
8689 * <p>
8790 * The validation includes:
8891 * <ul>
....@@ -99,7 +102,12 @@
99102 public LicenseBean validateLicense(File licFile, boolean excludeDateValidation) throws SeCurisException {
100103 LicenseBean licBean = load(licFile);
101104 SignatureHelper.getInstance().validateSignature(licBean);
102
- LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.LIC_TYPE_CODE, Params.get(Params.KEYS.APPLICATION_CODE)), Params.get(Params.KEYS.CUSTOMER_CODE), Params.get(Params.KEYS.PACK_CODE));
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
+ }
103111 LicenseValidator.getInstance().validateLogo(licBean);
104112
105113 if (!excludeDateValidation) {
....@@ -118,7 +126,21 @@
118126 * @throws SeCurisException
119127 */
120128 public SignedLicenseBean requestLicense(String nameOrReference, String email) throws SeCurisException {
121
- RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.LIC_TYPE_CODE, Params.get(Params.KEYS.APPLICATION_CODE)), Params.get(Params.KEYS.CUSTOMER_CODE), Params.get(Params.KEYS.PACK_CODE));
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));
131
+
132
+ SignedLicenseBean lic = requestLicenseToServer(req, nameOrReference, email);
133
+ return lic;
134
+ }
135
+
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);
122144
123145 SignedLicenseBean lic = requestLicenseToServer(req, nameOrReference, email);
124146 return lic;
....@@ -170,7 +192,8 @@
170192 }
171193
172194 /**
173
- * Creates a new request file with current hardware in the File passed as parameter
195
+ * Creates a new request file with current hardware in the File passed as
196
+ * parameter
174197 *
175198 * @param outputRequestFile
176199 * File where the request data will be saved
....@@ -178,7 +201,8 @@
178201 * @throws SeCurisException
179202 */
180203 public RequestBean createRequestFile(File outputRequestFile) throws SeCurisException {
181
- RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.LIC_TYPE_CODE, Params.get(Params.KEYS.APPLICATION_CODE)), Params.get(Params.KEYS.CUSTOMER_CODE), Params.get(Params.KEYS.PACK_CODE));
204
+ RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.LIC_TYPE_CODE, Params.get(Params.KEYS.APPLICATION_CODE)),
205
+ Params.get(Params.KEYS.CUSTOMER_CODE), Params.get(Params.KEYS.PACK_CODE));
182206
183207 ReqGenerator.getInstance().save(req, outputRequestFile);
184208
....@@ -186,11 +210,30 @@
186210 }
187211
188212 /**
189
- * Send the current license file to server, which is previously validated, to get a renewed one if it is prepared in server side.
213
+ * Creates a new request file with current hardware in the File passed as
214
+ * parameter
215
+ *
216
+ * @param outputRequestFile
217
+ * File where the request data will be saved
218
+ * @return The generated request bean
219
+ * @throws SeCurisException
220
+ */
221
+ public RequestBean createRequestFile(File outputRequestFile, String activationCode) throws SeCurisException {
222
+ RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), activationCode);
223
+
224
+ ReqGenerator.getInstance().save(req, outputRequestFile);
225
+
226
+ return req;
227
+ }
228
+
229
+ /**
230
+ * Send the current license file to server, which is previously validated,
231
+ * to get a renewed one if it is prepared in server side.
190232 *
191233 * @param licenseFile
192234 * Current and valid License file
193
- * @return New license bean if server creates a new one, otherwise the same current License bean will be returned
235
+ * @return New license bean if server creates a new one, otherwise the same
236
+ * current License bean will be returned
194237 * @throws SeCurisException
195238 */
196239 public SignedLicenseBean renew(File licenseFile) throws SeCurisException {
....@@ -220,18 +263,4 @@
220263 }
221264 }
222265
223
- public static void main(String[] args) throws SeCurisException {
224
- String filename = "/Users/rob/.doxr/doxr.lic";
225
-
226
- if (filename == null)
227
- filename = "./license.req";
228
- File file = new File(filename);
229
- LicenseManager.getInstance().validateLicense(file);
230
- LOG.info("Validation Ok");
231
- String r = "{\"licenseTypeCode\":\"DX01\",\"packCode\":\"DEVDX01\",\"arch\":\"x86_64\",\"osName\":\"darwin\",\"customerCode\":\"CIT\",\"macAddresses\":[\"60:c5:47:03:cf:c8\",\"b2:00:12:b6:88:e0\"]}";
232
- RequestBean rb = JsonUtils.json2object(r, RequestBean.class);
233
- System.out.println(rb);
234
- LOG.info("Request file {} generated OK", file.getAbsolutePath());
235
-
236
- }
237266 }
src/main/java/net/curisit/securis/LicenseValidator.java
....@@ -61,8 +61,16 @@
6161 public void validateHW(RequestBean reqBean, String licTypeCode, String customerCode, String packCode) throws SeCurisException {
6262 RequestBean currentHW = ReqGenerator.getInstance().createRequest(licTypeCode, customerCode, packCode);
6363 if (!currentHW.match(reqBean)) {
64
- throw new SeCurisException("Current System info mismatch the License System info:\n Licensed: \n" + JsonUtils.toPrettyJSON(reqBean)
65
- + "\n Expected: \n" + JsonUtils.toPrettyJSON(currentHW));
64
+ throw new SeCurisException("Current System info mismatch the License System info:\n Licensed: \n"
65
+ + JsonUtils.toPrettyJSON(reqBean, RequestBean.class) + "\n Expected: \n" + JsonUtils.toPrettyJSON(currentHW));
66
+ }
67
+ }
68
+
69
+ public void validateHW(RequestBean reqBean, String appCode, String activationCode) throws SeCurisException {
70
+ RequestBean currentHW = ReqGenerator.getInstance().createRequest(appCode, activationCode);
71
+ if (!currentHW.match(reqBean)) {
72
+ throw new SeCurisException("Current System info mismatch the License System info:\n Licensed: \n"
73
+ + JsonUtils.toPrettyJSON(reqBean, RequestBean.class) + "\n Expected: \n" + JsonUtils.toPrettyJSON(currentHW));
6674 }
6775 }
6876
src/main/java/net/curisit/securis/ReqGenerator.java
....@@ -44,6 +44,19 @@
4444 return req;
4545 }
4646
47
+ public RequestBean createRequest(String appCode, String activationCode) throws SeCurisException {
48
+ RequestBean req = new RequestBean();
49
+
50
+ req.setAppCode(appCode);
51
+ req.setActivationCode(activationCode);
52
+ req.setArch(HWInfo.getArch());
53
+ req.setCrcLogo(getCrcLogo());
54
+ req.setMacAddresses(HWInfo.getMACAddress());
55
+ req.setOsName(HWInfo.getOsName());
56
+
57
+ return req;
58
+ }
59
+
4760 public RequestBean loadRequest(File requestFile) throws SeCurisException {
4861 try {
4962 String json = new String(Files.readAllBytes(Paths.get(requestFile.toURI())), "utf-8");
src/main/java/net/curisit/securis/beans/LicenseBean.java
....@@ -11,7 +11,8 @@
1111
1212 @JsonAutoDetect
1313 @JsonPropertyOrder({
14
- "appName", "licenseCode", "licenseTypeCode", "expirationDate", "arch", "osName", "customerCode", "macAddresses", "crcLogo", "metadata", "packCode"
14
+ "appCode", "appName", "licenseCode", "activationCode", "licenseTypeCode", "expirationDate", "arch", "osName", "customerCode", "macAddresses",
15
+ "crcLogo", "metadata", "packCode"
1516 })
1617 public class LicenseBean extends RequestBean {
1718 private Date expirationDate;
src/main/java/net/curisit/securis/beans/RequestBean.java
....@@ -2,9 +2,7 @@
22
33 import java.util.Collections;
44 import java.util.List;
5
-
6
-import net.curisit.securis.SeCurisException;
7
-import net.curisit.securis.utils.JsonUtils;
5
+import java.util.Objects;
86
97 import com.fasterxml.jackson.annotation.JsonAutoDetect;
108 import com.fasterxml.jackson.annotation.JsonInclude;
....@@ -14,9 +12,11 @@
1412 @JsonAutoDetect
1513 @JsonInclude(Include.NON_NULL)
1614 @JsonPropertyOrder({
17
- "licenseTypeCode", "packCode", "arch", "osName", "customerCode", "macAddresses", "crcLogo"
15
+ "appCode", "activationCode", "licenseTypeCode", "packCode", "arch", "osName", "customerCode", "macAddresses", "crcLogo"
1816 })
1917 public class RequestBean {
18
+ private String appCode;
19
+ private String activationCode;
2020 private String customerCode;
2121 private String crcLogo;
2222 private String licenseTypeCode;
....@@ -24,6 +24,14 @@
2424 private List<String> macAddresses;
2525 private String osName;
2626 private String arch;
27
+
28
+ public String getActivationCode() {
29
+ return activationCode;
30
+ }
31
+
32
+ public void setActivationCode(String activationCode) {
33
+ this.activationCode = activationCode;
34
+ }
2735
2836 public String getCustomerCode() {
2937 return customerCode;
....@@ -48,9 +56,9 @@
4856 public void setMacAddresses(List<String> macAddresses) {
4957 // We need to be sure that the same mac addresses are always shown in
5058 // the same order, to generate a valid hash
51
- if (macAddresses != null) {
52
- Collections.<String> sort(macAddresses);
53
- }
59
+ if (macAddresses != null) {
60
+ Collections.<String> sort(macAddresses);
61
+ }
5462 this.macAddresses = macAddresses;
5563 }
5664
....@@ -71,13 +79,21 @@
7179 }
7280
7381 public boolean match(RequestBean rb) {
82
+ boolean result = true;
83
+ if (activationCode != null) {
84
+ result = result && activationCode.equals(rb.activationCode);
85
+ // appCode is optional
86
+ result = result && (appCode == null || appCode.equals(rb.appCode));
87
+ } else {
88
+ // If activationCode exists then we ignore other license identifiers
89
+ result = result && licenseTypeCode != null && licenseTypeCode.equals(rb.licenseTypeCode);
90
+ result = result && (customerCode != null && customerCode.equals(rb.customerCode));
91
+ result = result && (packCode != null && packCode.equals(rb.packCode));
92
+ }
7493
75
- boolean result = licenseTypeCode != null && licenseTypeCode.equals(rb.licenseTypeCode);
76
- result = result && (packCode != null && packCode.equals(rb.packCode));
7794 result = result && (arch != null && arch.equals(rb.arch));
7895 // CRC Logo validation is optional, only if request file contains it
7996 result = result && (crcLogo == null || crcLogo.equals(rb.crcLogo));
80
- result = result && (customerCode != null && customerCode.equals(rb.customerCode));
8197 result = result && (osName != null && osName.equals(rb.osName));
8298 result = result && (macAddresses != null && rb.macAddresses != null) && anyMacIsIncluded(rb.getMacAddresses());
8399
....@@ -95,6 +111,8 @@
95111 @Override
96112 public int hashCode() {
97113 int code = 0;
114
+ code += appCode != null ? appCode.hashCode() : 0;
115
+ code += activationCode != null ? activationCode.hashCode() : 0;
98116 code += licenseTypeCode != null ? licenseTypeCode.hashCode() : 0;
99117 code += arch != null ? arch.hashCode() : 0;
100118 code += crcLogo != null ? crcLogo.hashCode() : 0;
....@@ -111,14 +129,17 @@
111129 if (!(obj instanceof RequestBean))
112130 return false;
113131 RequestBean rb = (RequestBean) obj;
114
- boolean result = (rb.licenseTypeCode == null && licenseTypeCode == null)
115
- || (licenseTypeCode != null && licenseTypeCode.equals(rb.licenseTypeCode));
116
- result = result && ((rb.packCode == null && packCode == null) || (packCode != null && packCode.equals(rb.arch)));
117
- result = result && ((rb.arch == null && arch == null) || (arch != null && arch.equals(rb.arch)));
118
- result = result && ((rb.crcLogo == null && crcLogo == null) || (crcLogo != null && crcLogo.equals(rb.crcLogo)));
119
- result = result && ((rb.customerCode == null && customerCode == null) || (customerCode != null && customerCode.equals(rb.customerCode)));
120
- result = result && ((rb.osName == null && osName == null) || (osName != null && osName.equals(rb.osName)));
121
- result = result && ((rb.macAddresses == null && macAddresses == null) || (macAddresses != null && macAddresses.equals(rb.macAddresses)));
132
+
133
+ boolean result = true;
134
+ result = result && Objects.equals(appCode, rb.appCode);
135
+ result = result && Objects.equals(activationCode, rb.activationCode);
136
+ result = result && Objects.equals(licenseTypeCode, rb.licenseTypeCode);
137
+ result = result && Objects.equals(packCode, rb.packCode);
138
+ result = result && Objects.equals(arch, rb.arch);
139
+ result = result && Objects.equals(crcLogo, rb.crcLogo);
140
+ result = result && Objects.equals(customerCode, rb.customerCode);
141
+ result = result && Objects.equals(osName, rb.osName);
142
+ result = result && Objects.equals(macAddresses, rb.macAddresses);
122143 return result;
123144 }
124145
....@@ -138,13 +159,11 @@
138159 this.licenseTypeCode = licenseTypeCode;
139160 }
140161
141
- public static void main(String[] args) {
142
- String requestData = "{\n \"licenseTypeCode\": \"CI01\",\n \"packCode\": \"CIBP01\",\n \"arch\": \"amd64\",\n \"osName\": \"Windows Server 2012\",\n \"customerCode\": \"BP01\",\n \"macAddresses\": [\n \"02-71-1A-0E-5D-7C\"\n ],\n \"crcLogo\": \"153e09683eaa26d23c951f6605af5f874b533a917f73681e3e0a1f175aae3981\"\n}";
143
- try {
144
- JsonUtils.json2object(requestData, RequestBean.class);
145
- } catch (SeCurisException e) {
146
- // TODO Auto-generated catch block
147
- e.printStackTrace();
148
- }
162
+ public String getAppCode() {
163
+ return appCode;
164
+ }
165
+
166
+ public void setAppCode(String appCode) {
167
+ this.appCode = appCode;
149168 }
150169 }
src/main/java/net/curisit/securis/utils/JsonUtils.java
....@@ -135,6 +135,26 @@
135135 }
136136
137137 /**
138
+ * Create a JSON string from a object compatible or annotated with Jackson,
139
+ * i.e: <code>
140
+ * {"f1":2345,"f2":"Test de valor"}
141
+ *
142
+ * @param obj
143
+ * @return JSON string representation from object
144
+ */
145
+ public static String toPrettyJSON(Object obj, Class<?> klass) throws SeCurisException {
146
+ // and could also do other configuration...
147
+ try {
148
+ if (obj == null)
149
+ return null;
150
+ return MAPPER_PRETTY.writerWithType(klass).writeValueAsString(obj);
151
+ } catch (IOException e) {
152
+ LOG.error("Error formating JSON from object: {}", obj, e);
153
+ throw new SeCurisException("Error formating JSON from object: " + obj, e);
154
+ }
155
+ }
156
+
157
+ /**
138158 * Create a Map from a json string, i.e: <code>
139159 * {"f1":2345,"f2":"Test de valor"}
140160 * </code>