src/main/java/net/curisit/securis/ConnectionManager.java
.. .. @@ -41,7 +41,7 @@ 41 41 private final CloseableHttpClient httpClient; 42 42 43 43 private ConnectionManager() throws SeCurisException { 44 - String aux = Params.get(Params.KEYS.LICENSE_SERVER_URL, "https://securis.curistec.com/api");44 + String aux = Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL);45 45 if (aux.endsWith("/")) { 46 46 serverUrl = aux.substring(0, aux.length()-2); 47 47 } else { src/main/java/net/curisit/securis/License.java
.. .. @@ -3,6 +3,7 @@ 3 3 import java.io.File; 4 4 import java.net.URISyntaxException; 5 5 6 +import net.curisit.securis.beans.SignedLicenseBean;6 7 import net.curisit.securis.utils.Params; 7 8 8 9 import org.apache.commons.cli.CommandLine; .. .. @@ -14,6 +15,7 @@ 14 15 import org.apache.commons.cli.PosixParser; 15 16 import org.apache.logging.log4j.LogManager; 16 17 import org.apache.logging.log4j.Logger; 18 +import org.codehaus.jackson.map.DeserializerFactory.Config;17 19 18 20 /** 19 21 * Main class when SeCuris client is used from command line. .. .. @@ -40,7 +42,7 @@ 40 42 */ 41 43 public class License { 42 44 43 - private static final Logger LOG = LogManager.getLogger(License.class);45 + private static final Logger LOG = LogManager.getLogger(License.class);44 46 45 47 /** 46 48 * Aplication entry point when it used as CLI .. .. @@ -83,12 +85,20 @@ 83 85 } 84 86 85 87 if (cmd.hasOption('c')) { 86 - LicenseManager.getInstance().requestLicense();88 + SignedLicenseBean lic = LicenseManager.getInstance().requestLicense();89 + String filename = cmd.getOptionValue("lic_file");90 + if (filename == null)91 + filename = "./license.lic";92 + File file = new File(filename);93 + LicenseManager.getInstance().save(lic, file);94 + LOG.info("License file sucessfully saved in file: {}", file.getAbsolutePath());87 95 System.exit(0); 88 96 } 89 97 90 98 if (cmd.hasOption('t')) { 91 99 LicenseManager.getInstance().testServer(); 100 + LOG.info("Server is OK, url: {}", Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL));101 +92 102 System.exit(0); 93 103 } 94 104 .. .. @@ -163,7 +173,7 @@ 163 173 options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("validate").withDescription("Validate lic file.").hasArg(true).create('l')); 164 174 165 175 options.addOption("g", "gen_request", false, "Generate request file. If --rfile parameter is missing then It is generated in current directory."); 166 - options.addOption(OptionBuilder.withLongOpt("create").withDescription("Request a license file to server.").hasArg(false).create('c'));176 + options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("create").withDescription("Request a license file to server.").hasArg(true).hasOptionalArg().create('c'));167 177 options.addOption("t", "test_lc", false, "Test if License Server (LC) is available. "); 168 178 options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("renew").withDescription("Synchronize/renew the current license file.").hasArg(true).create('r')); 169 179 src/main/java/net/curisit/securis/LicenseManager.java
.. .. @@ -11,6 +11,7 @@ 11 11 import net.curisit.securis.beans.LicenseBean; 12 12 import net.curisit.securis.beans.RequestBean; 13 13 import net.curisit.securis.beans.SignedLicenseBean; 14 +import net.curisit.securis.beans.StatusBean;14 15 import net.curisit.securis.utils.JsonUtils; 15 16 import net.curisit.securis.utils.Params; 16 17 import net.curisit.securis.utils.SignatureHelper; .. .. @@ -86,37 +87,48 @@ 86 87 * @return The license bean returned by the server 87 88 * @throws SeCurisException 88 89 */ 89 - public LicenseBean requestLicense() throws SeCurisException {90 + public SignedLicenseBean requestLicense() throws SeCurisException {90 91 RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE)); 91 92 92 - LicenseBean lic = requestLicenseToServer(req);93 + SignedLicenseBean lic = requestLicenseToServer(req);93 94 return lic; 94 95 } 95 96 96 - /**97 - * Generate a license file using a {@link LicenseBean}98 - *99 - * @param license100 - * @param file101 - * @throws SeCurisException102 - */103 - public void save(LicenseBean license, File file) throws SeCurisException {104 - SignedLicenseBean signedLic = new SignedLicenseBean(license);105 - byte[] json;106 - try {107 - json = JsonUtils.toJSON(signedLic, true).getBytes("utf-8");108 - Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);109 - } catch (UnsupportedEncodingException e) {110 - LOG.error("Error creating json doc from license: " + license, e);111 - throw new SeCurisException("Error creating json doc from license: " + license, e);112 - } catch (IOException e) {113 - LOG.error("Error creating license file: " + file, e);114 - throw new SeCurisException("Error creating json doc from license: " + license, e);115 - }97 + /**98 + * Generate a license file using a {@link LicenseBean}99 + *100 + * @param license101 + * @param file102 + * @throws SeCurisException103 + */104 + public void save(LicenseBean license, File file) throws SeCurisException {105 + SignedLicenseBean signedLic = new SignedLicenseBean(license);106 + save(signedLic, file);107 + }116 108 117 - LOG.debug("License saved in {}", file);109 + /**110 + * Generate a license file using a {@link LicenseBean}111 + *112 + * @param license113 + * @param file114 + * @throws SeCurisException115 + */116 + public void save(SignedLicenseBean signedLic, File file) throws SeCurisException {117 + byte[] json;118 + try {119 + json = JsonUtils.toJSON(signedLic, true).getBytes("utf-8");120 + Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);121 + } catch (UnsupportedEncodingException e) {122 + LOG.error("Error creating json doc from license: " + signedLic, e);123 + throw new SeCurisException("Error creating json doc from license: " + signedLic, e);124 + } catch (IOException e) {125 + LOG.error("Error creating license file: " + file, e);126 + throw new SeCurisException("Error creating json doc from license: " + signedLic, e);127 + }118 128 119 - }129 + LOG.debug("License saved in {}", file);130 +131 + }120 132 121 133 private SignedLicenseBean requestLicenseToServer(RequestBean req) throws SeCurisException { 122 134 SignedLicenseBean lic = ConnectionManager.getInstance().executePost(Command.CREATE_LIC, SignedLicenseBean.class, req); .. .. @@ -157,8 +169,8 @@ 157 169 } 158 170 159 171 public void testServer() throws SeCurisException { 160 - String pingMsg = ConnectionManager.getInstance().executeGet(Command.RENEW_LIC, String.class);161 - if (!PING_MESSAGE.equals(pingMsg)) {172 + StatusBean status = ConnectionManager.getInstance().executeGet(Command.TEST, StatusBean.class);173 + if (!PING_MESSAGE.equals(status.getMessage())) {162 174 throw new SeCurisException("SeCuris Server is not running in given URL"); 163 175 } 164 176 } src/main/java/net/curisit/securis/beans/StatusBean.java
.. .. @@ -0,0 +1,32 @@ 1 +package net.curisit.securis.beans;2 +3 +import java.util.Date;4 +5 +import org.codehaus.jackson.annotate.JsonAutoDetect;6 +7 +@JsonAutoDetect8 +public class StatusBean extends RequestBean {9 + private Date date;10 + private String message;11 +12 + public StatusBean() {13 + }14 +15 + public Date getDate() {16 + return date;17 + }18 +19 + public void setDate(Date date) {20 + this.date = date;21 + }22 +23 + public String getMessage() {24 + return message;25 + }26 +27 + public void setMessage(String message) {28 + this.message = message;29 + }30 +31 +32 +}src/main/java/net/curisit/securis/utils/Params.java
.. .. @@ -22,7 +22,8 @@ 22 22 /** 23 23 * Key used to store config file resource location. In a web application, can be set as initial parameter in a servlet loaded on startup 24 24 */ 25 - public static final String KEY_CONFIG_FILE = "/securis-client.properties";25 + public static final String DEFAUT_SERVER_URL = "https://securis.curistec.com/api";26 + public static final String KEY_CONFIG_FILE = "/securis-client.properties";26 27 27 28 private static Properties params = null; 28 29