| .. | .. |
|---|
| 2 | 2 | |
|---|
| 3 | 3 | import java.io.File; |
|---|
| 4 | 4 | import java.io.IOException; |
|---|
| 5 | | -import java.nio.file.Files; |
|---|
| 6 | 5 | import java.text.MessageFormat; |
|---|
| 7 | 6 | import java.util.Date; |
|---|
| 8 | 7 | import java.util.HashMap; |
|---|
| .. | .. |
|---|
| 55 | 54 | import net.curisit.securis.utils.Config; |
|---|
| 56 | 55 | import net.curisit.securis.utils.EmailManager; |
|---|
| 57 | 56 | import net.curisit.securis.utils.JsonUtils; |
|---|
| 57 | +import net.curisit.securis.utils.LicUtils; |
|---|
| 58 | 58 | import net.curisit.securis.utils.TokenHelper; |
|---|
| 59 | 59 | |
|---|
| 60 | 60 | import org.apache.commons.io.IOUtils; |
|---|
| .. | .. |
|---|
| 320 | 320 | return Response.ok(lic).build(); |
|---|
| 321 | 321 | } |
|---|
| 322 | 322 | |
|---|
| 323 | + /** |
|---|
| 324 | + * Check if there is some pack with the same code |
|---|
| 325 | + * |
|---|
| 326 | + * @param code |
|---|
| 327 | + * Pack code |
|---|
| 328 | + * @param em |
|---|
| 329 | + * DB session object |
|---|
| 330 | + * @return <code>true</code> if code is already used, <code>false</code> |
|---|
| 331 | + * otherwise |
|---|
| 332 | + */ |
|---|
| 333 | + private boolean checkIfCodeExists(String code, EntityManager em) { |
|---|
| 334 | + TypedQuery<License> query = em.createNamedQuery("license-by-code", License.class); |
|---|
| 335 | + query.setParameter("code", code); |
|---|
| 336 | + int lics = query.getResultList().size(); |
|---|
| 337 | + return lics > 0; |
|---|
| 338 | + } |
|---|
| 339 | + |
|---|
| 323 | 340 | @POST |
|---|
| 324 | 341 | @Path("/") |
|---|
| 325 | 342 | @Consumes(MediaType.APPLICATION_JSON) |
|---|
| .. | .. |
|---|
| 330 | 347 | @Transactional |
|---|
| 331 | 348 | public Response create(License lic, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 332 | 349 | EntityManager em = emProvider.get(); |
|---|
| 350 | + |
|---|
| 351 | + if (checkIfCodeExists(lic.getCode(), em)) { |
|---|
| 352 | + throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The license code is already used in an existing license"); |
|---|
| 353 | + } |
|---|
| 354 | + if (!LicUtils.checkValidLicenseCodeCrc(lic.getCode())) { |
|---|
| 355 | + throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The license code is not valid"); |
|---|
| 356 | + } |
|---|
| 357 | + |
|---|
| 333 | 358 | Pack pack = null; |
|---|
| 334 | 359 | if (lic.getPackId() != null) { |
|---|
| 335 | 360 | pack = em.find(Pack.class, lic.getPackId()); |
|---|
| .. | .. |
|---|
| 376 | 401 | } else { |
|---|
| 377 | 402 | lic.setStatus(LicenseStatus.CREATED); |
|---|
| 378 | 403 | } |
|---|
| 404 | + lic.setCodeSuffix(LicUtils.getLicenseCodeSuffix(lic.getCode())); |
|---|
| 379 | 405 | lic.setCreatedBy(createdBy); |
|---|
| 380 | 406 | lic.setCreationTimestamp(new Date()); |
|---|
| 381 | 407 | lic.setModificationTimestamp(lic.getCreationTimestamp()); |
|---|
| 382 | | - LOG.info("LICENSE: {}", lic); |
|---|
| 408 | + |
|---|
| 383 | 409 | em.persist(lic); |
|---|
| 384 | | - LOG.info("LICENSE on HISTORY create"); |
|---|
| 385 | 410 | em.persist(licenseHelper.createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE)); |
|---|
| 386 | 411 | if (lic.getStatus() == LicenseStatus.ACTIVE) { |
|---|
| 387 | | - LOG.info("LICENSE ACTIVATION on HISTORY create"); |
|---|
| 388 | 412 | em.persist(licenseHelper.createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE, "Activated on creation")); |
|---|
| 389 | 413 | } |
|---|
| 390 | | - LOG.info("LICENSE created oK ??"); |
|---|
| 391 | 414 | |
|---|
| 392 | 415 | return Response.ok(lic).build(); |
|---|
| 393 | 416 | } |
|---|
| .. | .. |
|---|
| 599 | 622 | throw new SeCurisServiceException(Status.UNAUTHORIZED.getStatusCode(), "Unathorized access to license data"); |
|---|
| 600 | 623 | } |
|---|
| 601 | 624 | return lic; |
|---|
| 602 | | - } |
|---|
| 603 | | - |
|---|
| 604 | | - public static void main(String[] args) throws IOException { |
|---|
| 605 | | - File f = Files.createTempDirectory("securis-server").toFile(); |
|---|
| 606 | | - |
|---|
| 607 | | - LOG.info("f: {}", f); |
|---|
| 608 | | - f = new File(f, "config-server.lic"); |
|---|
| 609 | | - LOG.info("f: {}", f); |
|---|
| 610 | 625 | } |
|---|
| 611 | 626 | |
|---|
| 612 | 627 | @JsonAutoDetect |
|---|