From f7be2173201d6ef2d559ef4e8fdfef5534eee29e Mon Sep 17 00:00:00 2001
From: Roberto Sánchez <roberto.sanchez@curisit.net>
Date: Mon, 24 Feb 2014 10:10:05 +0000
Subject: [PATCH] #593 feature - Added basic functionality (without server sync) to validate licenses and create request files

---
 src/main/java/net/curisit/securis/LicenseManager.java |   57 ++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 50 insertions(+), 7 deletions(-)

diff --git a/src/main/java/net/curisit/securis/LicenseManager.java b/src/main/java/net/curisit/securis/LicenseManager.java
index 6d74838..7aa0ac3 100644
--- a/src/main/java/net/curisit/securis/LicenseManager.java
+++ b/src/main/java/net/curisit/securis/LicenseManager.java
@@ -6,6 +6,7 @@
 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;
 
@@ -25,23 +26,65 @@
 		return singleton;
 	}
 
-	public LicenseBean validateLicense(File licFile, String appCode, String customerCode) throws SeCurisException {
+	/**
+	 * 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
+	 * @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 validating license", e);
+			throw new SeCurisException("Error getting license data from file: " + licFile, e);
 		}
 		SignatureHelper.getInstance().validateSignature(licBean);
-		validateHW(licBean, appCode, customerCode);
+		LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE));
+		LicenseValidator.getInstance().validateLogo(licBean);
 
 		return licBean;
 	}
 
-	private void validateHW(RequestBean reqBean, String appCode, String customerCode) throws SeCurisException {
-		RequestBean currentHW = ReqGenerator.getInstance().createRequest(appCode, customerCode);
-		if (!currentHW.match(reqBean))
-			throw new SeCurisException("Current System info mismatch the License System info: " + JsonUtils.toJSON(reqBean));
+	/**
+	 * 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;
 	}
 
 }

--
Gitblit v1.3.2