From 06f5e0b58dd77a11519f5f3d8e3b9ffe99b3b291 Mon Sep 17 00:00:00 2001
From: rsanchez <rsanchez@curisit.net>
Date: Wed, 17 Dec 2014 12:30:22 +0000
Subject: [PATCH] #2205 feature - Added support for name and email in license requests

---
 src/main/java/net/curisit/securis/ConnectionManager.java |   14 ++
 src/main/java/net/curisit/securis/LicenseManager.java    |   15 +
 pom.xml                                                  |    2 
 src/main/java/net/curisit/securis/License.java           |  272 ++++++++++++++++++++++----------------------
 4 files changed, 162 insertions(+), 141 deletions(-)

diff --git a/pom.xml b/pom.xml
index da9e8a0..edc269a 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.3-SNAPSHOT</version>
+	<version>1.0.4-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 c46ebe9..5b9b3c1 100644
--- a/src/main/java/net/curisit/securis/ConnectionManager.java
+++ b/src/main/java/net/curisit/securis/ConnectionManager.java
@@ -8,6 +8,7 @@
 import java.security.NoSuchAlgorithmException;
 import java.security.cert.CertificateException;
 import java.security.cert.X509Certificate;
+import java.util.Map;
 
 import net.curisit.securis.beans.RequestBean;
 import net.curisit.securis.utils.JsonUtils;
