rsanchez
2014-12-11 5ff7160871b0ea6cbcc8ead0f0fc40cd15349bae
src/main/java/net/curisit/securis/LicenseManager.java
....@@ -28,195 +28,207 @@
2828 */
2929 public class LicenseManager {
3030
31
- private static final Logger LOG = LogManager.getLogger(License.class);
31
+ private static final Logger LOG = LogManager.getLogger(License.class);
3232
33
- private static LicenseManager singleton = new LicenseManager();
33
+ private static LicenseManager singleton = new LicenseManager();
3434
35
- public static final String PING_MESSAGE = "SeCuris API OK";
35
+ public static final String PING_MESSAGE = "SeCuris API OK";
3636
37
- private LicenseManager() {
38
- }
37
+ private LicenseManager() {}
3938
40
- public static LicenseManager getInstance() {
41
- return singleton;
42
- }
39
+ public static LicenseManager getInstance() {
40
+ return singleton;
41
+ }
4342
44
- /**
45
- * Loads a license from file
46
- *
47
- * @param licFile
48
- * @return The license bean
49
- * @throws SeCurisException
50
- */
51
- public LicenseBean load(File licFile) throws SeCurisException {
52
- LicenseBean licBean;
53
- try {
54
- licBean = JsonUtils.json2object(FileUtils.readFileToString(licFile), LicenseBean.class);
55
- } catch (IOException e) {
56
- throw new SeCurisException("Error getting license data from file: " + licFile, e);
57
- }
58
- return licBean;
59
- }
43
+ /**
44
+ * Loads a license from file
45
+ *
46
+ * @param licFile
47
+ * @return The license bean
48
+ * @throws SeCurisException
49
+ */
50
+ public LicenseBean load(File licFile) throws SeCurisException {
51
+ LicenseBean licBean;
52
+ try {
53
+ licBean = JsonUtils.json2object(FileUtils.readFileToString(licFile), LicenseBean.class);
54
+ } catch (IOException e) {
55
+ throw new SeCurisException("Error getting license data from file: " + licFile, e);
56
+ }
57
+ return licBean;
58
+ }
6059
61
- /**
62
- * Validates the license stored in {@code licFile} and get the corresponding
63
- * LicenseBean
64
- * <p>
65
- * The validation includes:
66
- * <ul>
67
- * <li>Signature</li>
68
- * <li>HW data</li>
69
- * <li>Logo CRC</li>
70
- * </ul>
71
- * </p>
72
- *
73
- * @param licFile
74
- * @return The license bean stored in file
75
- * @throws SeCurisException
76
- */
77
- public LicenseBean validateLicense(File licFile) throws SeCurisException {
60
+ /**
61
+ * Validates the license stored in {@code licFile} and get the corresponding
62
+ * LicenseBean
63
+ * <p>
64
+ * The validation includes:
65
+ * <ul>
66
+ * <li>Signature</li>
67
+ * <li>HW data</li>
68
+ * <li>Logo CRC</li>
69
+ * </ul>
70
+ * </p>
71
+ *
72
+ * @param licFile
73
+ * @return The license bean stored in file
74
+ * @throws SeCurisException
75
+ */
76
+ public LicenseBean validateLicense(File licFile) throws SeCurisException {
7877
79
- return validateLicense(licFile, false);
80
- }
78
+ return validateLicense(licFile, false);
79
+ }
8180
82
- /**
83
- * Validates the license stored in {@code licFile} and get the corresponding
84
- * LicenseBean. The License date is not validated
85
- * <p>
86
- * The validation includes:
87
- * <ul>
88
- * <li>Signature</li>
89
- * <li>HW data</li>
90
- * <li>Logo CRC</li>
91
- * </ul>
92
- * </p>
93
- *
94
- * @param licFile
95
- * @return The license bean stored in file
96
- * @throws SeCurisException
97
- */
98
- public LicenseBean validateLicense(File licFile, boolean excludeDateValidation) throws SeCurisException {
99
- LicenseBean licBean = load(licFile);
100
- SignatureHelper.getInstance().validateSignature(licBean);
101
- LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),
102
- Params.get(Params.KEYS.PACK_CODE));
103
- LicenseValidator.getInstance().validateLogo(licBean);
81
+ /**
82
+ * Validates the license stored in {@code licFile} and get the corresponding
83
+ * LicenseBean. The License date is not validated
84
+ * <p>
85
+ * The validation includes:
86
+ * <ul>
87
+ * <li>Signature</li>
88
+ * <li>HW data</li>
89
+ * <li>Logo CRC</li>
90
+ * </ul>
91
+ * </p>
92
+ *
93
+ * @param licFile
94
+ * @return The license bean stored in file
95
+ * @throws SeCurisException
96
+ */
97
+ public LicenseBean validateLicense(File licFile, boolean excludeDateValidation) throws SeCurisException {
98
+ LicenseBean licBean = load(licFile);
99
+ SignatureHelper.getInstance().validateSignature(licBean);
100
+ LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),
101
+ Params.get(Params.KEYS.PACK_CODE));
102
+ LicenseValidator.getInstance().validateLogo(licBean);
104103
105
- if (!excludeDateValidation) {
106
- if (new Date().after(licBean.getExpirationDate())) {
107
- throw new SeCurisException("License has expired");
108
- }
109
- }
104
+ if (!excludeDateValidation) {
105
+ if (new Date().after(licBean.getExpirationDate())) {
106
+ throw new ExpiredLicenseException();
107
+ }
108
+ }
110109
111
- return licBean;
112
- }
110
+ return licBean;
111
+ }
113112
114
- /**
115
- * Request to server for a valid license
116
- *
117
- * @return The license bean returned by the server
118
- * @throws SeCurisException
119
- */
120
- public SignedLicenseBean requestLicense() throws SeCurisException {
121
- RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),
122
- Params.get(Params.KEYS.PACK_CODE));
113
+ /**
114
+ * Request to server for a valid license
115
+ *
116
+ * @return The license bean returned by the server
117
+ * @throws SeCurisException
118
+ */
119
+ public SignedLicenseBean requestLicense() throws SeCurisException {
120
+ RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),
121
+ Params.get(Params.KEYS.PACK_CODE));
123122
124
- SignedLicenseBean lic = requestLicenseToServer(req);
125
- return lic;
126
- }
123
+ SignedLicenseBean lic = requestLicenseToServer(req);
124
+ return lic;
125
+ }
127126
128
- /**
129
- * Generate a license file using a {@link LicenseBean}
130
- *
131
- * @param license
132
- * @param file
133
- * @throws SeCurisException
134
- */
135
- public void save(LicenseBean license, File file) throws SeCurisException {
136
- SignedLicenseBean signedLic = new SignedLicenseBean(license);
137
- save(signedLic, file);
138
- }
127
+ /**
128
+ * Generate a license file using a {@link LicenseBean}
129
+ *
130
+ * @param license
131
+ * @param file
132
+ * @throws SeCurisException
133
+ */
134
+ public void save(LicenseBean license, File file) throws SeCurisException {
135
+ SignedLicenseBean signedLic = new SignedLicenseBean(license);
136
+ save(signedLic, file);
137
+ }
139138
140
- /**
141
- * Generate a license file using a {@link LicenseBean}
142
- *
143
- * @param license
144
- * @param file
145
- * @throws SeCurisException
146
- */
147
- public void save(SignedLicenseBean signedLic, File file) throws SeCurisException {
148
- byte[] json;
149
- try {
150
- json = JsonUtils.toPrettyJSON(signedLic).getBytes("utf-8");
151
- Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
152
- } catch (UnsupportedEncodingException e) {
153
- LOG.error("Error creating json doc from license: " + signedLic, e);
154
- throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
155
- } catch (IOException e) {
156
- LOG.error("Error creating license file: " + file, e);
157
- throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
158
- }
139
+ /**
140
+ * Generate a license file using a {@link LicenseBean}
141
+ *
142
+ * @param license
143
+ * @param file
144
+ * @throws SeCurisException
145
+ */
146
+ public void save(SignedLicenseBean signedLic, File file) throws SeCurisException {
147
+ byte[] json;
148
+ try {
149
+ json = JsonUtils.toPrettyJSON(signedLic).getBytes("utf-8");
150
+ Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
151
+ } catch (UnsupportedEncodingException e) {
152
+ LOG.error("Error creating json doc from license: " + signedLic, e);
153
+ throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
154
+ } catch (IOException e) {
155
+ LOG.error("Error creating license file: " + file, e);
156
+ throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
157
+ }
159158
160
- LOG.debug("License saved in {}", file);
159
+ LOG.debug("License saved in {}", file);
161160
162
- }
161
+ }
163162
164
- private SignedLicenseBean requestLicenseToServer(RequestBean req) throws SeCurisException {
165
- SignedLicenseBean lic = ConnectionManager.getInstance().executePost(Command.CREATE_LIC, SignedLicenseBean.class, req);
163
+ private SignedLicenseBean requestLicenseToServer(RequestBean req) throws SeCurisException {
164
+ SignedLicenseBean lic = ConnectionManager.getInstance().executePost(Command.CREATE_LIC, SignedLicenseBean.class, req);
166165
167
- return lic;
168
- }
166
+ return lic;
167
+ }
169168
170
- /**
171
- * Creates a new request file with current hardware in the File passed as
172
- * parameter
173
- *
174
- * @param outputRequestFile
175
- * File where the request data will be saved
176
- * @return The generated request bean
177
- * @throws SeCurisException
178
- */
179
- public RequestBean createRequestFile(File outputRequestFile) throws SeCurisException {
180
- RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),
181
- Params.get(Params.KEYS.PACK_CODE));
169
+ /**
170
+ * Creates a new request file with current hardware in the File passed as
171
+ * parameter
172
+ *
173
+ * @param outputRequestFile
174
+ * File where the request data will be saved
175
+ * @return The generated request bean
176
+ * @throws SeCurisException
177
+ */
178
+ public RequestBean createRequestFile(File outputRequestFile) throws SeCurisException {
179
+ RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),
180
+ Params.get(Params.KEYS.PACK_CODE));
182181
183
- ReqGenerator.getInstance().save(req, outputRequestFile);
182
+ ReqGenerator.getInstance().save(req, outputRequestFile);
184183
185
- return req;
186
- }
184
+ return req;
185
+ }
187186
188
- /**
189
- * Send the current license file to server, which is previously validated,
190
- * to get a renewed one if it is prepared in server side.
191
- *
192
- * @param licenseFile
193
- * Current and valid License file
194
- * @return New license bean if server creates a new one, otherwise the same
195
- * current License bean will be returned
196
- * @throws SeCurisException
197
- */
198
- public SignedLicenseBean renew(File licenseFile) throws SeCurisException {
199
- LicenseBean lic = validateLicense(licenseFile);
187
+ /**
188
+ * Send the current license file to server, which is previously validated,
189
+ * to get a renewed one if it is prepared in server side.
190
+ *
191
+ * @param licenseFile
192
+ * Current and valid License file
193
+ * @return New license bean if server creates a new one, otherwise the same
194
+ * current License bean will be returned
195
+ * @throws SeCurisException
196
+ */
197
+ public SignedLicenseBean renew(File licenseFile) throws SeCurisException {
198
+ LicenseBean lic = validateLicense(licenseFile);
200199
201
- SignedLicenseBean newLic = ConnectionManager.getInstance().executePost(Command.RENEW_LIC, SignedLicenseBean.class, lic);
200
+ SignedLicenseBean newLic = ConnectionManager.getInstance().executePost(Command.RENEW_LIC, SignedLicenseBean.class, lic);
202201
203
- return newLic;
204
- }
202
+ return newLic;
203
+ }
205204
206
- public void testServer() throws SeCurisException {
207
- StatusBean status = ConnectionManager.getInstance().executeGet(Command.TEST, StatusBean.class);
208
- if (!PING_MESSAGE.equals(status.getMessage())) {
209
- throw new SeCurisException("SeCuris Server is not running in given URL");
210
- }
211
- }
205
+ /**
206
+ * Check on SeCuris server if current license is still valid in server DB.
207
+ *
208
+ * @param licenseFile
209
+ * @throws SeCurisException
210
+ */
211
+ public void assertLicenseIsValid(File licenseFile) throws SeCurisException, IOException {
212
+ LicenseBean lic = validateLicense(licenseFile);
212213
213
- public static void main(String[] args) throws SeCurisException {
214
- String filename = null;
215
- if (filename == null)
216
- filename = "./license.req";
217
- File file = new File(filename);
218
- LicenseManager.getInstance().createRequestFile(file);
219
- LOG.info("Request file {} generated OK", file.getAbsolutePath());
214
+ ConnectionManager.getInstance().executePost(Command.VALIDATE, LicenseBean.class, lic);
220215
221
- }
216
+ }
217
+
218
+ public void testServer() throws SeCurisException {
219
+ StatusBean status = ConnectionManager.getInstance().executeGet(Command.TEST, StatusBean.class);
220
+ if (!PING_MESSAGE.equals(status.getMessage())) {
221
+ throw new SeCurisException("SeCuris Server is not running in given URL");
222
+ }
223
+ }
224
+
225
+ public static void main(String[] args) throws SeCurisException {
226
+ String filename = null;
227
+ if (filename == null)
228
+ filename = "./license.req";
229
+ File file = new File(filename);
230
+ LicenseManager.getInstance().createRequestFile(file);
231
+ LOG.info("Request file {} generated OK", file.getAbsolutePath());
232
+
233
+ }
222234 }