| .. | .. |
|---|
| 9 | 9 | import javax.persistence.EntityManager; |
|---|
| 10 | 10 | import javax.ws.rs.Consumes; |
|---|
| 11 | 11 | import javax.ws.rs.GET; |
|---|
| 12 | +import javax.ws.rs.HeaderParam; |
|---|
| 12 | 13 | import javax.ws.rs.POST; |
|---|
| 13 | 14 | import javax.ws.rs.Path; |
|---|
| 14 | 15 | import javax.ws.rs.Produces; |
|---|
| .. | .. |
|---|
| 120 | 121 | MediaType.APPLICATION_JSON |
|---|
| 121 | 122 | }) |
|---|
| 122 | 123 | @Transactional |
|---|
| 123 | | - public Response createFromRequest(RequestBean request, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException, |
|---|
| 124 | | - SeCurisException { |
|---|
| 124 | + public Response createFromRequest(RequestBean request, @HeaderParam(LicenseManager.HEADER_LICENSE_NAME_OR_REFERENCE) String nameOrReference, |
|---|
| 125 | + @HeaderParam(LicenseManager.HEADER_LICENSE_EMAIL) String userEmail) throws IOException, SeCurisServiceException, SeCurisException { |
|---|
| 125 | 126 | LOG.info("Request to get license: {}", request); |
|---|
| 126 | | - |
|---|
| 127 | | - SignedLicenseBean lic = createLicense(request, emProvider.get(), false); |
|---|
| 127 | + SignedLicenseBean lic = createLicense(request, emProvider.get(), false, nameOrReference, userEmail); |
|---|
| 128 | 128 | |
|---|
| 129 | 129 | return Response.ok(lic).build(); |
|---|
| 130 | 130 | } |
|---|
| .. | .. |
|---|
| 148 | 148 | }) |
|---|
| 149 | 149 | @Transactional |
|---|
| 150 | 150 | @SuppressWarnings("unchecked") |
|---|
| 151 | | - public Response createFromRequestFile(MultipartFormDataInput mpfdi, @Context BasicSecurityContext bsc) throws IOException, |
|---|
| 152 | | - SeCurisServiceException, SeCurisException { |
|---|
| 151 | + public Response createFromRequestFile(MultipartFormDataInput mpfdi, |
|---|
| 152 | + @HeaderParam(LicenseManager.HEADER_LICENSE_NAME_OR_REFERENCE) String nameOrReference, |
|---|
| 153 | + @HeaderParam(LicenseManager.HEADER_LICENSE_EMAIL) String userEmail) throws IOException, SeCurisServiceException, SeCurisException { |
|---|
| 153 | 154 | RequestBean req = new RequestBean(); |
|---|
| 154 | 155 | req.setPackCode(mpfdi.getFormDataPart("packCode", String.class, null)); |
|---|
| 155 | 156 | req.setLicenseTypeCode(mpfdi.getFormDataPart("licenseTypeCode", String.class, null)); |
|---|
| .. | .. |
|---|
| 159 | 160 | req.setMacAddresses(mpfdi.getFormDataPart("macAddresses", List.class, null)); |
|---|
| 160 | 161 | req.setOsName(mpfdi.getFormDataPart("osName", String.class, null)); |
|---|
| 161 | 162 | |
|---|
| 162 | | - return createFromRequest(req, bsc); |
|---|
| 163 | + return createFromRequest(req, nameOrReference, userEmail); |
|---|
| 163 | 164 | } |
|---|
| 164 | 165 | |
|---|
| 165 | 166 | /** |
|---|
| .. | .. |
|---|
| 194 | 195 | throw new SeCurisServiceException(ErrorCodes.LICENSE_NOT_READY_FOR_RENEW, "Only licenses with status 'Active' can be renew"); |
|---|
| 195 | 196 | } |
|---|
| 196 | 197 | |
|---|
| 197 | | - SignedLicenseBean signedLic = createLicense(previousLic, em, true); |
|---|
| 198 | + SignedLicenseBean signedLic = renewLicense(previousLic, em); |
|---|
| 198 | 199 | |
|---|
| 199 | 200 | return Response.ok(signedLic).build(); |
|---|
| 200 | 201 | } |
|---|
| .. | .. |
|---|
| 273 | 274 | throw new SeCurisServiceException(ErrorCodes.LICENSE_NOT_READY_FOR_RENEW, "The license is still valid, not ready for renew"); |
|---|
| 274 | 275 | } |
|---|
| 275 | 276 | |
|---|
| 276 | | - return createFromRequest(lic, bsc); |
|---|
| 277 | + return renewFromPreviousLicense(lic, bsc); |
|---|
| 278 | + } |
|---|
| 279 | + |
|---|
| 280 | + private SignedLicenseBean renewLicense(RequestBean req, EntityManager em) throws SeCurisServiceException { |
|---|
| 281 | + return createLicense(req, em, true, null, null); |
|---|
| 277 | 282 | } |
|---|
| 278 | 283 | |
|---|
| 279 | 284 | /** |
|---|
| .. | .. |
|---|
| 286 | 291 | * @return |
|---|
| 287 | 292 | * @throws SeCurisServiceException |
|---|
| 288 | 293 | */ |
|---|
| 289 | | - private SignedLicenseBean createLicense(RequestBean req, EntityManager em, boolean renew) throws SeCurisServiceException { |
|---|
| 294 | + private SignedLicenseBean createLicense(RequestBean req, EntityManager em, boolean renew, String nameOrReference, String email) |
|---|
| 295 | + throws SeCurisServiceException { |
|---|
| 290 | 296 | LicenseBean previousLicenseBean = null; |
|---|
| 291 | 297 | License lic = null; |
|---|
| 292 | 298 | if (renew) { |
|---|
| .. | .. |
|---|
| 295 | 301 | if (lic.getStatus() != LicenseStatus.ACTIVE && lic.getStatus() != LicenseStatus.PRE_ACTIVE) { |
|---|
| 296 | 302 | throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The current license has been cancelled"); |
|---|
| 297 | 303 | } |
|---|
| 298 | | - } else { |
|---|
| 299 | | - lic = new License(); |
|---|
| 300 | 304 | } |
|---|
| 301 | 305 | |
|---|
| 302 | 306 | if (!renew) { |
|---|
| 303 | | - License existingLicense = License.findLicenseByRequestData(lic.getRequestData(), em); |
|---|
| 304 | | - if (existingLicense != null) { |
|---|
| 305 | | - throw new SeCurisServiceException(ErrorCodes.DUPLICATED_REQUEST_DATA, "There is already an active license for current request data"); |
|---|
| 307 | + try { |
|---|
| 308 | + lic = License.findValidLicenseByRequestData(JsonUtils.toJSON(req), em); |
|---|
| 309 | + } catch (SeCurisException e1) { |
|---|
| 310 | + throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Request sent is not valid"); |
|---|
| 311 | + } |
|---|
| 312 | + if (lic != null) { |
|---|
| 313 | + try { |
|---|
| 314 | + if (lic.getStatus() == LicenseStatus.ACTIVE || lic.getStatus() == LicenseStatus.PRE_ACTIVE) { |
|---|
| 315 | + return JsonUtils.json2object(lic.getLicenseData(), SignedLicenseBean.class); |
|---|
| 316 | + } |
|---|
| 317 | + } catch (SeCurisException e) { |
|---|
| 318 | + throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Error trying to get the license bean from license code: " |
|---|
| 319 | + + lic.getCode()); |
|---|
| 320 | + } |
|---|
| 321 | + } else { |
|---|
| 322 | + lic = new License(); |
|---|
| 306 | 323 | } |
|---|
| 307 | 324 | } |
|---|
| 308 | 325 | Pack pack = em.createNamedQuery("pack-by-code", Pack.class).setParameter("code", req.getPackCode()).getSingleResult(); |
|---|
| .. | .. |
|---|
| 310 | 327 | if (!renew && pack.getNumAvailables() <= 0) { |
|---|
| 311 | 328 | throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "The current pack has no licenses availables"); |
|---|
| 312 | 329 | } |
|---|
| 330 | + if (!renew && lic.getStatus() == LicenseStatus.REQUESTED && !pack.isLicensePreactivation()) { |
|---|
| 331 | + throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "Current pack doesn't allow license preactivation"); |
|---|
| 332 | + } |
|---|
| 333 | + |
|---|
| 313 | 334 | SignedLicenseBean signedLicense; |
|---|
| 314 | 335 | try { |
|---|
| 315 | 336 | String licCode; |
|---|
| 316 | | - if (renew) { |
|---|
| 317 | | - licCode = previousLicenseBean.getLicenseCode(); |
|---|
| 337 | + if (renew || lic.getStatus() == LicenseStatus.REQUESTED) { |
|---|
| 338 | + licCode = lic.getCode(); |
|---|
| 318 | 339 | } else { |
|---|
| 319 | 340 | licCode = LicUtils.getLicenseCode(pack.getCode(), licenseHelper.getNextCodeSuffix(pack.getId(), em)); |
|---|
| 320 | 341 | } |
|---|
| .. | .. |
|---|
| 340 | 361 | lic.setModificationTimestamp(new Date()); |
|---|
| 341 | 362 | lic.setExpirationDate(signedLicense.getExpirationDate()); |
|---|
| 342 | 363 | User user = em.find(User.class, CLIENT_USERNAME); |
|---|
| 343 | | - if (!renew) { |
|---|
| 344 | | - |
|---|
| 364 | + if (!renew && lic.getStatus() != LicenseStatus.REQUESTED) { |
|---|
| 345 | 365 | lic.setPack(pack); |
|---|
| 346 | 366 | lic.setCreatedBy(user); |
|---|
| 347 | 367 | lic.setCreationTimestamp(new Date()); |
|---|
| 348 | | - lic.setStatus(LicenseStatus.PRE_ACTIVE); |
|---|
| 368 | + if (pack.isLicensePreactivation()) { |
|---|
| 369 | + lic.setStatus(LicenseStatus.PRE_ACTIVE); |
|---|
| 370 | + } else { |
|---|
| 371 | + lic.setStatus(LicenseStatus.REQUESTED); |
|---|
| 372 | + } |
|---|
| 349 | 373 | lic.setCode(signedLicense.getLicenseCode()); |
|---|
| 350 | 374 | lic.setCodeSuffix(LicUtils.getLicenseCodeSuffix(signedLicense.getLicenseCode())); |
|---|
| 375 | + lic.setEmail(email); |
|---|
| 376 | + lic.setFullName(nameOrReference); |
|---|
| 351 | 377 | em.persist(lic); |
|---|
| 352 | 378 | em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.CREATE)); |
|---|
| 353 | | - if (lic.getStatus() == LicenseStatus.ACTIVE) { |
|---|
| 379 | + if (pack.isLicensePreactivation()) { |
|---|
| 354 | 380 | em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.PRE_ACTIVATE, "Pre-activated on creation")); |
|---|
| 381 | + } else { |
|---|
| 382 | + LOG.warn("License ({}) created, but the pack doesn't allow preactivation", lic.getCode()); |
|---|
| 383 | + throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "Current pack doesn't allow license preactivation"); |
|---|
| 355 | 384 | } |
|---|
| 356 | 385 | } else { |
|---|
| 357 | | - lic.setStatus(LicenseStatus.ACTIVE); |
|---|
| 386 | + lic.setStatus(renew ? LicenseStatus.ACTIVE : LicenseStatus.PRE_ACTIVE); |
|---|
| 358 | 387 | em.merge(lic); |
|---|
| 359 | | - em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.RENEW)); |
|---|
| 388 | + if (renew) { |
|---|
| 389 | + em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.RENEW)); |
|---|
| 390 | + } else { |
|---|
| 391 | + em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.PRE_ACTIVATE, "Pre-activated after request")); |
|---|
| 392 | + } |
|---|
| 360 | 393 | } |
|---|
| 361 | 394 | |
|---|
| 362 | 395 | return signedLicense; |
|---|