@@ -79,10 +80,21 @@
 	}
 
 	public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException {
+		return executePost(command, returnType, req, null);
+	}
+
+	public <T> T executePost(String command, Class<T> returnType, RequestBean req, Map<String, String> headers) 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);
+
+		if (headers != null) {
+			for (String header : headers.keySet()) {
+				String headerValue = headers.get(header);
+				postRequest.addHeader(header, headerValue);
+			}
+		}
+
 		try {
 			postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req)));
 		} catch (UnsupportedEncodingException | SeCurisException e1) {
diff --git a/src/main/java/net/curisit/securis/License.java b/src/main/java/net/curisit/securis/License.java
index e33c9fb..14d0aa4 100644
--- a/src/main/java/net/curisit/securis/License.java
+++ b/src/main/java/net/curisit/securis/License.java
@@ -41,161 +41,163 @@
  */
 public class License {
 
-    private static final Logger LOG = LogManager.getLogger(License.class);
+	private static final Logger LOG = LogManager.getLogger(License.class);
 
-    /**
-     * Aplication entry point when it used as CLI
-     * 
-     * @param args
-     * @throws URISyntaxException
-     */
-    public static void main(String[] args) throws URISyntaxException {
+	/**
+	 * Aplication entry point when it used as CLI
+	 * 
+	 * @param args
+	 * @throws URISyntaxException
+	 */
+	public static void main(String[] args) throws URISyntaxException {
 
-        LOG.debug("SeCuris client tool init ");
+		LOG.debug("SeCuris client tool init ");
 
-        checkConfigFile();
-        CommandLine cmd = getCommandLine(args);
+		checkConfigFile();
+		CommandLine cmd = getCommandLine(args);
 
-        try {
-            if (cmd.hasOption('g')) {
-                String filename = cmd.getOptionValue("rfile");
-                if (filename == null)
-                    filename = "./license.req";
-                File file = new File(filename);
-                LicenseManager.getInstance().createRequestFile(file);
-                LOG.info("Request file {} generated OK", file.getAbsolutePath());
-                System.exit(0);
-            }
+		try {
+			if (cmd.hasOption('g')) {
+				String filename = cmd.getOptionValue("rfile");
+				if (filename == null)
+					filename = "./license.req";
+				File file = new File(filename);
+				LicenseManager.getInstance().createRequestFile(file);
+				LOG.info("Request file {} generated OK", file.getAbsolutePath());
+				System.exit(0);
+			}
 
-            if (cmd.hasOption('l')) {
-                String filename = cmd.getOptionValue("validate");
-                if (filename == null)
-                    filename = "./license.lic";
-                File file = new File(filename);
-                if (!file.exists()) {
-                    throw new SeCurisException("The license file doesn't exist: " + file.getAbsolutePath());
-                }
-                try {
-                    LicenseManager.getInstance().validateLicense(file);
-                    LOG.info("License file {} is valid", file.getAbsolutePath());
-                } catch (SeCurisException e) {
-                    LOG.info("License file {} is NOT valid", file.getAbsolutePath());
-                    LOG.info("Reason: {}", e.toString());
-                }
+			if (cmd.hasOption('l')) {
+				String filename = cmd.getOptionValue("validate");
+				if (filename == null)
+					filename = "./license.lic";
+				File file = new File(filename);
+				if (!file.exists()) {
+					throw new SeCurisException("The license file doesn't exist: " + file.getAbsolutePath());
+				}
+				try {
+					LicenseManager.getInstance().validateLicense(file);
+					LOG.info("License file {} is valid", file.getAbsolutePath());
+				} catch (SeCurisException e) {
+					LOG.info("License file {} is NOT valid", file.getAbsolutePath());
+					LOG.info("Reason: {}", e.toString());
+				}
 
-                System.exit(0);
-            }
+				System.exit(0);
+			}
 
-            if (cmd.hasOption('c')) {
-                SignedLicenseBean lic = LicenseManager.getInstance().requestLicense();
-                String filename = cmd.getOptionValue("c");
-                if (filename == null)
-                    filename = "./license.lic";
-                File file = new File(filename);
-                LicenseManager.getInstance().save(lic, file);
-                LOG.info("License file sucessfully saved in file: {}", file.getAbsolutePath());
-                System.exit(0);
-            }
+			if (cmd.hasOption('c')) {
+				// TODO: Change CLI to get name and email in license request
+				SignedLicenseBean lic = LicenseManager.getInstance().requestLicense(null, null);
+				String filename = cmd.getOptionValue("c");
+				if (filename == null) {
+					filename = "./license.lic";
+				}
+				File file = new File(filename);
+				LicenseManager.getInstance().save(lic, file);
+				LOG.info("License file sucessfully saved in file: {}", file.getAbsolutePath());
+				System.exit(0);
+			}
 
-            if (cmd.hasOption('t')) {
-                LicenseManager.getInstance().testServer();
-                LOG.info("Server is OK, url: {}", Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL));
+			if (cmd.hasOption('t')) {
+				LicenseManager.getInstance().testServer();
+				LOG.info("Server is OK, url: {}", Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL));
 
-                System.exit(0);
-            }
+				System.exit(0);
+			}
 
-            if (cmd.hasOption('r')) {
-                String licFilename = cmd.getOptionValue("renew");
-                checkMandatoryParameter(licFilename, "renew");
-                File file = new File(licFilename);
-                if (!file.exists()) {
-                    throw new SeCurisException("The license file doesn't exist: " + file.getAbsolutePath());
-                }
-                SignedLicenseBean newLic = LicenseManager.getInstance().renew(file);
-                File oldLicFile = new File(file.getAbsoluteFile() + ".old");
-                file.renameTo(oldLicFile);
-                LOG.info("Old license file has been renamed to: {}", oldLicFile.getAbsolutePath());
-                LicenseManager.getInstance().save(newLic, file);
-                LOG.info("New license file saved as: {}", file.getAbsolutePath());
-                System.exit(0);
-            }
+			if (cmd.hasOption('r')) {
+				String licFilename = cmd.getOptionValue("renew");
+				checkMandatoryParameter(licFilename, "renew");
+				File file = new File(licFilename);
+				if (!file.exists()) {
+					throw new SeCurisException("The license file doesn't exist: " + file.getAbsolutePath());
+				}
+				SignedLicenseBean newLic = LicenseManager.getInstance().renew(file);
+				File oldLicFile = new File(file.getAbsoluteFile() + ".old");
+				file.renameTo(oldLicFile);
+				LOG.info("Old license file has been renamed to: {}", oldLicFile.getAbsolutePath());
+				LicenseManager.getInstance().save(newLic, file);
+				LOG.info("New license file saved as: {}", file.getAbsolutePath());
+				System.exit(0);
+			}
 
-        } catch (SeCurisException e) {
-            LOG.error("The command generated an error: {}", e.toString());
-        }
-    }
+		} catch (SeCurisException e) {
+			LOG.error("The command generated an error: {}", e.toString());
+		}
+	}
 
