From 5ff7160871b0ea6cbcc8ead0f0fc40cd15349bae Mon Sep 17 00:00:00 2001
From: rsanchez <rsanchez@curisit.net>
Date: Thu, 11 Dec 2014 18:56:10 +0000
Subject: [PATCH] #2140 fix - Removed config file from jar and added a new exception for expired licenses

---
 src/main/java/net/curisit/securis/LicenseManager.java |  340 +++++++++++++++++++++++++++++---------------------------
 1 files changed, 176 insertions(+), 164 deletions(-)

diff --git a/src/main/java/net/curisit/securis/LicenseManager.java b/src/main/java/net/curisit/securis/LicenseManager.java
index 46365df..350cd38 100644
--- a/src/main/java/net/curisit/securis/LicenseManager.java
+++ b/src/main/java/net/curisit/securis/LicenseManager.java
@@ -28,195 +28,207 @@
  */
 public class LicenseManager {
 
-    private static final Logger LOG = LogManager.getLogger(License.class);
+	private static final Logger LOG = LogManager.getLogger(License.class);
 
-    private static LicenseManager singleton = new LicenseManager();
+	private static LicenseManager singleton = new LicenseManager();
 
-    public static final String PING_MESSAGE = "SeCuris API OK";
+	public static final String PING_MESSAGE = "SeCuris API OK";
 
-    private LicenseManager() {
-    }
+	private LicenseManager() {}
 
-    public static LicenseManager getInstance() {
-        return singleton;
-    }
+	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;
-    }
+	/**
+	 * 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;
+	}
 
-    /**
-     * 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 {
+	/**
+	 * 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 {
 
-        return validateLicense(licFile, false);
-    }
+		return validateLicense(licFile, false);
+	}
 
-    /**
-     * Validates the license stored in {@code licFile} and get the corresponding
-     * LicenseBean. The License date is not validated
-     * <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, boolean excludeDateValidation) throws SeCurisException {
-        LicenseBean licBean = load(licFile);
-        SignatureHelper.getInstance().validateSignature(licBean);
-        LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),
-                Params.get(Params.KEYS.PACK_CODE));
-        LicenseValidator.getInstance().validateLogo(licBean);
+	/**
+	 * Validates the license stored in {@code licFile} and get the corresponding
+	 * LicenseBean. The License date is not validated
+	 * <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, boolean excludeDateValidation) throws SeCurisException {
+		LicenseBean licBean = load(licFile);
+		SignatureHelper.getInstance().validateSignature(licBean);
+		LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),
+				Params.get(Params.KEYS.PACK_CODE));
+		LicenseValidator.getInstance().validateLogo(licBean);
 
-        if (!excludeDateValidation) {
-            if (new Date().after(licBean.getExpirationDate())) {
-                throw new SeCurisException("License has expired");
-            }
-        }
+		if (!excludeDateValidation) {
+			if (new Date().after(licBean.getExpirationDate())) {
+				throw new ExpiredLicenseException();
+			}
+		}
 
-        return licBean;
-    }
+		return licBean;
+	}
 
-    /**
-     * Request to server for a valid license
-     * 
-     * @return The license bean returned by the server
-     * @throws SeCurisException
-     */
-    public SignedLicenseBean requestLicense() throws SeCurisException {
-        RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),
-                Params.get(Params.KEYS.PACK_CODE));
+	/**
+	 * Request to server for a valid license
+	 * 
+	 * @return The license bean returned by the server
+	 * @throws SeCurisException
+	 */
+	public SignedLicenseBean requestLicense() throws SeCurisException {
+		RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),
+				Params.get(Params.KEYS.PACK_CODE));
 
