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.6-SNAPSHOT</version>6 + <version>1.1.0-SNAPSHOT</version>7 7 <build> 8 8 <plugins> 9 9 <plugin> src/main/java/net/curisit/securis/License.java
.. .. @@ -41,163 +41,166 @@ 41 41 */ 42 42 public class License { 43 43 44 - private static final Logger LOG = LogManager.getLogger(License.class);44 + private static final Logger LOG = LogManager.getLogger(License.class);45 45 46 - /**47 - * Aplication entry point when it used as CLI48 - *49 - * @param args50 - * @throws URISyntaxException51 - */52 - public static void main(String[] args) throws URISyntaxException {46 + /**47 + * Aplication entry point when it used as CLI48 + *49 + * @param args50 + * @throws URISyntaxException51 + */52 + public static void main(String[] args) throws URISyntaxException {53 53 54 - LOG.debug("SeCuris client tool init ");54 + LOG.debug("SeCuris client tool init ");55 55 56 - checkConfigFile();57 - CommandLine cmd = getCommandLine(args);56 + checkConfigFile();57 + CommandLine cmd = getCommandLine(args);58 58 59 - try {60 - if (cmd.hasOption('g')) {61 - String filename = cmd.getOptionValue("rfile");62 - if (filename == null)63 - filename = "./license.req";64 - File file = new File(filename);65 - LicenseManager.getInstance().createRequestFile(file);66 - LOG.info("Request file {} generated OK", file.getAbsolutePath());67 - System.exit(0);68 - }59 + try {60 + if (cmd.hasOption('g')) {61 + String filename = cmd.getOptionValue("rfile");62 + if (filename == null)63 + filename = "./license.req";64 + File file = new File(filename);65 + LicenseManager.getInstance().createRequestFile(file);66 + LOG.info("Request file {} generated OK", file.getAbsolutePath());67 + System.exit(0);68 + }69 69 70 - if (cmd.hasOption('l')) {71 - String filename = cmd.getOptionValue("validate");72 - if (filename == null)73 - filename = "./license.lic";74 - File file = new File(filename);75 - if (!file.exists()) {76 - throw new SeCurisException("The license file doesn't exist: " + file.getAbsolutePath());77 - }78 - try {79 - LicenseManager.getInstance().validateLicense(file);80 - LOG.info("License file {} is valid", file.getAbsolutePath());81 - } catch (SeCurisException e) {82 - LOG.info("License file {} is NOT valid", file.getAbsolutePath());83 - LOG.info("Reason: {}", e.toString());84 - }70 + if (cmd.hasOption('l')) {71 + String filename = cmd.getOptionValue("validate");72 + if (filename == null)73 + filename = "./license.lic";74 + File file = new File(filename);75 + if (!file.exists()) {76 + throw new SeCurisException("The license file doesn't exist: " + file.getAbsolutePath());77 + }78 + try {79 + LicenseManager.getInstance().validateLicense(file);80 + LOG.info("License file {} is valid", file.getAbsolutePath());81 + } catch (SeCurisException e) {82 + LOG.info("License file {} is NOT valid", file.getAbsolutePath());83 + LOG.info("Reason: {}", e.toString());84 + }85 85 86 - System.exit(0);87 - }86 + System.exit(0);87 + }88 88 89 - if (cmd.hasOption('c')) {90 - // TODO: Change CLI to get name and email in license request91 - SignedLicenseBean lic = LicenseManager.getInstance().requestLicense(null, null);92 - String filename = cmd.getOptionValue("c");93 - if (filename == null) {94 - filename = "./license.lic";95 - }96 - File file = new File(filename);97 - LicenseManager.getInstance().save(lic, file);98 - LOG.info("License file sucessfully saved in file: {}", file.getAbsolutePath());99 - System.exit(0);100 - }89 + if (cmd.hasOption('c')) {90 + // TODO: Change CLI to get name and email in license request91 + SignedLicenseBean lic = LicenseManager.getInstance().requestLicense(null, null);92 + String filename = cmd.getOptionValue("c");93 + if (filename == null) {94 + filename = "./license.lic";95 + }96 + File file = new File(filename);97 + LicenseManager.getInstance().save(lic, file);98 + LOG.info("License file sucessfully saved in file: {}", file.getAbsolutePath());99 + System.exit(0);100 + }101 101 102 - if (cmd.hasOption('t')) {103 - LicenseManager.getInstance().testServer();104 - LOG.info("Server is OK, url: {}", Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL));102 + if (cmd.hasOption('t')) {103 + LicenseManager.getInstance().testServer();104 + LOG.info("Server is OK, url: {}", Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL));105 105 106 - System.exit(0);107 - }106 + System.exit(0);107 + }108 108 109 - if (cmd.hasOption('r')) {110 - String licFilename = cmd.getOptionValue("renew");111 - checkMandatoryParameter(licFilename, "renew");112 - File file = new File(licFilename);113 - if (!file.exists()) {114 - throw new SeCurisException("The license file doesn't exist: " + file.getAbsolutePath());115 - }116 - SignedLicenseBean newLic = LicenseManager.getInstance().renew(file);117 - File oldLicFile = new File(file.getAbsoluteFile() + ".old");118 - file.renameTo(oldLicFile);119 - LOG.info("Old license file has been renamed to: {}", oldLicFile.getAbsolutePath());120 - LicenseManager.getInstance().save(newLic, file);121 - LOG.info("New license file saved as: {}", file.getAbsolutePath());122 - System.exit(0);123 - }109 + if (cmd.hasOption('r')) {110 + String licFilename = cmd.getOptionValue("renew");111 + checkMandatoryParameter(licFilename, "renew");112 + File file = new File(licFilename);113 + if (!file.exists()) {114 + throw new SeCurisException("The license file doesn't exist: " + file.getAbsolutePath());115 + }116 + SignedLicenseBean newLic = LicenseManager.getInstance().renew(file);117 + File oldLicFile = new File(file.getAbsoluteFile() + ".old");118 + file.renameTo(oldLicFile);119 + LOG.info("Old license file has been renamed to: {}", oldLicFile.getAbsolutePath());120 + LicenseManager.getInstance().save(newLic, file);121 + LOG.info("New license file saved as: {}", file.getAbsolutePath());122 + System.exit(0);123 + }124 124 125 - } catch (SeCurisException e) {126 - LOG.error("The command generated an error: {}", e.toString());127 - }128 - }125 + } catch (SeCurisException e) {126 + LOG.error("The command generated an error: {}", e.toString());127 + }128 + }129 129 130 - private static void checkMandatoryParameter(String value, String param) {131 - if (value == null) {132 - LOG.error("Parameter {} is mandatory. Use --help to get information about parameters", param);133 - System.exit(-5);134 - }135 - }130 + private static void checkMandatoryParameter(String value, String param) {131 + if (value == null) {132 + LOG.error("Parameter {} is mandatory. Use --help to get information about parameters", param);133 + System.exit(-5);134 + }135 + }136 136 137 - /**138 - * Checks that config file exists and contains mandatory parameters139 - */140 - private static void checkConfigFile() {141 - if (Params.get(Params.KEYS.LIC_TYPE_CODE) == null && Params.get(Params.KEYS.APPLICATION_CODE) == null) {142 - LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.LIC_TYPE_CODE);143 - System.exit(-3);144 - }145 - if (Params.get(Params.KEYS.CUSTOMER_CODE) == null) {146 - LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.CUSTOMER_CODE);147 - System.exit(-4);148 - }149 - if (Params.get(Params.KEYS.PACK_CODE) == null) {150 - LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.PACK_CODE);151 - System.exit(-6);152 - }153 - }137 + /**138 + * Checks that config file exists and contains mandatory parameters139 + */140 + private static void checkConfigFile() {141 + if (Params.get(Params.KEYS.APPLICATION_CODE) == null) {154 142 155 - private static CommandLine getCommandLine(String[] args) {156 - Options ops = prepareOptionCLI();157 - if (args.length == 0) {158 - printHelp(ops);159 - }160 - CommandLineParser parser = new PosixParser();161 - CommandLine cmd = null;162 - try {163 - cmd = parser.parse(ops, args);164 - } catch (ParseException e) {165 - printHelp(ops);166 - }143 + if (Params.get(Params.KEYS.LIC_TYPE_CODE) == null) {144 + LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.LIC_TYPE_CODE);145 + System.exit(-3);146 + }147 + if (Params.get(Params.KEYS.CUSTOMER_CODE) == null) {148 + LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.CUSTOMER_CODE);149 + System.exit(-4);150 + }151 + if (Params.get(Params.KEYS.PACK_CODE) == null) {152 + LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.PACK_CODE);153 + System.exit(-6);154 + }155 + } // else: The license will be got using activationCode156 + }167 157 168 - if (cmd.hasOption('h')) {169 - printHelp(ops);170 - }158 + private static CommandLine getCommandLine(String[] args) {159 + Options ops = prepareOptionCLI();160 + if (args.length == 0) {161 + printHelp(ops);162 + }163 + CommandLineParser parser = new PosixParser();164 + CommandLine cmd = null;165 + try {166 + cmd = parser.parse(ops, args);167 + } catch (ParseException e) {168 + printHelp(ops);169 + }171 170 172 - return cmd;173 - }171 + if (cmd.hasOption('h')) {172 + printHelp(ops);173 + }174 174 175 - private static void printHelp(Options ops) {176 - HelpFormatter formatter = new HelpFormatter();177 - formatter.printHelp("securis-client", ops, true);178 - System.exit(-1);179 - }175 + return cmd;176 + }180 177 181 - @SuppressWarnings("static-access")182 - private static Options prepareOptionCLI() {183 - Options options = new Options();178 + private static void printHelp(Options ops) {179 + HelpFormatter formatter = new HelpFormatter();180 + formatter.printHelp("securis-client", ops, true);181 + System.exit(-1);182 + }184 183 185 - options.addOption("h", "help", false, "Show help.");186 - options.addOption(OptionBuilder.withArgName("req_file").withLongOpt("rfile")187 - .withDescription("Set request file for its generation or for license requesting.").hasArg(true).create('r'));188 - // options.addOption(OptionBuilder.withArgName("url_license_server").withLongOpt("server").withDescription("License server url.").hasArg(true).create('s'));189 - options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("validate").withDescription("Validate lic file.").hasArg(true)190 - .create('l'));184 + @SuppressWarnings("static-access")185 + private static Options prepareOptionCLI() {186 + Options options = new Options();191 187 192 - options.addOption("g", "gen_request", false,193 - "Generate request file. If --rfile parameter is missing then It is generated in current directory.");194 - options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("create").withDescription("Request a license file to server.")195 - .hasArg(true).hasOptionalArg().create('c'));196 - options.addOption("t", "test_lc", false, "Test if License Server (LC) is available. ");197 - options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("renew").withDescription("Synchronize/renew the current license file.")198 - .hasArg(true).create('r'));188 + options.addOption("h", "help", false, "Show help.");189 + options.addOption(OptionBuilder.withArgName("req_file").withLongOpt("rfile")190 + .withDescription("Set request file for its generation or for license requesting.").hasArg(true).create('r'));191 + // options.addOption(OptionBuilder.withArgName("url_license_server").withLongOpt("server").withDescription("License server url.").hasArg(true).create('s'));192 + options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("validate").withDescription("Validate lic file.").hasArg(true)193 + .create('l'));199 194 200 - return options;201 - }195 + options.addOption("g", "gen_request", false,196 + "Generate request file. If --rfile parameter is missing then It is generated in current directory.");197 + options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("create").withDescription("Request a license file to server.")198 + .hasArg(true).hasOptionalArg().create('c'));199 + options.addOption("t", "test_lc", false, "Test if License Server (LC) is available. ");200 + options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("renew").withDescription("Synchronize/renew the current license file.")201 + .hasArg(true).create('r'));202 +203 + return options;204 + }202 205 203 206 } src/main/java/net/curisit/securis/LicenseManager.java
.. .. @@ -38,7 +38,8 @@ 38 38 public static final String HEADER_LICENSE_NAME_OR_REFERENCE = "X-SECURIS-LIC-NAMEREF"; 39 39 public static final String HEADER_LICENSE_EMAIL = "X-SECURIS-LIC-EMAIL"; 40 40 41 - private LicenseManager() {}41 + private LicenseManager() {42 + }42 43 43 44 public static LicenseManager getInstance() { 44 45 return singleton; .. .. @@ -62,7 +63,8 @@ 62 63 } 63 64 64 65 /** 65 - * Validates the license stored in {@code licFile} and get the corresponding LicenseBean66 + * Validates the license stored in {@code licFile} and get the corresponding67 + * LicenseBean66 68 * <p> 67 69 * The validation includes: 68 70 * <ul> .. .. @@ -82,7 +84,8 @@ 82 84 } 83 85 84 86 /** 85 - * Validates the license stored in {@code licFile} and get the corresponding LicenseBean. The License date is not validated87 + * Validates the license stored in {@code licFile} and get the corresponding88 + * LicenseBean. The License date is not validated86 89 * <p> 87 90 * The validation includes: 88 91 * <ul> .. .. @@ -99,7 +102,12 @@ 99 102 public LicenseBean validateLicense(File licFile, boolean excludeDateValidation) throws SeCurisException { 100 103 LicenseBean licBean = load(licFile); 101 104 SignatureHelper.getInstance().validateSignature(licBean); 102 - LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.LIC_TYPE_CODE, Params.get(Params.KEYS.APPLICATION_CODE)), Params.get(Params.KEYS.CUSTOMER_CODE), Params.get(Params.KEYS.PACK_CODE));105 + if (licBean.getActivationCode() != null) {106 + LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.APPLICATION_CODE), licBean.getActivationCode());107 + } else {108 + LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.LIC_TYPE_CODE, Params.get(Params.KEYS.APPLICATION_CODE)),109 + Params.get(Params.KEYS.CUSTOMER_CODE), Params.get(Params.KEYS.PACK_CODE));110 + }103 111 LicenseValidator.getInstance().validateLogo(licBean); 104 112 105 113 if (!excludeDateValidation) { .. .. @@ -118,7 +126,21 @@ 118 126 * @throws SeCurisException 119 127 */ 120 128 public SignedLicenseBean requestLicense(String nameOrReference, String email) throws SeCurisException { 121 - RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.LIC_TYPE_CODE, Params.get(Params.KEYS.APPLICATION_CODE)), Params.get(Params.KEYS.CUSTOMER_CODE), Params.get(Params.KEYS.PACK_CODE));129 + RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.LIC_TYPE_CODE, Params.get(Params.KEYS.APPLICATION_CODE)),130 + Params.get(Params.KEYS.CUSTOMER_CODE), Params.get(Params.KEYS.PACK_CODE));131 +132 + SignedLicenseBean lic = requestLicenseToServer(req, nameOrReference, email);133 + return lic;134 + }135 +136 + /**137 + * Request to server for a valid license138 + *139 + * @return The license bean returned by the server140 + * @throws SeCurisException141 + */142 + public SignedLicenseBean requestLicense(String nameOrReference, String email, String activationCode) throws SeCurisException {143 + RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), activationCode);122 144 123 145 SignedLicenseBean lic = requestLicenseToServer(req, nameOrReference, email); 124 146 return lic; .. .. @@ -170,7 +192,8 @@ 170 192 } 171 193 172 194 /** 173 - * Creates a new request file with current hardware in the File passed as parameter195 + * Creates a new request file with current hardware in the File passed as196 + * parameter174 197 * 175 198 * @param outputRequestFile 176 199 * File where the request data will be saved .. .. @@ -178,7 +201,8 @@ 178 201 * @throws SeCurisException 179 202 */ 180 203 public RequestBean createRequestFile(File outputRequestFile) throws SeCurisException { 181 - RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.LIC_TYPE_CODE, Params.get(Params.KEYS.APPLICATION_CODE)), Params.get(Params.KEYS.CUSTOMER_CODE), Params.get(Params.KEYS.PACK_CODE));204 + RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.LIC_TYPE_CODE, Params.get(Params.KEYS.APPLICATION_CODE)),205 + Params.get(Params.KEYS.CUSTOMER_CODE), Params.get(Params.KEYS.PACK_CODE));182 206 183 207 ReqGenerator.getInstance().save(req, outputRequestFile); 184 208 .. .. @@ -186,11 +210,30 @@ 186 210 } 187 211 188 212 /** 189 - * Send the current license file to server, which is previously validated, to get a renewed one if it is prepared in server side.213 + * Creates a new request file with current hardware in the File passed as214 + * parameter215 + *216 + * @param outputRequestFile217 + * File where the request data will be saved218 + * @return The generated request bean219 + * @throws SeCurisException220 + */221 + public RequestBean createRequestFile(File outputRequestFile, String activationCode) throws SeCurisException {222 + RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), activationCode);223 +224 + ReqGenerator.getInstance().save(req, outputRequestFile);225 +226 + return req;227 + }228 +229 + /**230 + * Send the current license file to server, which is previously validated,231 + * to get a renewed one if it is prepared in server side.190 232 * 191 233 * @param licenseFile 192 234 * Current and valid License file 193 - * @return New license bean if server creates a new one, otherwise the same current License bean will be returned235 + * @return New license bean if server creates a new one, otherwise the same236 + * current License bean will be returned194 237 * @throws SeCurisException 195 238 */ 196 239 public SignedLicenseBean renew(File licenseFile) throws SeCurisException { .. .. @@ -220,18 +263,4 @@ 220 263 } 221 264 } 222 265 223 - public static void main(String[] args) throws SeCurisException {224 - String filename = "/Users/rob/.doxr/doxr.lic";225 -226 - if (filename == null)227 - filename = "./license.req";228 - File file = new File(filename);229 - LicenseManager.getInstance().validateLicense(file);230 - LOG.info("Validation Ok");231 - String r = "{\"licenseTypeCode\":\"DX01\",\"packCode\":\"DEVDX01\",\"arch\":\"x86_64\",\"osName\":\"darwin\",\"customerCode\":\"CIT\",\"macAddresses\":[\"60:c5:47:03:cf:c8\",\"b2:00:12:b6:88:e0\"]}";232 - RequestBean rb = JsonUtils.json2object(r, RequestBean.class);233 - System.out.println(rb);234 - LOG.info("Request file {} generated OK", file.getAbsolutePath());235 -236 - }237 266 } src/main/java/net/curisit/securis/LicenseValidator.java
.. .. @@ -61,8 +61,16 @@ 61 61 public void validateHW(RequestBean reqBean, String licTypeCode, String customerCode, String packCode) throws SeCurisException { 62 62 RequestBean currentHW = ReqGenerator.getInstance().createRequest(licTypeCode, customerCode, packCode); 63 63 if (!currentHW.match(reqBean)) { 64 - throw new SeCurisException("Current System info mismatch the License System info:\n Licensed: \n" + JsonUtils.toPrettyJSON(reqBean)65 - + "\n Expected: \n" + JsonUtils.toPrettyJSON(currentHW));64 + throw new SeCurisException("Current System info mismatch the License System info:\n Licensed: \n"65 + + JsonUtils.toPrettyJSON(reqBean, RequestBean.class) + "\n Expected: \n" + JsonUtils.toPrettyJSON(currentHW));66 + }67 + }68 +69 + public void validateHW(RequestBean reqBean, String appCode, String activationCode) throws SeCurisException {70 + RequestBean currentHW = ReqGenerator.getInstance().createRequest(appCode, activationCode);71 + if (!currentHW.match(reqBean)) {72 + throw new SeCurisException("Current System info mismatch the License System info:\n Licensed: \n"73 + + JsonUtils.toPrettyJSON(reqBean, RequestBean.class) + "\n Expected: \n" + JsonUtils.toPrettyJSON(currentHW));66 74 } 67 75 } 68 76 src/main/java/net/curisit/securis/ReqGenerator.java
.. .. @@ -44,6 +44,19 @@ 44 44 return req; 45 45 } 46 46 47 + public RequestBean createRequest(String appCode, String activationCode) throws SeCurisException {48 + RequestBean req = new RequestBean();49 +50 + req.setAppCode(appCode);51 + req.setActivationCode(activationCode);52 + req.setArch(HWInfo.getArch());53 + req.setCrcLogo(getCrcLogo());54 + req.setMacAddresses(HWInfo.getMACAddress());55 + req.setOsName(HWInfo.getOsName());56 +57 + return req;58 + }59 +47 60 public RequestBean loadRequest(File requestFile) throws SeCurisException { 48 61 try { 49 62 String json = new String(Files.readAllBytes(Paths.get(requestFile.toURI())), "utf-8"); src/main/java/net/curisit/securis/beans/LicenseBean.java
.. .. @@ -11,7 +11,8 @@ 11 11 12 12 @JsonAutoDetect 13 13 @JsonPropertyOrder({ 14 - "appName", "licenseCode", "licenseTypeCode", "expirationDate", "arch", "osName", "customerCode", "macAddresses", "crcLogo", "metadata", "packCode"14 + "appCode", "appName", "licenseCode", "activationCode", "licenseTypeCode", "expirationDate", "arch", "osName", "customerCode", "macAddresses",15 + "crcLogo", "metadata", "packCode"15 16 }) 16 17 public class LicenseBean extends RequestBean { 17 18 private Date expirationDate; src/main/java/net/curisit/securis/beans/RequestBean.java
.. .. @@ -2,9 +2,7 @@ 2 2 3 3 import java.util.Collections; 4 4 import java.util.List; 5 -6 -import net.curisit.securis.SeCurisException;7 -import net.curisit.securis.utils.JsonUtils;5 +import java.util.Objects;8 6 9 7 import com.fasterxml.jackson.annotation.JsonAutoDetect; 10 8 import com.fasterxml.jackson.annotation.JsonInclude; .. .. @@ -14,9 +12,11 @@ 14 12 @JsonAutoDetect 15 13 @JsonInclude(Include.NON_NULL) 16 14 @JsonPropertyOrder({ 17 - "licenseTypeCode", "packCode", "arch", "osName", "customerCode", "macAddresses", "crcLogo"15 + "appCode", "activationCode", "licenseTypeCode", "packCode", "arch", "osName", "customerCode", "macAddresses", "crcLogo"18 16 }) 19 17 public class RequestBean { 18 + private String appCode;19 + private String activationCode;20 20 private String customerCode; 21 21 private String crcLogo; 22 22 private String licenseTypeCode; .. .. @@ -24,6 +24,14 @@ 24 24 private List<String> macAddresses; 25 25 private String osName; 26 26 private String arch; 27 +28 + public String getActivationCode() {29 + return activationCode;30 + }31 +32 + public void setActivationCode(String activationCode) {33 + this.activationCode = activationCode;34 + }27 35 28 36 public String getCustomerCode() { 29 37 return customerCode; .. .. @@ -48,9 +56,9 @@ 48 56 public void setMacAddresses(List<String> macAddresses) { 49 57 // We need to be sure that the same mac addresses are always shown in 50 58 // the same order, to generate a valid hash 51 - if (macAddresses != null) {52 - Collections.<String> sort(macAddresses);53 - }59 + if (macAddresses != null) {60 + Collections.<String> sort(macAddresses);61 + }54 62 this.macAddresses = macAddresses; 55 63 } 56 64 .. .. @@ -71,13 +79,21 @@ 71 79 } 72 80 73 81 public boolean match(RequestBean rb) { 82 + boolean result = true;83 + if (activationCode != null) {84 + result = result && activationCode.equals(rb.activationCode);85 + // appCode is optional86 + result = result && (appCode == null || appCode.equals(rb.appCode));87 + } else {88 + // If activationCode exists then we ignore other license identifiers89 + result = result && licenseTypeCode != null && licenseTypeCode.equals(rb.licenseTypeCode);90 + result = result && (customerCode != null && customerCode.equals(rb.customerCode));91 + result = result && (packCode != null && packCode.equals(rb.packCode));92 + }74 93 75 - boolean result = licenseTypeCode != null && licenseTypeCode.equals(rb.licenseTypeCode);76 - result = result && (packCode != null && packCode.equals(rb.packCode));77 94 result = result && (arch != null && arch.equals(rb.arch)); 78 95 // CRC Logo validation is optional, only if request file contains it 79 96 result = result && (crcLogo == null || crcLogo.equals(rb.crcLogo)); 80 - result = result && (customerCode != null && customerCode.equals(rb.customerCode));81 97 result = result && (osName != null && osName.equals(rb.osName)); 82 98 result = result && (macAddresses != null && rb.macAddresses != null) && anyMacIsIncluded(rb.getMacAddresses()); 83 99 .. .. @@ -95,6 +111,8 @@ 95 111 @Override 96 112 public int hashCode() { 97 113 int code = 0; 114 + code += appCode != null ? appCode.hashCode() : 0;115 + code += activationCode != null ? activationCode.hashCode() : 0;98 116 code += licenseTypeCode != null ? licenseTypeCode.hashCode() : 0; 99 117 code += arch != null ? arch.hashCode() : 0; 100 118 code += crcLogo != null ? crcLogo.hashCode() : 0; .. .. @@ -111,14 +129,17 @@ 111 129 if (!(obj instanceof RequestBean)) 112 130 return false; 113 131 RequestBean rb = (RequestBean) obj; 114 - boolean result = (rb.licenseTypeCode == null && licenseTypeCode == null)115 - || (licenseTypeCode != null && licenseTypeCode.equals(rb.licenseTypeCode));116 - result = result && ((rb.packCode == null && packCode == null) || (packCode != null && packCode.equals(rb.arch)));117 - result = result && ((rb.arch == null && arch == null) || (arch != null && arch.equals(rb.arch)));118 - result = result && ((rb.crcLogo == null && crcLogo == null) || (crcLogo != null && crcLogo.equals(rb.crcLogo)));119 - result = result && ((rb.customerCode == null && customerCode == null) || (customerCode != null && customerCode.equals(rb.customerCode)));120 - result = result && ((rb.osName == null && osName == null) || (osName != null && osName.equals(rb.osName)));121 - result = result && ((rb.macAddresses == null && macAddresses == null) || (macAddresses != null && macAddresses.equals(rb.macAddresses)));132 +133 + boolean result = true;134 + result = result && Objects.equals(appCode, rb.appCode);135 + result = result && Objects.equals(activationCode, rb.activationCode);136 + result = result && Objects.equals(licenseTypeCode, rb.licenseTypeCode);137 + result = result && Objects.equals(packCode, rb.packCode);138 + result = result && Objects.equals(arch, rb.arch);139 + result = result && Objects.equals(crcLogo, rb.crcLogo);140 + result = result && Objects.equals(customerCode, rb.customerCode);141 + result = result && Objects.equals(osName, rb.osName);142 + result = result && Objects.equals(macAddresses, rb.macAddresses);122 143 return result; 123 144 } 124 145 .. .. @@ -138,13 +159,11 @@ 138 159 this.licenseTypeCode = licenseTypeCode; 139 160 } 140 161 141 - public static void main(String[] args) {142 - String requestData = "{\n \"licenseTypeCode\": \"CI01\",\n \"packCode\": \"CIBP01\",\n \"arch\": \"amd64\",\n \"osName\": \"Windows Server 2012\",\n \"customerCode\": \"BP01\",\n \"macAddresses\": [\n \"02-71-1A-0E-5D-7C\"\n ],\n \"crcLogo\": \"153e09683eaa26d23c951f6605af5f874b533a917f73681e3e0a1f175aae3981\"\n}";143 - try {144 - JsonUtils.json2object(requestData, RequestBean.class);145 - } catch (SeCurisException e) {146 - // TODO Auto-generated catch block147 - e.printStackTrace();148 - }162 + public String getAppCode() {163 + return appCode;164 + }165 +166 + public void setAppCode(String appCode) {167 + this.appCode = appCode;149 168 } 150 169 } src/main/java/net/curisit/securis/utils/JsonUtils.java
.. .. @@ -135,6 +135,26 @@ 135 135 } 136 136 137 137 /** 138 + * Create a JSON string from a object compatible or annotated with Jackson,139 + * i.e: <code>140 + * {"f1":2345,"f2":"Test de valor"}141 + *142 + * @param obj143 + * @return JSON string representation from object144 + */145 + public static String toPrettyJSON(Object obj, Class<?> klass) throws SeCurisException {146 + // and could also do other configuration...147 + try {148 + if (obj == null)149 + return null;150 + return MAPPER_PRETTY.writerWithType(klass).writeValueAsString(obj);151 + } catch (IOException e) {152 + LOG.error("Error formating JSON from object: {}", obj, e);153 + throw new SeCurisException("Error formating JSON from object: " + obj, e);154 + }155 + }156 +157 + /**138 158 * Create a Map from a json string, i.e: <code> 139 159 * {"f1":2345,"f2":"Test de valor"} 140 160 * </code>