| .. | .. |
|---|
| 13 | 13 | import javax.persistence.EntityManager; |
|---|
| 14 | 14 | import javax.persistence.TypedQuery; |
|---|
| 15 | 15 | |
|---|
| 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; |
|---|
| 16 | 21 | import net.curisit.securis.beans.LicenseBean; |
|---|
| 17 | 22 | import net.curisit.securis.db.License; |
|---|
| 18 | 23 | import net.curisit.securis.db.LicenseHistory; |
|---|
| .. | .. |
|---|
| 24 | 29 | import net.curisit.securis.services.exception.SeCurisServiceException; |
|---|
| 25 | 30 | import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes; |
|---|
| 26 | 31 | |
|---|
| 27 | | -import org.apache.commons.io.FileUtils; |
|---|
| 28 | | -import org.apache.logging.log4j.LogManager; |
|---|
| 29 | | -import org.apache.logging.log4j.Logger; |
|---|
| 30 | | - |
|---|
| 31 | 32 | @ApplicationScoped |
|---|
| 32 | 33 | public class LicenseHelper { |
|---|
| 33 | 34 | |
|---|
| 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; |
|---|
| 38 | 38 | |
|---|
| 39 | | - @Inject |
|---|
| 40 | | - private UserHelper userHelper; |
|---|
| 39 | + @Inject |
|---|
| 40 | + private UserHelper userHelper; |
|---|
| 41 | 41 | |
|---|
| 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); |
|---|
| 53 | 53 | |
|---|
| 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 | + } |
|---|
| 56 | 56 | |
|---|
| 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 | + } |
|---|
| 75 | 75 | |
|---|
| 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 | + } |
|---|
| 85 | 85 | |
|---|
| 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 | + } |
|---|
| 89 | 89 | |
|---|
| 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 | + } |
|---|
| 104 | 104 | |
|---|
| 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 | + } |
|---|
| 110 | 110 | |
|---|
| 111 | | - return metadata; |
|---|
| 112 | | - } |
|---|
| 111 | + return metadata; |
|---|
| 112 | + } |
|---|
| 113 | 113 | |
|---|
| 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 | + } |
|---|
| 136 | 141 | |
|---|
| 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 | + } |
|---|
| 150 | 155 | |
|---|
| 151 | 156 | } |
|---|