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 */ 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 *

* The validation includes: *

*

* * @param licFile * @param appCode * @param customerCode * @return * @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 paramter * * @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); // TODO: Send the current LicenseBean to server to check if a new one is prepared. return lic; } }