| .. | .. |
|---|
| 1 | 1 | package net.curisit.securis.services; |
|---|
| 2 | 2 | |
|---|
| 3 | 3 | import java.io.File; |
|---|
| 4 | | -import java.io.FileWriter; |
|---|
| 5 | 4 | import java.io.IOException; |
|---|
| 6 | 5 | import java.nio.file.Files; |
|---|
| 7 | 6 | import java.text.MessageFormat; |
|---|
| .. | .. |
|---|
| 51 | 50 | import net.curisit.securis.security.Securable; |
|---|
| 52 | 51 | import net.curisit.securis.services.exception.SeCurisServiceException; |
|---|
| 53 | 52 | import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes; |
|---|
| 53 | +import net.curisit.securis.services.helpers.LicenseHelper; |
|---|
| 54 | +import net.curisit.securis.services.helpers.UserHelper; |
|---|
| 54 | 55 | import net.curisit.securis.utils.EmailManager; |
|---|
| 55 | 56 | import net.curisit.securis.utils.JsonUtils; |
|---|
| 56 | 57 | import net.curisit.securis.utils.Params; |
|---|
| .. | .. |
|---|
| 75 | 76 | private static final Logger LOG = LogManager.getLogger(LicenseResource.class); |
|---|
| 76 | 77 | |
|---|
| 77 | 78 | @Inject |
|---|
| 78 | | - TokenHelper tokenHelper; |
|---|
| 79 | + private TokenHelper tokenHelper; |
|---|
| 79 | 80 | |
|---|
| 80 | 81 | @Inject |
|---|
| 81 | | - EmailManager emailManager; |
|---|
| 82 | + private EmailManager emailManager; |
|---|
| 82 | 83 | |
|---|
| 83 | 84 | @Inject |
|---|
| 84 | | - Provider<EntityManager> emProvider; |
|---|
| 85 | + private UserHelper userHelper; |
|---|
| 85 | 86 | |
|---|
| 86 | 87 | @Inject |
|---|
| 87 | | - LicenseGenerator licenseGenerator; |
|---|
| 88 | + private LicenseHelper licenseHelper; |
|---|
| 89 | + |
|---|
| 90 | + @Inject |
|---|
| 91 | + private Provider<EntityManager> emProvider; |
|---|
| 92 | + |
|---|
| 93 | + @Inject |
|---|
| 94 | + private LicenseGenerator licenseGenerator; |
|---|
| 88 | 95 | |
|---|
| 89 | 96 | /** |
|---|
| 90 | 97 | * |
|---|
| .. | .. |
|---|
| 162 | 169 | LOG.error("License with id {} is not active, so It can not downloaded", licId, bsc.getUserPrincipal()); |
|---|
| 163 | 170 | throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "License is not active, so It can not be downloaded"); |
|---|
| 164 | 171 | } |
|---|
| 165 | | - em.persist(createLicenseHistoryAction(lic, getUser(bsc, em), LicenseHistory.Actions.DOWNLOAD)); |
|---|
| 172 | + em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.DOWNLOAD)); |
|---|
| 166 | 173 | return Response |
|---|
| 167 | 174 | .ok(lic.getLicenseData()) |
|---|
| 168 | 175 | .header("Content-Disposition", |
|---|
| .. | .. |
|---|
| 210 | 217 | lic.setModificationTimestamp(new Date()); |
|---|
| 211 | 218 | |
|---|
| 212 | 219 | 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)); |
|---|
| 215 | 222 | return Response.ok(lic).build(); |
|---|
| 216 | 223 | } |
|---|
| 217 | 224 | |
|---|
| .. | .. |
|---|
| 243 | 250 | throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "There is no license file available"); |
|---|
| 244 | 251 | } |
|---|
| 245 | 252 | |
|---|
| 246 | | - User user = getUser(bsc.getUserPrincipal().getName(), em); |
|---|
| 253 | + User user = userHelper.getUser(bsc.getUserPrincipal().getName(), em); |
|---|
| 247 | 254 | try { |
|---|
| 248 | 255 | String subject = MessageFormat.format(Params.get(Params.KEYS.EMAIL_LIC_DEFAULT_SUBJECT), lic.getPack().getAppName()); |
|---|
| 249 | 256 | String email_tpl = IOUtils.toString(this.getClass().getResourceAsStream("/lic_email_template.en")); |
|---|
| 250 | 257 | String body = MessageFormat.format(email_tpl, lic.getFullName(), app.getName()); |
|---|
| 251 | | - licFile = createTemporaryLicenseFile(lic, app.getLicenseFilename()); |
|---|
| 258 | + licFile = licenseHelper.createTemporaryLicenseFile(lic, app.getLicenseFilename()); |
|---|
| 252 | 259 | |
|---|
| 253 | 260 | emailManager.sendEmail(subject, body, lic.getEmail(), addCC ? user.getEmail() : null, licFile); |
|---|
| 254 | 261 | } catch (IOException e) { |
|---|
| .. | .. |
|---|
| 262 | 269 | |
|---|
| 263 | 270 | lic.setModificationTimestamp(new Date()); |
|---|
| 264 | 271 | 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())); |
|---|
| 266 | 273 | return Response.ok(lic).build(); |
|---|
| 267 | 274 | } |
|---|
| 268 | 275 | |
|---|
| .. | .. |
|---|
| 301 | 308 | + " can not be canceled without a reason"); |
|---|
| 302 | 309 | } |
|---|
| 303 | 310 | |
|---|
| 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); |
|---|
| 310 | 312 | 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 | | - |
|---|
| 321 | 313 | } |
|---|
| 322 | 314 | |
|---|
| 323 | 315 | @POST |
|---|
| .. | .. |
|---|
| 349 | 341 | } |
|---|
| 350 | 342 | } |
|---|
| 351 | 343 | |
|---|
| 352 | | - User createdBy = getUser(bsc.getUserPrincipal().getName(), em); |
|---|
| 344 | + User createdBy = userHelper.getUser(bsc.getUserPrincipal().getName(), em); |
|---|
| 353 | 345 | |
|---|
| 354 | 346 | if (lic.getRequestData() != null) { |
|---|
| 355 | 347 | License existingLicense = License.findLicenseByRequestData(lic.getRequestData(), em); |
|---|
| .. | .. |
|---|
| 380 | 372 | lic.setCreationTimestamp(new Date()); |
|---|
| 381 | 373 | lic.setModificationTimestamp(lic.getCreationTimestamp()); |
|---|
| 382 | 374 | em.persist(lic); |
|---|
| 383 | | - em.persist(createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE)); |
|---|
| 375 | + em.persist(licenseHelper.createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE)); |
|---|
| 384 | 376 | 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")); |
|---|
| 386 | 378 | } |
|---|
| 387 | 379 | |
|---|
| 388 | 380 | return Response.ok(lic).build(); |
|---|
| .. | .. |
|---|
| 486 | 478 | |
|---|
| 487 | 479 | currentLicense.setModificationTimestamp(new Date()); |
|---|
| 488 | 480 | 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)); |
|---|
| 490 | 482 | |
|---|
| 491 | 483 | return Response.ok(currentLicense).build(); |
|---|
| 492 | 484 | } |
|---|
| .. | .. |
|---|
| 547 | 539 | } |
|---|
| 548 | 540 | BlockedRequest blockedReq = new BlockedRequest(); |
|---|
| 549 | 541 | blockedReq.setCreationTimestamp(new Date()); |
|---|
| 550 | | - blockedReq.setBlockedBy(getUser(bsc, em)); |
|---|
| 542 | + blockedReq.setBlockedBy(userHelper.getUser(bsc, em)); |
|---|
| 551 | 543 | blockedReq.setRequestData(lic.getRequestData()); |
|---|
| 552 | 544 | |
|---|
| 553 | 545 | em.persist(blockedReq); |
|---|
| 554 | 546 | |
|---|
| 555 | | - em.persist(createLicenseHistoryAction(lic, getUser(bsc, em), LicenseHistory.Actions.BLOCK)); |
|---|
| 547 | + em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.BLOCK)); |
|---|
| 556 | 548 | return Response.ok(Utils.createMap("success", true, "id", licId)).build(); |
|---|
| 557 | 549 | } |
|---|
| 558 | 550 | |
|---|
| .. | .. |
|---|
| 571 | 563 | if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) { |
|---|
| 572 | 564 | BlockedRequest blockedReq = em.find(BlockedRequest.class, lic.getReqDataHash()); |
|---|
| 573 | 565 | 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)); |
|---|
| 575 | 567 | } else { |
|---|
| 576 | 568 | LOG.info("Request data for license {} is NOT blocked", licId); |
|---|
| 577 | 569 | } |
|---|
| .. | .. |
|---|
| 595 | 587 | throw new SeCurisServiceException(Status.UNAUTHORIZED.getStatusCode(), "Unathorized access to license data"); |
|---|
| 596 | 588 | } |
|---|
| 597 | 589 | 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); |
|---|
| 635 | 590 | } |
|---|
| 636 | 591 | |
|---|
| 637 | 592 | public static void main(String[] args) throws IOException { |
|---|