From bc372a688bf2062ba1e7e2ef2f9e69123d7873db Mon Sep 17 00:00:00 2001
From: Roberto Sánchez <roberto.sanchez@curisit.net>
Date: Tue, 25 Feb 2014 10:11:57 +0000
Subject: [PATCH] #593 doc - Added HOW-TO doc

---
 src/main/java/net/curisit/securis/LicenseManager.java |   82 +++++++++++++++++++++++++++++++++++++----
 1 files changed, 74 insertions(+), 8 deletions(-)

diff --git a/src/main/java/net/curisit/securis/LicenseManager.java b/src/main/java/net/curisit/securis/LicenseManager.java
index d3e7f16..83571f0 100644
--- a/src/main/java/net/curisit/securis/LicenseManager.java
+++ b/src/main/java/net/curisit/securis/LicenseManager.java
@@ -2,20 +2,30 @@
 
 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, sync, requesting, ...
+ * 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();
 
@@ -24,6 +34,23 @@
 
 	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;
 	}
 
 	/**
@@ -42,17 +69,56 @@
 	 * @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);
-		}
+		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;
 	}
 
 	/**
@@ -79,7 +145,7 @@
 	 * @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 {
+	public LicenseBean renew(File licenseFile) throws SeCurisException {
 		LicenseBean lic = validateLicense(licenseFile);
 		if (true)
 			throw new SeCurisException("Action not implemented yet");

--
Gitblit v1.3.2