| .. | .. |
|---|
| 2 | 2 | |
|---|
| 3 | 3 | import java.io.File; |
|---|
| 4 | 4 | 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; |
|---|
| 5 | 9 | |
|---|
| 6 | 10 | import net.curisit.securis.beans.LicenseBean; |
|---|
| 7 | 11 | import net.curisit.securis.beans.RequestBean; |
|---|
| 12 | +import net.curisit.securis.beans.SignedLicenseBean; |
|---|
| 8 | 13 | import net.curisit.securis.utils.JsonUtils; |
|---|
| 9 | 14 | import net.curisit.securis.utils.Params; |
|---|
| 15 | +import net.curisit.securis.utils.SignatureHelper; |
|---|
| 10 | 16 | |
|---|
| 11 | 17 | import org.apache.commons.io.FileUtils; |
|---|
| 18 | +import org.apache.logging.log4j.LogManager; |
|---|
| 19 | +import org.apache.logging.log4j.Logger; |
|---|
| 12 | 20 | |
|---|
| 13 | 21 | /** |
|---|
| 14 | | - * Manage all licenses tasks, just like, validation, sync, requesting, ... |
|---|
| 22 | + * Manage all licenses tasks, just like validation, renew, requesting, ... |
|---|
| 15 | 23 | * |
|---|
| 16 | 24 | * @author roberto <roberto.sanchez@curisit.net> |
|---|
| 17 | 25 | */ |
|---|
| 18 | 26 | public class LicenseManager { |
|---|
| 27 | + |
|---|
| 28 | + private static final Logger log = LogManager.getLogger(License.class); |
|---|
| 19 | 29 | |
|---|
| 20 | 30 | private static LicenseManager singleton = new LicenseManager(); |
|---|
| 21 | 31 | |
|---|
| .. | .. |
|---|
| 24 | 34 | |
|---|
| 25 | 35 | public static LicenseManager getInstance() { |
|---|
| 26 | 36 | 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; |
|---|
| 27 | 54 | } |
|---|
| 28 | 55 | |
|---|
| 29 | 56 | /** |
|---|
| .. | .. |
|---|
| 42 | 69 | * @throws SeCurisException |
|---|
| 43 | 70 | */ |
|---|
| 44 | 71 | 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); |
|---|
| 51 | 73 | SignatureHelper.getInstance().validateSignature(licBean); |
|---|
| 52 | 74 | LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE)); |
|---|
| 53 | 75 | LicenseValidator.getInstance().validateLogo(licBean); |
|---|
| 54 | 76 | |
|---|
| 55 | 77 | 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; |
|---|
| 56 | 122 | } |
|---|
| 57 | 123 | |
|---|
| 58 | 124 | /** |
|---|
| .. | .. |
|---|
| 79 | 145 | * @return New license bean if server creates a new one, otherwise the same current License bean will be returned |
|---|
| 80 | 146 | * @throws SeCurisException |
|---|
| 81 | 147 | */ |
|---|
| 82 | | - public LicenseBean sync(File licenseFile) throws SeCurisException { |
|---|
| 148 | + public LicenseBean renew(File licenseFile) throws SeCurisException { |
|---|
| 83 | 149 | LicenseBean lic = validateLicense(licenseFile); |
|---|
| 84 | 150 | if (true) |
|---|
| 85 | 151 | throw new SeCurisException("Action not implemented yet"); |
|---|