-        SignedLicenseBean lic = requestLicenseToServer(req);
-        return lic;
-    }
+		SignedLicenseBean 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);
-        save(signedLic, file);
-    }
+	/**
+	 * 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);
+		save(signedLic, file);
+	}
 
-    /**
-     * Generate a license file using a {@link LicenseBean}
-     * 
-     * @param license
-     * @param file
-     * @throws SeCurisException
-     */
-    public void save(SignedLicenseBean signedLic, File file) throws SeCurisException {
-        byte[] json;
-        try {
-            json = JsonUtils.toPrettyJSON(signedLic).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: " + signedLic, e);
-            throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
-        } catch (IOException e) {
-            LOG.error("Error creating license file: " + file, e);
-            throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
-        }
+	/**
+	 * Generate a license file using a {@link LicenseBean}
+	 * 
+	 * @param license
+	 * @param file
+	 * @throws SeCurisException
+	 */
+	public void save(SignedLicenseBean signedLic, File file) throws SeCurisException {
+		byte[] json;
+		try {
+			json = JsonUtils.toPrettyJSON(signedLic).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: " + signedLic, e);
+			throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
+		} catch (IOException e) {
+			LOG.error("Error creating license file: " + file, e);
+			throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
+		}
 
-        LOG.debug("License saved in {}", file);
+		LOG.debug("License saved in {}", file);
 
-    }
+	}
 
-    private SignedLicenseBean requestLicenseToServer(RequestBean req) throws SeCurisException {
-        SignedLicenseBean lic = ConnectionManager.getInstance().executePost(Command.CREATE_LIC, SignedLicenseBean.class, req);
+	private SignedLicenseBean requestLicenseToServer(RequestBean req) throws SeCurisException {
+		SignedLicenseBean lic = ConnectionManager.getInstance().executePost(Command.CREATE_LIC, SignedLicenseBean.class, req);
 
-        return lic;
-    }
+		return lic;
+	}
 
-    /**
-     * 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),
-                Params.get(Params.KEYS.PACK_CODE));
+	/**
+	 * 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),
+				Params.get(Params.KEYS.PACK_CODE));
 
-        ReqGenerator.getInstance().save(req, outputRequestFile);
+		ReqGenerator.getInstance().save(req, outputRequestFile);
 
-        return req;
-    }
+		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 SignedLicenseBean renew(File licenseFile) throws SeCurisException {
-        LicenseBean lic = validateLicense(licenseFile);
+	/**
+	 * 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 SignedLicenseBean renew(File licenseFile) throws SeCurisException {
+		LicenseBean lic = validateLicense(licenseFile);
 
-        SignedLicenseBean newLic = ConnectionManager.getInstance().executePost(Command.RENEW_LIC, SignedLicenseBean.class, lic);
+		SignedLicenseBean newLic = ConnectionManager.getInstance().executePost(Command.RENEW_LIC, SignedLicenseBean.class, lic);
 
-        return newLic;
-    }
+		return newLic;
+	}
 
-    public void testServer() throws SeCurisException {
-        StatusBean status = ConnectionManager.getInstance().executeGet(Command.TEST, StatusBean.class);
-        if (!PING_MESSAGE.equals(status.getMessage())) {
-            throw new SeCurisException("SeCuris Server is not running in given URL");
-        }
-    }
+	/**
+	 * Check on SeCuris server if current license is still valid in server DB.
+	 * 
+	 * @param licenseFile
+	 * @throws SeCurisException
+	 */
+	public void assertLicenseIsValid(File licenseFile) throws SeCurisException, IOException {
+		LicenseBean lic = validateLicense(licenseFile);
 
-    public static void main(String[] args) throws SeCurisException {
-        String filename = null;
-        if (filename == null)
-            filename = "./license.req";
-        File file = new File(filename);
-        LicenseManager.getInstance().createRequestFile(file);
-        LOG.info("Request file {} generated OK", file.getAbsolutePath());
+		ConnectionManager.getInstance().executePost(Command.VALIDATE, LicenseBean.class, lic);
 
-    }
+	}
+
+	public void testServer() throws SeCurisException {
+		StatusBean status = ConnectionManager.getInstance().executeGet(Command.TEST, StatusBean.class);
+		if (!PING_MESSAGE.equals(status.getMessage())) {
+			throw new SeCurisException("SeCuris Server is not running in given URL");
+		}
+	}
+
+	public static void main(String[] args) throws SeCurisException {
+		String filename = null;
+		if (filename == null)
+			filename = "./license.req";
+		File file = new File(filename);
+		LicenseManager.getInstance().createRequestFile(file);
+		LOG.info("Request file {} generated OK", file.getAbsolutePath());
+
+	}
 }

--
Gitblit v1.3.2