rsanchez
2015-09-24 2d59319508a0b2f326d9907fcd2d911a7b5536ae
#2756 feature - Fixed LicenseBean creation and extended the current API
4 files modified
changed files
etc/build/files/config/securis-client.properties patch | view | blame | history
src/main/java/net/curisit/securis/ConnectionManager.java patch | view | blame | history
src/main/java/net/curisit/securis/LicenseManager.java patch | view | blame | history
src/main/java/net/curisit/securis/beans/LicenseBean.java patch | view | blame | history
etc/build/files/config/securis-client.properties
....@@ -5,5 +5,4 @@
55 #########################################################################
66
77 license.server.url = https://securis.curistec.com/securis/api
8
-app.code = AP01
9
-customer.code = XX
8
+app.code = _AP_
src/main/java/net/curisit/securis/ConnectionManager.java
....@@ -33,134 +33,138 @@
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";
41
+ private static final String TOKEN_HEADER = "X-SECURIS-TOKEN";
4142
42
- private static ConnectionManager singleton;
43
+ private static final String API_CLIENT_TOKEN = "OTk3ODRiMzY5NzQ5MWI5NmYyZGQyODRiYjY2ZTU2YzdmMTZjYzM3YTY3N2ExM2M3ODI2MjU5ZTMzOTIyYjUzNSBfY2xpZW50IDE5NzAtMDEtMDFUMDA6NTk6NTkuOTk5KzAxMDA=";
44
+ private static ConnectionManager singleton;
4345
44
- private final String serverUrl;
45
- private final HttpClientBuilder httpClientBuilder;
46
+ private final String serverUrl;
47
+ private final HttpClientBuilder httpClientBuilder;
4648
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
- httpClientBuilder = createHttpClientBuilder();
55
- }
49
+ private ConnectionManager() throws SeCurisException {
50
+ String aux = Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL);
51
+ if (aux.endsWith("/")) {
52
+ serverUrl = aux.substring(0, aux.length() - 2);
53
+ } else {
54
+ serverUrl = aux;
55
+ }
56
+ httpClientBuilder = createHttpClientBuilder();
57
+ }
5658
57
- private HttpClientBuilder createHttpClientBuilder() 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);
73
- }
59
+ private HttpClientBuilder createHttpClientBuilder() throws SeCurisException {
60
+ SSLContextBuilder builder = new SSLContextBuilder();
61
+ SSLConnectionSocketFactory sslsf = null;
62
+ try {
63
+ builder.loadTrustMaterial((KeyStore) null, new TrustStrategy() {
64
+ @Override
65
+ public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
66
+ return true;
67
+ }
68
+ });
69
+ sslsf = new SSLConnectionSocketFactory(builder.build());
70
+ } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e1) {
71
+ LOG.error(e1);
72
+ throw new SeCurisException("Error creating SSL socket factory");
73
+ }
74
+ return HttpClientBuilder.create().setSSLSocketFactory(sslsf);
75
+ }
7476
75
- public synchronized static ConnectionManager getInstance() throws SeCurisException {
76
- if (singleton == null) {
77
- singleton = new ConnectionManager();
78
- }
79
- return singleton;
80
- }
77
+ public synchronized static ConnectionManager getInstance() throws SeCurisException {
78
+ if (singleton == null) {
79
+ singleton = new ConnectionManager();
80
+ }
81
+ return singleton;
82
+ }
8183
82
- public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException {
83
- return executePost(command, returnType, req, null);
84
- }
84
+ public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException {
85
+ return executePost(command, returnType, req, null);
86
+ }
8587
86
- public <T> T executePost(String command, Class<T> returnType, RequestBean req, Map<String, String> headers) throws SeCurisException {
87
- HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command));
88
- postRequest.addHeader("accept", JSON_MEDIA_TYPE);
89
- postRequest.addHeader("content-type", JSON_MEDIA_TYPE);
88
+ public <T> T executePost(String command, Class<T> returnType, RequestBean req, Map<String, String> headers) throws SeCurisException {
89
+ HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command));
90
+ postRequest.addHeader(TOKEN_HEADER, API_CLIENT_TOKEN);
91
+ postRequest.addHeader("accept", JSON_MEDIA_TYPE);
92
+ postRequest.addHeader("content-type", JSON_MEDIA_TYPE);
9093
91
- if (headers != null) {
92
- for (String header : headers.keySet()) {
93
- String headerValue = headers.get(header);
94
- postRequest.addHeader(header, headerValue);
95
- }
96
- }
94
+ if (headers != null) {
95
+ for (String header : headers.keySet()) {
96
+ String headerValue = headers.get(header);
97
+ postRequest.addHeader(header, headerValue);
98
+ }
99
+ }
97100
98
- try {
99
- postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req)));
100
- } catch (UnsupportedEncodingException | SeCurisException e1) {
101
- throw new SeCurisException("Error preparing POST command", e1);
102
- }
103
- HttpResponse response;
104
- try {
105
- response = httpClientBuilder.build().execute(postRequest);
101
+ try {
102
+ postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req)));
103
+ } catch (UnsupportedEncodingException | SeCurisException e1) {
104
+ throw new SeCurisException("Error preparing POST command", e1);
105
+ }
106
+ HttpResponse response;
107
+ try {
108
+ response = httpClientBuilder.build().execute(postRequest);
106109
107
- checkErrors(command, response);
110
+ checkErrors(command, response);
108111
109
- String jsonLic = IOUtils.toString(response.getEntity().getContent());
110
- LOG.debug("Response content read OK: {}", jsonLic);
111
- T responseBean = JsonUtils.json2object(jsonLic, returnType);
112
+ String jsonLic = IOUtils.toString(response.getEntity().getContent());
113
+ LOG.debug("Response content read OK: {}", jsonLic);
114
+ T responseBean = JsonUtils.json2object(jsonLic, returnType);
112115
113
- LOG.debug("Response bean read OK: {}", responseBean);
116
+ LOG.debug("Response bean read OK: {}", responseBean);
114117
115
- return responseBean;
116
- } catch (IOException e) {
117
- LOG.error("Error accessing SeCuris server with command: " + command, e);
118
- throw new SeCurisException("Error accessing SeCuris server with command: " + command, e);
119
- }
120
- }
118
+ return responseBean;
119
+ } catch (IOException e) {
120
+ LOG.error("Error accessing SeCuris server with command: " + command, e);
121
+ throw new SeCurisException("Error accessing SeCuris server with command: " + command, e);
122
+ }
123
+ }
121124
122
- private void checkErrors(String command, HttpResponse response) throws SeCurisException {
123
- if (response.getStatusLine().getStatusCode() != 200) {
124
- if (response.getStatusLine().getStatusCode() == HTTP_STATUS_APP_ERRROR) {
125
- String errorCode = response.getFirstHeader(ERROR_CODE_MESSAGE_HEADER).getValue();
126
- String errorMsg = response.getFirstHeader(ERROR_MESSAGE_HEADER).getValue();
127
- throw new SeCurisException(String.format("[%s] - %s", errorCode, errorMsg));
128
- }
129
- LOG.error("Unexpected error executing {}, Reason: {}", command, response.getStatusLine().getReasonPhrase());
130
- throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode(),
131
- new IOException("Unexpected server error"));
132
- }
125
+ private void checkErrors(String command, HttpResponse response) throws SeCurisException {
126
+ if (response.getStatusLine().getStatusCode() != 200) {
127
+ if (response.getStatusLine().getStatusCode() == HTTP_STATUS_APP_ERRROR) {
128
+ String errorCode = response.getFirstHeader(ERROR_CODE_MESSAGE_HEADER).getValue();
129
+ String errorMsg = response.getFirstHeader(ERROR_MESSAGE_HEADER).getValue();
130
+ throw new SeCurisException(String.format("[%s] - %s", errorCode, errorMsg));
131
+ }
132
+ LOG.error("Unexpected error executing {}, Reason: {}", command, response.getStatusLine().getReasonPhrase());
133
+ throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode(),
134
+ new IOException("Unexpected server error"));
135
+ }
133136
134
- }
137
+ }
135138
136
- public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException {
137
- HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command));
138
- getRequest.addHeader("accept", JSON_MEDIA_TYPE);
139
+ public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException {
140
+ HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command));
141
+ getRequest.addHeader(TOKEN_HEADER, API_CLIENT_TOKEN);
142
+ getRequest.addHeader("accept", JSON_MEDIA_TYPE);
139143
140
- HttpResponse response;
141
- try {
142
- response = httpClientBuilder.build().execute(getRequest);
143
- if (response.getStatusLine().getStatusCode() != 200) {
144
- throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
145
- }
146
- String jsonLic = IOUtils.toString(response.getEntity().getContent());
147
- LOG.debug("Response content read OK: {}", jsonLic);
148
- T responseBean = JsonUtils.json2object(jsonLic, returnType);
144
+ HttpResponse response;
145
+ try {
146
+ response = httpClientBuilder.build().execute(getRequest);
147
+ if (response.getStatusLine().getStatusCode() != 200) {
148
+ throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
149
+ }
150
+ String jsonLic = IOUtils.toString(response.getEntity().getContent());
151
+ LOG.debug("Response content read OK: {}", jsonLic);
152
+ T responseBean = JsonUtils.json2object(jsonLic, returnType);
149153
150
- LOG.debug("Response bean read OK: {}", responseBean);
154
+ LOG.debug("Response bean read OK: {}", responseBean);
151155
152
- return responseBean;
153
- } catch (IOException e) {
154
- LOG.error("Error acessing SeCuris server", e);
155
- throw new SeCurisException("Error accessing SeCuris server");
156
- }
157
- }
156
+ return responseBean;
157
+ } catch (IOException e) {
158
+ LOG.error("Error acessing SeCuris server", e);
159
+ throw new SeCurisException("Error accessing SeCuris server");
160
+ }
161
+ }
158162
159
- public static class Command {
160
- public static final String TEST = "ping";
161
- public static final String CREATE_LIC = "request";
162
- public static final String RENEW_LIC = "renew";
163
- public static final String VALIDATE = "validate";
164
- }
163
+ public static class Command {
164
+ public static final String TEST = "ping";
165
+ public static final String CREATE_LIC = "request";
166
+ public static final String RENEW_LIC = "renew";
167
+ public static final String VALIDATE = "validate";
168
+ }
165169
166170 }
src/main/java/net/curisit/securis/LicenseManager.java
....@@ -30,237 +30,257 @@
3030 */
3131 public class LicenseManager {
3232
33
- private static final Logger LOG = LogManager.getLogger(LicenseManager.class);
33
+ private static final Logger LOG = LogManager.getLogger(LicenseManager.class);
3434
35
- private static LicenseManager singleton = new LicenseManager();
35
+ private static LicenseManager singleton = new LicenseManager();
3636
37
- 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";
37
+ 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";
4040
41
- private LicenseManager() {
42
- }
41
+ private LicenseManager() {
42
+ }
4343
44
- public static LicenseManager getInstance() {
45
- return singleton;
46
- }
44
+ public static LicenseManager getInstance() {
45
+ return singleton;
46
+ }
4747
48
- /**
49
- * Loads a license from file
50
- *
51
- * @param licFile
52
- * @return The license bean
53
- * @throws SeCurisException
54
- */
55
- public LicenseBean load(File licFile) throws SeCurisException {
56
- LicenseBean licBean;
57
- try {
58
- licBean = JsonUtils.json2object(FileUtils.readFileToString(licFile), LicenseBean.class);
59
- } catch (IOException e) {
60
- throw new SeCurisException("Error getting license data from file: " + licFile, e);
61
- }
62
- return licBean;
63
- }
48
+ /**
49
+ * Loads a license from file
50
+ *
51
+ * @param licFile
52
+ * @return The license bean
53
+ * @throws SeCurisException
54
+ */
55
+ public LicenseBean load(File licFile) throws SeCurisException {
56
+ LicenseBean licBean;
57
+ try {
58
+ licBean = JsonUtils.json2object(FileUtils.readFileToString(licFile), LicenseBean.class);
59
+ } catch (IOException e) {
60
+ throw new SeCurisException("Error getting license data from file: " + licFile, e);
61
+ }
62
+ return licBean;
63
+ }
6464
65
- /**
66
- * Validates the license stored in {@code licFile} and get the corresponding
67
- * LicenseBean
68
- * <p>
69
- * The validation includes:
70
- * <ul>
71
- * <li>Signature</li>
72
- * <li>HW data</li>
73
- * <li>Logo CRC</li>
74
- * </ul>
75
- * </p>
76
- *
77
- * @param licFile
78
- * @return The license bean stored in file
79
- * @throws SeCurisException
80
- */
81
- public LicenseBean validateLicense(File licFile) throws SeCurisException {
65
+ /**
66
+ * Validates the license stored in {@code licFile} and get the corresponding
67
+ * LicenseBean
68
+ * <p>
69
+ * The validation includes:
70
+ * <ul>
71
+ * <li>Signature</li>
72
+ * <li>HW data</li>
73
+ * <li>Logo CRC</li>
74
+ * </ul>
75
+ * </p>
76
+ *
77
+ * @param licFile
78
+ * @return The license bean stored in file
79
+ * @throws SeCurisException
80
+ */
81
+ public LicenseBean validateLicense(File licFile) throws SeCurisException {
8282
83
- return validateLicense(licFile, false);
84
- }
83
+ return validateLicense(licFile, false);
84
+ }
8585
86
- /**
87
- * Validates the license stored in {@code licFile} and get the corresponding
88
- * LicenseBean. The License date is not validated
89
- * <p>
90
- * The validation includes:
91
- * <ul>
92
- * <li>Signature</li>
93
- * <li>HW data</li>
94
- * <li>Logo CRC</li>
95
- * </ul>
96
- * </p>
97
- *
98
- * @param licFile
99
- * @return The license bean stored in file
100
- * @throws SeCurisException
101
- */
102
- public LicenseBean validateLicense(File licFile, boolean excludeDateValidation) throws SeCurisException {
103
- LicenseBean licBean = load(licFile);
104
- SignatureHelper.getInstance().validateSignature(licBean);
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
- }
111
- LicenseValidator.getInstance().validateLogo(licBean);
86
+ /**
87
+ * Validates the license stored in {@code licFile} and get the corresponding
88
+ * LicenseBean. The License date is not validated
89
+ * <p>
90
+ * The validation includes:
91
+ * <ul>
92
+ * <li>Signature</li>
93
+ * <li>HW data</li>
94
+ * <li>Logo CRC</li>
95
+ * </ul>
96
+ * </p>
97
+ *
98
+ * @param licFile
99
+ * @return The license bean stored in file
100
+ * @throws SeCurisException
101
+ */
102
+ public LicenseBean validateLicense(File licFile, boolean excludeDateValidation) throws SeCurisException {
103
+ LicenseBean licBean = load(licFile);
104
+ SignatureHelper.getInstance().validateSignature(licBean);
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
+ }
111
+ LicenseValidator.getInstance().validateLogo(licBean);
112112
113
- if (!excludeDateValidation) {
114
- if (new Date().after(licBean.getExpirationDate())) {
115
- throw new ExpiredLicenseException();
116
- }
117
- }
113
+ if (!excludeDateValidation) {
114
+ if (new Date().after(licBean.getExpirationDate())) {
115
+ throw new ExpiredLicenseException();
116
+ }
117
+ }
118118
119
- return licBean;
120
- }
119
+ return licBean;
120
+ }
121121
122
- /**
123
- * Request to server for a valid license
124
- *
125
- * @return The license bean returned by the server
126
- * @throws SeCurisException
127
- */
128
- public SignedLicenseBean requestLicense(String nameOrReference, String email) throws SeCurisException {
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));
122
+ /**
123
+ * Request to server for a valid license
124
+ *
125
+ * @return The license bean returned by the server
126
+ * @throws SeCurisException
127
+ */
128
+ public SignedLicenseBean requestLicense(String nameOrReference, String email) throws SeCurisException {
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));
131131
132
- SignedLicenseBean lic = requestLicenseToServer(req, nameOrReference, email);
133
- return lic;
134
- }
132
+ SignedLicenseBean lic = requestLicenseToServer(req, nameOrReference, email);
133
+ return lic;
134
+ }
135135
136
- /**
137
- * Request to server for a valid license
138
- *
139
- * @return The license bean returned by the server
140
- * @throws SeCurisException
141
- */
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);
136
+ /**
137
+ * Request to server for a valid license
138
+ *
139
+ * @return The license bean returned by the server
140
+ * @throws SeCurisException
141
+ */
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);
144144
145
- SignedLicenseBean lic = requestLicenseToServer(req, nameOrReference, email);
146
- return lic;
147
- }
145
+ SignedLicenseBean lic = requestLicenseToServer(req, nameOrReference, email);
146
+ return lic;
147
+ }
148148
149
- /**
150
- * Generate a license file using a {@link LicenseBean}
151
- *
152
- * @param license
153
- * @param file
154
- * @throws SeCurisException
155
- */
156
- public void save(LicenseBean license, File file) throws SeCurisException {
157
- SignedLicenseBean signedLic = new SignedLicenseBean(license);
158
- save(signedLic, file);
159
- }
149
+ /**
150
+ * Request to server for a valid license
151
+ *
152
+ * @return The license bean returned by the server
153
+ * @throws SeCurisException
154
+ */
155
+ public SignedLicenseBean requestLicense(String activationCode) throws SeCurisException {
156
+ return requestLicense(null, null, activationCode);
157
+ }
160158
161
- /**
162
- * Generate a license file using a {@link LicenseBean}
163
- *
164
- * @param license
165
- * @param file
166
- * @throws SeCurisException
167
- */
168
- public void save(SignedLicenseBean signedLic, File file) throws SeCurisException {
169
- byte[] json;
170
- try {
171
- json = JsonUtils.toPrettyJSON(signedLic).getBytes("utf-8");
172
- Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
173
- } catch (UnsupportedEncodingException e) {
174
- LOG.error("Error creating json doc from license: " + signedLic, e);
175
- throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
176
- } catch (IOException e) {
177
- LOG.error("Error creating license file: " + file, e);
178
- throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
179
- }
159
+ /**
160
+ * Generate a license file using a {@link LicenseBean}
161
+ *
162
+ * @param license
163
+ * @param file
164
+ * @throws SeCurisException
165
+ */
166
+ public void save(LicenseBean license, File file) throws SeCurisException {
167
+ SignedLicenseBean signedLic = new SignedLicenseBean(license);
168
+ save(signedLic, file);
169
+ }
180170
181
- LOG.debug("License saved in {}", file);
171
+ /**
172
+ * Generate a license file using a {@link LicenseBean}
173
+ *
174
+ * @param license
175
+ * @param file
176
+ * @throws SeCurisException
177
+ */
178
+ public void save(SignedLicenseBean signedLic, File file) throws SeCurisException {
179
+ byte[] json;
180
+ try {
181
+ json = JsonUtils.toPrettyJSON(signedLic).getBytes("utf-8");
182
+ Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
183
+ } catch (UnsupportedEncodingException e) {
184
+ LOG.error("Error creating json doc from license: " + signedLic, e);
185
+ throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
186
+ } catch (IOException e) {
187
+ LOG.error("Error creating license file: " + file, e);
188
+ throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
189
+ }
182190
183
- }
191
+ LOG.debug("License saved in {}", file);
184192
185
- private SignedLicenseBean requestLicenseToServer(RequestBean req, String nameOrReference, String email) throws SeCurisException {
186
- Map<String, String> headers = new HashMap<String, String>();
187
- headers.put(HEADER_LICENSE_NAME_OR_REFERENCE, nameOrReference);
188
- headers.put(HEADER_LICENSE_EMAIL, email);
189
- SignedLicenseBean lic = ConnectionManager.getInstance().executePost(Command.CREATE_LIC, SignedLicenseBean.class, req, headers);
193
+ }
190194
191
- return lic;
192
- }
195
+ private SignedLicenseBean requestLicenseToServer(RequestBean req, String nameOrReference, String email) throws SeCurisException {
196
+ Map<String, String> headers = new HashMap<String, String>();
197
+ headers.put(HEADER_LICENSE_NAME_OR_REFERENCE, nameOrReference);
198
+ headers.put(HEADER_LICENSE_EMAIL, email);
199
+ SignedLicenseBean lic = ConnectionManager.getInstance().executePost(Command.CREATE_LIC, SignedLicenseBean.class, req, headers);
193200
194
- /**
195
- * Creates a new request file with current hardware in the File passed as
196
- * parameter
197
- *
198
- * @param outputRequestFile
199
- * File where the request data will be saved
200
- * @return The generated request bean
201
- * @throws SeCurisException
202
- */
203
- public RequestBean createRequestFile(File outputRequestFile) throws SeCurisException {
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));
201
+ return lic;
202
+ }
206203
207
- ReqGenerator.getInstance().save(req, outputRequestFile);
204
+ /**
205
+ * Creates a new request file with current hardware in the File passed as
206
+ * parameter
207
+ *
208
+ * @param outputRequestFile
209
+ * File where the request data will be saved
210
+ * @return The generated request bean
211
+ * @throws SeCurisException
212
+ */
213
+ public RequestBean createRequestFile(File outputRequestFile) throws SeCurisException {
214
+ RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.LIC_TYPE_CODE, Params.get(Params.KEYS.APPLICATION_CODE)),
215
+ Params.get(Params.KEYS.CUSTOMER_CODE), Params.get(Params.KEYS.PACK_CODE));
208216
209
- return req;
210
- }
217
+ ReqGenerator.getInstance().save(req, outputRequestFile);
211218
212
- /**
213
- * Creates a new request file with current hardware in the File passed as
214
- * parameter
215
- *
216
- * @param outputRequestFile
217
- * File where the request data will be saved
218
- * @return The generated request bean
219
- * @throws SeCurisException
220
- */
221
- public RequestBean createRequestFile(File outputRequestFile, String activationCode) throws SeCurisException {
222
- RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), activationCode);
219
+ return req;
220
+ }
223221
224
- ReqGenerator.getInstance().save(req, outputRequestFile);
222
+ /**
223
+ * Creates a new request file with current hardware in the File passed as
224
+ * parameter
225
+ *
226
+ * @param outputRequestFile
227
+ * File where the request data will be saved
228
+ * @return The generated request bean
229
+ * @throws SeCurisException
230
+ */
231
+ public RequestBean createRequestFile(File outputRequestFile, String activationCode) throws SeCurisException {
232
+ RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), activationCode);
225233
226
- return req;
227
- }
234
+ ReqGenerator.getInstance().save(req, outputRequestFile);
228235
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.
232
- *
233
- * @param licenseFile
234
- * Current and valid License file
235
- * @return New license bean if server creates a new one, otherwise the same
236
- * current License bean will be returned
237
- * @throws SeCurisException
238
- */
239
- public SignedLicenseBean renew(File licenseFile) throws SeCurisException {
240
- LicenseBean lic = validateLicense(licenseFile, true);
236
+ return req;
237
+ }
241238
242
- SignedLicenseBean newLic = ConnectionManager.getInstance().executePost(Command.RENEW_LIC, SignedLicenseBean.class, lic);
239
+ /**
240
+ * Send the current license file to server, which is previously validated,
241
+ * to get a renewed one if it is prepared in server side.
242
+ *
243
+ * @param licenseFile
244
+ * Current and valid License file
245
+ * @return New license bean if server creates a new one, otherwise the same
246
+ * current License bean will be returned
247
+ * @throws SeCurisException
248
+ */
249
+ public SignedLicenseBean renew(File licenseFile) throws SeCurisException {
250
+ LicenseBean lic = validateLicense(licenseFile, true);
243251
244
- return newLic;
245
- }
252
+ SignedLicenseBean newLic = ConnectionManager.getInstance().executePost(Command.RENEW_LIC, SignedLicenseBean.class, lic);
246253
247
- /**
248
- * Check on SeCuris server if current license is still valid in server DB.
249
- *
250
- * @param licenseFile
251
- * @throws SeCurisException
252
- */
253
- public void assertLicenseIsValid(File licenseFile) throws SeCurisException, IOException {
254
- LicenseBean lic = validateLicense(licenseFile);
255
- // We need to snd the signed version to validate signature on server
256
- ConnectionManager.getInstance().executePost(Command.VALIDATE, LicenseBean.class, new SignedLicenseBean(lic));
257
- }
254
+ return newLic;
255
+ }
258256
259
- public void testServer() throws SeCurisException {
260
- StatusBean status = ConnectionManager.getInstance().executeGet(Command.TEST, StatusBean.class);
261
- if (!PING_MESSAGE.equals(status.getMessage())) {
262
- throw new SeCurisException("SeCuris Server is not running in given URL");
263
- }
264
- }
257
+ /**
258
+ * Check on SeCuris server if current license is still valid in server DB.
259
+ *
260
+ * @param licenseFile
261
+ * @throws SeCurisException
262
+ */
263
+ public void assertLicenseIsValid(File licenseFile) throws SeCurisException, IOException {
264
+ LicenseBean lic = validateLicense(licenseFile);
265
+ // We need to snd the signed version to validate signature on server
266
+ ConnectionManager.getInstance().executePost(Command.VALIDATE, LicenseBean.class, new SignedLicenseBean(lic));
267
+ }
265268
269
+ public void testServer() throws SeCurisException {
270
+ StatusBean status = ConnectionManager.getInstance().executeGet(Command.TEST, StatusBean.class);
271
+ if (!PING_MESSAGE.equals(status.getMessage())) {
272
+ throw new SeCurisException("SeCuris Server is not running in given URL");
273
+ }
274
+ }
275
+
276
+ public static void main(String[] args) throws SeCurisException {
277
+ System.out.println("APPLICATION_CODE: " + Params.get(Params.KEYS.APPLICATION_CODE));
278
+ System.out.println("LICENSE_SERVER_URL: " + Params.get(Params.KEYS.LICENSE_SERVER_URL));
279
+ LicenseManager lm = LicenseManager.getInstance();
280
+ // LicenseBean lic =
281
+ // lm.requestLicense("aaf88d6c-6622-492a-93ec-10f3d1dc7120");
282
+ LicenseBean lic = lm.requestLicense("Rob", "rsanchez@curisit.net");
283
+ System.out.println(lic.getLicenseCode() + " " + lic.getExpirationDate());
284
+ // LicenseBean lic = lm.
285
+ }
266286 }
src/main/java/net/curisit/securis/beans/LicenseBean.java
....@@ -29,6 +29,8 @@
2929 }
3030
3131 public LicenseBean(RequestBean req) {
32
+ super.setAppCode(req.getAppCode());
33
+ super.setActivationCode(req.getActivationCode());
3234 super.setPackCode(req.getPackCode());
3335 super.setLicenseTypeCode(req.getLicenseTypeCode());
3436 super.setCustomerCode(req.getCustomerCode());