From ef499980be449d15421591dad77030c16198e62d Mon Sep 17 00:00:00 2001 From: eparovyshnaya Date: Tue, 27 Oct 2020 08:56:29 +0300 Subject: [PATCH] Bug 567032 issue floating license configuration wizard Rework existing LicenseOperatorService to extend it with floating license issuing. Signed-off-by: eparovyshnaya --- .../META-INF/MANIFEST.MF | 13 +- .../META-INF/MANIFEST.MF | 3 +- .../licenses/core/IssuePersonalLicense.java | 188 ++++++++++++++++ .../core/LicenseGrantFromRequest.java | 47 ++++ .../core/LicenseOperatorServiceImpl.java | 209 +----------------- .../core/PersonalLicensePackFromRequest.java | 59 +++++ 6 files changed, 310 insertions(+), 209 deletions(-) create mode 100644 bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/IssuePersonalLicense.java create mode 100644 bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/LicenseGrantFromRequest.java create mode 100644 bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/PersonalLicensePackFromRequest.java diff --git a/bundles/org.eclipse.passage.lic.floating.model/META-INF/MANIFEST.MF b/bundles/org.eclipse.passage.lic.floating.model/META-INF/MANIFEST.MF index 6b3e3a298..573337cdc 100644 --- a/bundles/org.eclipse.passage.lic.floating.model/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.passage.lic.floating.model/META-INF/MANIFEST.MF @@ -12,15 +12,10 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="0.0.0";x-installation:= org.eclipse.emf.ecore;bundle-version="0.0.0";visibility:=reexport, org.eclipse.passage.lic.emf.meta;bundle-version="0.6.0", org.eclipse.passage.lic.floating;bundle-version="0.0.0";visibility:=reexport -Export-Package: org.eclipse.passage.lic.floating.model.api; - x-friends:="org.eclipse.passage.lic.floating.edit", +Export-Package: org.eclipse.passage.lic.floating.model.api;x-friends:="org.eclipse.passage.lic.floating.edit,org.eclipse.passage.loc.dashboard.ui,org.eclipse.passage.loc.api", org.eclipse.passage.lic.floating.model.impl;x-internal:=true, - org.eclipse.passage.lic.floating.model.meta; - x-friends:="org.eclipse.passage.lic.floating.e4.ui, - org.eclipse.passage.lic.floating.edit", - org.eclipse.passage.lic.floating.model.util; - x-friends:="org.eclipse.passage.lic.floating.edit", - org.eclipse.passage.lic.internal.floating.model; - x-friends:="org.eclipse.passage.loc.dashboard.ui" + org.eclipse.passage.lic.floating.model.meta;x-friends:="org.eclipse.passage.lic.floating.e4.ui,org.eclipse.passage.lic.floating.edit", + org.eclipse.passage.lic.floating.model.util;x-friends:="org.eclipse.passage.lic.floating.edit", + org.eclipse.passage.lic.internal.floating.model;x-friends:="org.eclipse.passage.loc.dashboard.ui" Bundle-ClassPath: . Bundle-ActivationPolicy: lazy diff --git a/bundles/org.eclipse.passage.loc.api/META-INF/MANIFEST.MF b/bundles/org.eclipse.passage.loc.api/META-INF/MANIFEST.MF index 55dacb7af..d4de415c6 100644 --- a/bundles/org.eclipse.passage.loc.api/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.passage.loc.api/META-INF/MANIFEST.MF @@ -26,4 +26,5 @@ Require-Bundle: org.eclipse.e4.core.services;bundle-version="0.0.0", org.eclipse.passage.lic.licenses;bundle-version="1.0.0";visibility:=reexport, org.eclipse.passage.lic.products;bundle-version="1.0.0";visibility:=reexport, org.eclipse.passage.lic.users;bundle-version="1.0.0";visibility:=reexport, - org.eclipse.passage.lic.users.model;bundle-version="1.0.0" + org.eclipse.passage.lic.users.model;bundle-version="1.0.0", + org.eclipse.passage.lic.floating.model;bundle-version="0.1.0" diff --git a/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/IssuePersonalLicense.java b/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/IssuePersonalLicense.java new file mode 100644 index 000000000..3d3bc5314 --- /dev/null +++ b/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/IssuePersonalLicense.java @@ -0,0 +1,188 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.loc.internal.licenses.core; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Path; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.UUID; +import java.util.function.Supplier; + +import org.eclipse.emf.common.command.CommandStack; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EReference; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.emf.edit.command.AddCommand; +import org.eclipse.emf.edit.domain.EditingDomain; +import org.eclipse.emf.edit.domain.IEditingDomainProvider; +import org.eclipse.passage.lic.emf.ecore.LicensingEcore; +import org.eclipse.passage.lic.internal.api.LicensedProduct; +import org.eclipse.passage.lic.internal.api.ServiceInvocationResult; +import org.eclipse.passage.lic.internal.api.diagnostic.Trouble; +import org.eclipse.passage.lic.internal.api.io.StreamCodec; +import org.eclipse.passage.lic.internal.base.BaseLicensedProduct; +import org.eclipse.passage.lic.internal.base.BaseServiceInvocationResult; +import org.eclipse.passage.lic.internal.base.io.FileNameFromLicensedProduct; +import org.eclipse.passage.lic.internal.base.io.PassageFileExtension; +import org.eclipse.passage.lic.internal.base.io.UserHomeProductResidence; +import org.eclipse.passage.lic.internal.licenses.model.AssignGrantIdentifiers; +import org.eclipse.passage.lic.licenses.model.api.LicensePack; +import org.eclipse.passage.lic.licenses.model.meta.LicensesPackage; +import org.eclipse.passage.lic.products.ProductVersionDescriptor; +import org.eclipse.passage.lic.users.UserDescriptor; +import org.eclipse.passage.lic.users.model.api.User; +import org.eclipse.passage.lic.users.model.api.UserLicense; +import org.eclipse.passage.lic.users.model.meta.UsersFactory; +import org.eclipse.passage.lic.users.model.meta.UsersPackage; +import org.eclipse.passage.loc.internal.api.CodecSupplier; +import org.eclipse.passage.loc.internal.api.IssuedLicense; +import org.eclipse.passage.loc.internal.api.OperatorEvents; +import org.eclipse.passage.loc.internal.api.OperatorLicenseEvents; +import org.eclipse.passage.loc.internal.api.OperatorProductService; +import org.eclipse.passage.loc.internal.api.PersonalLicenseRequest; +import org.eclipse.passage.loc.internal.licenses.core.i18n.LicensesCoreMessages; +import org.eclipse.passage.loc.internal.licenses.trouble.code.LicenseIssuingFailed; +import org.eclipse.passage.loc.internal.licenses.trouble.code.LicenseIssuingIsPartial; +import org.eclipse.passage.loc.internal.licenses.trouble.code.LicenseValidationFailed; +import org.eclipse.passage.loc.internal.products.ProductRegistry; +import org.eclipse.passage.loc.internal.users.UserRegistry; +import org.eclipse.passage.loc.internal.users.UserRegistryEvents; +import org.osgi.service.event.EventAdmin; + +@SuppressWarnings("restriction") +final class IssuePersonalLicense { + + private final UserRegistry users; + private final ProductRegistry products; + private final OperatorProductService operator; + private final EventAdmin events; + + IssuePersonalLicense(UserRegistry users, ProductRegistry products, OperatorProductService operator, + EventAdmin events) { + this.users = users; + this.products = products; + this.operator = operator; + this.events = events; + } + + ServiceInvocationResult issue(PersonalLicenseRequest request, Supplier template) { + LicensePack license = EcoreUtil.copy(template.get()); + String errors = LicensingEcore.extractValidationError(license); + if (errors != null) { + return new BaseServiceInvocationResult<>(new Trouble(new LicenseValidationFailed(), errors)); + } + LicensedProduct product = new BaseLicensedProduct(license.getProductIdentifier(), license.getProductVersion()); + Date issueDate = new Date(); + license.setIdentifier(UUID.randomUUID().toString()); + license.setIssueDate(issueDate); + new AssignGrantIdentifiers().accept(license); + String userIdentifier = license.getUserIdentifier(); + UserDescriptor userDescriptor = users.getUser(userIdentifier); + Map attachments = new HashMap(); + UserLicense userLicense = null; + if (userDescriptor instanceof User) { + User user = (User) userDescriptor; + String conditionType = userDescriptor.getPreferredConditionType(); + String expression = userDescriptor.getPreferredConditionExpression(); + userLicense = UsersFactory.eINSTANCE.createUserLicense(); + userLicense.setPackIdentifier(license.getIdentifier()); + userLicense.setIssueDate(issueDate); + userLicense.setPlanIdentifier(request.plan()); + userLicense.setValidFrom(request.validFrom()); + userLicense.setValidUntil(request.validUntil()); + userLicense.setConditionExpression(expression); + userLicense.setConditionType(conditionType); + userLicense.setProductIdentifier(product.identifier()); + userLicense.setProductVersion(product.version()); + if (users instanceof IEditingDomainProvider) { + IEditingDomainProvider edp = (IEditingDomainProvider) users; + EditingDomain editingDomain = edp.getEditingDomain(); + EReference structured = UsersPackage.eINSTANCE.getUser_UserLicenses(); + CommandStack stack = editingDomain.getCommandStack(); + stack.execute(AddCommand.create(editingDomain, user, structured, userLicense)); + } else { + user.getUserLicenses().add(userLicense); + } + events.postEvent(OperatorEvents.create(UserRegistryEvents.USER_LICENSE_CREATE, userLicense)); + try { + // FIXME: define parameters + user.eResource().save(null); + attachments.put(userLicense.eClass().getName(), userLicense); + } catch (IOException e) { + return new BaseServiceInvocationResult<>(new Trouble(new LicenseIssuingFailed(), + LicensesCoreMessages.LicenseOperatorServiceImpl_error_io, e)); + } + } + Path path = new UserHomeProductResidence(product).get(); + Path decrypted = path.resolve(license.getIdentifier() + new PassageFileExtension.LicenseDecrypted().get()); + URI uri = URI.createFileURI(decrypted.toString()); + ResourceSet resourceSet = new ResourceSetImpl(); + Resource resource = resourceSet.createResource(uri); + resource.getContents().add(license); + try { + resource.save(null); + events.postEvent(OperatorLicenseEvents.decodedIssued(decrypted.toString())); + } catch (IOException e) { + return new BaseServiceInvocationResult<>(new Trouble(new LicenseIssuingFailed(), + LicensesCoreMessages.LicenseOperatorServiceImpl_failed_to_save_decoded, e)); + } + + Optional codec = codec(product); + if (!codec.isPresent()) { + return new BaseServiceInvocationResult<>(new Trouble(new LicenseIssuingIsPartial(), // + String.format(LicensesCoreMessages.LicenseOperatorServiceImpl_w_no_encoding, decrypted))); + } + + Path privateKeyPath = path.resolve(// + new FileNameFromLicensedProduct(product, new PassageFileExtension.PrivateKey()).get()); + File privateProductToken = privateKeyPath.toFile(); + if (!privateProductToken.exists()) { + String pattern = LicensesCoreMessages.LicenseOperatorServiceImpl_private_key_not_found; + String message = String.format(pattern, privateProductToken.getAbsolutePath()); + return new BaseServiceInvocationResult<>(new Trouble(new LicenseIssuingFailed(), message)); + } + + Path encrypted = path.resolve(license.getIdentifier() + new PassageFileExtension.LicenseEncrypted().get()); + File licenseEncoded = encrypted.toFile(); + try (FileInputStream licenseInput = new FileInputStream(decrypted.toFile()); + FileOutputStream licenseOutput = new FileOutputStream(licenseEncoded); + FileInputStream keyStream = new FileInputStream(privateProductToken)) { + String username = product.identifier(); + ProductVersionDescriptor pvd = products.getProductVersion(product.identifier(), product.version()); + String password = operator.createPassword(pvd); + codec.get().encode(licenseInput, licenseOutput, keyStream, username, password); + events.postEvent(OperatorLicenseEvents.encodedIssued(encrypted.toString())); + attachments.put(LicensesPackage.eNAME, encrypted); + return new BaseServiceInvocationResult<>(new BaseIssuedLicense(userLicense, encrypted, decrypted)); + } catch (Exception e) { + return new BaseServiceInvocationResult<>(// + new Trouble(// + new LicenseIssuingFailed(), // + LicensesCoreMessages.LicenseOperatorServiceImpl_export_error, e)); + } + } + + private Optional codec(LicensedProduct product) { + return new CodecSupplier(product).get(); + } + +} diff --git a/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/LicenseGrantFromRequest.java b/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/LicenseGrantFromRequest.java new file mode 100644 index 000000000..aae13e2ce --- /dev/null +++ b/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/LicenseGrantFromRequest.java @@ -0,0 +1,47 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.loc.internal.licenses.core; + +import java.util.function.Supplier; + +import org.eclipse.passage.lic.licenses.LicensePlanFeatureDescriptor; +import org.eclipse.passage.lic.licenses.model.api.LicenseGrant; +import org.eclipse.passage.lic.licenses.model.meta.LicensesFactory; +import org.eclipse.passage.loc.internal.api.PersonalLicenseRequest; + +final class LicenseGrantFromRequest implements Supplier { + + private final LicensePlanFeatureDescriptor feature; + private final PersonalLicenseRequest request; + + public LicenseGrantFromRequest(LicensePlanFeatureDescriptor feature, PersonalLicenseRequest request) { + this.feature = feature; + this.request = request; + } + + @Override + public LicenseGrant get() { + LicensesFactory licenseFactory = LicensesFactory.eINSTANCE; + LicenseGrant grant = licenseFactory.createLicenseGrant(); + grant.setFeatureIdentifier(feature.getFeatureIdentifier()); + grant.setMatchVersion(feature.getMatchVersion()); + grant.setMatchRule(feature.getMatchRule()); + grant.setCapacity(1); + grant.setConditionExpression(request.conditionExpression()); + grant.setConditionType(request.conditionType()); + grant.setValidFrom(request.validFrom()); + grant.setValidUntil(request.validUntil()); + return grant; + } + +} diff --git a/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/LicenseOperatorServiceImpl.java b/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/LicenseOperatorServiceImpl.java index d22150b6a..137f6326b 100644 --- a/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/LicenseOperatorServiceImpl.java +++ b/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/LicenseOperatorServiceImpl.java @@ -12,69 +12,20 @@ *******************************************************************************/ package org.eclipse.passage.loc.internal.licenses.core; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.nio.file.Path; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; import java.util.Objects; -import java.util.Optional; -import java.util.UUID; +import java.util.function.Supplier; -import org.eclipse.emf.common.command.CommandStack; -import org.eclipse.emf.common.util.EList; -import org.eclipse.emf.common.util.URI; -import org.eclipse.emf.ecore.EReference; -import org.eclipse.emf.ecore.resource.Resource; -import org.eclipse.emf.ecore.resource.ResourceSet; -import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; -import org.eclipse.emf.ecore.util.EcoreUtil; -import org.eclipse.emf.edit.command.AddCommand; -import org.eclipse.emf.edit.domain.EditingDomain; -import org.eclipse.emf.edit.domain.IEditingDomainProvider; import org.eclipse.osgi.service.environment.EnvironmentInfo; -import org.eclipse.passage.lic.emf.ecore.LicensingEcore; -import org.eclipse.passage.lic.internal.api.LicensedProduct; import org.eclipse.passage.lic.internal.api.ServiceInvocationResult; -import org.eclipse.passage.lic.internal.api.diagnostic.Trouble; -import org.eclipse.passage.lic.internal.api.io.StreamCodec; -import org.eclipse.passage.lic.internal.base.BaseLicensedProduct; -import org.eclipse.passage.lic.internal.base.BaseServiceInvocationResult; -import org.eclipse.passage.lic.internal.base.io.FileNameFromLicensedProduct; -import org.eclipse.passage.lic.internal.base.io.PassageFileExtension; -import org.eclipse.passage.lic.internal.base.io.UserHomeProductResidence; -import org.eclipse.passage.lic.internal.licenses.model.AssignGrantIdentifiers; import org.eclipse.passage.lic.licenses.LicensePackDescriptor; -import org.eclipse.passage.lic.licenses.LicensePlanDescriptor; -import org.eclipse.passage.lic.licenses.LicensePlanFeatureDescriptor; -import org.eclipse.passage.lic.licenses.model.api.LicenseGrant; import org.eclipse.passage.lic.licenses.model.api.LicensePack; -import org.eclipse.passage.lic.licenses.model.meta.LicensesFactory; -import org.eclipse.passage.lic.licenses.model.meta.LicensesPackage; -import org.eclipse.passage.lic.products.ProductVersionDescriptor; -import org.eclipse.passage.lic.users.UserDescriptor; -import org.eclipse.passage.lic.users.model.api.User; -import org.eclipse.passage.lic.users.model.api.UserLicense; -import org.eclipse.passage.lic.users.model.meta.UsersFactory; -import org.eclipse.passage.lic.users.model.meta.UsersPackage; -import org.eclipse.passage.loc.internal.api.CodecSupplier; import org.eclipse.passage.loc.internal.api.IssuedLicense; -import org.eclipse.passage.loc.internal.api.PersonalLicenseRequest; -import org.eclipse.passage.loc.internal.api.OperatorEvents; -import org.eclipse.passage.loc.internal.api.OperatorLicenseEvents; import org.eclipse.passage.loc.internal.api.OperatorLicenseService; import org.eclipse.passage.loc.internal.api.OperatorProductService; +import org.eclipse.passage.loc.internal.api.PersonalLicenseRequest; import org.eclipse.passage.loc.internal.licenses.LicenseRegistry; -import org.eclipse.passage.loc.internal.licenses.core.i18n.LicensesCoreMessages; -import org.eclipse.passage.loc.internal.licenses.trouble.code.LicenseIssuingFailed; -import org.eclipse.passage.loc.internal.licenses.trouble.code.LicenseIssuingIsPartial; -import org.eclipse.passage.loc.internal.licenses.trouble.code.LicenseValidationFailed; import org.eclipse.passage.loc.internal.products.ProductRegistry; import org.eclipse.passage.loc.internal.users.UserRegistry; -import org.eclipse.passage.loc.internal.users.UserRegistryEvents; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; import org.osgi.service.event.EventAdmin; @@ -159,158 +110,18 @@ public void unbindProductOperatorService(OperatorProductService productService) @Override public ServiceInvocationResult issueLicensePack(PersonalLicenseRequest request, LicensePackDescriptor template) { - Objects.requireNonNull("LicenseOperatorServiceImpl::issueLicensePack: cannot issue license for null request"); //$NON-NLS-1$ - LicensePack pack = null; - if (template instanceof LicensePack) { - pack = (LicensePack) template; - } else { - pack = createLicensePack(request); - } - LicensePack license = EcoreUtil.copy(pack); - String errors = LicensingEcore.extractValidationError(license); - if (errors != null) { - return new BaseServiceInvocationResult<>(new Trouble(new LicenseValidationFailed(), errors)); - } - LicensedProduct product = new BaseLicensedProduct(license.getProductIdentifier(), license.getProductVersion()); - - Date issueDate = new Date(); - license.setIdentifier(UUID.randomUUID().toString()); - license.setIssueDate(issueDate); - new AssignGrantIdentifiers().accept(license); - String userIdentifier = template.getUserIdentifier(); - UserDescriptor userDescriptor = userRegistry.getUser(userIdentifier); - Map attachments = new HashMap(); - UserLicense userLicense = null; - if (userDescriptor instanceof User) { - User user = (User) userDescriptor; - String conditionType = userDescriptor.getPreferredConditionType(); - String expression = userDescriptor.getPreferredConditionExpression(); - userLicense = UsersFactory.eINSTANCE.createUserLicense(); - userLicense.setPackIdentifier(license.getIdentifier()); - userLicense.setIssueDate(issueDate); - userLicense.setPlanIdentifier(request.plan()); - userLicense.setValidFrom(request.validFrom()); - userLicense.setValidUntil(request.validUntil()); - userLicense.setConditionExpression(expression); - userLicense.setConditionType(conditionType); - userLicense.setProductIdentifier(product.identifier()); - userLicense.setProductVersion(product.version()); - if (userRegistry instanceof IEditingDomainProvider) { - IEditingDomainProvider edp = (IEditingDomainProvider) userRegistry; - EditingDomain editingDomain = edp.getEditingDomain(); - EReference structured = UsersPackage.eINSTANCE.getUser_UserLicenses(); - CommandStack stack = editingDomain.getCommandStack(); - stack.execute(AddCommand.create(editingDomain, user, structured, userLicense)); - } else { - user.getUserLicenses().add(userLicense); - } - eventAdmin.postEvent(OperatorEvents.create(UserRegistryEvents.USER_LICENSE_CREATE, userLicense)); - try { - // FIXME: define parameters - user.eResource().save(null); - attachments.put(userLicense.eClass().getName(), userLicense); - } catch (IOException e) { - return new BaseServiceInvocationResult<>(new Trouble(new LicenseIssuingFailed(), - LicensesCoreMessages.LicenseOperatorServiceImpl_error_io, e)); - } - } - Path path = new UserHomeProductResidence(product).get(); - Path decrypted = path.resolve(license.getIdentifier() + new PassageFileExtension.LicenseDecrypted().get()); - URI uri = URI.createFileURI(decrypted.toString()); - ResourceSet resourceSet = new ResourceSetImpl(); - Resource resource = resourceSet.createResource(uri); - resource.getContents().add(license); - try { - resource.save(null); - eventAdmin.postEvent(OperatorLicenseEvents.decodedIssued(decrypted.toString())); - } catch (IOException e) { - return new BaseServiceInvocationResult<>(new Trouble(new LicenseIssuingFailed(), - LicensesCoreMessages.LicenseOperatorServiceImpl_failed_to_save_decoded, e)); - } - - Optional codec = codec(product); - if (!codec.isPresent()) { - return new BaseServiceInvocationResult<>(new Trouble(new LicenseIssuingIsPartial(), // - String.format(LicensesCoreMessages.LicenseOperatorServiceImpl_w_no_encoding, decrypted))); - } - - Path privateKeyPath = path.resolve(// - new FileNameFromLicensedProduct(product, new PassageFileExtension.PrivateKey()).get()); - File privateProductToken = privateKeyPath.toFile(); - if (!privateProductToken.exists()) { - String pattern = LicensesCoreMessages.LicenseOperatorServiceImpl_private_key_not_found; - String message = String.format(pattern, privateProductToken.getAbsolutePath()); - return new BaseServiceInvocationResult<>(new Trouble(new LicenseIssuingFailed(), message)); - } - - Path encrypted = path.resolve(license.getIdentifier() + new PassageFileExtension.LicenseEncrypted().get()); - File licenseEncoded = encrypted.toFile(); - try (FileInputStream licenseInput = new FileInputStream(decrypted.toFile()); - FileOutputStream licenseOutput = new FileOutputStream(licenseEncoded); - FileInputStream keyStream = new FileInputStream(privateProductToken)) { - String username = product.identifier(); - ProductVersionDescriptor pvd = productRegistry.getProductVersion(product.identifier(), product.version()); - String password = operatorProductService.createPassword(pvd); - codec.get().encode(licenseInput, licenseOutput, keyStream, username, password); - eventAdmin.postEvent(OperatorLicenseEvents.encodedIssued(encrypted.toString())); - attachments.put(LicensesPackage.eNAME, encrypted); - return new BaseServiceInvocationResult<>(new BaseIssuedLicense(userLicense, encrypted, decrypted)); - } catch (Exception e) { - return new BaseServiceInvocationResult<>(// - new Trouble(// - new LicenseIssuingFailed(), // - LicensesCoreMessages.LicenseOperatorServiceImpl_export_error, e)); - } - } - - private Optional codec(LicensedProduct product) { - return new CodecSupplier(product).get(); + Objects.requireNonNull(request, + "LicenseOperatorServiceImpl::issueLicensePack: cannot issue license for null request"); //$NON-NLS-1$ + Supplier pack = (template instanceof LicensePack) // + ? () -> LicensePack.class.cast(template)// + : () -> createLicensePack(request); + return new IssuePersonalLicense(userRegistry, productRegistry, operatorProductService, eventAdmin) + .issue(request, pack); } @Override public LicensePack createLicensePack(PersonalLicenseRequest request) { - LicensesFactory licenseFactory = LicensesFactory.eINSTANCE; - LicensePack licensePack = licenseFactory.createLicensePack(); - if (request == null) { - return licensePack; - } - licensePack.setRequestIdentifier(request.identifier()); - licensePack.setUserIdentifier(request.user()); - licensePack.setUserFullName(request.userFullName()); - licensePack.setProductIdentifier(request.productIdentifier()); - licensePack.setProductVersion(request.productVersion()); - String planIdentifier = request.plan(); - licensePack.setPlanIdentifier(planIdentifier); - LicensePlanDescriptor licensePlan = licenseRegistry.getLicensePlan(planIdentifier); - if (licensePlan == null) { - return licensePack; - } - Iterable features = licensePlan.getLicensePlanFeatures(); - Date from = request.validFrom(); - Date until = request.validUntil(); - String conditionType = request.conditionType(); - String expression = request.conditionExpression(); - EList grants = licensePack.getLicenseGrants(); - for (LicensePlanFeatureDescriptor planFeature : features) { - LicenseGrant grant = createLicenseGrant(planFeature, from, until, conditionType, expression); - grants.add(grant); - } - return licensePack; - } - - private LicenseGrant createLicenseGrant(LicensePlanFeatureDescriptor planFeature, Date from, Date until, - String conditionType, String expression) { - LicensesFactory licenseFactory = LicensesFactory.eINSTANCE; - LicenseGrant grant = licenseFactory.createLicenseGrant(); - grant.setFeatureIdentifier(planFeature.getFeatureIdentifier()); - grant.setMatchVersion(planFeature.getMatchVersion()); - grant.setMatchRule(planFeature.getMatchRule()); - grant.setCapacity(1); - grant.setConditionExpression(expression); - grant.setConditionType(conditionType); - grant.setValidFrom(from); - grant.setValidUntil(until); - return grant; + return new PersonalLicensePackFromRequest(request, licenseRegistry).get(); } } diff --git a/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/PersonalLicensePackFromRequest.java b/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/PersonalLicensePackFromRequest.java new file mode 100644 index 000000000..165092440 --- /dev/null +++ b/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/PersonalLicensePackFromRequest.java @@ -0,0 +1,59 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.loc.internal.licenses.core; + +import java.util.function.Supplier; + +import org.eclipse.emf.common.util.EList; +import org.eclipse.passage.lic.licenses.LicensePlanDescriptor; +import org.eclipse.passage.lic.licenses.LicensePlanFeatureDescriptor; +import org.eclipse.passage.lic.licenses.model.api.LicenseGrant; +import org.eclipse.passage.lic.licenses.model.api.LicensePack; +import org.eclipse.passage.lic.licenses.model.meta.LicensesFactory; +import org.eclipse.passage.loc.internal.api.PersonalLicenseRequest; +import org.eclipse.passage.loc.internal.licenses.LicenseRegistry; + +final class PersonalLicensePackFromRequest implements Supplier { + private final PersonalLicenseRequest request; + private LicenseRegistry registry; + + PersonalLicensePackFromRequest(PersonalLicenseRequest request, LicenseRegistry registry) { + this.request = request; + this.registry = registry; + } + + @Override + public LicensePack get() { + LicensesFactory licenseFactory = LicensesFactory.eINSTANCE; + LicensePack pack = licenseFactory.createLicensePack(); + pack.setRequestIdentifier(request.identifier()); + pack.setUserIdentifier(request.user()); + pack.setUserFullName(request.userFullName()); + pack.setProductIdentifier(request.productIdentifier()); + pack.setProductVersion(request.productVersion()); + String planIdentifier = request.plan(); + pack.setPlanIdentifier(planIdentifier); + LicensePlanDescriptor licensePlan = registry.getLicensePlan(planIdentifier); + if (licensePlan == null) { + return pack; // FIXME: ServiceInvocationResult should probably be used + } + Iterable features = licensePlan.getLicensePlanFeatures(); + EList grants = pack.getLicenseGrants(); + for (LicensePlanFeatureDescriptor planFeature : features) { + LicenseGrant grant = new LicenseGrantFromRequest(planFeature, request).get(); + grants.add(grant); + } + return pack; + } + +}