rsanchez
2014-12-17 8200793f22c0ec9fc1ab9026406fe4d3a8cbaab7
securis/src/main/java/net/curisit/securis/services/ApiResource.java
....@@ -9,6 +9,7 @@
99 import javax.persistence.EntityManager;
1010 import javax.ws.rs.Consumes;
1111 import javax.ws.rs.GET;
12
+import javax.ws.rs.HeaderParam;
1213 import javax.ws.rs.POST;
1314 import javax.ws.rs.Path;
1415 import javax.ws.rs.Produces;
....@@ -120,11 +121,10 @@
120121 MediaType.APPLICATION_JSON
121122 })
122123 @Transactional
123
- public Response createFromRequest(RequestBean request, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException,
124
- SeCurisException {
124
+ public Response createFromRequest(RequestBean request, @HeaderParam(LicenseManager.HEADER_LICENSE_NAME_OR_REFERENCE) String nameOrReference,
125
+ @HeaderParam(LicenseManager.HEADER_LICENSE_EMAIL) String userEmail) throws IOException, SeCurisServiceException, SeCurisException {
125126 LOG.info("Request to get license: {}", request);
126
-
127
- SignedLicenseBean lic = createLicense(request, emProvider.get(), false);
127
+ SignedLicenseBean lic = createLicense(request, emProvider.get(), false, nameOrReference, userEmail);
128128
129129 return Response.ok(lic).build();
130130 }
....@@ -148,8 +148,9 @@
148148 })
149149 @Transactional
150150 @SuppressWarnings("unchecked")
151
- public Response createFromRequestFile(MultipartFormDataInput mpfdi, @Context BasicSecurityContext bsc) throws IOException,
152
- SeCurisServiceException, SeCurisException {
151
+ public Response createFromRequestFile(MultipartFormDataInput mpfdi,
152
+ @HeaderParam(LicenseManager.HEADER_LICENSE_NAME_OR_REFERENCE) String nameOrReference,
153
+ @HeaderParam(LicenseManager.HEADER_LICENSE_EMAIL) String userEmail) throws IOException, SeCurisServiceException, SeCurisException {
153154 RequestBean req = new RequestBean();
154155 req.setPackCode(mpfdi.getFormDataPart("packCode", String.class, null));
155156 req.setLicenseTypeCode(mpfdi.getFormDataPart("licenseTypeCode", String.class, null));
....@@ -159,7 +160,7 @@
159160 req.setMacAddresses(mpfdi.getFormDataPart("macAddresses", List.class, null));
160161 req.setOsName(mpfdi.getFormDataPart("osName", String.class, null));
161162
162
- return createFromRequest(req, bsc);
163
+ return createFromRequest(req, nameOrReference, userEmail);
163164 }
164165
165166 /**
....@@ -194,7 +195,7 @@
194195 throw new SeCurisServiceException(ErrorCodes.LICENSE_NOT_READY_FOR_RENEW, "Only licenses with status 'Active' can be renew");
195196 }
196197
197
- SignedLicenseBean signedLic = createLicense(previousLic, em, true);
198
+ SignedLicenseBean signedLic = renewLicense(previousLic, em);
198199
199200 return Response.ok(signedLic).build();
200201 }
....@@ -273,7 +274,11 @@
273274 throw new SeCurisServiceException(ErrorCodes.LICENSE_NOT_READY_FOR_RENEW, "The license is still valid, not ready for renew");
274275 }
275276
276
- return createFromRequest(lic, bsc);
277
+ return renewFromPreviousLicense(lic, bsc);
278
+ }
279
+
280
+ private SignedLicenseBean renewLicense(RequestBean req, EntityManager em) throws SeCurisServiceException {
281
+ return createLicense(req, em, true, null, null);
277282 }
278283
279284 /**
....@@ -286,7 +291,8 @@
286291 * @return
287292 * @throws SeCurisServiceException
288293 */
289
- private SignedLicenseBean createLicense(RequestBean req, EntityManager em, boolean renew) throws SeCurisServiceException {
294
+ private SignedLicenseBean createLicense(RequestBean req, EntityManager em, boolean renew, String nameOrReference, String email)
295
+ throws SeCurisServiceException {
290296 LicenseBean previousLicenseBean = null;
291297 License lic = null;
292298 if (renew) {
....@@ -295,14 +301,25 @@
295301 if (lic.getStatus() != LicenseStatus.ACTIVE && lic.getStatus() != LicenseStatus.PRE_ACTIVE) {
296302 throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The current license has been cancelled");
297303 }
298
- } else {
299
- lic = new License();
300304 }
301305
302306 if (!renew) {
303
- License existingLicense = License.findLicenseByRequestData(lic.getRequestData(), em);
304
- if (existingLicense != null) {
305
- throw new SeCurisServiceException(ErrorCodes.DUPLICATED_REQUEST_DATA, "There is already an active license for current request data");
307
+ try {
308
+ lic = License.findValidLicenseByRequestData(JsonUtils.toJSON(req), em);
309
+ } catch (SeCurisException e1) {
310
+ throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Request sent is not valid");
311
+ }
312
+ if (lic != null) {
313
+ try {
314
+ if (lic.getStatus() == LicenseStatus.ACTIVE || lic.getStatus() == LicenseStatus.PRE_ACTIVE) {
315
+ return JsonUtils.json2object(lic.getLicenseData(), SignedLicenseBean.class);
316
+ }
317
+ } catch (SeCurisException e) {
318
+ throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Error trying to get the license bean from license code: "
319
+ + lic.getCode());
320
+ }
321
+ } else {
322
+ lic = new License();
306323 }
307324 }
308325 Pack pack = em.createNamedQuery("pack-by-code", Pack.class).setParameter("code", req.getPackCode()).getSingleResult();
....@@ -310,11 +327,15 @@
310327 if (!renew && pack.getNumAvailables() <= 0) {
311328 throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "The current pack has no licenses availables");
312329 }
330
+ if (!renew && lic.getStatus() == LicenseStatus.REQUESTED && !pack.isLicensePreactivation()) {
331
+ throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "Current pack doesn't allow license preactivation");
332
+ }
333
+
313334 SignedLicenseBean signedLicense;
314335 try {
315336 String licCode;
316
- if (renew) {
317
- licCode = previousLicenseBean.getLicenseCode();
337
+ if (renew || lic.getStatus() == LicenseStatus.REQUESTED) {
338
+ licCode = lic.getCode();
318339 } else {
319340 licCode = LicUtils.getLicenseCode(pack.getCode(), licenseHelper.getNextCodeSuffix(pack.getId(), em));
320341 }
....@@ -340,23 +361,35 @@
340361 lic.setModificationTimestamp(new Date());
341362 lic.setExpirationDate(signedLicense.getExpirationDate());
342363 User user = em.find(User.class, CLIENT_USERNAME);
343
- if (!renew) {
344
-
364
+ if (!renew && lic.getStatus() != LicenseStatus.REQUESTED) {
345365 lic.setPack(pack);
346366 lic.setCreatedBy(user);
347367 lic.setCreationTimestamp(new Date());
348
- lic.setStatus(LicenseStatus.PRE_ACTIVE);
368
+ if (pack.isLicensePreactivation()) {
369
+ lic.setStatus(LicenseStatus.PRE_ACTIVE);
370
+ } else {
371
+ lic.setStatus(LicenseStatus.REQUESTED);
372
+ }
349373 lic.setCode(signedLicense.getLicenseCode());
350374 lic.setCodeSuffix(LicUtils.getLicenseCodeSuffix(signedLicense.getLicenseCode()));
375
+ lic.setEmail(email);
376
+ lic.setFullName(nameOrReference);
351377 em.persist(lic);
352378 em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.CREATE));
353
- if (lic.getStatus() == LicenseStatus.ACTIVE) {
379
+ if (pack.isLicensePreactivation()) {
354380 em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.PRE_ACTIVATE, "Pre-activated on creation"));
381
+ } else {
382
+ LOG.warn("License ({}) created, but the pack doesn't allow preactivation", lic.getCode());
383
+ throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "Current pack doesn't allow license preactivation");
355384 }
356385 } else {
357
- lic.setStatus(LicenseStatus.ACTIVE);
386
+ lic.setStatus(renew ? LicenseStatus.ACTIVE : LicenseStatus.PRE_ACTIVE);
358387 em.merge(lic);
359
- em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.RENEW));
388
+ if (renew) {
389
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.RENEW));
390
+ } else {
391
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.PRE_ACTIVATE, "Pre-activated after request"));
392
+ }
360393 }
361394
362395 return signedLicense;