rsanchez
2014-10-28 1d9d7b5f03b3e7b6af5600574a0ae6053843b77b
securis/src/main/java/net/curisit/securis/services/LicenseResource.java
....@@ -1,7 +1,6 @@
11 package net.curisit.securis.services;
22
33 import java.io.File;
4
-import java.io.FileWriter;
54 import java.io.IOException;
65 import java.nio.file.Files;
76 import java.text.MessageFormat;
....@@ -51,6 +50,8 @@
5150 import net.curisit.securis.security.Securable;
5251 import net.curisit.securis.services.exception.SeCurisServiceException;
5352 import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes;
53
+import net.curisit.securis.services.helpers.LicenseHelper;
54
+import net.curisit.securis.services.helpers.UserHelper;
5455 import net.curisit.securis.utils.EmailManager;
5556 import net.curisit.securis.utils.JsonUtils;
5657 import net.curisit.securis.utils.Params;
....@@ -75,16 +76,22 @@
7576 private static final Logger LOG = LogManager.getLogger(LicenseResource.class);
7677
7778 @Inject
78
- TokenHelper tokenHelper;
79
+ private TokenHelper tokenHelper;
7980
8081 @Inject
81
- EmailManager emailManager;
82
+ private EmailManager emailManager;
8283
8384 @Inject
84
- Provider<EntityManager> emProvider;
85
+ private UserHelper userHelper;
8586
8687 @Inject
87
- LicenseGenerator licenseGenerator;
88
+ private LicenseHelper licenseHelper;
89
+
90
+ @Inject
91
+ private Provider<EntityManager> emProvider;
92
+
93
+ @Inject
94
+ private LicenseGenerator licenseGenerator;
8895
8996 /**
9097 *
....@@ -162,7 +169,7 @@
162169 LOG.error("License with id {} is not active, so It can not downloaded", licId, bsc.getUserPrincipal());
163170 throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "License is not active, so It can not be downloaded");
164171 }
165
- em.persist(createLicenseHistoryAction(lic, getUser(bsc, em), LicenseHistory.Actions.DOWNLOAD));
172
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.DOWNLOAD));
166173 return Response
167174 .ok(lic.getLicenseData())
168175 .header("Content-Disposition",
....@@ -210,8 +217,8 @@
210217 lic.setModificationTimestamp(new Date());
211218
212219 em.persist(lic);
213
- User user = getUser(bsc.getUserPrincipal().getName(), em);
214
- em.persist(createLicenseHistoryAction(lic, user, LicenseHistory.Actions.ACTIVATE));
220
+ User user = userHelper.getUser(bsc.getUserPrincipal().getName(), em);
221
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.ACTIVATE));
215222 return Response.ok(lic).build();
216223 }
217224
....@@ -243,12 +250,12 @@
243250 throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "There is no license file available");
244251 }
245252
246
- User user = getUser(bsc.getUserPrincipal().getName(), em);
253
+ User user = userHelper.getUser(bsc.getUserPrincipal().getName(), em);
247254 try {
248255 String subject = MessageFormat.format(Params.get(Params.KEYS.EMAIL_LIC_DEFAULT_SUBJECT), lic.getPack().getAppName());
249256 String email_tpl = IOUtils.toString(this.getClass().getResourceAsStream("/lic_email_template.en"));
250257 String body = MessageFormat.format(email_tpl, lic.getFullName(), app.getName());
251
- licFile = createTemporaryLicenseFile(lic, app.getLicenseFilename());
258
+ licFile = licenseHelper.createTemporaryLicenseFile(lic, app.getLicenseFilename());
252259
253260 emailManager.sendEmail(subject, body, lic.getEmail(), addCC ? user.getEmail() : null, licFile);
254261 } catch (IOException e) {
....@@ -262,7 +269,7 @@
262269
263270 lic.setModificationTimestamp(new Date());
264271 em.persist(lic);
265
- em.persist(createLicenseHistoryAction(lic, user, LicenseHistory.Actions.SEND, "Email sent to: " + lic.getEmail()));
272
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.SEND, "Email sent to: " + lic.getEmail()));
266273 return Response.ok(lic).build();
267274 }
268275
....@@ -301,23 +308,8 @@
301308 + " can not be canceled without a reason");
302309 }
303310
304
- lic.setStatus(LicenseStatus.CANCELLED);
305
- lic.setCancelledById(bsc.getUserPrincipal().getName());
306
- lic.setModificationTimestamp(new Date());
307
- em.persist(lic);
308
-
309
- em.persist(createLicenseHistoryAction(lic, getUser(bsc, em), LicenseHistory.Actions.CANCEL, "Cancellation reason: " + reason));
311
+ licenseHelper.cancelLicense(lic, reason, bsc, em);
310312 return Response.ok(lic).build();
311
- }
312
-
313
- /**
314
- * Cancel the license
315
- *
316
- * @param lic
317
- * @param em
318
- */
319
- public static void cancelLicense(License lic, EntityManager em) throws SeCurisServiceException {
320
-
321313 }
322314
323315 @POST
....@@ -349,7 +341,7 @@
349341 }
350342 }
351343
352
- User createdBy = getUser(bsc.getUserPrincipal().getName(), em);
344
+ User createdBy = userHelper.getUser(bsc.getUserPrincipal().getName(), em);
353345
354346 if (lic.getRequestData() != null) {
355347 License existingLicense = License.findLicenseByRequestData(lic.getRequestData(), em);
....@@ -380,9 +372,9 @@
380372 lic.setCreationTimestamp(new Date());
381373 lic.setModificationTimestamp(lic.getCreationTimestamp());
382374 em.persist(lic);
383
- em.persist(createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE));
375
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE));
384376 if (lic.getStatus() == LicenseStatus.ACTIVE) {
385
- em.persist(createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE, "Activated on creation"));
377
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE, "Activated on creation"));
386378 }
387379
388380 return Response.ok(lic).build();
....@@ -486,7 +478,7 @@
486478
487479 currentLicense.setModificationTimestamp(new Date());
488480 em.persist(currentLicense);
489
- em.persist(createLicenseHistoryAction(lic, getUser(bsc, em), LicenseHistory.Actions.MODIFY));
481
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.MODIFY));
490482
491483 return Response.ok(currentLicense).build();
492484 }
....@@ -547,12 +539,12 @@
547539 }
548540 BlockedRequest blockedReq = new BlockedRequest();
549541 blockedReq.setCreationTimestamp(new Date());
550
- blockedReq.setBlockedBy(getUser(bsc, em));
542
+ blockedReq.setBlockedBy(userHelper.getUser(bsc, em));
551543 blockedReq.setRequestData(lic.getRequestData());
552544
553545 em.persist(blockedReq);
554546
555
- em.persist(createLicenseHistoryAction(lic, getUser(bsc, em), LicenseHistory.Actions.BLOCK));
547
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.BLOCK));
556548 return Response.ok(Utils.createMap("success", true, "id", licId)).build();
557549 }
558550
....@@ -571,7 +563,7 @@
571563 if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) {
572564 BlockedRequest blockedReq = em.find(BlockedRequest.class, lic.getReqDataHash());
573565 em.remove(blockedReq);
574
- em.persist(createLicenseHistoryAction(lic, getUser(bsc, em), LicenseHistory.Actions.UNBLOCK));
566
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.UNBLOCK));
575567 } else {
576568 LOG.info("Request data for license {} is NOT blocked", licId);
577569 }
....@@ -595,43 +587,6 @@
595587 throw new SeCurisServiceException(Status.UNAUTHORIZED.getStatusCode(), "Unathorized access to license data");
596588 }
597589 return lic;
598
- }
599
-
600
- private User getUser(BasicSecurityContext bsc, EntityManager em) throws SeCurisServiceException {
601
- String username = bsc.getUserPrincipal().getName();
602
- return getUser(username, em);
603
- }
604
-
605
- private User getUser(String username, EntityManager em) throws SeCurisServiceException {
606
- User user = null;
607
- if (username != null) {
608
- user = em.find(User.class, username);
609
- if (user == null) {
610
- throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "User not found with username: " + username);
611
- }
612
- }
613
- return user;
614
- }
615
-
616
- private File createTemporaryLicenseFile(License lic, String licFileName) throws IOException {
617
- File f = Files.createTempDirectory("securis-server").toFile();
618
- f = new File(f, licFileName);
619
- IOUtils.write(lic.getLicenseData(), new FileWriter(f));
620
- return f;
621
- }
622
-
623
- private LicenseHistory createLicenseHistoryAction(License lic, User user, String action, String comments) {
624
- LicenseHistory lh = new LicenseHistory();
625
- lh.setLicense(lic);
626
- lh.setUser(user);
627
- lh.setTimestamp(new Date());
628
- lh.setAction(action);
629
- lh.setComments(comments);
630
- return lh;
631
- }
632
-
633
- private LicenseHistory createLicenseHistoryAction(License lic, User user, String action) {
634
- return createLicenseHistoryAction(lic, user, action, null);
635590 }
636591
637592 public static void main(String[] args) throws IOException {