From 1d9d7b5f03b3e7b6af5600574a0ae6053843b77b Mon Sep 17 00:00:00 2001
From: rsanchez <rsanchez@curisit.net>
Date: Tue, 28 Oct 2014 13:07:05 +0000
Subject: [PATCH] #2021 feature - Fixing pack cacnellation and deletion actions
---
securis/src/main/java/net/curisit/securis/services/LicenseResource.java | 97 +++++++++++++-----------------------------------
1 files changed, 26 insertions(+), 71 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 ae36938..e338592 100644
--- a/securis/src/main/java/net/curisit/securis/services/LicenseResource.java
+++ b/securis/src/main/java/net/curisit/securis/services/LicenseResource.java
@@ -1,7 +1,6 @@
package net.curisit.securis.services;
import java.io.File;
-import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.text.MessageFormat;
@@ -51,6 +50,8 @@
import net.curisit.securis.security.Securable;
import net.curisit.securis.services.exception.SeCurisServiceException;
import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes;
+import net.curisit.securis.services.helpers.LicenseHelper;
+import net.curisit.securis.services.helpers.UserHelper;
import net.curisit.securis.utils.EmailManager;
import net.curisit.securis.utils.JsonUtils;
import net.curisit.securis.utils.Params;
@@ -75,16 +76,22 @@
private static final Logger LOG = LogManager.getLogger(LicenseResource.class);
@Inject
- TokenHelper tokenHelper;
+ private TokenHelper tokenHelper;
@Inject
- EmailManager emailManager;
+ private EmailManager emailManager;
@Inject
- Provider<EntityManager> emProvider;
+ private UserHelper userHelper;
@Inject
- LicenseGenerator licenseGenerator;
+ private LicenseHelper licenseHelper;
+
+ @Inject
+ private Provider<EntityManager> emProvider;
+
+ @Inject
+ private LicenseGenerator licenseGenerator;
/**
*
@@ -162,7 +169,7 @@
LOG.error("License with id {} is not active, so It can not downloaded", licId, bsc.getUserPrincipal());
throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "License is not active, so It can not be downloaded");
}
- em.persist(createLicenseHistoryAction(lic, getUser(bsc, em), LicenseHistory.Actions.DOWNLOAD));
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.DOWNLOAD));
return Response
.ok(lic.getLicenseData())
.header("Content-Disposition",
@@ -210,8 +217,8 @@
lic.setModificationTimestamp(new Date());
em.persist(lic);
- User user = getUser(bsc.getUserPrincipal().getName(), em);
- em.persist(createLicenseHistoryAction(lic, user, LicenseHistory.Actions.ACTIVATE));
+ User user = userHelper.getUser(bsc.getUserPrincipal().getName(), em);
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.ACTIVATE));
return Response.ok(lic).build();
}
@@ -243,12 +250,12 @@
throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "There is no license file available");
}
- User user = getUser(bsc.getUserPrincipal().getName(), em);
+ User user = userHelper.getUser(bsc.getUserPrincipal().getName(), em);
try {
String subject = MessageFormat.format(Params.get(Params.KEYS.EMAIL_LIC_DEFAULT_SUBJECT), lic.getPack().getAppName());
String email_tpl = IOUtils.toString(this.getClass().getResourceAsStream("/lic_email_template.en"));
String body = MessageFormat.format(email_tpl, lic.getFullName(), app.getName());
- licFile = createTemporaryLicenseFile(lic, app.getLicenseFilename());
+ licFile = licenseHelper.createTemporaryLicenseFile(lic, app.getLicenseFilename());
emailManager.sendEmail(subject, body, lic.getEmail(), addCC ? user.getEmail() : null, licFile);
} catch (IOException e) {
@@ -262,7 +269,7 @@
lic.setModificationTimestamp(new Date());
em.persist(lic);
- em.persist(createLicenseHistoryAction(lic, user, LicenseHistory.Actions.SEND, "Email sent to: " + lic.getEmail()));
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.SEND, "Email sent to: " + lic.getEmail()));
return Response.ok(lic).build();
}
@@ -301,23 +308,8 @@
+ " can not be canceled without a reason");
}
- lic.setStatus(LicenseStatus.CANCELLED);
- lic.setCancelledById(bsc.getUserPrincipal().getName());
- lic.setModificationTimestamp(new Date());
- em.persist(lic);
-
- em.persist(createLicenseHistoryAction(lic, getUser(bsc, em), LicenseHistory.Actions.CANCEL, "Cancellation reason: " + reason));
+ licenseHelper.cancelLicense(lic, reason, bsc, em);
return Response.ok(lic).build();
- }
-
- /**
- * Cancel the license
- *
- * @param lic
- * @param em
- */
- public static void cancelLicense(License lic, EntityManager em) throws SeCurisServiceException {
-
}
@POST
@@ -349,7 +341,7 @@
}
}
- User createdBy = getUser(bsc.getUserPrincipal().getName(), em);
+ User createdBy = userHelper.getUser(bsc.getUserPrincipal().getName(), em);
if (lic.getRequestData() != null) {
License existingLicense = License.findLicenseByRequestData(lic.getRequestData(), em);
@@ -380,9 +372,9 @@
lic.setCreationTimestamp(new Date());
lic.setModificationTimestamp(lic.getCreationTimestamp());
em.persist(lic);
- em.persist(createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE));
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE));
if (lic.getStatus() == LicenseStatus.ACTIVE) {
- em.persist(createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE, "Activated on creation"));
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE, "Activated on creation"));
}
return Response.ok(lic).build();
@@ -486,7 +478,7 @@
currentLicense.setModificationTimestamp(new Date());
em.persist(currentLicense);
- em.persist(createLicenseHistoryAction(lic, getUser(bsc, em), LicenseHistory.Actions.MODIFY));
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.MODIFY));
return Response.ok(currentLicense).build();
}
@@ -547,12 +539,12 @@
}
BlockedRequest blockedReq = new BlockedRequest();
blockedReq.setCreationTimestamp(new Date());
- blockedReq.setBlockedBy(getUser(bsc, em));
+ blockedReq.setBlockedBy(userHelper.getUser(bsc, em));
blockedReq.setRequestData(lic.getRequestData());
em.persist(blockedReq);
- em.persist(createLicenseHistoryAction(lic, getUser(bsc, em), LicenseHistory.Actions.BLOCK));
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.BLOCK));
return Response.ok(Utils.createMap("success", true, "id", licId)).build();
}
@@ -571,7 +563,7 @@
if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) {
BlockedRequest blockedReq = em.find(BlockedRequest.class, lic.getReqDataHash());
em.remove(blockedReq);
- em.persist(createLicenseHistoryAction(lic, getUser(bsc, em), LicenseHistory.Actions.UNBLOCK));
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.UNBLOCK));
} else {
LOG.info("Request data for license {} is NOT blocked", licId);
}
@@ -595,43 +587,6 @@
throw new SeCurisServiceException(Status.UNAUTHORIZED.getStatusCode(), "Unathorized access to license data");
}
return lic;
- }
-
- private User getUser(BasicSecurityContext bsc, EntityManager em) throws SeCurisServiceException {
- String username = bsc.getUserPrincipal().getName();
- return getUser(username, em);
- }
-
- private User getUser(String username, EntityManager em) throws SeCurisServiceException {
- User user = null;
- if (username != null) {
- user = em.find(User.class, username);
- if (user == null) {
- throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "User not found with username: " + username);
- }
- }
- return user;
- }
-
- private File createTemporaryLicenseFile(License lic, String licFileName) throws IOException {
- File f = Files.createTempDirectory("securis-server").toFile();
- f = new File(f, licFileName);
- IOUtils.write(lic.getLicenseData(), new FileWriter(f));
- return f;
- }
-
- private LicenseHistory createLicenseHistoryAction(License lic, User user, String action, String comments) {
- LicenseHistory lh = new LicenseHistory();
- lh.setLicense(lic);
- lh.setUser(user);
- lh.setTimestamp(new Date());
- lh.setAction(action);
- lh.setComments(comments);
- return lh;
- }
-
- private LicenseHistory createLicenseHistoryAction(License lic, User user, String action) {
- return createLicenseHistoryAction(lic, user, action, null);
}
public static void main(String[] args) throws IOException {
--
Gitblit v1.3.2