rsanchez
2014-12-17 06f5e0b58dd77a11519f5f3d8e3b9ffe99b3b291
#2205 feature - Added support for name and email in license requests
4 files modified
changed files
pom.xml patch | view | blame | history
src/main/java/net/curisit/securis/ConnectionManager.java patch | view | blame | history
src/main/java/net/curisit/securis/License.java patch | view | blame | history
src/main/java/net/curisit/securis/LicenseManager.java patch | view | blame | history
pom.xml
....@@ -3,7 +3,7 @@
33 <modelVersion>4.0.0</modelVersion>
44 <groupId>net.curisit</groupId>
55 <artifactId>securis-client</artifactId>
6
- <version>1.0.3-SNAPSHOT</version>
6
+ <version>1.0.4-SNAPSHOT</version>
77 <build>
88 <plugins>
99 <plugin>
src/main/java/net/curisit/securis/ConnectionManager.java
....@@ -8,6 +8,7 @@
88 import java.security.NoSuchAlgorithmException;
99 import java.security.cert.CertificateException;
1010 import java.security.cert.X509Certificate;
11
+import java.util.Map;
1112
1213 import net.curisit.securis.beans.RequestBean;
1314 import net.curisit.securis.utils.JsonUtils;
....@@ -79,10 +80,21 @@
7980 }
8081
8182 public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException {
83
+ return executePost(command, returnType, req, null);
84
+ }
85
+
86
+ public <T> T executePost(String command, Class<T> returnType, RequestBean req, Map<String, String> headers) throws SeCurisException {
8287 HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command));
8388 postRequest.addHeader("accept", JSON_MEDIA_TYPE);
84
-
8589 postRequest.addHeader("content-type", JSON_MEDIA_TYPE);
90
+
91
+ if (headers != null) {
92
+ for (String header : headers.keySet()) {
93
+ String headerValue = headers.get(header);
94
+ postRequest.addHeader(header, headerValue);
95
+ }
96
+ }
97
+
8698 try {
8799 postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req)));
88100 } catch (UnsupportedEncodingException | SeCurisException e1) {
src/main/java/net/curisit/securis/License.java
....@@ -41,161 +41,163 @@
4141 */
4242 public class License {
4343
44
- private static final Logger LOG = LogManager.getLogger(License.class);
44
+ private static final Logger LOG = LogManager.getLogger(License.class);
4545
46
- /**
47
- * Aplication entry point when it used as CLI
48
- *
49
- * @param args
50
- * @throws URISyntaxException
51
- */
52
- public static void main(String[] args) throws URISyntaxException {
46
+ /**
47
+ * Aplication entry point when it used as CLI
48
+ *
49
+ * @param args
50
+ * @throws URISyntaxException
51
+ */
52
+ public static void main(String[] args) throws URISyntaxException {
5353
54
- LOG.debug("SeCuris client tool init ");
54
+ LOG.debug("SeCuris client tool init ");
5555
56
- checkConfigFile();
57
- CommandLine cmd = getCommandLine(args);
56
+ checkConfigFile();
57
+ CommandLine cmd = getCommandLine(args);
5858
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
+ }
6969
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
+ }
8585
86
- System.exit(0);
87
- }
86
+ System.exit(0);
87
+ }
8888
89
- if (cmd.hasOption('c')) {
90
- SignedLicenseBean lic = LicenseManager.getInstance().requestLicense();
91
- String filename = cmd.getOptionValue("c");
92
- if (filename == null)
93
- filename = "./license.lic";
94
- File file = new File(filename);
95
- LicenseManager.getInstance().save(lic, file);
96
- LOG.info("License file sucessfully saved in file: {}", file.getAbsolutePath());
97
- System.exit(0);
98
- }
89
+ if (cmd.hasOption('c')) {
90
+ // TODO: Change CLI to get name and email in license request
91
+ 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
+ }
99101
100
- if (cmd.hasOption('t')) {
101
- LicenseManager.getInstance().testServer();
102
- 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));
103105
104
- System.exit(0);
105
- }
106
+ System.exit(0);
107
+ }
106108
107
- if (cmd.hasOption('r')) {
108
- String licFilename = cmd.getOptionValue("renew");
109
- checkMandatoryParameter(licFilename, "renew");
110
- File file = new File(licFilename);
111
- if (!file.exists()) {
112
- throw new SeCurisException("The license file doesn't exist: " + file.getAbsolutePath());
113
- }
114
- SignedLicenseBean newLic = LicenseManager.getInstance().renew(file);
115
- File oldLicFile = new File(file.getAbsoluteFile() + ".old");
116
- file.renameTo(oldLicFile);
117
- LOG.info("Old license file has been renamed to: {}", oldLicFile.getAbsolutePath());
118
- LicenseManager.getInstance().save(newLic, file);
119
- LOG.info("New license file saved as: {}", file.getAbsolutePath());
120
- System.exit(0);
121
- }
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
+ }
122124
123
- } catch (SeCurisException e) {
124
- LOG.error("The command generated an error: {}", e.toString());
125
- }
126
- }
125
+ } catch (SeCurisException e) {
126
+ LOG.error("The command generated an error: {}", e.toString());
127
+ }
128
+ }
127129
128
- private static void checkMandatoryParameter(String value, String param) {
129
- if (value == null) {
130
- LOG.error("Parameter {} is mandatory. Use --help to get information about parameters", param);
131
- System.exit(-5);
132
- }
133
- }
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
+ }
134136
135
- /**
136
- * Checks that config file exists and contains mandatory parameters
137
- */
138
- private static void checkConfigFile() {
139
- if (Params.get(Params.KEYS.APPLICATION_CODE) == null) {
140
- LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.APPLICATION_CODE);
141
- System.exit(-3);
142
- }
143
- if (Params.get(Params.KEYS.CUSTOMER_CODE) == null) {
144
- LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.CUSTOMER_CODE);
145
- System.exit(-4);
146
- }
147
- if (Params.get(Params.KEYS.PACK_CODE) == null) {
148
- LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.PACK_CODE);
149
- System.exit(-6);
150
- }
151
- }
137
+ /**
138
+ * Checks that config file exists and contains mandatory parameters
139
+ */
140
+ private static void checkConfigFile() {
141
+ if (Params.get(Params.KEYS.APPLICATION_CODE) == null) {
142
+ LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.APPLICATION_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
+ }
152154
153
- private static CommandLine getCommandLine(String[] args) {
154
- Options ops = prepareOptionCLI();
155
- if (args.length == 0) {
156
- printHelp(ops);
157
- }
158
- CommandLineParser parser = new PosixParser();
159
- CommandLine cmd = null;
160
- try {
161
- cmd = parser.parse(ops, args);
162
- } catch (ParseException e) {
163
- printHelp(ops);
164
- }
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
+ }
165167
166
- if (cmd.hasOption('h')) {
167
- printHelp(ops);
168
- }
168
+ if (cmd.hasOption('h')) {
169
+ printHelp(ops);
170
+ }
169171
170
- return cmd;
171
- }
172
+ return cmd;
173
+ }
172174
173
- private static void printHelp(Options ops) {
174
- HelpFormatter formatter = new HelpFormatter();
175
- formatter.printHelp("securis-client", ops, true);
176
- System.exit(-1);
177
- }
175
+ private static void printHelp(Options ops) {
176
+ HelpFormatter formatter = new HelpFormatter();
177
+ formatter.printHelp("securis-client", ops, true);
178
+ System.exit(-1);
179
+ }
178180
179
- @SuppressWarnings("static-access")
180
- private static Options prepareOptionCLI() {
181
- Options options = new Options();
181
+ @SuppressWarnings("static-access")
182
+ private static Options prepareOptionCLI() {
183
+ Options options = new Options();
182184
183
- options.addOption("h", "help", false, "Show help.");
184
- options.addOption(OptionBuilder.withArgName("req_file").withLongOpt("rfile")
185
- .withDescription("Set request file for its generation or for license requesting.").hasArg(true).create('r'));
186
- // options.addOption(OptionBuilder.withArgName("url_license_server").withLongOpt("server").withDescription("License server url.").hasArg(true).create('s'));
187
- options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("validate").withDescription("Validate lic file.").hasArg(true)
188
- .create('l'));
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'));
189191
190
- options.addOption("g", "gen_request", false,
191
- "Generate request file. If --rfile parameter is missing then It is generated in current directory.");
192
- options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("create").withDescription("Request a license file to server.")
193
- .hasArg(true).hasOptionalArg().create('c'));
194
- options.addOption("t", "test_lc", false, "Test if License Server (LC) is available. ");
195
- options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("renew").withDescription("Synchronize/renew the current license file.")
196
- .hasArg(true).create('r'));
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'));
197199
198
- return options;
199
- }
200
+ return options;
201
+ }
200202
201203 }
src/main/java/net/curisit/securis/LicenseManager.java
....@@ -7,6 +7,8 @@
77 import java.nio.file.Paths;
88 import java.nio.file.StandardOpenOption;
99 import java.util.Date;
10
+import java.util.HashMap;
11
+import java.util.Map;
1012
1113 import net.curisit.securis.ConnectionManager.Command;
1214 import net.curisit.securis.beans.LicenseBean;
....@@ -33,6 +35,8 @@
3335 private static LicenseManager singleton = new LicenseManager();
3436
3537 public static final String PING_MESSAGE = "SeCuris API OK";
38
+ public static final String HEADER_LICENSE_NAME_OR_REFERENCE = "X-SECURIS-LIC-NAMEREF";
39
+ public static final String HEADER_LICENSE_EMAIL = "X-SECURIS-LIC-EMAIL";
3640
3741 private LicenseManager() {}
3842
....@@ -116,11 +120,11 @@
116120 * @return The license bean returned by the server
117121 * @throws SeCurisException
118122 */
119
- public SignedLicenseBean requestLicense() throws SeCurisException {
123
+ public SignedLicenseBean requestLicense(String nameOrReference, String email) throws SeCurisException {
120124 RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),
121125 Params.get(Params.KEYS.PACK_CODE));
122126
123
- SignedLicenseBean lic = requestLicenseToServer(req);
127
+ SignedLicenseBean lic = requestLicenseToServer(req, nameOrReference, email);
124128 return lic;
125129 }
126130
....@@ -160,8 +164,11 @@
160164
161165 }
162166
163
- private SignedLicenseBean requestLicenseToServer(RequestBean req) throws SeCurisException {
164
- SignedLicenseBean lic = ConnectionManager.getInstance().executePost(Command.CREATE_LIC, SignedLicenseBean.class, req);
167
+ private SignedLicenseBean requestLicenseToServer(RequestBean req, String nameOrReference, String email) throws SeCurisException {
168
+ Map<String, String> headers = new HashMap<String, String>();
169
+ headers.put(HEADER_LICENSE_NAME_OR_REFERENCE, nameOrReference);
170
+ headers.put(HEADER_LICENSE_EMAIL, email);
171
+ SignedLicenseBean lic = ConnectionManager.getInstance().executePost(Command.CREATE_LIC, SignedLicenseBean.class, req, headers);
165172
166173 return lic;
167174 }