Roberto Sánchez
2014-02-24 ef282bd185482f2cab9648a8293aa74db85d787f
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
package net.curisit.securis;
import java.io.File;
import java.io.IOException;
import net.curisit.securis.beans.LicenseBean;
import net.curisit.securis.beans.RequestBean;
import net.curisit.securis.utils.JsonUtils;
import net.curisit.securis.utils.Params;
import org.apache.commons.io.FileUtils;
/**
 * Manage all licenses tasks, just like, validation, sync, requesting, ...
 * 
 * @author roberto <roberto.sanchez@curisit.net>
 */
public class LicenseManager {
   private static LicenseManager singleton = new LicenseManager();
   private LicenseManager() {
   }
   public static LicenseManager getInstance() {
       return singleton;
   }
   /**
    * 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;
       try {
           licBean = JsonUtils.json2object(FileUtils.readFileToString(licFile), LicenseBean.class);
       } catch (IOException e) {
           throw new SeCurisException("Error getting license data from file: " + licFile, e);
       }
       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;
   }
   /**
    * 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 sync(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;
   }
}