rsanchez
2014-12-01 85e2a65874fcd41771b30ebfff93f86edd4f32b3
securis/src/main/java/net/curisit/securis/services/LicenseResource.java
....@@ -2,7 +2,6 @@
22
33 import java.io.File;
44 import java.io.IOException;
5
-import java.nio.file.Files;
65 import java.text.MessageFormat;
76 import java.util.Date;
87 import java.util.HashMap;
....@@ -55,6 +54,7 @@
5554 import net.curisit.securis.utils.Config;
5655 import net.curisit.securis.utils.EmailManager;
5756 import net.curisit.securis.utils.JsonUtils;
57
+import net.curisit.securis.utils.LicUtils;
5858 import net.curisit.securis.utils.TokenHelper;
5959
6060 import org.apache.commons.io.IOUtils;
....@@ -320,6 +320,23 @@
320320 return Response.ok(lic).build();
321321 }
322322
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
+
323340 @POST
324341 @Path("/")
325342 @Consumes(MediaType.APPLICATION_JSON)
....@@ -330,6 +347,14 @@
330347 @Transactional
331348 public Response create(License lic, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
332349 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
+
333358 Pack pack = null;
334359 if (lic.getPackId() != null) {
335360 pack = em.find(Pack.class, lic.getPackId());
....@@ -376,18 +401,16 @@
376401 } else {
377402 lic.setStatus(LicenseStatus.CREATED);
378403 }
404
+ lic.setCodeSuffix(LicUtils.getLicenseCodeSuffix(lic.getCode()));
379405 lic.setCreatedBy(createdBy);
380406 lic.setCreationTimestamp(new Date());
381407 lic.setModificationTimestamp(lic.getCreationTimestamp());
382
- LOG.info("LICENSE: {}", lic);
408
+
383409 em.persist(lic);
384
- LOG.info("LICENSE on HISTORY create");
385410 em.persist(licenseHelper.createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE));
386411 if (lic.getStatus() == LicenseStatus.ACTIVE) {
387
- LOG.info("LICENSE ACTIVATION on HISTORY create");
388412 em.persist(licenseHelper.createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE, "Activated on creation"));
389413 }
390
- LOG.info("LICENSE created oK ??");
391414
392415 return Response.ok(lic).build();
393416 }
....@@ -599,14 +622,6 @@
599622 throw new SeCurisServiceException(Status.UNAUTHORIZED.getStatusCode(), "Unathorized access to license data");
600623 }
601624 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);
610625 }
611626
612627 @JsonAutoDetect