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
---
/dev/null | 4
src/main/java/net/curisit/securis/ConnectionManager.java | 197 +++++++++--------
src/main/java/net/curisit/securis/SeCurisException.java | 8
src/main/java/net/curisit/securis/utils/LicUtils.java | 46 +++
src/main/java/net/curisit/securis/LicenseManager.java | 340 ++++++++++++++++--------------
src/main/java/net/curisit/securis/ExpiredLicenseException.java | 19 +
pom.xml | 2
7 files changed, 336 insertions(+), 280 deletions(-)
diff --git a/pom.xml b/pom.xml
index 58b7550..da9e8a0 100644
--- a/pom.xml
+++ b/pom.xml
@@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.curisit</groupId>
<artifactId>securis-client</artifactId>
- <version>1.0.2-SNAPSHOT</version>
+ <version>1.0.3-SNAPSHOT</version>
<build>
<plugins>
<plugin>
diff --git a/src/main/java/net/curisit/securis/ConnectionManager.java b/src/main/java/net/curisit/securis/ConnectionManager.java
index 56c7ce9..c8140f1 100644
--- a/src/main/java/net/curisit/securis/ConnectionManager.java
+++ b/src/main/java/net/curisit/securis/ConnectionManager.java
@@ -33,121 +33,122 @@
*/
public class ConnectionManager {
- private static final Logger LOG = LogManager.getLogger(ConnectionManager.class);
- private static final int HTTP_STATUS_APP_ERRROR = 418;
- private static final String JSON_MEDIA_TYPE = "application/json";
- private static final String ERROR_MESSAGE_HEADER = "X-SECURIS-ERROR-MSG";
- private static final String ERROR_CODE_MESSAGE_HEADER = "X-SECURIS-ERROR-CODE";
+ private static final Logger LOG = LogManager.getLogger(ConnectionManager.class);
+ private static final int HTTP_STATUS_APP_ERRROR = 418;
+ private static final String JSON_MEDIA_TYPE = "application/json";
+ private static final String ERROR_MESSAGE_HEADER = "X-SECURIS-ERROR-MSG";
+ private static final String ERROR_CODE_MESSAGE_HEADER = "X-SECURIS-ERROR-CODE";
- private static ConnectionManager singleton;
+ private static ConnectionManager singleton;
- private final String serverUrl;
- private final CloseableHttpClient httpClient;
+ private final String serverUrl;
+ private final CloseableHttpClient httpClient;
- private ConnectionManager() throws SeCurisException {
- String aux = Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL);
- if (aux.endsWith("/")) {
- serverUrl = aux.substring(0, aux.length() - 2);
- } else {
- serverUrl = aux;
- }
- httpClient = createHttpClient();
- }
+ private ConnectionManager() throws SeCurisException {
+ String aux = Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL);
+ if (aux.endsWith("/")) {
+ serverUrl = aux.substring(0, aux.length() - 2);
+ } else {
+ serverUrl = aux;
+ }
+ httpClient = createHttpClient();
+ }
- private CloseableHttpClient createHttpClient() throws SeCurisException {
- SSLContextBuilder builder = new SSLContextBuilder();
- SSLConnectionSocketFactory sslsf = null;
- try {
- builder.loadTrustMaterial((KeyStore) null, new TrustStrategy() {
- @Override
- public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
- return true;
- }
- });
- sslsf = new SSLConnectionSocketFactory(builder.build());
- } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e1) {
- LOG.error(e1);
- throw new SeCurisException("Error creating SSL socket factory");
- }
- return HttpClientBuilder.create().setSSLSocketFactory(sslsf).build();
- }
+ private CloseableHttpClient createHttpClient() throws SeCurisException {
+ SSLContextBuilder builder = new SSLContextBuilder();
+ SSLConnectionSocketFactory sslsf = null;
+ try {
+ builder.loadTrustMaterial((KeyStore) null, new TrustStrategy() {
+ @Override
+ public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
+ return true;
+ }
+ });
+ sslsf = new SSLConnectionSocketFactory(builder.build());
+ } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e1) {
+ LOG.error(e1);
+ throw new SeCurisException("Error creating SSL socket factory");
+ }
+ return HttpClientBuilder.create().setSSLSocketFactory(sslsf).build();
+ }
- public synchronized static ConnectionManager getInstance() throws SeCurisException {
- if (singleton == null) {
- singleton = new ConnectionManager();
- }
- return singleton;
- }
+ public synchronized static ConnectionManager getInstance() throws SeCurisException {
+ if (singleton == null) {
+ singleton = new ConnectionManager();
+ }
+ return singleton;
+ }
- public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException {
- HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command));
- postRequest.addHeader("accept", JSON_MEDIA_TYPE);
+ public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException {
+ HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command));
+ postRequest.addHeader("accept", JSON_MEDIA_TYPE);
- postRequest.addHeader("content-type", JSON_MEDIA_TYPE);
- try {
- postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req)));
- } catch (UnsupportedEncodingException | SeCurisException e1) {
- throw new SeCurisException("Error preparing POST command", e1);
- }
- HttpResponse response;
- try {
- response = httpClient.execute(postRequest);
+ postRequest.addHeader("content-type", JSON_MEDIA_TYPE);
+ try {
+ postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req)));
+ } catch (UnsupportedEncodingException | SeCurisException e1) {
+ throw new SeCurisException("Error preparing POST command", e1);
+ }
+ HttpResponse response;
+ try {
+ response = httpClient.execute(postRequest);
- checkErrors(command, response);
+ checkErrors(command, response);
- String jsonLic = IOUtils.toString(response.getEntity().getContent());
- LOG.debug("Response content read OK: {}", jsonLic);
- T responseBean = JsonUtils.json2object(jsonLic, returnType);
+ String jsonLic = IOUtils.toString(response.getEntity().getContent());
+ LOG.debug("Response content read OK: {}", jsonLic);
+ T responseBean = JsonUtils.json2object(jsonLic, returnType);
- LOG.debug("Response bean read OK: {}", responseBean);
+ LOG.debug("Response bean read OK: {}", responseBean);
- return responseBean;
- } catch (IOException e) {
- LOG.error("Error accessing SeCuris server", e);
- throw new SeCurisException("Error accessing SeCuris server");
- }
- }
+ return responseBean;
+ } catch (IOException e) {
+ LOG.error("Error accessing SeCuris server", e);
+ throw new SeCurisException("Error accessing SeCuris server", e);
+ }
+ }
- private void checkErrors(String command, HttpResponse response) throws SeCurisException {
- if (response.getStatusLine().getStatusCode() != 200) {
- if (response.getStatusLine().getStatusCode() == HTTP_STATUS_APP_ERRROR) {
- String errorCode = response.getFirstHeader(ERROR_CODE_MESSAGE_HEADER).getValue();
- String errorMsg = response.getFirstHeader(ERROR_MESSAGE_HEADER).getValue();
- throw new SeCurisException(String.format("[%s] - %s", errorCode, errorMsg));
- }
- LOG.error("Unexpected error executing {}, Reason: {}", command, response.getStatusLine().getReasonPhrase());
- throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
- }
+ private void checkErrors(String command, HttpResponse response) throws SeCurisException {
+ if (response.getStatusLine().getStatusCode() != 200) {
+ if (response.getStatusLine().getStatusCode() == HTTP_STATUS_APP_ERRROR) {
+ String errorCode = response.getFirstHeader(ERROR_CODE_MESSAGE_HEADER).getValue();
+ String errorMsg = response.getFirstHeader(ERROR_MESSAGE_HEADER).getValue();
+ throw new SeCurisException(String.format("[%s] - %s", errorCode, errorMsg));
+ }
+ LOG.error("Unexpected error executing {}, Reason: {}", command, response.getStatusLine().getReasonPhrase());
+ throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
+ }
- }
+ }
- public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException {
- HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command));
- getRequest.addHeader("accept", JSON_MEDIA_TYPE);
+ public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException {
+ HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command));
+ getRequest.addHeader("accept", JSON_MEDIA_TYPE);
- HttpResponse response;
- try {
- response = httpClient.execute(getRequest);
- if (response.getStatusLine().getStatusCode() != 200) {
- throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
- }
- String jsonLic = IOUtils.toString(response.getEntity().getContent());
- LOG.debug("Response content read OK: {}", jsonLic);
- T responseBean = JsonUtils.json2object(jsonLic, returnType);
+ HttpResponse response;
+ try {
+ response = httpClient.execute(getRequest);
+ if (response.getStatusLine().getStatusCode() != 200) {
+ throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
+ }
+ String jsonLic = IOUtils.toString(response.getEntity().getContent());
+ LOG.debug("Response content read OK: {}", jsonLic);
+ T responseBean = JsonUtils.json2object(jsonLic, returnType);
- LOG.debug("Response bean read OK: {}", responseBean);
+ LOG.debug("Response bean read OK: {}", responseBean);
- return responseBean;
- } catch (IOException e) {
- LOG.error("Error acessing SeCuris server", e);
- throw new SeCurisException("Error accessing SeCuris server");
- }
- }
+ return responseBean;
+ } catch (IOException e) {
+ LOG.error("Error acessing SeCuris server", e);
+ throw new SeCurisException("Error accessing SeCuris server");
+ }
+ }
- public static class Command {
- public static final String TEST = "ping";
- public static final String CREATE_LIC = "request";
- public static final String RENEW_LIC = "renew";
- }
+ public static class Command {
+ public static final String TEST = "ping";
+ public static final String CREATE_LIC = "request";
+ public static final String RENEW_LIC = "renew";
+ public static final String VALIDATE = "validate";
+ }
}
diff --git a/src/main/java/net/curisit/securis/ExpiredLicenseException.java b/src/main/java/net/curisit/securis/ExpiredLicenseException.java
new file mode 100644
index 0000000..c0bed2d
--- /dev/null
+++ b/src/main/java/net/curisit/securis/ExpiredLicenseException.java
@@ -0,0 +1,19 @@
+package net.curisit.securis;
+
+public class ExpiredLicenseException extends SeCurisException {
+
+ private static final long serialVersionUID = 5702956178417661458L;
+
+ public ExpiredLicenseException() {
+ super("License has expired", null);
+ }
+
+ public ExpiredLicenseException(String msg, Exception e) {
+ super(msg, e);
+ }
+
+ public ExpiredLicenseException(String msg) {
+ super(msg);
+ }
+
+}
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());
+
+ }
}
diff --git a/src/main/java/net/curisit/securis/SeCurisException.java b/src/main/java/net/curisit/securis/SeCurisException.java
index cf50171..db800a1 100644
--- a/src/main/java/net/curisit/securis/SeCurisException.java
+++ b/src/main/java/net/curisit/securis/SeCurisException.java
@@ -1,13 +1,11 @@
package net.curisit.securis;
-import java.net.URISyntaxException;
public class SeCurisException extends Exception {
private static final long serialVersionUID = 5702956178417661458L;
- public SeCurisException() {
- }
+ public SeCurisException() {}
public SeCurisException(String msg, Exception e) {
super(msg, e);
@@ -17,8 +15,4 @@
super(msg);
}
- public static void main(String[] args) throws URISyntaxException {
- License.main(new String[]
- { "-g" });
- }
}
diff --git a/src/main/java/net/curisit/securis/utils/LicUtils.java b/src/main/java/net/curisit/securis/utils/LicUtils.java
index 234b14c..4b90d69 100644
--- a/src/main/java/net/curisit/securis/utils/LicUtils.java
+++ b/src/main/java/net/curisit/securis/utils/LicUtils.java
@@ -4,6 +4,9 @@
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
import java.util.zip.CRC32;
import org.apache.logging.log4j.LogManager;
@@ -69,9 +72,9 @@
}
public static Integer getLicenseCodeSuffix(String licCode) {
- String[] parts = licCode.split("-");
+ String[] parts = splitLicCode(licCode);
- return new Integer(parts[2]);
+ return new Integer(parts[parts.length - 1]);
}
public static String getLicenseCode(String packCode, Integer licSufixCode) {
@@ -87,17 +90,48 @@
* @return true if license code format and its CRC are valid
*/
public static boolean checkValidLicenseCodeCrc(String licCode) {
- String[] parts = licCode.split("-");
- if (parts.length != 3) {
+ String[] parts = splitLicCode(licCode);
+ if (parts == null) {
return false;
}
String crc = getLicenseCrc(parts[0], parts[2]);
return crc.equals(parts[1]);
}
+ /**
+ * We convert the license code in 3 parts: <br>
+ * "P1-344-1 = ["P1", "344", "1"] <br>
+ * "P1-OTHER-344-1 = ["P1-OTHER", "344", "1"] <br>
+ * "P1-1 = null
+ *
+ * @param code
+ * @return The 2 license code packs or null if code is not valid
+ */
+ private static String[] splitLicCode(String code) {
+ List<Integer> separators = new ArrayList<>();
+ for (int i = 0; i < code.length(); i++) {
+ if (code.charAt(i) == '-') {
+ separators.add(i);
+ }
+ }
+ if (separators.size() < 2) {
+ return null;
+ }
+
+ int i0 = separators.get(separators.size() - 2);
+ int i1 = separators.get(separators.size() - 1);
+ String[] parts = new String[3];
+ parts[0] = code.substring(0, i0);
+ parts[1] = code.substring(i0 + 1, i1);
+ parts[2] = code.substring(i1 + 1);
+
+ return parts;
+ }
+
public static void main(String[] args) {
- String code = getLicenseCode("PCK01", 5);
+ String code = getLicenseCode("PC-K01", 5);
System.out.println(code);
- System.out.println("Is valid ? " + checkValidLicenseCodeCrc("PCK01-512-"));
+ System.out.println("Is valid ? " + checkValidLicenseCodeCrc(code));
+ System.out.println("Is valid ? " + Arrays.asList(splitLicCode(code)));
}
}
diff --git a/src/main/resources/securis-client.properties b/src/main/resources/securis-client.properties
deleted file mode 100644
index 5382344..0000000
--- a/src/main/resources/securis-client.properties
+++ /dev/null
@@ -1,4 +0,0 @@
-license.server.url = https://securis.curistec.com/api
-app.code = CI01
-customer.code = CT01
-pack.code = P1
--
Gitblit v1.3.2