/**
   * Displays the initial setup screen. If initial setup is already complete, this will forward to
   * the license page.
   */
  public static void index() {
    checkCompleteAndLicensed();
    if (!isLicensed()) {
      license();
    }

    SetupForm setup = new SetupForm();
    setup.loadDefaults();
    render(setup);
  }
 /**
  * Validate passwords from the initial setup form, rendering the result as JSON.
  *
  * @param setup the initial setup form.
  */
 @Restrictions({
   @Restrict({"SYSTEM_ADMIN", "SECURITY_ADMIN"}),
   @Restrict({"RESTRICTED_SYSTEM_ADMIN", "RESTRICTED_SECURITY_ADMIN"})
 })
 public static void validatePasswords(SetupForm setup) {
   setup.validatePasswords();
   if (Validation.hasErrors()) {
     renderJSON(ValidationResponse.collectErrors());
   } else {
     renderJSON(ValidationResponse.valid());
   }
 }
  /**
   * Tests the SMTP settings from the initial setup form, rendering the result as JSON.
   *
   * @param setup the initial setup form.
   */
  @Restrictions({
    @Restrict({"SYSTEM_ADMIN", "SECURITY_ADMIN"}),
    @Restrict({"RESTRICTED_SYSTEM_ADMIN", "RESTRICTED_SECURITY_ADMIN"})
  })
  public static void testSmtpSettings(SetupForm setup) {
    setup.validateSmtp();
    Validation.required("setup.smtpTo", setup.smtpTo);
    Validation.email("setup.smtpTo", setup.smtpTo);
    if (Validation.hasErrors()) {
      renderJSON(ValidationResponse.collectErrors());
    }

    MailSettingsValidator.Settings settings = new MailSettingsValidator.Settings();

    if (StringUtils.isNotEmpty(setup.nameservers)
        && !InetAddresses.isInetAddress(setup.smtpServer)) {
      Set<String> ips = Sets.newHashSet(setup.nameservers.split(","));

      try {
        settings.server = DnsUtils.getHostIpAddress(ips, setup.smtpServer).getHostAddress();
      } catch (ViPRException e) {
        renderJSON(ValidationResponse.invalid(e.getMessage()));
      }
    } else {
      settings.server = setup.smtpServer;
    }
    settings.port = ConfigProperties.getPort(setup.smtpPort, setup.smtpEnableTls);
    settings.username = setup.smtpUsername;
    settings.password = PasswordUtil.decryptedValue(setup.smtpPassword);
    settings.channel = StringUtils.equals("yes", setup.smtpEnableTls) ? "starttls" : "clear";
    settings.authType = setup.smtpAuthType;
    settings.fromAddress = setup.smtpFrom;

    try {
      MailSettingsValidator.validate(settings, setup.smtpTo);
    } catch (RuntimeException e) {
      Logger.error(e, "Failed to send email");
      Validation.addError(null, "setup.testEmail.failure", e.getMessage());
      if (StringUtils.isEmpty(setup.nameservers)) {
        Validation.addError(null, "setup.smtpServer.invalidEmptyNameserver");
      }
    }

    if (Validation.hasErrors()) {
      renderJSON(ValidationResponse.collectErrors());
    } else {
      renderJSON(ValidationResponse.valid(MessagesUtils.get("setup.testEmail.success")));
    }
  }
  /**
   * Saves the initial setup form. This is restricted to admin users.
   *
   * @param setup the initial setup form.
   */
  @Restrictions({
    @Restrict({"SYSTEM_ADMIN", "SECURITY_ADMIN"}),
    @Restrict({"RESTRICTED_SYSTEM_ADMIN", "RESTRICTED_SECURITY_ADMIN"})
  })
  public static void save(@Valid SetupForm setup) {
    checkCompleteAndLicensed();
    setup.validate();
    if (Validation.hasErrors()) {
      protectPasswords();
      params.flash();
      Validation.keep();
      index();
    }

    Map<String, String> properties = getUpdatedProperties(setup);
    completeInitialSetup(properties);
  }