示例#1
0
  /**
   * Generates the new ApiKey and stores it in the db.
   *
   * @param applicationName
   * @param applicationUrl
   * @param email
   * @param phone
   * @param description
   * @return The new ApiKey or null if there was a problem such as the key
   * @throws IllegalArgumentException
   * @throws HibernateException
   */
  public ApiKey generateApiKey(
      String applicationName, String applicationUrl, String email, String phone, String description)
      throws IllegalArgumentException, HibernateException {
    // Make sure don't already have key for this application name
    List<ApiKey> currentApiKeys = getApiKeys();
    for (ApiKey currentApiKey : currentApiKeys) {
      if (currentApiKey.getApplicationName().equals(applicationName)) {
        // Already have a key for that application so return null
        logger.error("Already have key for application name \"{}\"", applicationName);
        throw new IllegalArgumentException(
            "Already have key for " + "application name \"" + applicationName + "\"");
      }
    }

    // Determine what the key should be
    String key = generateKey(applicationName);

    // Create the new ApiKey
    ApiKey newApiKey = new ApiKey(applicationName, key, applicationUrl, email, phone, description);

    // Store new ApiKey in database
    newApiKey.storeApiKey(dbName);

    // Return the new key
    return newApiKey;
  }