-    private static void checkMandatoryParameter(String value, String param) {
-        if (value == null) {
-            LOG.error("Parameter {} is mandatory. Use --help to get information about parameters", param);
-            System.exit(-5);
-        }
-    }
+	private static void checkMandatoryParameter(String value, String param) {
+		if (value == null) {
+			LOG.error("Parameter {} is mandatory. Use --help to get information about parameters", param);
+			System.exit(-5);
+		}
+	}
 
-    /**
-     * Checks that config file exists and contains mandatory parameters
-     */
-    private static void checkConfigFile() {
-        if (Params.get(Params.KEYS.APPLICATION_CODE) == null) {
-            LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.APPLICATION_CODE);
-            System.exit(-3);
-        }
-        if (Params.get(Params.KEYS.CUSTOMER_CODE) == null) {
-            LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.CUSTOMER_CODE);
-            System.exit(-4);
-        }
-        if (Params.get(Params.KEYS.PACK_CODE) == null) {
-            LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.PACK_CODE);
-            System.exit(-6);
-        }
-    }
+	/**
+	 * Checks that config file exists and contains mandatory parameters
+	 */
+	private static void checkConfigFile() {
+		if (Params.get(Params.KEYS.APPLICATION_CODE) == null) {
+			LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.APPLICATION_CODE);
+			System.exit(-3);
+		}
+		if (Params.get(Params.KEYS.CUSTOMER_CODE) == null) {
+			LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.CUSTOMER_CODE);
+			System.exit(-4);
+		}
+		if (Params.get(Params.KEYS.PACK_CODE) == null) {
+			LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.PACK_CODE);
+			System.exit(-6);
+		}
+	}
 
-    private static CommandLine getCommandLine(String[] args) {
-        Options ops = prepareOptionCLI();
-        if (args.length == 0) {
-            printHelp(ops);
-        }
-        CommandLineParser parser = new PosixParser();
-        CommandLine cmd = null;
-        try {
-            cmd = parser.parse(ops, args);
-        } catch (ParseException e) {
-            printHelp(ops);
-        }
+	private static CommandLine getCommandLine(String[] args) {
+		Options ops = prepareOptionCLI();
+		if (args.length == 0) {
+			printHelp(ops);
+		}
+		CommandLineParser parser = new PosixParser();
+		CommandLine cmd = null;
+		try {
+			cmd = parser.parse(ops, args);
+		} catch (ParseException e) {
+			printHelp(ops);
+		}
 
-        if (cmd.hasOption('h')) {
-            printHelp(ops);
-        }
+		if (cmd.hasOption('h')) {
+			printHelp(ops);
+		}
 
-        return cmd;
-    }
+		return cmd;
+	}
 
-    private static void printHelp(Options ops) {
-        HelpFormatter formatter = new HelpFormatter();
-        formatter.printHelp("securis-client", ops, true);
-        System.exit(-1);
-    }
+	private static void printHelp(Options ops) {
+		HelpFormatter formatter = new HelpFormatter();
+		formatter.printHelp("securis-client", ops, true);
+		System.exit(-1);
+	}
 
