Roberto Sánchez
2014-02-25 bc372a688bf2062ba1e7e2ef2f9e69123d7873db
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package net.curisit.securis;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import net.curisit.securis.beans.LicenseBean;
import net.curisit.securis.beans.RequestBean;
import net.curisit.securis.beans.SignedLicenseBean;
import net.curisit.securis.utils.JsonUtils;
import net.curisit.securis.utils.Params;
import net.curisit.securis.utils.SignatureHelper;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
 * Manage all licenses tasks, just like validation, renew, requesting, ...
 * 
 * @author roberto <roberto.sanchez@curisit.net>
 */
public class LicenseManager {
   private static final Logger log = LogManager.getLogger(License.class);
   private static LicenseManager singleton = new LicenseManager();
   private LicenseManager() {
   }
   public static LicenseManager getInstance() {
       return singleton;
   }
   /**
    * Loads a license from file
    * 
    * @param licFile
    * @return The license bean
    * @throws SeCurisException
    */
   public LicenseBean load(File licFile) throws SeCurisException {
       LicenseBean licBean;
       try {
           licBean = JsonUtils.json2object(FileUtils.readFileToString(licFile), LicenseBean.class);
       } catch (IOException e) {
           throw new SeCurisException("Error getting license data from file: " + licFile, e);
       }
       return licBean;
   }
   /**
    * Validates the license stored in {@code licFile} and get the corresponding LicenseBean
    * <p>
    * The validation includes:
    * <ul>
    * <li>Signature</li>
    * <li>HW data</li>
    * <li>Logo CRC</li>
    * </ul>
    * </p>
    * 
    * @param licFile
    * @return The license bean stored in file
    * @throws SeCurisException
    */
   public LicenseBean validateLicense(File licFile) throws SeCurisException {
       LicenseBean licBean = load(licFile);
       SignatureHelper.getInstance().validateSignature(licBean);
       LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE));
       LicenseValidator.getInstance().validateLogo(licBean);
       return licBean;
   }
   /**
    * Request to server for a valid license
    * 
    * @return The license bean returned by the server
    * @throws SeCurisException
    */
   public LicenseBean requestLicense() throws SeCurisException {
       RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE));
       if (true)
           throw new SeCurisException("Action not implemented yet");
       LicenseBean lic = requestLicenseToServer(req);
       return lic;
   }
   /**
    * Generate a license file using a {@link LicenseBean}
    * 
    * @param license
    * @param file
    * @throws SeCurisException
    */
   public void save(LicenseBean license, File file) throws SeCurisException {
       SignedLicenseBean signedLic = new SignedLicenseBean(license);
       byte[] json;
       try {
           json = JsonUtils.toJSON(signedLic, true).getBytes("utf-8");
           Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
       } catch (UnsupportedEncodingException e) {
           log.error("Error creating json doc from license: " + license, e);
           throw new SeCurisException("Error creating json doc from license: " + license, e);
       } catch (IOException e) {
           log.error("Error creating license file: " + file, e);
           throw new SeCurisException("Error creating json doc from license: " + license, e);
       }
       log.debug("License saved in {}", file);
   }
   private LicenseBean requestLicenseToServer(RequestBean req) {
       // TODO Prepare call to server sending the request bean to get a valid license
       return null;
   }
   /**
    * Creates a new request file with current hardware in the File passed as parameter
    * 
    * @param outputRequestFile
    *            File where the request data will be saved
    * @return The generated request bean
    * @throws SeCurisException
    */
   public RequestBean createRequestFile(File outputRequestFile) throws SeCurisException {
       RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE));
       ReqGenerator.getInstance().save(req, outputRequestFile);
       return req;
   }
   /**
    * Send the current license file to server, which is previously validated, to get a renewed one if it is prepared in server side.
    * 
    * @param licenseFile
    *            Current and valid License file
    * @return New license bean if server creates a new one, otherwise the same current License bean will be returned
    * @throws SeCurisException
    */
   public LicenseBean renew(File licenseFile) throws SeCurisException {
       LicenseBean lic = validateLicense(licenseFile);
       if (true)
           throw new SeCurisException("Action not implemented yet");
       // TODO: Send the current LicenseBean to server to check if a new one is prepared.
       return lic;
   }
}