| .. | .. |
|---|
| 4 | 4 | import java.io.IOException; |
|---|
| 5 | 5 | import java.nio.file.Files; |
|---|
| 6 | 6 | import java.util.Date; |
|---|
| 7 | +import java.util.HashMap; |
|---|
| 8 | +import java.util.Map; |
|---|
| 9 | +import java.util.Set; |
|---|
| 7 | 10 | |
|---|
| 8 | 11 | import javax.inject.Inject; |
|---|
| 9 | 12 | import javax.inject.Singleton; |
|---|
| 10 | 13 | import javax.persistence.EntityManager; |
|---|
| 14 | +import javax.persistence.TypedQuery; |
|---|
| 11 | 15 | |
|---|
| 16 | +import net.curisit.securis.beans.LicenseBean; |
|---|
| 12 | 17 | import net.curisit.securis.db.License; |
|---|
| 13 | 18 | import net.curisit.securis.db.LicenseHistory; |
|---|
| 14 | 19 | import net.curisit.securis.db.LicenseStatus; |
|---|
| 20 | +import net.curisit.securis.db.Pack; |
|---|
| 21 | +import net.curisit.securis.db.PackMetadata; |
|---|
| 15 | 22 | import net.curisit.securis.db.User; |
|---|
| 16 | 23 | import net.curisit.securis.security.BasicSecurityContext; |
|---|
| 17 | 24 | import net.curisit.securis.services.exception.SeCurisServiceException; |
|---|
| 25 | +import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes; |
|---|
| 18 | 26 | |
|---|
| 19 | 27 | import org.apache.commons.io.FileUtils; |
|---|
| 20 | 28 | import org.apache.logging.log4j.LogManager; |
|---|
| .. | .. |
|---|
| 23 | 31 | @Singleton |
|---|
| 24 | 32 | public class LicenseHelper { |
|---|
| 25 | 33 | |
|---|
| 34 | + @SuppressWarnings("unused") |
|---|
| 26 | 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; |
|---|
| 27 | 38 | |
|---|
| 28 | 39 | @Inject |
|---|
| 29 | 40 | private UserHelper userHelper; |
|---|
| .. | .. |
|---|
| 41 | 52 | em.persist(lic); |
|---|
| 42 | 53 | |
|---|
| 43 | 54 | em.persist(createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.CANCEL, "Cancellation reason: " + reason)); |
|---|
| 55 | + } |
|---|
| 44 | 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 | + } |
|---|
| 45 | 74 | } |
|---|
| 46 | 75 | |
|---|
| 47 | 76 | public LicenseHistory createLicenseHistoryAction(License lic, User user, String action, String comments) { |
|---|
| .. | .. |
|---|
| 58 | 87 | return createLicenseHistoryAction(lic, user, action, null); |
|---|
| 59 | 88 | } |
|---|
| 60 | 89 | |
|---|
| 90 | + /** |
|---|
| 91 | + * Create a license file in a temporary directory |
|---|
| 92 | + * |
|---|
| 93 | + * @param lic |
|---|
| 94 | + * @param licFileName |
|---|
| 95 | + * @return |
|---|
| 96 | + * @throws IOException |
|---|
| 97 | + */ |
|---|
| 61 | 98 | public File createTemporaryLicenseFile(License lic, String licFileName) throws IOException { |
|---|
| 62 | 99 | File f = Files.createTempDirectory("securis-server").toFile(); |
|---|
| 63 | 100 | f = new File(f, licFileName); |
|---|
| 64 | 101 | FileUtils.writeStringToFile(f, lic.getLicenseData()); |
|---|
| 65 | 102 | return f; |
|---|
| 66 | 103 | } |
|---|
| 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 | + } |
|---|
| 110 | + |
|---|
| 111 | + return metadata; |
|---|
| 112 | + } |
|---|
| 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 | + validPeriod = pack.getRenewValidPeriod() * MS_PER_DAY; |
|---|
| 129 | + } |
|---|
| 130 | + Date expirationDate = new Date(new Date().getTime() + validPeriod); |
|---|
| 131 | + return expirationDate; |
|---|
| 132 | + } |
|---|
| 133 | + |
|---|
| 134 | + /** |
|---|
| 135 | + * Get the next free code suffis for a given Pack |
|---|
| 136 | + * |
|---|
| 137 | + * @param packId |
|---|
| 138 | + * @param em |
|---|
| 139 | + * @return |
|---|
| 140 | + */ |
|---|
| 141 | + public int getNextCodeSuffix(int packId, EntityManager em) { |
|---|
| 142 | + TypedQuery<Integer> query = em.createNamedQuery("last-code-suffix-used-in-pack", Integer.class); |
|---|
| 143 | + query.setParameter("packId", packId); |
|---|
| 144 | + Integer lastCodeSuffix = query.getSingleResult(); |
|---|
| 145 | + return lastCodeSuffix == null ? 1 : lastCodeSuffix + 1; |
|---|
| 146 | + } |
|---|
| 147 | + |
|---|
| 67 | 148 | } |
|---|