Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.labs64.netlicensing.examples.CallEveryAPIMethod Maven / Gradle / Ivy
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.labs64.netlicensing.examples;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import com.labs64.netlicensing.domain.Constants;
import com.labs64.netlicensing.domain.entity.Country;
import com.labs64.netlicensing.domain.entity.License;
import com.labs64.netlicensing.domain.entity.LicenseTemplate;
import com.labs64.netlicensing.domain.entity.Licensee;
import com.labs64.netlicensing.domain.entity.PaymentMethod;
import com.labs64.netlicensing.domain.entity.Product;
import com.labs64.netlicensing.domain.entity.ProductModule;
import com.labs64.netlicensing.domain.entity.Token;
import com.labs64.netlicensing.domain.entity.Transaction;
import com.labs64.netlicensing.domain.entity.impl.LicenseImpl;
import com.labs64.netlicensing.domain.entity.impl.LicenseTemplateImpl;
import com.labs64.netlicensing.domain.entity.impl.LicenseeImpl;
import com.labs64.netlicensing.domain.entity.impl.ProductImpl;
import com.labs64.netlicensing.domain.entity.impl.ProductModuleImpl;
import com.labs64.netlicensing.domain.entity.impl.TokenImpl;
import com.labs64.netlicensing.domain.vo.Context;
import com.labs64.netlicensing.domain.vo.Currency;
import com.labs64.netlicensing.domain.vo.LicenseType;
import com.labs64.netlicensing.domain.vo.Page;
import com.labs64.netlicensing.domain.vo.SecurityMode;
import com.labs64.netlicensing.domain.vo.TokenType;
import com.labs64.netlicensing.domain.vo.ValidationParameters;
import com.labs64.netlicensing.domain.vo.ValidationResult;
import com.labs64.netlicensing.exception.NetLicensingException;
import com.labs64.netlicensing.service.LicenseService;
import com.labs64.netlicensing.service.LicenseTemplateService;
import com.labs64.netlicensing.service.LicenseeService;
import com.labs64.netlicensing.service.PaymentMethodService;
import com.labs64.netlicensing.service.ProductModuleService;
import com.labs64.netlicensing.service.ProductService;
import com.labs64.netlicensing.service.TokenService;
import com.labs64.netlicensing.service.TransactionService;
import com.labs64.netlicensing.service.UtilityService;
import com.labs64.netlicensing.util.ServiceHelper;
import com.labs64.netlicensing.utils.ConsoleWriter;
import com.labs64.netlicensing.utils.TestHelpers;
public class CallEveryAPIMethod implements NetLicensingExample {
/**
* Exit codes
*/
private final static int CODE_OK = 0;
private final static int CODE_ERROR = 1;
private final static String DEMO_NUMBER_PREFIX = "DEMO-";
private static final String randomLicenseeSecret = UUID.randomUUID().toString();
@Override
@SuppressWarnings("deprecation")
public void execute() {
final Context context = new Context();
context.setBaseUrl("https://go.netlicensing.io/core/v2/rest");
context.setSecurityMode(SecurityMode.BASIC_AUTHENTICATION);
context.setUsername("demo");
context.setPassword("demo");
final String randomNumber = RandomStringUtils.randomAlphanumeric(8);
final String productNumber = numberWithPrefix("P", randomNumber);
final String productModuleNumber = numberWithPrefix("PM", randomNumber);
final String licenseTemplateNumber = numberWithPrefix("LT", randomNumber);
final String licenseeNumber = numberWithPrefix("L", randomNumber);
final String licenseNumber = numberWithPrefix("LC", randomNumber);
final String licenseeName = numberWithPrefix("Licensee ", RandomStringUtils.randomAlphanumeric(8));
final ConsoleWriter out = new ConsoleWriter();
int exitCode = CODE_OK;
try {
// region ********* Lists
final Page licenseTypes = UtilityService.listLicenseTypes(context);
out.writePage("License Types:", licenseTypes);
final Page licensingModels = UtilityService.listLicensingModels(context);
out.writePage("Licensing Models:", licensingModels);
final Page countries = UtilityService.listCountries(context, null);
out.writePage("Countries:", countries);
// endregion
// region ********* Product
final Product newProduct = new ProductImpl();
newProduct.setNumber(productNumber);
newProduct.setName("Demo product");
Product product = ProductService.create(context, newProduct);
out.writeObject("Added product:", product);
product = ProductService.get(context, productNumber);
out.writeObject("Got product:", product);
Page products = ProductService.list(context, null);
out.writePage("Got the following products:", products);
final Product updateProduct = new ProductImpl();
updateProduct.addProperty("Updated property name", "Updated value");
product = ProductService.update(context, productNumber, updateProduct);
out.writeObject("Updated product:", product);
ProductService.delete(context, productNumber, true);
out.writeMessage("Deleted Product!");
products = ProductService.list(context, null);
out.writePage("Got the following Products:", products);
product = ProductService.create(context, newProduct);
out.writeObject("Added product again:", product);
products = ProductService.list(context, null);
out.writePage("Got the following Products:", products);
// endregion
// region ********* ProductModule
final ProductModule newProductModule = new ProductModuleImpl();
newProductModule.setNumber(productModuleNumber);
newProductModule.setName("Demo product module");
newProductModule.setLicensingModel(Constants.LicensingModel.TryAndBuy.NAME);
ProductModule productModule = ProductModuleService.create(context, productNumber, newProductModule);
out.writeObject("Added product module:", productModule);
productModule = ProductModuleService.get(context, productModuleNumber);
out.writeObject("Got product module:", productModule);
Page productModules = ProductModuleService.list(context, null);
out.writePage("Got the following product modules:", productModules);
final ProductModule updateProductModule = new ProductModuleImpl();
updateProductModule.addProperty("Updated property name", "Updated property value");
productModule = ProductModuleService.update(context, productModuleNumber, updateProductModule);
out.writeObject("Updated product module:", productModule);
ProductModuleService.delete(context, productModuleNumber, true);
out.writeMessage("Deleted product module!");
productModules = ProductModuleService.list(context, null);
out.writePage("Got the following product modules:", productModules);
productModule = ProductModuleService.create(context, productNumber, newProductModule);
out.writeObject("Added product module again:", productModule);
productModules = ProductModuleService.list(context, null);
out.writePage("Got the following product modules:", productModules);
// endregion
// region ********* LicenseTemplate
final LicenseTemplate newLicenseTemplate = new LicenseTemplateImpl();
newLicenseTemplate.setNumber(licenseTemplateNumber);
newLicenseTemplate.setName("Demo Evaluation Period");
newLicenseTemplate.setLicenseType(LicenseType.FEATURE);
newLicenseTemplate.setPrice(new BigDecimal(12.5));
newLicenseTemplate.setCurrency(Currency.EUR);
newLicenseTemplate.setAutomatic(false);
newLicenseTemplate.setHidden(false);
out.writeObject("Adding license template:", newLicenseTemplate);
LicenseTemplate licenseTemplate = LicenseTemplateService.create(context, productModuleNumber,
newLicenseTemplate);
out.writeObject("Added license template:", licenseTemplate);
licenseTemplate = LicenseTemplateService.get(context, licenseTemplateNumber);
out.writeObject("Got licenseTemplate:", licenseTemplate);
Page licenseTemplates = LicenseTemplateService.list(context, null);
out.writePage("Got the following license templates:", licenseTemplates);
final LicenseTemplate updateLicenseTemplate = new LicenseTemplateImpl();
updateLicenseTemplate.addProperty("Updated property name", "Updated value");
licenseTemplate = LicenseTemplateService.update(context, licenseTemplateNumber,
updateLicenseTemplate);
out.writeObject("Updated license template:", licenseTemplate);
LicenseTemplateService.delete(context, licenseTemplateNumber, true);
out.writeMessage("Deleted license template!");
licenseTemplates = LicenseTemplateService.list(context, null);
out.writePage("Got the following license templates:", licenseTemplates);
licenseTemplate = LicenseTemplateService.create(context, productModuleNumber, newLicenseTemplate);
out.writeObject("Added license template again:", licenseTemplate);
licenseTemplates = LicenseTemplateService.list(context, null);
out.writePage("Got the following license templates:", licenseTemplates);
// endregion
// region ********* Licensee
final Licensee newLicensee = new LicenseeImpl();
newLicensee.setNumber(licenseeNumber);
Licensee licensee = LicenseeService.create(context, productNumber, newLicensee);
out.writeObject("Added licensee:", licensee);
Page licensees = LicenseeService.list(context, null);
out.writePage("Got the following licensees:", licensees);
LicenseeService.delete(context, licenseeNumber, true);
out.writeMessage("Deleted licensee!");
licensees = LicenseeService.list(context, null);
out.writePage("Got the following licensees after delete:", licensees);
licensee = LicenseeService.create(context, productNumber, newLicensee);
out.writeObject("Added licensee again:", licensee);
licensee = LicenseeService.get(context, licenseeNumber);
out.writeObject("Got licensee:", licensee);
final Licensee updateLicensee = new LicenseeImpl();
updateLicensee.addProperty("Updated property name", "Updated value");
licensee = LicenseeService.update(context, licenseeNumber, updateLicensee);
out.writeObject("Updated licensee:", licensee);
licensees = LicenseeService.list(context, null);
out.writePage("Got the following licensees:", licensees);
// endregion
// region ********* License
final License newLicense = new LicenseImpl();
newLicense.setNumber(licenseNumber);
License license = LicenseService.create(context, licenseeNumber, licenseTemplateNumber, null,
newLicense);
out.writeObject("Added license:", license);
Page licenses = LicenseService.list(context, null);
out.writePage("Got the following licenses:", licenses);
LicenseService.delete(context, licenseNumber, true);
out.writeMessage("Deleted license!");
licenses = LicenseService.list(context, null);
out.writePage("Got the following licenses:", licenses);
license = LicenseService.create(context, licenseeNumber, licenseTemplateNumber, null,
newLicense);
out.writeObject("Added license again:", license);
license = LicenseService.get(context, licenseNumber);
out.writeObject("Got license:", license);
final License updateLicense = new LicenseImpl();
updateLicense.addProperty("Updated property name", "Updated value");
license = LicenseService.update(context, licenseNumber, null, updateLicense);
out.writeObject("Updated license:", license);
// endregion
// region ********* PaymentMethod
final Page paymentMethods = PaymentMethodService.list(context, null);
out.writePage("Got the following payment methods:", paymentMethods);
// endregion
// region ********* Token
// load private/public test keys
final String privateKey = TestHelpers.loadFileContent("rsa_private.pem");
final String publicKey = TestHelpers.loadFileContent("rsa_public.pem");
final String publicKey_wrong = TestHelpers.loadFileContent("rsa_public_wrong.pem");
out.writeObject("loaded privateKey:", privateKey);
out.writeObject("loaded publicKey:", publicKey);
out.writeObject("loaded publicKey_wrong:", publicKey_wrong);
final Token newToken = new TokenImpl();
newToken.setTokenType(TokenType.APIKEY);
newToken.addProperty(Constants.Token.TOKEN_PROP_PRIVATE_KEY, privateKey);
final Token apiKey = TokenService.create(context, newToken);
out.writeObject("Created APIKey:", apiKey);
context.setApiKey(apiKey.getNumber());
newToken.setTokenType(TokenType.SHOP);
newToken.addProperty(Constants.Licensee.LICENSEE_NUMBER, licenseeNumber);
context.setSecurityMode(SecurityMode.APIKEY_IDENTIFICATION);
final Token shopToken = TokenService.create(context, newToken);
context.setSecurityMode(SecurityMode.BASIC_AUTHENTICATION);
out.writeObject("Got the following shop token:", shopToken);
final String filter = Constants.Token.TOKEN_TYPE + "=" + TokenType.SHOP.name();
Page tokens = TokenService.list(context, filter);
out.writePage("Got the following shop tokens:", tokens);
TokenService.delete(context, shopToken.getNumber());
out.writeMessage("Deleted shop token!");
tokens = TokenService.list(context, filter);
out.writePage("Got the following shop tokens after delete:", tokens);
// endregion
// region ********* Validate
final ValidationParameters validationParameters = new ValidationParameters();
validationParameters.put(productModuleNumber, "paramKey", "paramValue");
validationParameters.setLicenseeSecret(randomLicenseeSecret);
validationParameters.setLicenseeName(licenseeName);
validationParameters.setProductNumber(productNumber);
ValidationResult validationResult = null;
// Validate using Basic Auth
context.setSecurityMode(SecurityMode.BASIC_AUTHENTICATION);
validationResult = LicenseeService.validate(context, licenseeNumber, validationParameters);
out.writeObject("Validation result (Basic Auth):", validationResult);
// Validate using APIKey
context.setSecurityMode(SecurityMode.APIKEY_IDENTIFICATION);
validationResult = LicenseeService.validate(context, licenseeNumber, validationParameters);
out.writeObject("Validation result (APIKey):", validationResult);
// Validate using APIKey signed
context.setSecurityMode(SecurityMode.APIKEY_IDENTIFICATION);
context.setPublicKey(publicKey);
validationResult = LicenseeService.validate(context, licenseeNumber, validationParameters);
out.writeObject("Validation result (APIKey / signed):", validationResult);
// Validate using APIKey wrongly signed
context.setSecurityMode(SecurityMode.APIKEY_IDENTIFICATION);
context.setPublicKey(publicKey_wrong);
try {
validationResult = LicenseeService.validate(context, licenseeNumber, validationParameters);
} catch (final NetLicensingException e) {
out.writeException("Validation result exception (APIKey / wrongly signed):", e);
}
// reset context for futher use
context.setSecurityMode(SecurityMode.BASIC_AUTHENTICATION);
context.setPublicKey(null);
// endregion
// region ********* Transfer
Licensee transferLicensee = new LicenseeImpl();
transferLicensee.setNumber("TR" + licenseeNumber);
transferLicensee.getProperties().put(Constants.Licensee.PROP_MARKED_FOR_TRANSFER, Boolean.toString(true));
transferLicensee = LicenseeService.create(context, productNumber, transferLicensee);
out.writeObject("Added transfer licensee:", transferLicensee);
final License transferLicense = new LicenseImpl();
transferLicense.setNumber("LTR" + licenseNumber);
final License newTransferLicense = LicenseService.create(context, transferLicensee.getNumber(),
licenseTemplateNumber, null, transferLicense);
out.writeObject("Added license for transfer:", newTransferLicense);
LicenseeService.transfer(context, licensee.getNumber(), transferLicensee.getNumber());
licenses = LicenseService.list(context, "licenseeNumber=" + licensee.getNumber());
out.writePage("Got the following licenses after transfer:", licenses);
Licensee transferLicenseeWithApiKey = new LicenseeImpl();
transferLicenseeWithApiKey.setNumber("Key" + licenseeNumber);
transferLicenseeWithApiKey.getProperties().put(Constants.Licensee.PROP_MARKED_FOR_TRANSFER,
Boolean.toString(true));
transferLicenseeWithApiKey = LicenseeService.create(context, productNumber, transferLicenseeWithApiKey);
final License transferLicenseWithApiKey = new LicenseImpl();
transferLicenseWithApiKey.setNumber("Key" + licenseNumber);
LicenseService.create(context, transferLicenseeWithApiKey.getNumber(), licenseTemplateNumber, null,
transferLicenseWithApiKey);
context.setSecurityMode(SecurityMode.APIKEY_IDENTIFICATION);
LicenseeService.transfer(context, licensee.getNumber(), transferLicenseeWithApiKey.getNumber());
context.setSecurityMode(SecurityMode.BASIC_AUTHENTICATION);
licenses = LicenseService.list(context, "licenseeNumber=" + licensee.getNumber());
out.writePage("Got the following licenses after transfer:", licenses);
// endregion
// region ********* Transactions
Page transactions = TransactionService.list(context,
Constants.Transaction.SOURCE_SHOP_ONLY + "=" + Boolean.TRUE.toString());
out.writePage("Got the following transactions shop only:", transactions);
transactions = TransactionService.list(context, null);
out.writePage("Got the following transactions after transfer:", transactions);
final List allTransactions = ServiceHelper.listAll(context, null, Transaction.class);
out.writeObject("Retrieved all transactions, total count: ", allTransactions.size());
// endregion
out.writeMessage("All done.");
} catch (final NetLicensingException e) {
out.writeException("Got NetLicensing exception:", e);
exitCode = CODE_ERROR;
} catch (final Exception e) {
out.writeException("Got exception:", e);
exitCode = CODE_ERROR;
} finally {
// Cleanup
try {
// delete APIKey in case it was used (exists)
if (StringUtils.isNotBlank(context.getApiKey())) {
TokenService.delete(context, context.getApiKey());
context.setApiKey(null);
}
// delete test product with all its related items
ProductService.delete(context, productNumber, true);
} catch (final NetLicensingException e) {
out.writeException("Got NetLicensing exception during cleanup:", e);
exitCode = CODE_ERROR;
} catch (final Exception e) {
out.writeException("Got exception during cleanup:", e);
exitCode = CODE_ERROR;
}
}
if (exitCode == CODE_ERROR) {
System.exit(exitCode);
}
}
private static String numberWithPrefix(final String prefix, final String number) {
return String.format("%s%s%s", DEMO_NUMBER_PREFIX, prefix, number);
}
}