| .. | .. |
|---|
| 31 | 31 | import javax.ws.rs.core.Response; |
|---|
| 32 | 32 | import javax.ws.rs.core.Response.Status; |
|---|
| 33 | 33 | |
|---|
| 34 | | -import net.curisit.integrity.commons.JsonUtils; |
|---|
| 35 | 34 | import net.curisit.integrity.commons.Utils; |
|---|
| 36 | | -import net.curisit.integrity.exception.CurisException; |
|---|
| 37 | 35 | import net.curisit.securis.DefaultExceptionHandler; |
|---|
| 38 | 36 | import net.curisit.securis.LicenseGenerator; |
|---|
| 39 | 37 | import net.curisit.securis.SeCurisException; |
|---|
| .. | .. |
|---|
| 41 | 39 | import net.curisit.securis.beans.RequestBean; |
|---|
| 42 | 40 | import net.curisit.securis.beans.SignedLicenseBean; |
|---|
| 43 | 41 | import net.curisit.securis.db.Application; |
|---|
| 42 | +import net.curisit.securis.db.BlockedRequest; |
|---|
| 44 | 43 | import net.curisit.securis.db.License; |
|---|
| 45 | 44 | import net.curisit.securis.db.LicenseHistory; |
|---|
| 46 | 45 | import net.curisit.securis.db.LicenseStatus; |
|---|
| .. | .. |
|---|
| 52 | 51 | import net.curisit.securis.services.exception.SeCurisServiceException; |
|---|
| 53 | 52 | import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes; |
|---|
| 54 | 53 | import net.curisit.securis.utils.EmailManager; |
|---|
| 54 | +import net.curisit.securis.utils.JsonUtils; |
|---|
| 55 | 55 | import net.curisit.securis.utils.Params; |
|---|
| 56 | 56 | import net.curisit.securis.utils.TokenHelper; |
|---|
| 57 | 57 | |
|---|
| .. | .. |
|---|
| 331 | 331 | // directly to ACTIVE |
|---|
| 332 | 332 | lic.setStatus(LicenseStatus.ACTIVE); |
|---|
| 333 | 333 | try { |
|---|
| 334 | + lic.setRequestData(JsonUtils.toJSON((RequestBean) signedLicense)); |
|---|
| 335 | + if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) { |
|---|
| 336 | + throw new SeCurisServiceException(ErrorCodes.BLOCKED_REQUEST_DATA, "Given request data is blocked and cannot be activate"); |
|---|
| 337 | + } |
|---|
| 334 | 338 | lic.setLicenseData(JsonUtils.toJSON(signedLicense)); |
|---|
| 335 | | - } catch (CurisException e) { |
|---|
| 339 | + } catch (SeCurisException e) { |
|---|
| 336 | 340 | LOG.error("Error generaing license JSON", e); |
|---|
| 337 | 341 | throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Error generaing license JSON"); |
|---|
| 338 | 342 | } |
|---|
| .. | .. |
|---|
| 386 | 390 | RequestBean rb = null; |
|---|
| 387 | 391 | try { |
|---|
| 388 | 392 | rb = JsonUtils.json2object(requestData, RequestBean.class); |
|---|
| 389 | | - } catch (CurisException e) { |
|---|
| 393 | + } catch (SeCurisException e) { |
|---|
| 390 | 394 | throw new SeCurisServiceException(ErrorCodes.INVALID_REQUEST_DATA_FORMAT, "Request data has not a valid format"); |
|---|
| 391 | 395 | } |
|---|
| 392 | 396 | |
|---|
| .. | .. |
|---|
| 422 | 426 | currentLicense.setCode(lic.getCode()); |
|---|
| 423 | 427 | currentLicense.setFullName(lic.getFullName()); |
|---|
| 424 | 428 | currentLicense.setEmail(lic.getEmail()); |
|---|
| 425 | | - if (lic.getRequestData() != null) { |
|---|
| 429 | + if (lic.getRequestData() != null && currentLicense.getStatus() == LicenseStatus.CREATED) { |
|---|
| 426 | 430 | SignedLicenseBean signedLicense = generateLicense(lic, em); |
|---|
| 427 | | - // If user provide a request data the license status is passed |
|---|
| 428 | | - // directly to ACTIVE |
|---|
| 429 | 431 | lic.setStatus(LicenseStatus.ACTIVE); |
|---|
| 430 | 432 | try { |
|---|
| 433 | + // Next line is necessary to normalize the String that contains |
|---|
| 434 | + // the request. |
|---|
| 435 | + lic.setRequestData(JsonUtils.toJSON((RequestBean) signedLicense)); |
|---|
| 436 | + if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) { |
|---|
| 437 | + throw new SeCurisServiceException(ErrorCodes.BLOCKED_REQUEST_DATA, "Given request data is blocked and cannot be activate"); |
|---|
| 438 | + } |
|---|
| 431 | 439 | lic.setLicenseData(JsonUtils.toJSON(signedLicense)); |
|---|
| 432 | | - } catch (CurisException e) { |
|---|
| 440 | + } catch (SeCurisException e) { |
|---|
| 433 | 441 | LOG.error("Error generaing license JSON", e); |
|---|
| 434 | 442 | throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Error generaing license JSON"); |
|---|
| 435 | 443 | } |
|---|
| .. | .. |
|---|
| 465 | 473 | } |
|---|
| 466 | 474 | |
|---|
| 467 | 475 | @DELETE |
|---|
| 468 | | - @Path("/{licId}") |
|---|
| 476 | + @Path("/{licId}/block") |
|---|
| 469 | 477 | @Transactional |
|---|
| 470 | 478 | @Securable |
|---|
| 471 | 479 | @Produces({ |
|---|
| 472 | 480 | MediaType.APPLICATION_JSON |
|---|
| 473 | 481 | }) |
|---|
| 474 | 482 | public Response block(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 475 | | - LOG.info("Deleting license with id: {}", licId); |
|---|
| 483 | + LOG.info("Blocking license with id: {}", licId); |
|---|
| 476 | 484 | EntityManager em = emProvider.get(); |
|---|
| 477 | 485 | License lic = getCurrentLicense(licId, bsc, em); |
|---|
| 478 | 486 | |
|---|
| 479 | | - if (lic.getStatus() != LicenseStatus.CANCELLED || lic.getStatus() != LicenseStatus.CREATED) { |
|---|
| 480 | | - LOG.error("License {} can not be deleted with status {}", lic.getCode(), lic.getStatus()); |
|---|
| 481 | | - return Response.status(Status.FORBIDDEN) |
|---|
| 482 | | - .header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License can not be deleted in current status").build(); |
|---|
| 487 | + if (lic.getStatus() != LicenseStatus.CANCELLED) { |
|---|
| 488 | + LOG.error("License can only be blocked in CANCELLED status, current: {}", lic.getStatus().name()); |
|---|
| 489 | + throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "License can only be blocked in CANCELLED status"); |
|---|
| 483 | 490 | } |
|---|
| 491 | + if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) { |
|---|
| 492 | + throw new SeCurisServiceException(ErrorCodes.BLOCKED_REQUEST_DATA, "Given request data is already blocked"); |
|---|
| 493 | + } |
|---|
| 494 | + BlockedRequest blockedReq = new BlockedRequest(); |
|---|
| 495 | + blockedReq.setCreationTimestamp(new Date()); |
|---|
| 496 | + blockedReq.setBlockedBy(getUser(bsc, em)); |
|---|
| 497 | + blockedReq.setRequestData(lic.getRequestData()); |
|---|
| 484 | 498 | |
|---|
| 485 | | - em.remove(lic); |
|---|
| 499 | + em.persist(blockedReq); |
|---|
| 500 | + |
|---|
| 501 | + em.persist(createLicenseHistoryAction(lic, getUser(bsc, em), LicenseHistory.Actions.BLOCK)); |
|---|
| 486 | 502 | return Response.ok(Utils.createMap("success", true, "id", licId)).build(); |
|---|
| 487 | 503 | } |
|---|
| 488 | 504 | |
|---|