rsanchez
2014-12-11 5ff7160871b0ea6cbcc8ead0f0fc40cd15349bae
#2140 fix - Removed config file from jar and added a new exception for
expired licenses
1 files deleted
1 files added
5 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/ExpiredLicenseException.java patch | view | blame | history
src/main/java/net/curisit/securis/LicenseManager.java patch | view | blame | history
src/main/java/net/curisit/securis/SeCurisException.java patch | view | blame | history
src/main/java/net/curisit/securis/utils/LicUtils.java patch | view | blame | history
src/main/resources/securis-client.properties 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.2-SNAPSHOT</version>
6
+ <version>1.0.3-SNAPSHOT</version>
77 <build>
88 <plugins>
99 <plugin>
src/main/java/net/curisit/securis/ConnectionManager.java
....@@ -33,121 +33,122 @@
3333 */
3434 public class ConnectionManager {
3535
36
- private static final Logger LOG = LogManager.getLogger(ConnectionManager.class);
37
- private static final int HTTP_STATUS_APP_ERRROR = 418;
38
- private static final String JSON_MEDIA_TYPE = "application/json";
39
- private static final String ERROR_MESSAGE_HEADER = "X-SECURIS-ERROR-MSG";
40
- private static final String ERROR_CODE_MESSAGE_HEADER = "X-SECURIS-ERROR-CODE";
36
+ private static final Logger LOG = LogManager.getLogger(ConnectionManager.class);
37
+ private static final int HTTP_STATUS_APP_ERRROR = 418;
38
+ private static final String JSON_MEDIA_TYPE = "application/json";
39
+ private static final String ERROR_MESSAGE_HEADER = "X-SECURIS-ERROR-MSG";
40
+ private static final String ERROR_CODE_MESSAGE_HEADER = "X-SECURIS-ERROR-CODE";
4141
42
- private static ConnectionManager singleton;
42
+ private static ConnectionManager singleton;
4343
44
- private final String serverUrl;
45
- private final CloseableHttpClient httpClient;
44
+ private final String serverUrl;
45
+ private final CloseableHttpClient httpClient;
4646
47
- private ConnectionManager() throws SeCurisException {
48
- String aux = Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL);
49
- if (aux.endsWith("/")) {
50
- serverUrl = aux.substring(0, aux.length() - 2);
51
- } else {
52
- serverUrl = aux;
53
- }
54
- httpClient = createHttpClient();
55
- }
47
+ private ConnectionManager() throws SeCurisException {
48
+ String aux = Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL);
49
+ if (aux.endsWith("/")) {
50
+ serverUrl = aux.substring(0, aux.length() - 2);
51
+ } else {
52
+ serverUrl = aux;
53
+ }
54
+ httpClient = createHttpClient();
55
+ }
5656
57
- private CloseableHttpClient createHttpClient() throws SeCurisException {
58
- SSLContextBuilder builder = new SSLContextBuilder();
59
- SSLConnectionSocketFactory sslsf = null;
60
- try {
61
- builder.loadTrustMaterial((KeyStore) null, new TrustStrategy() {
62
- @Override
63
- public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
64
- return true;
65
- }
66
- });
67
- sslsf = new SSLConnectionSocketFactory(builder.build());
68
- } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e1) {
69
- LOG.error(e1);
70
- throw new SeCurisException("Error creating SSL socket factory");
71
- }
72
- return HttpClientBuilder.create().setSSLSocketFactory(sslsf).build();
73
- }
57
+ private CloseableHttpClient createHttpClient() throws SeCurisException {
58
+ SSLContextBuilder builder = new SSLContextBuilder();
59
+ SSLConnectionSocketFactory sslsf = null;
60
+ try {
61
+ builder.loadTrustMaterial((KeyStore) null, new TrustStrategy() {
62
+ @Override
63
+ public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
64
+ return true;
65
+ }
66
+ });
67
+ sslsf = new SSLConnectionSocketFactory(builder.build());
68
+ } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e1) {
69
+ LOG.error(e1);
70
+ throw new SeCurisException("Error creating SSL socket factory");
71
+ }
72
+ return HttpClientBuilder.create().setSSLSocketFactory(sslsf).build();
73
+ }
7474
75
- public synchronized static ConnectionManager getInstance() throws SeCurisException {
76
- if (singleton == null) {
77
- singleton = new ConnectionManager();
78
- }
79
- return singleton;
80
- }
75
+ public synchronized static ConnectionManager getInstance() throws SeCurisException {
76
+ if (singleton == null) {
77
+ singleton = new ConnectionManager();
78
+ }
79
+ return singleton;
80
+ }
8181
82
- public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException {
83
- HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command));
84
- postRequest.addHeader("accept", JSON_MEDIA_TYPE);
82
+ public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException {
83
+ HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command));
84
+ postRequest.addHeader("accept", JSON_MEDIA_TYPE);
8585
86
- postRequest.addHeader("content-type", JSON_MEDIA_TYPE);
87
- try {
88
- postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req)));
89
- } catch (UnsupportedEncodingException | SeCurisException e1) {
90
- throw new SeCurisException("Error preparing POST command", e1);
91
- }
92
- HttpResponse response;
93
- try {
94
- response = httpClient.execute(postRequest);
86
+ postRequest.addHeader("content-type", JSON_MEDIA_TYPE);
87
+ try {
88
+ postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req)));
89
+ } catch (UnsupportedEncodingException | SeCurisException e1) {
90
+ throw new SeCurisException("Error preparing POST command", e1);
91
+ }
92
+ HttpResponse response;
93
+ try {
94
+ response = httpClient.execute(postRequest);
9595
96
- checkErrors(command, response);
96
+ checkErrors(command, response);
9797
98
- String jsonLic = IOUtils.toString(response.getEntity().getContent());
99
- LOG.debug("Response content read OK: {}", jsonLic);
100
- T responseBean = JsonUtils.json2object(jsonLic, returnType);
98
+ String jsonLic = IOUtils.toString(response.getEntity().getContent());
99
+ LOG.debug("Response content read OK: {}", jsonLic);
100
+ T responseBean = JsonUtils.json2object(jsonLic, returnType);
101101
102
- LOG.debug("Response bean read OK: {}", responseBean);
102
+ LOG.debug("Response bean read OK: {}", responseBean);
103103
104
- return responseBean;
105
- } catch (IOException e) {
106
- LOG.error("Error accessing SeCuris server", e);
107
- throw new SeCurisException("Error accessing SeCuris server");
108
- }
109
- }
104
+ return responseBean;
105
+ } catch (IOException e) {
106
+ LOG.error("Error accessing SeCuris server", e);
107
+ throw new SeCurisException("Error accessing SeCuris server", e);
108
+ }
109
+ }
110110
111
- private void checkErrors(String command, HttpResponse response) throws SeCurisException {
112
- if (response.getStatusLine().getStatusCode() != 200) {
113
- if (response.getStatusLine().getStatusCode() == HTTP_STATUS_APP_ERRROR) {
114
- String errorCode = response.getFirstHeader(ERROR_CODE_MESSAGE_HEADER).getValue();
115
- String errorMsg = response.getFirstHeader(ERROR_MESSAGE_HEADER).getValue();
116
- throw new SeCurisException(String.format("[%s] - %s", errorCode, errorMsg));
117
- }
118
- LOG.error("Unexpected error executing {}, Reason: {}", command, response.getStatusLine().getReasonPhrase());
119
- throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
120
- }
111
+ private void checkErrors(String command, HttpResponse response) throws SeCurisException {
112
+ if (response.getStatusLine().getStatusCode() != 200) {
113
+ if (response.getStatusLine().getStatusCode() == HTTP_STATUS_APP_ERRROR) {
114
+ String errorCode = response.getFirstHeader(ERROR_CODE_MESSAGE_HEADER).getValue();
115
+ String errorMsg = response.getFirstHeader(ERROR_MESSAGE_HEADER).getValue();
116
+ throw new SeCurisException(String.format("[%s] - %s", errorCode, errorMsg));
117
+ }
118
+ LOG.error("Unexpected error executing {}, Reason: {}", command, response.getStatusLine().getReasonPhrase());
119
+ throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
120
+ }
121121
122
- }
122
+ }
123123
124
- public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException {
125
- HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command));
126
- getRequest.addHeader("accept", JSON_MEDIA_TYPE);
124
+ public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException {
125
+ HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command));
126
+ getRequest.addHeader("accept", JSON_MEDIA_TYPE);
127127
128
- HttpResponse response;
129
- try {
130
- response = httpClient.execute(getRequest);
131
- if (response.getStatusLine().getStatusCode() != 200) {
132
- throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
133
- }
134
- String jsonLic = IOUtils.toString(response.getEntity().getContent());
135
- LOG.debug("Response content read OK: {}", jsonLic);
136
- T responseBean = JsonUtils.json2object(jsonLic, returnType);
128
+ HttpResponse response;
129
+ try {
130
+ response = httpClient.execute(getRequest);
131
+ if (response.getStatusLine().getStatusCode() != 200) {
132
+ throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
133
+ }
134
+ String jsonLic = IOUtils.toString(response.getEntity().getContent());
135
+ LOG.debug("Response content read OK: {}", jsonLic);
136
+ T responseBean = JsonUtils.json2object(jsonLic, returnType);
137137
138
- LOG.debug("Response bean read OK: {}", responseBean);
138
+ LOG.debug("Response bean read OK: {}", responseBean);
139139
140
- return responseBean;
141
- } catch (IOException e) {
142
- LOG.error("Error acessing SeCuris server", e);
143
- throw new SeCurisException("Error accessing SeCuris server");
144
- }
145
- }
140
+ return responseBean;
141
+ } catch (IOException e) {
142
+ LOG.error("Error acessing SeCuris server", e);
143
+ throw new SeCurisException("Error accessing SeCuris server");
144
+ }
145
+ }
146146
147
- public static class Command {
148
- public static final String TEST = "ping";
149
- public static final String CREATE_LIC = "request";
150
- public static final String RENEW_LIC = "renew";
151
- }
147
+ public static class Command {
148
+ public static final String TEST = "ping";
149
+ public static final String CREATE_LIC = "request";
150
+ public static final String RENEW_LIC = "renew";
151
+ public static final String VALIDATE = "validate";
152
+ }
152153
153154 }
src/main/java/net/curisit/securis/ExpiredLicenseException.java
....@@ -0,0 +1,19 @@
1
+package net.curisit.securis;
2
+
3
+public class ExpiredLicenseException extends SeCurisException {
4
+
5
+ private static final long serialVersionUID = 5702956178417661458L;
6
+
7
+ public ExpiredLicenseException() {
8
+ super("License has expired", null);
9
+ }
10
+
11
+ public ExpiredLicenseException(String msg, Exception e) {
12
+ super(msg, e);
13
+ }
14
+
15
+ public ExpiredLicenseException(String msg) {
16
+ super(msg);
17
+ }
18
+
19
+}
src/main/java/net/curisit/securis/LicenseManager.java
....@@ -28,195 +28,207 @@
2828 */
2929 public class LicenseManager {
3030
31
- private static final Logger LOG = LogManager.getLogger(License.class);
31
+ private static final Logger LOG = LogManager.getLogger(License.class);
3232
33
- private static LicenseManager singleton = new LicenseManager();
33
+ private static LicenseManager singleton = new LicenseManager();
3434
35
- public static final String PING_MESSAGE = "SeCuris API OK";
35
+ public static final String PING_MESSAGE = "SeCuris API OK";
3636
37
- private LicenseManager() {
38
- }
37
+ private LicenseManager() {}
3938
40
- public static LicenseManager getInstance() {
41
- return singleton;
42
- }
39
+ public static LicenseManager getInstance() {
40
+ return singleton;
41
+ }
4342
44
- /**
45
- * Loads a license from file
46
- *
47
- * @param licFile
48
- * @return The license bean
49
- * @throws SeCurisException
50
- */
51
- public LicenseBean load(File licFile) throws SeCurisException {
52
- LicenseBean licBean;
53
- try {
54
- licBean = JsonUtils.json2object(FileUtils.readFileToString(licFile), LicenseBean.class);
55
- } catch (IOException e) {
56
- throw new SeCurisException("Error getting license data from file: " + licFile, e);
57
- }
58
- return licBean;
59
- }
43
+ /**
44
+ * Loads a license from file
45
+ *
46
+ * @param licFile
47
+ * @return The license bean
48
+ * @throws SeCurisException
49
+ */
50
+ public LicenseBean load(File licFile) throws SeCurisException {
51
+ LicenseBean licBean;
52
+ try {
53
+ licBean = JsonUtils.json2object(FileUtils.readFileToString(licFile), LicenseBean.class);
54
+ } catch (IOException e) {
55
+ throw new SeCurisException("Error getting license data from file: " + licFile, e);
56
+ }
57
+ return licBean;
58
+ }
6059
61
- /**
62
- * Validates the license stored in {@code licFile} and get the corresponding
63
- * LicenseBean
64
- * <p>
65
- * The validation includes:
66
- * <ul>
67
- * <li>Signature</li>
68
- * <li>HW data</li>
69
- * <li>Logo CRC</li>
70
- * </ul>
71
- * </p>
72
- *
73
- * @param licFile
74
- * @return The license bean stored in file
75
- * @throws SeCurisException
76
- */
77
- public LicenseBean validateLicense(File licFile) throws SeCurisException {
60
+ /**
61
+ * Validates the license stored in {@code licFile} and get the corresponding
62
+ * 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 {
7877
79
- return validateLicense(licFile, false);
80
- }
78
+ return validateLicense(licFile, false);
79
+ }
8180
82
- /**
83
- * Validates the license stored in {@code licFile} and get the corresponding
84
- * LicenseBean. The License date is not validated
85
- * <p>
86
- * The validation includes:
87
- * <ul>
88
- * <li>Signature</li>
89
- * <li>HW data</li>
90
- * <li>Logo CRC</li>
91
- * </ul>
92
- * </p>
93
- *
94
- * @param licFile
95
- * @return The license bean stored in file
96
- * @throws SeCurisException
97
- */
98
- public LicenseBean validateLicense(File licFile, boolean excludeDateValidation) throws SeCurisException {
99
- LicenseBean licBean = load(licFile);
100
- SignatureHelper.getInstance().validateSignature(licBean);
101
- LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),
102
- Params.get(Params.KEYS.PACK_CODE));
103
- LicenseValidator.getInstance().validateLogo(licBean);
81
+ /**
82
+ * Validates the license stored in {@code licFile} and get the corresponding
83
+ * LicenseBean. The License date is not validated
84
+ * <p>
85
+ * The validation includes:
86
+ * <ul>
87
+ * <li>Signature</li>
88
+ * <li>HW data</li>
89
+ * <li>Logo CRC</li>
90
+ * </ul>
91
+ * </p>
92
+ *
93
+ * @param licFile
94
+ * @return The license bean stored in file
95
+ * @throws SeCurisException
96
+ */
97
+ public LicenseBean validateLicense(File licFile, boolean excludeDateValidation) throws SeCurisException {
98
+ LicenseBean licBean = load(licFile);
99
+ SignatureHelper.getInstance().validateSignature(licBean);
100
+ LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),
101
+ Params.get(Params.KEYS.PACK_CODE));
102
+ LicenseValidator.getInstance().validateLogo(licBean);
104103
105
- if (!excludeDateValidation) {
106
- if (new Date().after(licBean.getExpirationDate())) {
107
- throw new SeCurisException("License has expired");
108
- }
109
- }
104
+ if (!excludeDateValidation) {
105
+ if (new Date().after(licBean.getExpirationDate())) {
106
+ throw new ExpiredLicenseException();
107
+ }
108
+ }
110109
111
- return licBean;
112
- }
110
+ return licBean;
111
+ }
113112
114
- /**
115
- * Request to server for a valid license
116
- *
117
- * @return The license bean returned by the server
118
- * @throws SeCurisException
119
- */
120
- public SignedLicenseBean requestLicense() throws SeCurisException {
121
- RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),
122
- Params.get(Params.KEYS.PACK_CODE));
113
+ /**
114
+ * Request to server for a valid license
115
+ *
116
+ * @return The license bean returned by the server
117
+ * @throws SeCurisException
118
+ */
119
+ public SignedLicenseBean requestLicense() throws SeCurisException {
120
+ RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),
121
+ Params.get(Params.KEYS.PACK_CODE));
123122
124
- SignedLicenseBean lic = requestLicenseToServer(req);
125
- return lic;
126
- }
123
+ SignedLicenseBean lic = requestLicenseToServer(req);
124
+ return lic;
125
+ }
127126
128
- /**
129
- * Generate a license file using a {@link LicenseBean}
130
- *
131
- * @param license
132
- * @param file
133
- * @throws SeCurisException
134
- */
135
- public void save(LicenseBean license, File file) throws SeCurisException {
136
- SignedLicenseBean signedLic = new SignedLicenseBean(license);
137
- save(signedLic, file);
138
- }
127
+ /**
128
+ * Generate a license file using a {@link LicenseBean}
129
+ *
130
+ * @param license
131
+ * @param file
132
+ * @throws SeCurisException
133
+ */
134
+ public void save(LicenseBean license, File file) throws SeCurisException {
135
+ SignedLicenseBean signedLic = new SignedLicenseBean(license);
136
+ save(signedLic, file);
137
+ }
139138
140
- /**
141
- * Generate a license file using a {@link LicenseBean}
142
- *
143
- * @param license
144
- * @param file
145
- * @throws SeCurisException
146
- */
147
- public void save(SignedLicenseBean signedLic, File file) throws SeCurisException {
148
- byte[] json;
149
- try {
150
- json = JsonUtils.toPrettyJSON(signedLic).getBytes("utf-8");
151
- Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
152
- } catch (UnsupportedEncodingException e) {
153
- LOG.error("Error creating json doc from license: " + signedLic, e);
154
- throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
155
- } catch (IOException e) {
156
- LOG.error("Error creating license file: " + file, e);
157
- throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
158
- }
139
+ /**
140
+ * Generate a license file using a {@link LicenseBean}
141
+ *
142
+ * @param license
143
+ * @param file
144
+ * @throws SeCurisException
145
+ */
146
+ public void save(SignedLicenseBean signedLic, File file) throws SeCurisException {
147
+ byte[] json;
148
+ try {
149
+ json = JsonUtils.toPrettyJSON(signedLic).getBytes("utf-8");
150
+ Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
151
+ } catch (UnsupportedEncodingException e) {
152
+ LOG.error("Error creating json doc from license: " + signedLic, e);
153
+ throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
154
+ } catch (IOException e) {
155
+ LOG.error("Error creating license file: " + file, e);
156
+ throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
157
+ }
159158
160
- LOG.debug("License saved in {}", file);
159
+ LOG.debug("License saved in {}", file);
161160
162
- }
161
+ }
163162
164
- private SignedLicenseBean requestLicenseToServer(RequestBean req) throws SeCurisException {
165
- SignedLicenseBean lic = ConnectionManager.getInstance().executePost(Command.CREATE_LIC, SignedLicenseBean.class, req);
163
+ private SignedLicenseBean requestLicenseToServer(RequestBean req) throws SeCurisException {
164
+ SignedLicenseBean lic = ConnectionManager.getInstance().executePost(Command.CREATE_LIC, SignedLicenseBean.class, req);
166165
167
- return lic;
168
- }
166
+ return lic;
167
+ }
169168
170
- /**
171
- * Creates a new request file with current hardware in the File passed as
172
- * parameter
173
- *
174
- * @param outputRequestFile
175
- * File where the request data will be saved
176
- * @return The generated request bean
177
- * @throws SeCurisException
178
- */
179
- public RequestBean createRequestFile(File outputRequestFile) throws SeCurisException {
180
- RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),
181
- Params.get(Params.KEYS.PACK_CODE));
169
+ /**
170
+ * Creates a new request file with current hardware in the File passed as
171
+ * parameter
172
+ *
173
+ * @param outputRequestFile
174
+ * File where the request data will be saved
175
+ * @return The generated request bean
176
+ * @throws SeCurisException
177
+ */
178
+ public RequestBean createRequestFile(File outputRequestFile) throws SeCurisException {
179
+ RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),
180
+ Params.get(Params.KEYS.PACK_CODE));
182181
183
- ReqGenerator.getInstance().save(req, outputRequestFile);
182
+ ReqGenerator.getInstance().save(req, outputRequestFile);
184183
185
- return req;
186
- }
184
+ return req;
185
+ }
187186
188
- /**
189
- * Send the current license file to server, which is previously validated,
190
- * to get a renewed one if it is prepared in server side.
191
- *
192
- * @param licenseFile
193
- * Current and valid License file
194
- * @return New license bean if server creates a new one, otherwise the same
195
- * current License bean will be returned
196
- * @throws SeCurisException
197
- */
198
- public SignedLicenseBean renew(File licenseFile) throws SeCurisException {
199
- LicenseBean lic = validateLicense(licenseFile);
187
+ /**
188
+ * Send the current license file to server, which is previously validated,
189
+ * to get a renewed one if it is prepared in server side.
190
+ *
191
+ * @param licenseFile
192
+ * Current and valid License file
193
+ * @return New license bean if server creates a new one, otherwise the same
194
+ * current License bean will be returned
195
+ * @throws SeCurisException
196
+ */
197
+ public SignedLicenseBean renew(File licenseFile) throws SeCurisException {
198
+ LicenseBean lic = validateLicense(licenseFile);
200199
201
- SignedLicenseBean newLic = ConnectionManager.getInstance().executePost(Command.RENEW_LIC, SignedLicenseBean.class, lic);
200
+ SignedLicenseBean newLic = ConnectionManager.getInstance().executePost(Command.RENEW_LIC, SignedLicenseBean.class, lic);
202201
203
- return newLic;
204
- }
202
+ return newLic;
203
+ }
205204
206
- public void testServer() throws SeCurisException {
207
- StatusBean status = ConnectionManager.getInstance().executeGet(Command.TEST, StatusBean.class);
208
- if (!PING_MESSAGE.equals(status.getMessage())) {
209
- throw new SeCurisException("SeCuris Server is not running in given URL");
210
- }
211
- }
205
+ /**
206
+ * Check on SeCuris server if current license is still valid in server DB.
207
+ *
208
+ * @param licenseFile
209
+ * @throws SeCurisException
210
+ */
211
+ public void assertLicenseIsValid(File licenseFile) throws SeCurisException, IOException {
212
+ LicenseBean lic = validateLicense(licenseFile);
212213
213
- public static void main(String[] args) throws SeCurisException {
214
- String filename = null;
215
- if (filename == null)
216
- filename = "./license.req";
217
- File file = new File(filename);
218
- LicenseManager.getInstance().createRequestFile(file);
219
- LOG.info("Request file {} generated OK", file.getAbsolutePath());
214
+ ConnectionManager.getInstance().executePost(Command.VALIDATE, LicenseBean.class, lic);
220215
221
- }
216
+ }
217
+
218
+ public void testServer() throws SeCurisException {
219
+ StatusBean status = ConnectionManager.getInstance().executeGet(Command.TEST, StatusBean.class);
220
+ if (!PING_MESSAGE.equals(status.getMessage())) {
221
+ throw new SeCurisException("SeCuris Server is not running in given URL");
222
+ }
223
+ }
224
+
225
+ public static void main(String[] args) throws SeCurisException {
226
+ String filename = null;
227
+ if (filename == null)
228
+ filename = "./license.req";
229
+ File file = new File(filename);
230
+ LicenseManager.getInstance().createRequestFile(file);
231
+ LOG.info("Request file {} generated OK", file.getAbsolutePath());
232
+
233
+ }
222234 }
src/main/java/net/curisit/securis/SeCurisException.java
....@@ -1,13 +1,11 @@
11 package net.curisit.securis;
22
3
-import java.net.URISyntaxException;
43
54 public class SeCurisException extends Exception {
65
76 private static final long serialVersionUID = 5702956178417661458L;
87
9
- public SeCurisException() {
10
- }
8
+ public SeCurisException() {}
119
1210 public SeCurisException(String msg, Exception e) {
1311 super(msg, e);
....@@ -17,8 +15,4 @@
1715 super(msg);
1816 }
1917
20
- public static void main(String[] args) throws URISyntaxException {
21
- License.main(new String[]
22
- { "-g" });
23
- }
2418 }
src/main/java/net/curisit/securis/utils/LicUtils.java
....@@ -4,6 +4,9 @@
44 import java.nio.charset.Charset;
55 import java.security.MessageDigest;
66 import java.security.NoSuchAlgorithmException;
7
+import java.util.ArrayList;
8
+import java.util.Arrays;
9
+import java.util.List;
710 import java.util.zip.CRC32;
811
912 import org.apache.logging.log4j.LogManager;
....@@ -69,9 +72,9 @@
6972 }
7073
7174 public static Integer getLicenseCodeSuffix(String licCode) {
72
- String[] parts = licCode.split("-");
75
+ String[] parts = splitLicCode(licCode);
7376
74
- return new Integer(parts[2]);
77
+ return new Integer(parts[parts.length - 1]);
7578 }
7679
7780 public static String getLicenseCode(String packCode, Integer licSufixCode) {
....@@ -87,17 +90,48 @@
8790 * @return true if license code format and its CRC are valid
8891 */
8992 public static boolean checkValidLicenseCodeCrc(String licCode) {
90
- String[] parts = licCode.split("-");
91
- if (parts.length != 3) {
93
+ String[] parts = splitLicCode(licCode);
94
+ if (parts == null) {
9295 return false;
9396 }
9497 String crc = getLicenseCrc(parts[0], parts[2]);
9598 return crc.equals(parts[1]);
9699 }
97100
101
+ /**
102
+ * We convert the license code in 3 parts: <br>
103
+ * "P1-344-1 = ["P1", "344", "1"] <br>
104
+ * "P1-OTHER-344-1 = ["P1-OTHER", "344", "1"] <br>
105
+ * "P1-1 = null
106
+ *
107
+ * @param code
108
+ * @return The 2 license code packs or null if code is not valid
109
+ */
110
+ private static String[] splitLicCode(String code) {
111
+ List<Integer> separators = new ArrayList<>();
112
+ for (int i = 0; i < code.length(); i++) {
113
+ if (code.charAt(i) == '-') {
114
+ separators.add(i);
115
+ }
116
+ }
117
+ if (separators.size() < 2) {
118
+ return null;
119
+ }
120
+
121
+ int i0 = separators.get(separators.size() - 2);
122
+ int i1 = separators.get(separators.size() - 1);
123
+ String[] parts = new String[3];
124
+ parts[0] = code.substring(0, i0);
125
+ parts[1] = code.substring(i0 + 1, i1);
126
+ parts[2] = code.substring(i1 + 1);
127
+
128
+ return parts;
129
+ }
130
+
98131 public static void main(String[] args) {
99
- String code = getLicenseCode("PCK01", 5);
132
+ String code = getLicenseCode("PC-K01", 5);
100133 System.out.println(code);
101
- System.out.println("Is valid ? " + checkValidLicenseCodeCrc("PCK01-512-"));
134
+ System.out.println("Is valid ? " + checkValidLicenseCodeCrc(code));
135
+ System.out.println("Is valid ? " + Arrays.asList(splitLicCode(code)));
102136 }
103137 }
src/main/resources/securis-client.properties
deleted file mode 100644
....@@ -1,4 +0,0 @@
1
-license.server.url = https://securis.curistec.com/api
2
-app.code = CI01
3
-customer.code = CT01
4
-pack.code = P1