From 1493d0f7be4ccf14631ba3e0b31cf63ca7fcf32c Mon Sep 17 00:00:00 2001
From: rsanchez <rsanchez@curisit.net>
Date: Tue, 02 Dec 2014 17:57:50 +0000
Subject: [PATCH] #396 fix - Many issues fixed: Block/Unblock commands, Upload request data, Send second email, ...
---
securis/src/main/java/net/curisit/securis/services/LicenseResource.java | 74 +++++++++++++++++++++----------------
1 files changed, 42 insertions(+), 32 deletions(-)
diff --git a/securis/src/main/java/net/curisit/securis/services/LicenseResource.java b/securis/src/main/java/net/curisit/securis/services/LicenseResource.java
index d3165b4..f829f34 100644
--- a/securis/src/main/java/net/curisit/securis/services/LicenseResource.java
+++ b/securis/src/main/java/net/curisit/securis/services/LicenseResource.java
@@ -55,7 +55,6 @@
import net.curisit.securis.utils.EmailManager;
import net.curisit.securis.utils.JsonUtils;
import net.curisit.securis.utils.LicUtils;
-import net.curisit.securis.utils.TokenHelper;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ObjectUtils;
@@ -77,9 +76,6 @@
public class LicenseResource {
private static final Logger LOG = LogManager.getLogger(LicenseResource.class);
-
- @Inject
- private TokenHelper tokenHelper;
@Inject
private EmailManager emailManager;
@@ -206,6 +202,11 @@
throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License with id " + licId
+ " can not be activated from the current license status");
}
+
+ if (lic.getPack().getNumAvailables() == 0) {
+ throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "The pack has not available licenses");
+ }
+
validateRequestData(lic.getPack(), lic.getRequestData());
License existingLicense = License.findLicenseByRequestData(lic.getRequestData(), em);
@@ -250,11 +251,11 @@
Application app = lic.getPack().getLicenseType().getApplication();
File licFile = null;
if (lic.getLicenseData() == null) {
- throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "There is no license file available");
+ throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "There is no license file available");
}
if (lic.getFullName() == null) {
- throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "Please add an user name in license data to send it the license file");
+ throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "Please add an user name in license data to send it the license file");
}
User user = userHelper.getUser(bsc.getUserPrincipal().getName(), em);
@@ -276,7 +277,7 @@
}
// lic.setModificationTimestamp(new Date());
- em.merge(lic);
+ // em.merge(lic);
em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.SEND, "Email sent to: " + lic.getEmail()));
return Response.ok(lic).build();
}
@@ -384,19 +385,23 @@
.header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "There is already an active license for current request data")
.type(MediaType.APPLICATION_JSON).entity(existingLicense).build();
}
- SignedLicenseBean signedLicense = generateLicense(lic, em);
- // If user provide a request data the license status is passed
- // directly to ACTIVE
- lic.setStatus(LicenseStatus.ACTIVE);
- try {
- lic.setRequestData(JsonUtils.toJSON((RequestBean) signedLicense));
- if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) {
- throw new SeCurisServiceException(ErrorCodes.BLOCKED_REQUEST_DATA, "Given request data is blocked and cannot be activated");
+
+ if (pack.getNumAvailables() > 0) {
+
+ SignedLicenseBean signedLicense = generateLicense(lic, em);
+ // If user provide a request data the license status is passed
+ // directly to ACTIVE
+ lic.setStatus(LicenseStatus.ACTIVE);
+ try {
+ lic.setRequestData(JsonUtils.toJSON(signedLicense, RequestBean.class));
+ if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) {
+ throw new SeCurisServiceException(ErrorCodes.BLOCKED_REQUEST_DATA, "Given request data is blocked and cannot be activated");
+ }
+ lic.setLicenseData(JsonUtils.toJSON(signedLicense));
+ } catch (SeCurisException e) {
+ LOG.error("Error generating license JSON", e);
+ throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Error generating license JSON");
}
- lic.setLicenseData(JsonUtils.toJSON(signedLicense));
- } catch (SeCurisException e) {
- LOG.error("Error generating license JSON", e);
- throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Error generating license JSON");
}
} else {
lic.setStatus(LicenseStatus.CREATED);
@@ -493,14 +498,15 @@
if (lic.getRequestData() != null) {
SignedLicenseBean signedLicense = generateLicense(lic, em);
try {
- // Next line is necessary to normalize the String that
+ // Next 2 lines are necessary to normalize the String that
// contains
// the request.
- lic.setRequestData(JsonUtils.toJSON((RequestBean) signedLicense));
- if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) {
+ currentLicense.setRequestData(JsonUtils.toJSON(signedLicense, RequestBean.class));
+ LOG.info("JSON generated for request: \n{}", currentLicense.getRequestData());
+ if (BlockedRequest.isRequestBlocked(currentLicense.getRequestData(), em)) {
throw new SeCurisServiceException(ErrorCodes.BLOCKED_REQUEST_DATA, "Given request data is blocked and cannot be used again");
}
- lic.setLicenseData(JsonUtils.toJSON(signedLicense));
+ currentLicense.setLicenseData(JsonUtils.toJSON(signedLicense));
} catch (SeCurisException e) {
LOG.error("Error generaing license JSON", e);
throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Error generaing license JSON");
@@ -534,18 +540,15 @@
LOG.error("License {} can not be deleted with status {}", lic.getCode(), lic.getStatus());
throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "License can not be deleted in current status: " + lic.getStatus().name());
}
- if (lic.getStatus() == LicenseStatus.CANCELLED) {
+ if (lic.getStatus() == LicenseStatus.BLOCKED) {
// If license is removed and it's blocked then the blocked request
// should be removed, that is,
// the license deletion will unblock the request data
- TypedQuery<License> query = em.createNamedQuery("list-licenses-by-req-data", License.class);
- query.setParameter("hash", lic.getReqDataHash());
- List<License> list = query.getResultList();
- if (list == null || list.size() == 0) {
- BlockedRequest br = em.find(BlockedRequest.class, lic.getReqDataHash());
- if (br != null) {
- em.remove(br);
- }
+ BlockedRequest blockedReq = em.find(BlockedRequest.class, lic.getReqDataHash());
+ if (blockedReq != null) {
+ // This if is to avoid some race condition or if the request has
+ // been already removed manually
+ em.remove(blockedReq);
}
}
@@ -578,6 +581,9 @@
blockedReq.setRequestData(lic.getRequestData());
em.persist(blockedReq);
+ lic.setStatus(LicenseStatus.BLOCKED);
+ lic.setModificationTimestamp(new Date());
+ em.merge(lic);
em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.BLOCK));
return Response.ok(Utils.createMap("success", true, "id", licId)).build();
@@ -598,6 +604,10 @@
if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) {
BlockedRequest blockedReq = em.find(BlockedRequest.class, lic.getReqDataHash());
em.remove(blockedReq);
+
+ lic.setStatus(LicenseStatus.CANCELLED);
+ lic.setModificationTimestamp(new Date());
+ em.merge(lic);
em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.UNBLOCK));
} else {
LOG.info("Request data for license {} is NOT blocked", licId);
--
Gitblit v1.3.2