rsanchez
2014-12-02 1493d0f7be4ccf14631ba3e0b31cf63ca7fcf32c
securis/src/main/java/net/curisit/securis/services/LicenseResource.java
....@@ -55,7 +55,6 @@
5555 import net.curisit.securis.utils.EmailManager;
5656 import net.curisit.securis.utils.JsonUtils;
5757 import net.curisit.securis.utils.LicUtils;
58
-import net.curisit.securis.utils.TokenHelper;
5958
6059 import org.apache.commons.io.IOUtils;
6160 import org.apache.commons.lang3.ObjectUtils;
....@@ -77,9 +76,6 @@
7776 public class LicenseResource {
7877
7978 private static final Logger LOG = LogManager.getLogger(LicenseResource.class);
80
-
81
- @Inject
82
- private TokenHelper tokenHelper;
8379
8480 @Inject
8581 private EmailManager emailManager;
....@@ -206,6 +202,11 @@
206202 throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License with id " + licId
207203 + " can not be activated from the current license status");
208204 }
205
+
206
+ if (lic.getPack().getNumAvailables() == 0) {
207
+ throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "The pack has not available licenses");
208
+ }
209
+
209210 validateRequestData(lic.getPack(), lic.getRequestData());
210211
211212 License existingLicense = License.findLicenseByRequestData(lic.getRequestData(), em);
....@@ -250,11 +251,11 @@
250251 Application app = lic.getPack().getLicenseType().getApplication();
251252 File licFile = null;
252253 if (lic.getLicenseData() == null) {
253
- throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "There is no license file available");
254
+ throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "There is no license file available");
254255 }
255256
256257 if (lic.getFullName() == null) {
257
- throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "Please add an user name in license data to send it the license file");
258
+ throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "Please add an user name in license data to send it the license file");
258259 }
259260
260261 User user = userHelper.getUser(bsc.getUserPrincipal().getName(), em);
....@@ -276,7 +277,7 @@
276277 }
277278
278279 // lic.setModificationTimestamp(new Date());
279
- em.merge(lic);
280
+ // em.merge(lic);
280281 em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.SEND, "Email sent to: " + lic.getEmail()));
281282 return Response.ok(lic).build();
282283 }
....@@ -384,19 +385,23 @@
384385 .header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "There is already an active license for current request data")
385386 .type(MediaType.APPLICATION_JSON).entity(existingLicense).build();
386387 }
387
- SignedLicenseBean signedLicense = generateLicense(lic, em);
388
- // If user provide a request data the license status is passed
389
- // directly to ACTIVE
390
- lic.setStatus(LicenseStatus.ACTIVE);
391
- try {
392
- lic.setRequestData(JsonUtils.toJSON((RequestBean) signedLicense));
393
- if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) {
394
- throw new SeCurisServiceException(ErrorCodes.BLOCKED_REQUEST_DATA, "Given request data is blocked and cannot be activated");
388
+
389
+ if (pack.getNumAvailables() > 0) {
390
+
391
+ SignedLicenseBean signedLicense = generateLicense(lic, em);
392
+ // If user provide a request data the license status is passed
393
+ // directly to ACTIVE
394
+ lic.setStatus(LicenseStatus.ACTIVE);
395
+ try {
396
+ lic.setRequestData(JsonUtils.toJSON(signedLicense, RequestBean.class));
397
+ if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) {
398
+ throw new SeCurisServiceException(ErrorCodes.BLOCKED_REQUEST_DATA, "Given request data is blocked and cannot be activated");
399
+ }
400
+ lic.setLicenseData(JsonUtils.toJSON(signedLicense));
401
+ } catch (SeCurisException e) {
402
+ LOG.error("Error generating license JSON", e);
403
+ throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Error generating license JSON");
395404 }
396
- lic.setLicenseData(JsonUtils.toJSON(signedLicense));
397
- } catch (SeCurisException e) {
398
- LOG.error("Error generating license JSON", e);
399
- throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Error generating license JSON");
400405 }
401406 } else {
402407 lic.setStatus(LicenseStatus.CREATED);
....@@ -493,14 +498,15 @@
493498 if (lic.getRequestData() != null) {
494499 SignedLicenseBean signedLicense = generateLicense(lic, em);
495500 try {
496
- // Next line is necessary to normalize the String that
501
+ // Next 2 lines are necessary to normalize the String that
497502 // contains
498503 // the request.
499
- lic.setRequestData(JsonUtils.toJSON((RequestBean) signedLicense));
500
- if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) {
504
+ currentLicense.setRequestData(JsonUtils.toJSON(signedLicense, RequestBean.class));
505
+ LOG.info("JSON generated for request: \n{}", currentLicense.getRequestData());
506
+ if (BlockedRequest.isRequestBlocked(currentLicense.getRequestData(), em)) {
501507 throw new SeCurisServiceException(ErrorCodes.BLOCKED_REQUEST_DATA, "Given request data is blocked and cannot be used again");
502508 }
503
- lic.setLicenseData(JsonUtils.toJSON(signedLicense));
509
+ currentLicense.setLicenseData(JsonUtils.toJSON(signedLicense));
504510 } catch (SeCurisException e) {
505511 LOG.error("Error generaing license JSON", e);
506512 throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Error generaing license JSON");
....@@ -534,18 +540,15 @@
534540 LOG.error("License {} can not be deleted with status {}", lic.getCode(), lic.getStatus());
535541 throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "License can not be deleted in current status: " + lic.getStatus().name());
536542 }
537
- if (lic.getStatus() == LicenseStatus.CANCELLED) {
543
+ if (lic.getStatus() == LicenseStatus.BLOCKED) {
538544 // If license is removed and it's blocked then the blocked request
539545 // should be removed, that is,
540546 // the license deletion will unblock the request data
541
- TypedQuery<License> query = em.createNamedQuery("list-licenses-by-req-data", License.class);
542
- query.setParameter("hash", lic.getReqDataHash());
543
- List<License> list = query.getResultList();
544
- if (list == null || list.size() == 0) {
545
- BlockedRequest br = em.find(BlockedRequest.class, lic.getReqDataHash());
546
- if (br != null) {
547
- em.remove(br);
548
- }
547
+ BlockedRequest blockedReq = em.find(BlockedRequest.class, lic.getReqDataHash());
548
+ if (blockedReq != null) {
549
+ // This if is to avoid some race condition or if the request has
550
+ // been already removed manually
551
+ em.remove(blockedReq);
549552 }
550553 }
551554
....@@ -578,6 +581,9 @@
578581 blockedReq.setRequestData(lic.getRequestData());
579582
580583 em.persist(blockedReq);
584
+ lic.setStatus(LicenseStatus.BLOCKED);
585
+ lic.setModificationTimestamp(new Date());
586
+ em.merge(lic);
581587
582588 em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.BLOCK));
583589 return Response.ok(Utils.createMap("success", true, "id", licId)).build();
....@@ -598,6 +604,10 @@
598604 if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) {
599605 BlockedRequest blockedReq = em.find(BlockedRequest.class, lic.getReqDataHash());
600606 em.remove(blockedReq);
607
+
608
+ lic.setStatus(LicenseStatus.CANCELLED);
609
+ lic.setModificationTimestamp(new Date());
610
+ em.merge(lic);
601611 em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.UNBLOCK));
602612 } else {
603613 LOG.info("Request data for license {} is NOT blocked", licId);