rsanchez
2017-04-18 523edc2956a42bda7d33884fcbbb6018addda4b0
securis/src/main/java/net/curisit/securis/services/helpers/LicenseHelper.java
....@@ -13,6 +13,11 @@
1313 import javax.persistence.EntityManager;
1414 import javax.persistence.TypedQuery;
1515
16
+import org.apache.commons.io.FileUtils;
17
+import org.apache.logging.log4j.LogManager;
18
+import org.apache.logging.log4j.Logger;
19
+
20
+import net.curisit.integrity.exception.CurisRuntimeException;
1621 import net.curisit.securis.beans.LicenseBean;
1722 import net.curisit.securis.db.License;
1823 import net.curisit.securis.db.LicenseHistory;
....@@ -24,128 +29,128 @@
2429 import net.curisit.securis.services.exception.SeCurisServiceException;
2530 import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes;
2631
27
-import org.apache.commons.io.FileUtils;
28
-import org.apache.logging.log4j.LogManager;
29
-import org.apache.logging.log4j.Logger;
30
-
3132 @ApplicationScoped
3233 public class LicenseHelper {
3334
34
- @SuppressWarnings("unused")
35
- private static final Logger LOG = LogManager.getLogger(LicenseHelper.class);
36
- private static final long MS_PER_DAY = 24L * 3600L * 1000L;
37
- private static final int DEFAULT_VALID_LIC_PERIOD = 7;
35
+ @SuppressWarnings("unused")
36
+ private static final Logger LOG = LogManager.getLogger(LicenseHelper.class);
37
+ private static final long MS_PER_DAY = 24L * 3600L * 1000L;
3838
39
- @Inject
40
- private UserHelper userHelper;
39
+ @Inject
40
+ private UserHelper userHelper;
4141
42
- /**
43
- * Cancel the license
44
- *
45
- * @param lic
46
- * @param em
47
- */
48
- public void cancelLicense(License lic, String reason, BasicSecurityContext bsc, EntityManager em) throws SeCurisServiceException {
49
- lic.setStatus(LicenseStatus.CANCELLED);
50
- lic.setCancelledById(bsc.getUserPrincipal().getName());
51
- lic.setModificationTimestamp(new Date());
52
- em.persist(lic);
42
+ /**
43
+ * Cancel the license
44
+ *
45
+ * @param lic
46
+ * @param em
47
+ */
48
+ public void cancelLicense(License lic, String reason, BasicSecurityContext bsc, EntityManager em) throws SeCurisServiceException {
49
+ lic.setStatus(LicenseStatus.CANCELLED);
50
+ lic.setCancelledById(bsc.getUserPrincipal().getName());
51
+ lic.setModificationTimestamp(new Date());
52
+ em.persist(lic);
5353
54
- em.persist(createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.CANCEL, "Cancellation reason: " + reason));
55
- }
54
+ em.persist(createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.CANCEL, "Cancellation reason: " + reason));
55
+ }
5656
57
- /**
58
- * Validates that the passed license is still valid
59
- *
60
- * @param lic
61
- * @param reason
62
- * @param bsc
63
- * @param em
64
- * @throws SeCurisServiceException
65
- */
66
- public void assertLicenseStatusIsActive(LicenseBean licBean, EntityManager em) throws SeCurisServiceException {
67
- License lic = License.findLicenseByCode(licBean.getLicenseCode(), em);
68
- if (lic == null) {
69
- throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "Current license code doesn't exist");
70
- }
71
- if (lic.getStatus() != LicenseStatus.ACTIVE && lic.getStatus() != LicenseStatus.PRE_ACTIVE) {
72
- throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "Current license in not active");
73
- }
74
- }
57
+ /**
58
+ * Validates that the passed license exists and is still valid
59
+ *
60
+ * @param licBean
61
+ * @param em
62
+ * @return The License instance in DB
63
+ * @throws SeCurisServiceException
64
+ */
65
+ public License getActiveLicenseFromDB(LicenseBean licBean, EntityManager em) throws SeCurisServiceException {
66
+ License lic = License.findLicenseByCode(licBean.getLicenseCode(), em);
67
+ if (lic == null) {
68
+ throw new SeCurisServiceException(ErrorCodes.LICENSE_DATA_IS_NOT_VALID, "Current license code doesn't exist");
69
+ }
70
+ if (lic.getStatus() != LicenseStatus.ACTIVE && lic.getStatus() != LicenseStatus.PRE_ACTIVE) {
71
+ throw new SeCurisServiceException(ErrorCodes.LICENSE_DATA_IS_NOT_VALID, "Current license in not active");
72
+ }
73
+ return lic;
74
+ }
7575
76
- public LicenseHistory createLicenseHistoryAction(License lic, User user, String action, String comments) {
77
- LicenseHistory lh = new LicenseHistory();
78
- lh.setLicense(lic);
79
- lh.setUser(user);
80
- lh.setCreationTimestamp(new Date());
81
- lh.setAction(action);
82
- lh.setComments(comments);
83
- return lh;
84
- }
76
+ public LicenseHistory createLicenseHistoryAction(License lic, User user, String action, String comments) {
77
+ LicenseHistory lh = new LicenseHistory();
78
+ lh.setLicense(lic);
79
+ lh.setUser(user);
80
+ lh.setCreationTimestamp(new Date());
81
+ lh.setAction(action);
82
+ lh.setComments(comments);
83
+ return lh;
84
+ }
8585
86
- public LicenseHistory createLicenseHistoryAction(License lic, User user, String action) {
87
- return createLicenseHistoryAction(lic, user, action, null);
88
- }
86
+ public LicenseHistory createLicenseHistoryAction(License lic, User user, String action) {
87
+ return createLicenseHistoryAction(lic, user, action, null);
88
+ }
8989
90
- /**
91
- * Create a license file in a temporary directory
92
- *
93
- * @param lic
94
- * @param licFileName
95
- * @return
96
- * @throws IOException
97
- */
98
- public File createTemporaryLicenseFile(License lic, String licFileName) throws IOException {
99
- File f = Files.createTempDirectory("securis-server").toFile();
100
- f = new File(f, licFileName);
101
- FileUtils.writeStringToFile(f, lic.getLicenseData());
102
- return f;
103
- }
90
+ /**
91
+ * Create a license file in a temporary directory
92
+ *
93
+ * @param lic
94
+ * @param licFileName
95
+ * @return
96
+ * @throws IOException
97
+ */
98
+ public File createTemporaryLicenseFile(License lic, String licFileName) throws IOException {
99
+ File f = Files.createTempDirectory("securis-server").toFile();
100
+ f = new File(f, licFileName);
101
+ FileUtils.writeStringToFile(f, lic.getLicenseData());
102
+ return f;
103
+ }
104104
105
- public Map<String, Object> extractPackMetadata(Set<PackMetadata> packMetadata) {
106
- Map<String, Object> metadata = new HashMap<>();
107
- for (PackMetadata md : packMetadata) {
108
- metadata.put(md.getKey(), md.getValue());
109
- }
105
+ public Map<String, Object> extractPackMetadata(Set<PackMetadata> packMetadata) {
106
+ Map<String, Object> metadata = new HashMap<>();
107
+ for (PackMetadata md : packMetadata) {
108
+ metadata.put(md.getKey(), md.getValue());
109
+ }
110110
111
- return metadata;
112
- }
111
+ return metadata;
112
+ }
113113
114
- /**
115
- * If the action is a renew the expiration date is got form pack end valid
116
- * date, if the action is a pre-activation the expiration date is calculated
117
- * using the pack default valid period
118
- *
119
- * @param pack
120
- * @param isPreActivation
121
- * @return
122
- */
123
- public Date getExpirationDateFromPack(Pack pack, boolean isPreActivation) {
124
- Long validPeriod;
125
- if (isPreActivation) {
126
- validPeriod = pack.getPreactivationValidPeriod() * MS_PER_DAY;
127
- } else {
128
- if (pack.getRenewValidPeriod() <= 0) {
129
- return pack.getEndValidDate();
130
- }
131
- validPeriod = pack.getRenewValidPeriod() * MS_PER_DAY;
132
- }
133
- Date expirationDate = new Date(new Date().getTime() + validPeriod);
134
- return expirationDate;
135
- }
114
+ /**
115
+ * If the action is a renew the expiration date is got form pack end valid
116
+ * date, if the action is a pre-activation the expiration date is calculated
117
+ * using the pack default valid period
118
+ *
119
+ * @param pack
120
+ * @param isPreActivation
121
+ * @return
122
+ */
123
+ public Date getExpirationDateFromPack(Pack pack, boolean isPreActivation) {
124
+ Long validPeriod;
125
+ if (pack.getEndValidDate().before(new Date())) {
126
+ throw new CurisRuntimeException("Pack end valid period is reached, no new licenses can be activated.");
127
+ }
128
+ if (isPreActivation) {
129
+ validPeriod = pack.getPreactivationValidPeriod() * MS_PER_DAY;
130
+ } else {
131
+ if (pack.getRenewValidPeriod() <= 0) {
132
+ return pack.getEndValidDate();
133
+ }
134
+ long renewPeriod = pack.getRenewValidPeriod() * MS_PER_DAY;
135
+ long expirationPeriod = pack.getEndValidDate().getTime() - new Date().getTime();
136
+ validPeriod = renewPeriod < expirationPeriod ? renewPeriod : expirationPeriod;
137
+ }
138
+ Date expirationDate = new Date(new Date().getTime() + validPeriod);
139
+ return expirationDate;
140
+ }
136141
137
- /**
138
- * Get the next free code suffis for a given Pack
139
- *
140
- * @param packId
141
- * @param em
142
- * @return
143
- */
144
- public int getNextCodeSuffix(int packId, EntityManager em) {
145
- TypedQuery<Integer> query = em.createNamedQuery("last-code-suffix-used-in-pack", Integer.class);
146
- query.setParameter("packId", packId);
147
- Integer lastCodeSuffix = query.getSingleResult();
148
- return lastCodeSuffix == null ? 1 : lastCodeSuffix + 1;
149
- }
142
+ /**
143
+ * Get the next free code suffis for a given Pack
144
+ *
145
+ * @param packId
146
+ * @param em
147
+ * @return
148
+ */
149
+ public int getNextCodeSuffix(int packId, EntityManager em) {
150
+ TypedQuery<Integer> query = em.createNamedQuery("last-code-suffix-used-in-pack", Integer.class);
151
+ query.setParameter("packId", packId);
152
+ Integer lastCodeSuffix = query.getSingleResult();
153
+ return lastCodeSuffix == null ? 1 : lastCodeSuffix + 1;
154
+ }
150155
151156 }