rsanchez
2014-09-30 f1702d6537568b1677254e27d772d6aa6d658e2c
#2021 fix - Added renew command in client
3 files modified
changed files
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
src/main/java/net/curisit/securis/ConnectionManager.java
....@@ -1,5 +1,6 @@
11 package net.curisit.securis;
22
3
+import java.awt.PageAttributes.MediaType;
34 import java.io.IOException;
45 import java.io.UnsupportedEncodingException;
56 import java.security.KeyManagementException;
....@@ -8,6 +9,8 @@
89 import java.security.NoSuchAlgorithmException;
910 import java.security.cert.CertificateException;
1011 import java.security.cert.X509Certificate;
12
+
13
+import javax.activation.MimeType;
1114
1215 import net.curisit.securis.beans.RequestBean;
1316 import net.curisit.securis.utils.JsonUtils;
....@@ -34,6 +37,10 @@
3437 public class ConnectionManager {
3538
3639 private static final Logger LOG = LogManager.getLogger(ConnectionManager.class);
40
+ private static final int HTTP_STATUS_APP_ERRROR = 418;
41
+ private static final String JSON_MEDIA_TYPE= "application/json";
42
+ private static final String ERROR_MESSAGE_HEADER = "X-SECURIS-ERROR-MSG";
43
+ private static final String ERROR_CODE_MESSAGE_HEADER = "X-SECURIS-ERROR-CODE";
3744
3845 private static ConnectionManager singleton;
3946
....@@ -78,8 +85,9 @@
7885
7986 public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException {
8087 HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command));
81
- postRequest.addHeader("accept", "application/json");
82
- postRequest.addHeader("content-type", "application/json");
88
+ postRequest.addHeader("accept", JSON_MEDIA_TYPE);
89
+
90
+ postRequest.addHeader("content-type", JSON_MEDIA_TYPE);
8391 try {
8492 postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req)));
8593 } catch (UnsupportedEncodingException | SeCurisException e1) {
....@@ -88,27 +96,38 @@
8896 HttpResponse response;
8997 try {
9098 response = httpClient.execute(postRequest);
91
- if (response.getStatusLine().getStatusCode() != 200) {
92
- throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
93
- }
99
+
100
+ checkErrors(command, response);
101
+
94102 String jsonLic = IOUtils.toString(response.getEntity().getContent());
95
- LOG.info("License read OK: {}", jsonLic);
103
+ LOG.debug("Response content read OK: {}", jsonLic);
96104 T responseBean = JsonUtils.json2object(jsonLic, returnType);
97105
98
- LOG.info("Response bean read OK: {}", responseBean);
99
- LOG.info("JSON to write in file: {}", JsonUtils.toJSON(responseBean));
106
+ LOG.debug("Response bean read OK: {}", responseBean);
100107
101108 return responseBean;
102109 } catch (IOException e) {
103
- LOG.error("Error acessing SeCuris server", e);
110
+ LOG.error("Error accessing SeCuris server", e);
104111 throw new SeCurisException("Error accessing SeCuris server");
105112 }
106113 }
107114
115
+ private void checkErrors(String command, HttpResponse response) throws SeCurisException {
116
+ if (response.getStatusLine().getStatusCode() != 200) {
117
+ if (response.getStatusLine().getStatusCode() == HTTP_STATUS_APP_ERRROR) {
118
+ String errorCode = response.getFirstHeader(ERROR_CODE_MESSAGE_HEADER).getValue();
119
+ String errorMsg = response.getFirstHeader(ERROR_MESSAGE_HEADER).getValue();
120
+ throw new SeCurisException(String.format("[%s] - %s", errorCode, errorMsg));
121
+ }
122
+ LOG.error("Unexpected error executing {}, Reason: {}", command, response.getStatusLine().getReasonPhrase());
123
+ throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
124
+ }
125
+
126
+ }
108127
109128 public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException {
110129 HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command));
111
- getRequest.addHeader("accept", "application/json");
130
+ getRequest.addHeader("accept", JSON_MEDIA_TYPE);
112131
113132 HttpResponse response;
114133 try {
....@@ -117,11 +136,10 @@
117136 throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
118137 }
119138 String jsonLic = IOUtils.toString(response.getEntity().getContent());
120
- LOG.info("License read OK: {}", jsonLic);
139
+ LOG.debug("Response content read OK: {}", jsonLic);
121140 T responseBean = JsonUtils.json2object(jsonLic, returnType);
122141
123
- LOG.info("Response bean read OK: {}", responseBean);
124
- LOG.info("JSON to write in file: {}", JsonUtils.toJSON(responseBean));
142
+ LOG.debug("Response bean read OK: {}", responseBean);
125143
126144 return responseBean;
127145 } catch (IOException e) {
src/main/java/net/curisit/securis/License.java
....@@ -73,6 +73,9 @@
7373 if (filename == null)
7474 filename = "./license.lic";
7575 File file = new File(filename);
76
+ if (!file.exists()) {
77
+ throw new SeCurisException("The license file doesn't exist: " + file.getAbsolutePath());
78
+ }
7679 try {
7780 LicenseManager.getInstance().validateLicense(file);
7881 LOG.info("License file {} is valid", file.getAbsolutePath());
....@@ -86,7 +89,7 @@
8689
8790 if (cmd.hasOption('c')) {
8891 SignedLicenseBean lic = LicenseManager.getInstance().requestLicense();
89
- String filename = cmd.getOptionValue("lic_file");
92
+ String filename = cmd.getOptionValue("c");
9093 if (filename == null)
9194 filename = "./license.lic";
9295 File file = new File(filename);
....@@ -105,7 +108,16 @@
105108 if (cmd.hasOption('r')) {
106109 String licFilename = cmd.getOptionValue("renew");
107110 checkMandatoryParameter(licFilename, "renew");
108
- LOG.warn("This command is not yet implemented");
111
+ File file = new File(licFilename);
112
+ if (!file.exists()) {
113
+ throw new SeCurisException("The license file doesn't exist: " + file.getAbsolutePath());
114
+ }
115
+ SignedLicenseBean newLic = LicenseManager.getInstance().renew(file);
116
+ File oldLicFile = new File(file.getAbsoluteFile() + ".old");
117
+ file.renameTo(oldLicFile);
118
+ LOG.info("Old license file has been renamed to: {}", oldLicFile.getAbsolutePath());
119
+ LicenseManager.getInstance().save(newLic, file);
120
+ LOG.info("New license file saved as: {}", file.getAbsolutePath());
109121 System.exit(0);
110122 }
111123
src/main/java/net/curisit/securis/LicenseManager.java
....@@ -6,6 +6,7 @@
66 import java.nio.file.Files;
77 import java.nio.file.Paths;
88 import java.nio.file.StandardOpenOption;
9
+import java.util.Date;
910
1011 import net.curisit.securis.ConnectionManager.Command;
1112 import net.curisit.securis.beans.LicenseBean;
....@@ -57,29 +58,55 @@
5758 return licBean;
5859 }
5960
60
- /**
61
- * Validates the license stored in {@code licFile} and get the corresponding LicenseBean
62
- * <p>
63
- * The validation includes:
64
- * <ul>
65
- * <li>Signature</li>
66
- * <li>HW data</li>
67
- * <li>Logo CRC</li>
68
- * </ul>
69
- * </p>
70
- *
71
- * @param licFile
72
- * @return The license bean stored in file
73
- * @throws SeCurisException
74
- */
75
- public LicenseBean validateLicense(File licFile) throws SeCurisException {
76
- LicenseBean licBean = load(licFile);
77
- SignatureHelper.getInstance().validateSignature(licBean);
78
- LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE));
79
- LicenseValidator.getInstance().validateLogo(licBean);
61
+ /**
62
+ * Validates the license stored in {@code licFile} and get the corresponding LicenseBean
63
+ * <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 licFile
73
+ * @return The license bean stored in file
74
+ * @throws SeCurisException
75
+ */
76
+ public LicenseBean validateLicense(File licFile) throws SeCurisException {
8077
81
- return licBean;
82
- }
78
+ return validateLicense(licFile, false);
79
+ }
80
+
81
+ /**
82
+ * Validates the license stored in {@code licFile} and get the corresponding LicenseBean. The License date is not validated
83
+ * <p>
84
+ * The validation includes:
85
+ * <ul>
86
+ * <li>Signature</li>
87
+ * <li>HW data</li>
88
+ * <li>Logo CRC</li>
89
+ * </ul>
90
+ * </p>
91
+ *
92
+ * @param licFile
93
+ * @return The license bean stored in file
94
+ * @throws SeCurisException
95
+ */
96
+ public LicenseBean validateLicense(File licFile, boolean excludeDateValidation) throws SeCurisException {
97
+ LicenseBean licBean = load(licFile);
98
+ SignatureHelper.getInstance().validateSignature(licBean);
99
+ LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE));
100
+ LicenseValidator.getInstance().validateLogo(licBean);
101
+
102
+ if (!excludeDateValidation) {
103
+ if (new Date().after(licBean.getExpirationDate())) {
104
+ throw new SeCurisException("License has expired");
105
+ }
106
+ }
107
+
108
+ return licBean;
109
+ }
83110
84111 /**
85112 * Request to server for a valid license