pom.xml
.. .. @@ -3,7 +3,7 @@ 3 3 <modelVersion>4.0.0</modelVersion> 4 4 <groupId>net.curisit</groupId> 5 5 <artifactId>securis-client</artifactId> 6 - <version>1.0.2-SNAPSHOT</version>6 + <version>1.0.3-SNAPSHOT</version>7 7 <build> 8 8 <plugins> 9 9 <plugin> src/main/java/net/curisit/securis/ConnectionManager.java
.. .. @@ -33,121 +33,122 @@ 33 33 */ 34 34 public class ConnectionManager { 35 35 36 - private static final Logger LOG = LogManager.getLogger(ConnectionManager.class);37 - private static final int HTTP_STATUS_APP_ERRROR = 418;38 - private static final String JSON_MEDIA_TYPE = "application/json";39 - private static final String ERROR_MESSAGE_HEADER = "X-SECURIS-ERROR-MSG";40 - private static final String ERROR_CODE_MESSAGE_HEADER = "X-SECURIS-ERROR-CODE";36 + private static final Logger LOG = LogManager.getLogger(ConnectionManager.class);37 + private static final int HTTP_STATUS_APP_ERRROR = 418;38 + private static final String JSON_MEDIA_TYPE = "application/json";39 + private static final String ERROR_MESSAGE_HEADER = "X-SECURIS-ERROR-MSG";40 + private static final String ERROR_CODE_MESSAGE_HEADER = "X-SECURIS-ERROR-CODE";41 41 42 - private static ConnectionManager singleton;42 + private static ConnectionManager singleton;43 43 44 - private final String serverUrl;45 - private final CloseableHttpClient httpClient;44 + private final String serverUrl;45 + private final CloseableHttpClient httpClient;46 46 47 - private ConnectionManager() throws SeCurisException {48 - String aux = Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL);49 - if (aux.endsWith("/")) {50 - serverUrl = aux.substring(0, aux.length() - 2);51 - } else {52 - serverUrl = aux;53 - }54 - httpClient = createHttpClient();55 - }47 + private ConnectionManager() throws SeCurisException {48 + String aux = Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL);49 + if (aux.endsWith("/")) {50 + serverUrl = aux.substring(0, aux.length() - 2);51 + } else {52 + serverUrl = aux;53 + }54 + httpClient = createHttpClient();55 + }56 56 57 - private CloseableHttpClient createHttpClient() throws SeCurisException {58 - SSLContextBuilder builder = new SSLContextBuilder();59 - SSLConnectionSocketFactory sslsf = null;60 - try {61 - builder.loadTrustMaterial((KeyStore) null, new TrustStrategy() {62 - @Override63 - public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {64 - return true;65 - }66 - });67 - sslsf = new SSLConnectionSocketFactory(builder.build());68 - } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e1) {69 - LOG.error(e1);70 - throw new SeCurisException("Error creating SSL socket factory");71 - }72 - return HttpClientBuilder.create().setSSLSocketFactory(sslsf).build();73 - }57 + private CloseableHttpClient createHttpClient() throws SeCurisException {58 + SSLContextBuilder builder = new SSLContextBuilder();59 + SSLConnectionSocketFactory sslsf = null;60 + try {61 + builder.loadTrustMaterial((KeyStore) null, new TrustStrategy() {62 + @Override63 + public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {64 + return true;65 + }66 + });67 + sslsf = new SSLConnectionSocketFactory(builder.build());68 + } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e1) {69 + LOG.error(e1);70 + throw new SeCurisException("Error creating SSL socket factory");71 + }72 + return HttpClientBuilder.create().setSSLSocketFactory(sslsf).build();73 + }74 74 75 - public synchronized static ConnectionManager getInstance() throws SeCurisException {76 - if (singleton == null) {77 - singleton = new ConnectionManager();78 - }79 - return singleton;80 - }75 + public synchronized static ConnectionManager getInstance() throws SeCurisException {76 + if (singleton == null) {77 + singleton = new ConnectionManager();78 + }79 + return singleton;80 + }81 81 82 - public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException {83 - HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command));84 - postRequest.addHeader("accept", JSON_MEDIA_TYPE);82 + public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException {83 + HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command));84 + postRequest.addHeader("accept", JSON_MEDIA_TYPE);85 85 86 - postRequest.addHeader("content-type", JSON_MEDIA_TYPE);87 - try {88 - postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req)));89 - } catch (UnsupportedEncodingException | SeCurisException e1) {90 - throw new SeCurisException("Error preparing POST command", e1);91 - }92 - HttpResponse response;93 - try {94 - response = httpClient.execute(postRequest);86 + postRequest.addHeader("content-type", JSON_MEDIA_TYPE);87 + try {88 + postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req)));89 + } catch (UnsupportedEncodingException | SeCurisException e1) {90 + throw new SeCurisException("Error preparing POST command", e1);91 + }92 + HttpResponse response;93 + try {94 + response = httpClient.execute(postRequest);95 95 96 - checkErrors(command, response);96 + checkErrors(command, response);97 97 98 - String jsonLic = IOUtils.toString(response.getEntity().getContent());99 - LOG.debug("Response content read OK: {}", jsonLic);100 - T responseBean = JsonUtils.json2object(jsonLic, returnType);98 + String jsonLic = IOUtils.toString(response.getEntity().getContent());99 + LOG.debug("Response content read OK: {}", jsonLic);100 + T responseBean = JsonUtils.json2object(jsonLic, returnType);101 101 102 - LOG.debug("Response bean read OK: {}", responseBean);102 + LOG.debug("Response bean read OK: {}", responseBean);103 103 104 - return responseBean;105 - } catch (IOException e) {106 - LOG.error("Error accessing SeCuris server", e);107 - throw new SeCurisException("Error accessing SeCuris server");108 - }109 - }104 + return responseBean;105 + } catch (IOException e) {106 + LOG.error("Error accessing SeCuris server", e);107 + throw new SeCurisException("Error accessing SeCuris server", e);108 + }109 + }110 110 111 - private void checkErrors(String command, HttpResponse response) throws SeCurisException {112 - if (response.getStatusLine().getStatusCode() != 200) {113 - if (response.getStatusLine().getStatusCode() == HTTP_STATUS_APP_ERRROR) {114 - String errorCode = response.getFirstHeader(ERROR_CODE_MESSAGE_HEADER).getValue();115 - String errorMsg = response.getFirstHeader(ERROR_MESSAGE_HEADER).getValue();116 - throw new SeCurisException(String.format("[%s] - %s", errorCode, errorMsg));117 - }118 - LOG.error("Unexpected error executing {}, Reason: {}", command, response.getStatusLine().getReasonPhrase());119 - throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());120 - }111 + private void checkErrors(String command, HttpResponse response) throws SeCurisException {112 + if (response.getStatusLine().getStatusCode() != 200) {113 + if (response.getStatusLine().getStatusCode() == HTTP_STATUS_APP_ERRROR) {114 + String errorCode = response.getFirstHeader(ERROR_CODE_MESSAGE_HEADER).getValue();115 + String errorMsg = response.getFirstHeader(ERROR_MESSAGE_HEADER).getValue();116 + throw new SeCurisException(String.format("[%s] - %s", errorCode, errorMsg));117 + }118 + LOG.error("Unexpected error executing {}, Reason: {}", command, response.getStatusLine().getReasonPhrase());119 + throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());120 + }121 121 122 - }122 + }123 123 124 - public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException {125 - HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command));126 - getRequest.addHeader("accept", JSON_MEDIA_TYPE);124 + public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException {125 + HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command));126 + getRequest.addHeader("accept", JSON_MEDIA_TYPE);127 127 128 - HttpResponse response;129 - try {130 - response = httpClient.execute(getRequest);131 - if (response.getStatusLine().getStatusCode() != 200) {132 - throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());133 - }134 - String jsonLic = IOUtils.toString(response.getEntity().getContent());135 - LOG.debug("Response content read OK: {}", jsonLic);136 - T responseBean = JsonUtils.json2object(jsonLic, returnType);128 + HttpResponse response;129 + try {130 + response = httpClient.execute(getRequest);131 + if (response.getStatusLine().getStatusCode() != 200) {132 + throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());133 + }134 + String jsonLic = IOUtils.toString(response.getEntity().getContent());135 + LOG.debug("Response content read OK: {}", jsonLic);136 + T responseBean = JsonUtils.json2object(jsonLic, returnType);137 137 138 - LOG.debug("Response bean read OK: {}", responseBean);138 + LOG.debug("Response bean read OK: {}", responseBean);139 139 140 - return responseBean;141 - } catch (IOException e) {142 - LOG.error("Error acessing SeCuris server", e);143 - throw new SeCurisException("Error accessing SeCuris server");144 - }145 - }140 + return responseBean;141 + } catch (IOException e) {142 + LOG.error("Error acessing SeCuris server", e);143 + throw new SeCurisException("Error accessing SeCuris server");144 + }145 + }146 146 147 - public static class Command {148 - public static final String TEST = "ping";149 - public static final String CREATE_LIC = "request";150 - public static final String RENEW_LIC = "renew";151 - }147 + public static class Command {148 + public static final String TEST = "ping";149 + public static final String CREATE_LIC = "request";150 + public static final String RENEW_LIC = "renew";151 + public static final String VALIDATE = "validate";152 + }152 153 153 154 } src/main/java/net/curisit/securis/ExpiredLicenseException.java
.. .. @@ -0,0 +1,19 @@ 1 +package net.curisit.securis;2 +3 +public class ExpiredLicenseException extends SeCurisException {4 +5 + private static final long serialVersionUID = 5702956178417661458L;6 +7 + public ExpiredLicenseException() {8 + super("License has expired", null);9 + }10 +11 + public ExpiredLicenseException(String msg, Exception e) {12 + super(msg, e);13 + }14 +15 + public ExpiredLicenseException(String msg) {16 + super(msg);17 + }18 +19 +}src/main/java/net/curisit/securis/LicenseManager.java
.. .. @@ -28,195 +28,207 @@ 28 28 */ 29 29 public class LicenseManager { 30 30 31 - private static final Logger LOG = LogManager.getLogger(License.class);31 + private static final Logger LOG = LogManager.getLogger(License.class);32 32 33 - private static LicenseManager singleton = new LicenseManager();33 + private static LicenseManager singleton = new LicenseManager();34 34 35 - public static final String PING_MESSAGE = "SeCuris API OK";35 + public static final String PING_MESSAGE = "SeCuris API OK";36 36 37 - private LicenseManager() {38 - }37 + private LicenseManager() {}39 38 40 - public static LicenseManager getInstance() {41 - return singleton;42 - }39 + public static LicenseManager getInstance() {40 + return singleton;41 + }43 42 44 - /**45 - * Loads a license from file46 - *47 - * @param licFile48 - * @return The license bean49 - * @throws SeCurisException50 - */51 - public LicenseBean load(File licFile) throws SeCurisException {52 - LicenseBean licBean;53 - try {54 - licBean = JsonUtils.json2object(FileUtils.readFileToString(licFile), LicenseBean.class);55 - } catch (IOException e) {56 - throw new SeCurisException("Error getting license data from file: " + licFile, e);57 - }58 - return licBean;59 - }43 + /**44 + * Loads a license from file45 + *46 + * @param licFile47 + * @return The license bean48 + * @throws SeCurisException49 + */50 + public LicenseBean load(File licFile) throws SeCurisException {51 + LicenseBean licBean;52 + try {53 + licBean = JsonUtils.json2object(FileUtils.readFileToString(licFile), LicenseBean.class);54 + } catch (IOException e) {55 + throw new SeCurisException("Error getting license data from file: " + licFile, e);56 + }57 + return licBean;58 + }60 59 61 - /**62 - * Validates the license stored in {@code licFile} and get the corresponding63 - * LicenseBean64 - * <p>65 - * The validation includes:66 - * <ul>67 - * <li>Signature</li>68 - * <li>HW data</li>69 - * <li>Logo CRC</li>70 - * </ul>71 - * </p>72 - *73 - * @param licFile74 - * @return The license bean stored in file75 - * @throws SeCurisException76 - */77 - public LicenseBean validateLicense(File licFile) throws SeCurisException {60 + /**61 + * Validates the license stored in {@code licFile} and get the corresponding62 + * LicenseBean63 + * <p>64 + * The validation includes:65 + * <ul>66 + * <li>Signature</li>67 + * <li>HW data</li>68 + * <li>Logo CRC</li>69 + * </ul>70 + * </p>71 + *72 + * @param licFile73 + * @return The license bean stored in file74 + * @throws SeCurisException75 + */76 + public LicenseBean validateLicense(File licFile) throws SeCurisException {78 77 79 - return validateLicense(licFile, false);80 - }78 + return validateLicense(licFile, false);79 + }81 80 82 - /**83 - * Validates the license stored in {@code licFile} and get the corresponding84 - * LicenseBean. The License date is not validated85 - * <p>86 - * The validation includes:87 - * <ul>88 - * <li>Signature</li>89 - * <li>HW data</li>90 - * <li>Logo CRC</li>91 - * </ul>92 - * </p>93 - *94 - * @param licFile95 - * @return The license bean stored in file96 - * @throws SeCurisException97 - */98 - public LicenseBean validateLicense(File licFile, boolean excludeDateValidation) throws SeCurisException {99 - LicenseBean licBean = load(licFile);100 - SignatureHelper.getInstance().validateSignature(licBean);101 - LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),102 - Params.get(Params.KEYS.PACK_CODE));103 - LicenseValidator.getInstance().validateLogo(licBean);81 + /**82 + * Validates the license stored in {@code licFile} and get the corresponding83 + * LicenseBean. The License date is not validated84 + * <p>85 + * The validation includes:86 + * <ul>87 + * <li>Signature</li>88 + * <li>HW data</li>89 + * <li>Logo CRC</li>90 + * </ul>91 + * </p>92 + *93 + * @param licFile94 + * @return The license bean stored in file95 + * @throws SeCurisException96 + */97 + public LicenseBean validateLicense(File licFile, boolean excludeDateValidation) throws SeCurisException {98 + LicenseBean licBean = load(licFile);99 + SignatureHelper.getInstance().validateSignature(licBean);100 + LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),101 + Params.get(Params.KEYS.PACK_CODE));102 + LicenseValidator.getInstance().validateLogo(licBean);104 103 105 - if (!excludeDateValidation) {106 - if (new Date().after(licBean.getExpirationDate())) {107 - throw new SeCurisException("License has expired");108 - }109 - }104 + if (!excludeDateValidation) {105 + if (new Date().after(licBean.getExpirationDate())) {106 + throw new ExpiredLicenseException();107 + }108 + }110 109 111 - return licBean;112 - }110 + return licBean;111 + }113 112 114 - /**115 - * Request to server for a valid license116 - *117 - * @return The license bean returned by the server118 - * @throws SeCurisException119 - */120 - public SignedLicenseBean requestLicense() throws SeCurisException {121 - RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),122 - Params.get(Params.KEYS.PACK_CODE));113 + /**114 + * Request to server for a valid license115 + *116 + * @return The license bean returned by the server117 + * @throws SeCurisException118 + */119 + public SignedLicenseBean requestLicense() throws SeCurisException {120 + RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),121 + Params.get(Params.KEYS.PACK_CODE));123 122 124 - SignedLicenseBean lic = requestLicenseToServer(req);125 - return lic;126 - }123 + SignedLicenseBean lic = requestLicenseToServer(req);124 + return lic;125 + }127 126 128 - /**129 - * Generate a license file using a {@link LicenseBean}130 - *131 - * @param license132 - * @param file133 - * @throws SeCurisException134 - */135 - public void save(LicenseBean license, File file) throws SeCurisException {136 - SignedLicenseBean signedLic = new SignedLicenseBean(license);137 - save(signedLic, file);138 - }127 + /**128 + * Generate a license file using a {@link LicenseBean}129 + *130 + * @param license131 + * @param file132 + * @throws SeCurisException133 + */134 + public void save(LicenseBean license, File file) throws SeCurisException {135 + SignedLicenseBean signedLic = new SignedLicenseBean(license);136 + save(signedLic, file);137 + }139 138 140 - /**141 - * Generate a license file using a {@link LicenseBean}142 - *143 - * @param license144 - * @param file145 - * @throws SeCurisException146 - */147 - public void save(SignedLicenseBean signedLic, File file) throws SeCurisException {148 - byte[] json;149 - try {150 - json = JsonUtils.toPrettyJSON(signedLic).getBytes("utf-8");151 - Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);152 - } catch (UnsupportedEncodingException e) {153 - LOG.error("Error creating json doc from license: " + signedLic, e);154 - throw new SeCurisException("Error creating json doc from license: " + signedLic, e);155 - } catch (IOException e) {156 - LOG.error("Error creating license file: " + file, e);157 - throw new SeCurisException("Error creating json doc from license: " + signedLic, e);158 - }139 + /**140 + * Generate a license file using a {@link LicenseBean}141 + *142 + * @param license143 + * @param file144 + * @throws SeCurisException145 + */146 + public void save(SignedLicenseBean signedLic, File file) throws SeCurisException {147 + byte[] json;148 + try {149 + json = JsonUtils.toPrettyJSON(signedLic).getBytes("utf-8");150 + Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);151 + } catch (UnsupportedEncodingException e) {152 + LOG.error("Error creating json doc from license: " + signedLic, e);153 + throw new SeCurisException("Error creating json doc from license: " + signedLic, e);154 + } catch (IOException e) {155 + LOG.error("Error creating license file: " + file, e);156 + throw new SeCurisException("Error creating json doc from license: " + signedLic, e);157 + }159 158 160 - LOG.debug("License saved in {}", file);159 + LOG.debug("License saved in {}", file);161 160 162 - }161 + }163 162 164 - private SignedLicenseBean requestLicenseToServer(RequestBean req) throws SeCurisException {165 - SignedLicenseBean lic = ConnectionManager.getInstance().executePost(Command.CREATE_LIC, SignedLicenseBean.class, req);163 + private SignedLicenseBean requestLicenseToServer(RequestBean req) throws SeCurisException {164 + SignedLicenseBean lic = ConnectionManager.getInstance().executePost(Command.CREATE_LIC, SignedLicenseBean.class, req);166 165 167 - return lic;168 - }166 + return lic;167 + }169 168 170 - /**171 - * Creates a new request file with current hardware in the File passed as172 - * parameter173 - *174 - * @param outputRequestFile175 - * File where the request data will be saved176 - * @return The generated request bean177 - * @throws SeCurisException178 - */179 - public RequestBean createRequestFile(File outputRequestFile) throws SeCurisException {180 - RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),181 - Params.get(Params.KEYS.PACK_CODE));169 + /**170 + * Creates a new request file with current hardware in the File passed as171 + * parameter172 + *173 + * @param outputRequestFile174 + * File where the request data will be saved175 + * @return The generated request bean176 + * @throws SeCurisException177 + */178 + public RequestBean createRequestFile(File outputRequestFile) throws SeCurisException {179 + RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),180 + Params.get(Params.KEYS.PACK_CODE));182 181 183 - ReqGenerator.getInstance().save(req, outputRequestFile);182 + ReqGenerator.getInstance().save(req, outputRequestFile);184 183 185 - return req;186 - }184 + return req;185 + }187 186 188 - /**189 - * Send the current license file to server, which is previously validated,190 - * to get a renewed one if it is prepared in server side.191 - *192 - * @param licenseFile193 - * Current and valid License file194 - * @return New license bean if server creates a new one, otherwise the same195 - * current License bean will be returned196 - * @throws SeCurisException197 - */198 - public SignedLicenseBean renew(File licenseFile) throws SeCurisException {199 - LicenseBean lic = validateLicense(licenseFile);187 + /**188 + * Send the current license file to server, which is previously validated,189 + * to get a renewed one if it is prepared in server side.190 + *191 + * @param licenseFile192 + * Current and valid License file193 + * @return New license bean if server creates a new one, otherwise the same194 + * current License bean will be returned195 + * @throws SeCurisException196 + */197 + public SignedLicenseBean renew(File licenseFile) throws SeCurisException {198 + LicenseBean lic = validateLicense(licenseFile);200 199 201 - SignedLicenseBean newLic = ConnectionManager.getInstance().executePost(Command.RENEW_LIC, SignedLicenseBean.class, lic);200 + SignedLicenseBean newLic = ConnectionManager.getInstance().executePost(Command.RENEW_LIC, SignedLicenseBean.class, lic);202 201 203 - return newLic;204 - }202 + return newLic;203 + }205 204 206 - public void testServer() throws SeCurisException {207 - StatusBean status = ConnectionManager.getInstance().executeGet(Command.TEST, StatusBean.class);208 - if (!PING_MESSAGE.equals(status.getMessage())) {209 - throw new SeCurisException("SeCuris Server is not running in given URL");210 - }211 - }205 + /**206 + * Check on SeCuris server if current license is still valid in server DB.207 + *208 + * @param licenseFile209 + * @throws SeCurisException210 + */211 + public void assertLicenseIsValid(File licenseFile) throws SeCurisException, IOException {212 + LicenseBean lic = validateLicense(licenseFile);212 213 213 - public static void main(String[] args) throws SeCurisException {214 - String filename = null;215 - if (filename == null)216 - filename = "./license.req";217 - File file = new File(filename);218 - LicenseManager.getInstance().createRequestFile(file);219 - LOG.info("Request file {} generated OK", file.getAbsolutePath());214 + ConnectionManager.getInstance().executePost(Command.VALIDATE, LicenseBean.class, lic);220 215 221 - }216 + }217 +218 + public void testServer() throws SeCurisException {219 + StatusBean status = ConnectionManager.getInstance().executeGet(Command.TEST, StatusBean.class);220 + if (!PING_MESSAGE.equals(status.getMessage())) {221 + throw new SeCurisException("SeCuris Server is not running in given URL");222 + }223 + }224 +225 + public static void main(String[] args) throws SeCurisException {226 + String filename = null;227 + if (filename == null)228 + filename = "./license.req";229 + File file = new File(filename);230 + LicenseManager.getInstance().createRequestFile(file);231 + LOG.info("Request file {} generated OK", file.getAbsolutePath());232 +233 + }222 234 } src/main/java/net/curisit/securis/SeCurisException.java
.. .. @@ -1,13 +1,11 @@ 1 1 package net.curisit.securis; 2 2 3 -import java.net.URISyntaxException;4 3 5 4 public class SeCurisException extends Exception { 6 5 7 6 private static final long serialVersionUID = 5702956178417661458L; 8 7 9 - public SeCurisException() {10 - }8 + public SeCurisException() {}11 9 12 10 public SeCurisException(String msg, Exception e) { 13 11 super(msg, e); .. .. @@ -17,8 +15,4 @@ 17 15 super(msg); 18 16 } 19 17 20 - public static void main(String[] args) throws URISyntaxException {21 - License.main(new String[]22 - { "-g" });23 - }24 18 } src/main/java/net/curisit/securis/utils/LicUtils.java
.. .. @@ -4,6 +4,9 @@ 4 4 import java.nio.charset.Charset; 5 5 import java.security.MessageDigest; 6 6 import java.security.NoSuchAlgorithmException; 7 +import java.util.ArrayList;8 +import java.util.Arrays;9 +import java.util.List;7 10 import java.util.zip.CRC32; 8 11 9 12 import org.apache.logging.log4j.LogManager; .. .. @@ -69,9 +72,9 @@ 69 72 } 70 73 71 74 public static Integer getLicenseCodeSuffix(String licCode) { 72 - String[] parts = licCode.split("-");75 + String[] parts = splitLicCode(licCode);73 76 74 - return new Integer(parts[2]);77 + return new Integer(parts[parts.length - 1]);75 78 } 76 79 77 80 public static String getLicenseCode(String packCode, Integer licSufixCode) { .. .. @@ -87,17 +90,48 @@ 87 90 * @return true if license code format and its CRC are valid 88 91 */ 89 92 public static boolean checkValidLicenseCodeCrc(String licCode) { 90 - String[] parts = licCode.split("-");91 - if (parts.length != 3) {93 + String[] parts = splitLicCode(licCode);94 + if (parts == null) {92 95 return false; 93 96 } 94 97 String crc = getLicenseCrc(parts[0], parts[2]); 95 98 return crc.equals(parts[1]); 96 99 } 97 100 101 + /**102 + * We convert the license code in 3 parts: <br>103 + * "P1-344-1 = ["P1", "344", "1"] <br>104 + * "P1-OTHER-344-1 = ["P1-OTHER", "344", "1"] <br>105 + * "P1-1 = null106 + *107 + * @param code108 + * @return The 2 license code packs or null if code is not valid109 + */110 + private static String[] splitLicCode(String code) {111 + List<Integer> separators = new ArrayList<>();112 + for (int i = 0; i < code.length(); i++) {113 + if (code.charAt(i) == '-') {114 + separators.add(i);115 + }116 + }117 + if (separators.size() < 2) {118 + return null;119 + }120 +121 + int i0 = separators.get(separators.size() - 2);122 + int i1 = separators.get(separators.size() - 1);123 + String[] parts = new String[3];124 + parts[0] = code.substring(0, i0);125 + parts[1] = code.substring(i0 + 1, i1);126 + parts[2] = code.substring(i1 + 1);127 +128 + return parts;129 + }130 +98 131 public static void main(String[] args) { 99 - String code = getLicenseCode("PCK01", 5);132 + String code = getLicenseCode("PC-K01", 5);100 133 System.out.println(code); 101 - System.out.println("Is valid ? " + checkValidLicenseCodeCrc("PCK01-512-"));134 + System.out.println("Is valid ? " + checkValidLicenseCodeCrc(code));135 + System.out.println("Is valid ? " + Arrays.asList(splitLicCode(code)));102 136 } 103 137 } src/main/resources/securis-client.propertiesdeleted file mode 100644
.. .. @@ -1,4 +0,0 @@ 1 -license.server.url = https://securis.curistec.com/api2 -app.code = CI013 -customer.code = CT014 -pack.code = P1