| .. | .. |
|---|
| 55 | 55 | import net.curisit.securis.utils.EmailManager; |
|---|
| 56 | 56 | import net.curisit.securis.utils.JsonUtils; |
|---|
| 57 | 57 | import net.curisit.securis.utils.LicUtils; |
|---|
| 58 | | -import net.curisit.securis.utils.TokenHelper; |
|---|
| 59 | 58 | |
|---|
| 60 | 59 | import org.apache.commons.io.IOUtils; |
|---|
| 61 | 60 | import org.apache.commons.lang3.ObjectUtils; |
|---|
| .. | .. |
|---|
| 77 | 76 | public class LicenseResource { |
|---|
| 78 | 77 | |
|---|
| 79 | 78 | private static final Logger LOG = LogManager.getLogger(LicenseResource.class); |
|---|
| 80 | | - |
|---|
| 81 | | - @Inject |
|---|
| 82 | | - private TokenHelper tokenHelper; |
|---|
| 83 | 79 | |
|---|
| 84 | 80 | @Inject |
|---|
| 85 | 81 | private EmailManager emailManager; |
|---|
| .. | .. |
|---|
| 206 | 202 | throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License with id " + licId |
|---|
| 207 | 203 | + " can not be activated from the current license status"); |
|---|
| 208 | 204 | } |
|---|
| 205 | + |
|---|
| 206 | + if (lic.getPack().getNumAvailables() == 0) { |
|---|
| 207 | + throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "The pack has not available licenses"); |
|---|
| 208 | + } |
|---|
| 209 | + |
|---|
| 209 | 210 | validateRequestData(lic.getPack(), lic.getRequestData()); |
|---|
| 210 | 211 | |
|---|
| 211 | 212 | License existingLicense = License.findLicenseByRequestData(lic.getRequestData(), em); |
|---|
| .. | .. |
|---|
| 250 | 251 | Application app = lic.getPack().getLicenseType().getApplication(); |
|---|
| 251 | 252 | File licFile = null; |
|---|
| 252 | 253 | if (lic.getLicenseData() == null) { |
|---|
| 253 | | - throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "There is no license file available"); |
|---|
| 254 | + throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "There is no license file available"); |
|---|
| 254 | 255 | } |
|---|
| 255 | 256 | |
|---|
| 256 | 257 | if (lic.getFullName() == null) { |
|---|
| 257 | | - throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "Please add an user name in license data to send it the license file"); |
|---|
| 258 | + throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "Please add an user name in license data to send it the license file"); |
|---|
| 258 | 259 | } |
|---|
| 259 | 260 | |
|---|
| 260 | 261 | User user = userHelper.getUser(bsc.getUserPrincipal().getName(), em); |
|---|
| .. | .. |
|---|
| 276 | 277 | } |
|---|
| 277 | 278 | |
|---|
| 278 | 279 | // lic.setModificationTimestamp(new Date()); |
|---|
| 279 | | - em.merge(lic); |
|---|
| 280 | + // em.merge(lic); |
|---|
| 280 | 281 | em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.SEND, "Email sent to: " + lic.getEmail())); |
|---|
| 281 | 282 | return Response.ok(lic).build(); |
|---|
| 282 | 283 | } |
|---|
| .. | .. |
|---|
| 384 | 385 | .header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "There is already an active license for current request data") |
|---|
| 385 | 386 | .type(MediaType.APPLICATION_JSON).entity(existingLicense).build(); |
|---|
| 386 | 387 | } |
|---|
| 387 | | - SignedLicenseBean signedLicense = generateLicense(lic, em); |
|---|
| 388 | | - // If user provide a request data the license status is passed |
|---|
| 389 | | - // directly to ACTIVE |
|---|
| 390 | | - lic.setStatus(LicenseStatus.ACTIVE); |
|---|
| 391 | | - try { |
|---|
| 392 | | - lic.setRequestData(JsonUtils.toJSON((RequestBean) signedLicense)); |
|---|
| 393 | | - if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) { |
|---|
| 394 | | - throw new SeCurisServiceException(ErrorCodes.BLOCKED_REQUEST_DATA, "Given request data is blocked and cannot be activated"); |
|---|
| 388 | + |
|---|
| 389 | + if (pack.getNumAvailables() > 0) { |
|---|
| 390 | + |
|---|
| 391 | + SignedLicenseBean signedLicense = generateLicense(lic, em); |
|---|
| 392 | + // If user provide a request data the license status is passed |
|---|
| 393 | + // directly to ACTIVE |
|---|
| 394 | + lic.setStatus(LicenseStatus.ACTIVE); |
|---|
| 395 | + try { |
|---|
| 396 | + lic.setRequestData(JsonUtils.toJSON(signedLicense, RequestBean.class)); |
|---|
| 397 | + if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) { |
|---|
| 398 | + throw new SeCurisServiceException(ErrorCodes.BLOCKED_REQUEST_DATA, "Given request data is blocked and cannot be activated"); |
|---|
| 399 | + } |
|---|
| 400 | + lic.setLicenseData(JsonUtils.toJSON(signedLicense)); |
|---|
| 401 | + } catch (SeCurisException e) { |
|---|
| 402 | + LOG.error("Error generating license JSON", e); |
|---|
| 403 | + throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Error generating license JSON"); |
|---|
| 395 | 404 | } |
|---|
| 396 | | - lic.setLicenseData(JsonUtils.toJSON(signedLicense)); |
|---|
| 397 | | - } catch (SeCurisException e) { |
|---|
| 398 | | - LOG.error("Error generating license JSON", e); |
|---|
| 399 | | - throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Error generating license JSON"); |
|---|
| 400 | 405 | } |
|---|
| 401 | 406 | } else { |
|---|
| 402 | 407 | lic.setStatus(LicenseStatus.CREATED); |
|---|
| .. | .. |
|---|
| 493 | 498 | if (lic.getRequestData() != null) { |
|---|
| 494 | 499 | SignedLicenseBean signedLicense = generateLicense(lic, em); |
|---|
| 495 | 500 | try { |
|---|
| 496 | | - // Next line is necessary to normalize the String that |
|---|
| 501 | + // Next 2 lines are necessary to normalize the String that |
|---|
| 497 | 502 | // contains |
|---|
| 498 | 503 | // the request. |
|---|
| 499 | | - lic.setRequestData(JsonUtils.toJSON((RequestBean) signedLicense)); |
|---|
| 500 | | - if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) { |
|---|
| 504 | + currentLicense.setRequestData(JsonUtils.toJSON(signedLicense, RequestBean.class)); |
|---|
| 505 | + LOG.info("JSON generated for request: \n{}", currentLicense.getRequestData()); |
|---|
| 506 | + if (BlockedRequest.isRequestBlocked(currentLicense.getRequestData(), em)) { |
|---|
| 501 | 507 | throw new SeCurisServiceException(ErrorCodes.BLOCKED_REQUEST_DATA, "Given request data is blocked and cannot be used again"); |
|---|
| 502 | 508 | } |
|---|
| 503 | | - lic.setLicenseData(JsonUtils.toJSON(signedLicense)); |
|---|
| 509 | + currentLicense.setLicenseData(JsonUtils.toJSON(signedLicense)); |
|---|
| 504 | 510 | } catch (SeCurisException e) { |
|---|
| 505 | 511 | LOG.error("Error generaing license JSON", e); |
|---|
| 506 | 512 | throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Error generaing license JSON"); |
|---|
| .. | .. |
|---|
| 534 | 540 | LOG.error("License {} can not be deleted with status {}", lic.getCode(), lic.getStatus()); |
|---|
| 535 | 541 | throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "License can not be deleted in current status: " + lic.getStatus().name()); |
|---|
| 536 | 542 | } |
|---|
| 537 | | - if (lic.getStatus() == LicenseStatus.CANCELLED) { |
|---|
| 543 | + if (lic.getStatus() == LicenseStatus.BLOCKED) { |
|---|
| 538 | 544 | // If license is removed and it's blocked then the blocked request |
|---|
| 539 | 545 | // should be removed, that is, |
|---|
| 540 | 546 | // the license deletion will unblock the request data |
|---|
| 541 | | - TypedQuery<License> query = em.createNamedQuery("list-licenses-by-req-data", License.class); |
|---|
| 542 | | - query.setParameter("hash", lic.getReqDataHash()); |
|---|
| 543 | | - List<License> list = query.getResultList(); |
|---|
| 544 | | - if (list == null || list.size() == 0) { |
|---|
| 545 | | - BlockedRequest br = em.find(BlockedRequest.class, lic.getReqDataHash()); |
|---|
| 546 | | - if (br != null) { |
|---|
| 547 | | - em.remove(br); |
|---|
| 548 | | - } |
|---|
| 547 | + BlockedRequest blockedReq = em.find(BlockedRequest.class, lic.getReqDataHash()); |
|---|
| 548 | + if (blockedReq != null) { |
|---|
| 549 | + // This if is to avoid some race condition or if the request has |
|---|
| 550 | + // been already removed manually |
|---|
| 551 | + em.remove(blockedReq); |
|---|
| 549 | 552 | } |
|---|
| 550 | 553 | } |
|---|
| 551 | 554 | |
|---|
| .. | .. |
|---|
| 578 | 581 | blockedReq.setRequestData(lic.getRequestData()); |
|---|
| 579 | 582 | |
|---|
| 580 | 583 | em.persist(blockedReq); |
|---|
| 584 | + lic.setStatus(LicenseStatus.BLOCKED); |
|---|
| 585 | + lic.setModificationTimestamp(new Date()); |
|---|
| 586 | + em.merge(lic); |
|---|
| 581 | 587 | |
|---|
| 582 | 588 | em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.BLOCK)); |
|---|
| 583 | 589 | return Response.ok(Utils.createMap("success", true, "id", licId)).build(); |
|---|
| .. | .. |
|---|
| 598 | 604 | if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) { |
|---|
| 599 | 605 | BlockedRequest blockedReq = em.find(BlockedRequest.class, lic.getReqDataHash()); |
|---|
| 600 | 606 | em.remove(blockedReq); |
|---|
| 607 | + |
|---|
| 608 | + lic.setStatus(LicenseStatus.CANCELLED); |
|---|
| 609 | + lic.setModificationTimestamp(new Date()); |
|---|
| 610 | + em.merge(lic); |
|---|
| 601 | 611 | em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.UNBLOCK)); |
|---|
| 602 | 612 | } else { |
|---|
| 603 | 613 | LOG.info("Request data for license {} is NOT blocked", licId); |
|---|