-    @SuppressWarnings("static-access")
-    private static Options prepareOptionCLI() {
-        Options options = new Options();
+	@SuppressWarnings("static-access")
+	private static Options prepareOptionCLI() {
+		Options options = new Options();
 
-        options.addOption("h", "help", false, "Show help.");
-        options.addOption(OptionBuilder.withArgName("req_file").withLongOpt("rfile")
-                .withDescription("Set request file for its generation or for license requesting.").hasArg(true).create('r'));
-        // options.addOption(OptionBuilder.withArgName("url_license_server").withLongOpt("server").withDescription("License server url.").hasArg(true).create('s'));
-        options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("validate").withDescription("Validate lic file.").hasArg(true)
-                .create('l'));
+		options.addOption("h", "help", false, "Show help.");
+		options.addOption(OptionBuilder.withArgName("req_file").withLongOpt("rfile")
+				.withDescription("Set request file for its generation or for license requesting.").hasArg(true).create('r'));
+		// options.addOption(OptionBuilder.withArgName("url_license_server").withLongOpt("server").withDescription("License server url.").hasArg(true).create('s'));
+		options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("validate").withDescription("Validate lic file.").hasArg(true)
+				.create('l'));
 
-        options.addOption("g", "gen_request", false,
-                "Generate request file. If --rfile parameter is missing then It is generated in current directory.");
-        options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("create").withDescription("Request a license file to server.")
-                .hasArg(true).hasOptionalArg().create('c'));
-        options.addOption("t", "test_lc", false, "Test if License Server (LC) is available. ");
-        options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("renew").withDescription("Synchronize/renew the current license file.")
-                .hasArg(true).create('r'));
+		options.addOption("g", "gen_request", false,
+				"Generate request file. If --rfile parameter is missing then It is generated in current directory.");
+		options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("create").withDescription("Request a license file to server.")
+				.hasArg(true).hasOptionalArg().create('c'));
+		options.addOption("t", "test_lc", false, "Test if License Server (LC) is available. ");
+		options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("renew").withDescription("Synchronize/renew the current license file.")
+				.hasArg(true).create('r'));
 
-        return options;
-    }
+		return options;
+	}
 
 }
diff --git a/src/main/java/net/curisit/securis/LicenseManager.java b/src/main/java/net/curisit/securis/LicenseManager.java
index 540119b..96b5f9c 100644
--- a/src/main/java/net/curisit/securis/LicenseManager.java
+++ b/src/main/java/net/curisit/securis/LicenseManager.java
@@ -7,6 +7,8 @@
 import java.nio.file.Paths;
 import java.nio.file.StandardOpenOption;
 import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
 
 import net.curisit.securis.ConnectionManager.Command;
 import net.curisit.securis.beans.LicenseBean;
@@ -33,6 +35,8 @@
 	private static LicenseManager singleton = new LicenseManager();
 
 	public static final String PING_MESSAGE = "SeCuris API OK";
+	public static final String HEADER_LICENSE_NAME_OR_REFERENCE = "X-SECURIS-LIC-NAMEREF";
+	public static final String HEADER_LICENSE_EMAIL = "X-SECURIS-LIC-EMAIL";
 
 	private LicenseManager() {}
 
@@ -116,11 +120,11 @@
 	 * @return The license bean returned by the server
 	 * @throws SeCurisException
 	 */
-	public SignedLicenseBean requestLicense() throws SeCurisException {
+	public SignedLicenseBean requestLicense(String nameOrReference, String email) 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);
+		SignedLicenseBean lic = requestLicenseToServer(req, nameOrReference, email);
 		return lic;
 	}
 
@@ -160,8 +164,11 @@
 
 	}
 
-	private SignedLicenseBean requestLicenseToServer(RequestBean req) throws SeCurisException {
-		SignedLicenseBean lic = ConnectionManager.getInstance().executePost(Command.CREATE_LIC, SignedLicenseBean.class, req);
+	private SignedLicenseBean requestLicenseToServer(RequestBean req, String nameOrReference, String email) throws SeCurisException {
+		Map<String, String> headers = new HashMap<String, String>();
+		headers.put(HEADER_LICENSE_NAME_OR_REFERENCE, nameOrReference);
+		headers.put(HEADER_LICENSE_EMAIL, email);
+		SignedLicenseBean lic = ConnectionManager.getInstance().executePost(Command.CREATE_LIC, SignedLicenseBean.class, req, headers);
 
 		return lic;
 	}

--
Gitblit v1.3.2