private CvalInfo askLicenseServer( String productName, String productKey, String productVersion, CvalInfo info) throws UnreachableCvalServerException { int majorVersion = computeMajorVersion(productVersion); // If we have a valid license info here, it means that we got it from // cache. // We add a grace time when so as if the server is unreachable // we allow the user to use the product. if (info != null && info.getExpiredEpoch() != null && !"evaluation".equals(info.getType())) { long ts = info.getExpiredEpoch().getTime() + GRACE_DAYS_MSECS; info.setExpiredEpoch(new Date(ts)); } boolean validCache = info != null && info.isValidInfo(productName, productKey) && info.isValidVersion(majorVersion) && !info.isLicenseExpired(); // if we have a validCache we set the timeout smaller int timeout = validCache ? 2000 : 10000; try { CvalInfo srvinfo = parseJson(provider.askServer(productName + "-" + productVersion, productKey, timeout)); if (srvinfo != null && srvinfo.isValidInfo(productName, productKey) && srvinfo.isValidVersion(majorVersion)) { // We always cache the info if it is valid although it is // expired cacheLicenseInfo(srvinfo); info = srvinfo; } } catch (FileNotFoundException e) { // 404 return null; } catch (Exception e) { if (info == null) { throw new UnreachableCvalServerException(productName, e); } } return info; }
static String composeMessage(String title, String version, String key, CvalInfo info) { String msg = ""; int majorVers = computeMajorVersion(version); if (info != null && !info.isValidVersion(majorVers)) { msg = getErrorMessage("invalid", title, majorVers); } else if (info != null && info.getMessage() != null) { msg = info.getMessage().replace("\\n", "\n"); } else if (info != null && info.isLicenseExpired()) { String type = "evaluation".equals(info.getType()) ? "Evaluation license" : "License"; msg = getErrorMessage("expired", title, majorVers, type); } else if (key == null) { msg = getErrorMessage("none", title, majorVers); } else { msg = getErrorMessage("invalid", title, majorVers); } return msg; }
/** * Validate whether there is a valid license key for a product. * * @param productName for example vaadin-touchkit * @param productVersion for instance 4.0.1 * @return CvalInfo Server response or cache response if server is offline * @throws InvalidCvalException when there is no a valid license for the product * @throws UnreachableCvalServerException when we have license key but server is unreachable */ public CvalInfo validateProduct(String productName, String productVersion, String productTitle) throws InvalidCvalException, UnreachableCvalServerException { String key = getDeveloperLicenseKey(productName, productVersion, productTitle); CvalInfo info = null; if (key != null && !key.isEmpty()) { info = getCachedLicenseInfo(productName); if (info != null && !info.isValidInfo(productName, key)) { deleteCache(productName); info = null; } info = askLicenseServer(productName, key, productVersion, info); if (info != null && info.isValidInfo(productName, key) && info.isValidVersion(computeMajorVersion(productVersion)) && !info.isLicenseExpired()) { return info; } } throw new InvalidCvalException(productName, productVersion, productTitle, key, info); }