Roberto Sánchez
2014-02-25 bc372a688bf2062ba1e7e2ef2f9e69123d7873db
src/main/java/net/curisit/securis/LicenseManager.java
....@@ -2,20 +2,30 @@
22
33 import java.io.File;
44 import java.io.IOException;
5
+import java.io.UnsupportedEncodingException;
6
+import java.nio.file.Files;
7
+import java.nio.file.Paths;
8
+import java.nio.file.StandardOpenOption;
59
610 import net.curisit.securis.beans.LicenseBean;
711 import net.curisit.securis.beans.RequestBean;
12
+import net.curisit.securis.beans.SignedLicenseBean;
813 import net.curisit.securis.utils.JsonUtils;
914 import net.curisit.securis.utils.Params;
15
+import net.curisit.securis.utils.SignatureHelper;
1016
1117 import org.apache.commons.io.FileUtils;
18
+import org.apache.logging.log4j.LogManager;
19
+import org.apache.logging.log4j.Logger;
1220
1321 /**
14
- * Manage all licenses tasks, just like, validation, sync, requesting, ...
22
+ * Manage all licenses tasks, just like validation, renew, requesting, ...
1523 *
1624 * @author roberto <roberto.sanchez@curisit.net>
1725 */
1826 public class LicenseManager {
27
+
28
+ private static final Logger log = LogManager.getLogger(License.class);
1929
2030 private static LicenseManager singleton = new LicenseManager();
2131
....@@ -24,6 +34,23 @@
2434
2535 public static LicenseManager getInstance() {
2636 return singleton;
37
+ }
38
+
39
+ /**
40
+ * Loads a license from file
41
+ *
42
+ * @param licFile
43
+ * @return The license bean
44
+ * @throws SeCurisException
45
+ */
46
+ public LicenseBean load(File licFile) throws SeCurisException {
47
+ LicenseBean licBean;
48
+ try {
49
+ licBean = JsonUtils.json2object(FileUtils.readFileToString(licFile), LicenseBean.class);
50
+ } catch (IOException e) {
51
+ throw new SeCurisException("Error getting license data from file: " + licFile, e);
52
+ }
53
+ return licBean;
2754 }
2855
2956 /**
....@@ -42,17 +69,56 @@
4269 * @throws SeCurisException
4370 */
4471 public LicenseBean validateLicense(File licFile) throws SeCurisException {
45
- LicenseBean licBean;
46
- try {
47
- licBean = JsonUtils.json2object(FileUtils.readFileToString(licFile), LicenseBean.class);
48
- } catch (IOException e) {
49
- throw new SeCurisException("Error getting license data from file: " + licFile, e);
50
- }
72
+ LicenseBean licBean = load(licFile);
5173 SignatureHelper.getInstance().validateSignature(licBean);
5274 LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE));
5375 LicenseValidator.getInstance().validateLogo(licBean);
5476
5577 return licBean;
78
+ }
79
+
80
+ /**
81
+ * Request to server for a valid license
82
+ *
83
+ * @return The license bean returned by the server
84
+ * @throws SeCurisException
85
+ */
86
+ public LicenseBean requestLicense() throws SeCurisException {
87
+ RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE));
88
+ if (true)
89
+ throw new SeCurisException("Action not implemented yet");
90
+ LicenseBean lic = requestLicenseToServer(req);
91
+ return lic;
92
+ }
93
+
94
+ /**
95
+ * Generate a license file using a {@link LicenseBean}
96
+ *
97
+ * @param license
98
+ * @param file
99
+ * @throws SeCurisException
100
+ */
101
+ public void save(LicenseBean license, File file) throws SeCurisException {
102
+ SignedLicenseBean signedLic = new SignedLicenseBean(license);
103
+ byte[] json;
104
+ try {
105
+ json = JsonUtils.toJSON(signedLic, true).getBytes("utf-8");
106
+ Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
107
+ } catch (UnsupportedEncodingException e) {
108
+ log.error("Error creating json doc from license: " + license, e);
109
+ throw new SeCurisException("Error creating json doc from license: " + license, e);
110
+ } catch (IOException e) {
111
+ log.error("Error creating license file: " + file, e);
112
+ throw new SeCurisException("Error creating json doc from license: " + license, e);
113
+ }
114
+
115
+ log.debug("License saved in {}", file);
116
+
117
+ }
118
+
119
+ private LicenseBean requestLicenseToServer(RequestBean req) {
120
+ // TODO Prepare call to server sending the request bean to get a valid license
121
+ return null;
56122 }
57123
58124 /**
....@@ -79,7 +145,7 @@
79145 * @return New license bean if server creates a new one, otherwise the same current License bean will be returned
80146 * @throws SeCurisException
81147 */
82
- public LicenseBean sync(File licenseFile) throws SeCurisException {
148
+ public LicenseBean renew(File licenseFile) throws SeCurisException {
83149 LicenseBean lic = validateLicense(licenseFile);
84150 if (true)
85151 throw new SeCurisException("Action not implemented yet");