/** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    final String S_ProcName = "doGet";
    ICFInternetSchemaObj schemaObj;
    HttpSession sess = request.getSession(false);
    if (sess == null) {
      sess = request.getSession(true);
      schemaObj = new CFInternetSchemaPooledObj();
      sess.setAttribute("SchemaObj", schemaObj);
    } else {
      schemaObj = (ICFInternetSchemaObj) sess.getAttribute("SchemaObj");
      if (schemaObj == null) {
        schemaObj = new CFInternetSchemaPooledObj();
        sess.setAttribute("SchemaObj", schemaObj);
      }
    }

    ICFInternetSchema dbSchema = null;
    try {
      CFSecurityAuthorization auth = schemaObj.getAuthorization();
      if (auth != null) {
        response.sendRedirect("CFInternetSMWarSecurityMainHtml");
        return;
      }

      dbSchema = (ICFInternetSchema) CFInternetSchemaPool.getSchemaPool().getInstance();
      schemaObj.setBackingStore(dbSchema);
      schemaObj.beginTransaction();
      ICFSecuritySecUserObj systemUser =
          schemaObj.getSecUserTableObj().readSecUserByULoginIdx("system");
      String passwordHash = systemUser.getRequiredPasswordHash();
      if ((passwordHash == null)
          || (passwordHash.length() <= 0)
          || passwordHash.equals("bootstrap")) {
        response.sendRedirect("CFInternetSMWarSetSystemPasswordHtml");
        return;
      }

      ICFSecuritySysClusterObj sysCluster =
          schemaObj.getSysClusterTableObj().readSysClusterByIdIdx(1, false);
      if (sysCluster == null) {
        throw CFLib.getDefaultExceptionFactory()
            .newNullArgumentException(getClass(), S_ProcName, 0, "sysCluster");
      }

      ICFSecurityClusterObj resolvedCluster = sysCluster.getRequiredContainerCluster();
      if (resolvedCluster == null) {
        throw CFLib.getDefaultExceptionFactory()
            .newNullArgumentException(getClass(), S_ProcName, "resolvedCluster");
      }
      String clusterDomainName = resolvedCluster.getRequiredFullDomainName();
      String clusterDescription = resolvedCluster.getRequiredDescription();

      String confirmationUUIDStr = (String) request.getParameter("ConfirmationUUID");
      if ((confirmationUUIDStr == null) || (confirmationUUIDStr.length() <= 0)) {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
        out.println("<HTML>");
        out.println("<BODY>");
        out.println("<form method=\"post\" formaction=\"CFInternetSMWarConfirmEMailAddressHtml\">");
        out.println(
            "<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
        out.println("<H2 style=\"text-align:center\">ERROR</H2>");
        out.println("<p>");
        out.println("<center>");
        out.println("<p>");
        out.println(
            "The ConfirmationUUID parameter was missing in your request.  Please use the link provided by your confirmation email.");
        out.println("</BODY>");
        out.println("</HTML>");
        return;
      }

      UUID confirmationUUID = UUID.fromString(confirmationUUIDStr);
      if (confirmationUUID == null) {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
        out.println("<HTML>");
        out.println("<BODY>");
        out.println("<form method=\"post\" formaction=\"CFInternetSMWarConfirmEMailAddressHtml\">");
        out.println(
            "<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
        out.println("<H2 style=\"text-align:center\">ERROR</H2>");
        out.println("<p>");
        out.println("<center>");
        out.println("<p>");
        out.println(
            "Invalid ConfirmationUUID \""
                + confirmationUUIDStr
                + "\".  Please use the link provided by your confirmation email.");
        out.println("</BODY>");
        out.println("</HTML>");
        return;
      }

      ICFSecuritySecUserObj confirmUser = null;
      Iterator<ICFSecuritySecUserObj> secUserForUUID =
          schemaObj.getSecUserTableObj().readSecUserByEMConfIdx(confirmationUUID).iterator();
      if (secUserForUUID.hasNext()) {
        confirmUser = secUserForUUID.next();
        if (secUserForUUID.hasNext()) {
          throw CFLib.getDefaultExceptionFactory()
              .newRuntimeException(
                  getClass(),
                  S_ProcName,
                  "Multiple SecUser instances found for ConfirmationUUID \""
                      + confirmationUUIDStr
                      + "\"");
        }
      } else {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
        out.println("<HTML>");
        out.println("<BODY>");
        out.println("<form method=\"post\" formaction=\"CFInternetSMWarConfirmEMailAddressHtml\">");
        out.println(
            "<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
        out.println("<H2 style=\"text-align:center\">ERROR</H2>");
        out.println("<p>");
        out.println("<center>");
        out.println("<p>");
        out.println(
            "Invalid ConfirmationUUID \""
                + confirmationUUIDStr
                + "\".  Please use the link provided by your confirmation email.");
        out.println("</BODY>");
        out.println("</HTML>");
        return;
      }

      ICFSecurityClusterObj systemCluster =
          schemaObj.getClusterTableObj().readClusterByUDomainNameIdx("system");
      ICFSecurityTenantObj systemTenant =
          schemaObj
              .getTenantTableObj()
              .readTenantByUNameIdx(systemCluster.getRequiredId(), "system");
      ICFSecuritySecSessionObj systemSession = schemaObj.getSecSessionTableObj().newInstance();
      ICFSecuritySecSessionEditObj editSystemSession =
          (ICFSecuritySecSessionEditObj) systemSession.beginEdit();
      editSystemSession.setRequiredContainerSecUser(systemUser);
      editSystemSession.setRequiredStart(Calendar.getInstance());
      systemSession = editSystemSession.create();
      editSystemSession.endEdit();

      auth = new CFSecurityAuthorization();
      auth.setSecCluster(systemCluster);
      auth.setSecTenant(systemTenant);
      auth.setSecSession(systemSession);
      schemaObj.setAuthorization(auth);

      ICFSecuritySecUserEditObj editConfirmUser = confirmUser.beginEdit();
      editConfirmUser.setOptionalEMailConfirmationUuid(null);
      editConfirmUser.update();
      editConfirmUser.endEdit();

      editSystemSession = (ICFSecuritySecSessionEditObj) systemSession.beginEdit();
      editSystemSession.setOptionalFinish(Calendar.getInstance());
      editSystemSession.update();
      editSystemSession.endEdit();

      schemaObj.commit();

      schemaObj.setAuthorization(null);

      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
      out.println("<HTML>");
      out.println("<BODY>");
      out.println("<form method=\"post\" formaction=\"CFInternetSMWarConfirmEMailAddressHtml\">");
      out.println(
          "<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
      out.println("<H2 style=\"text-align:center\">EMail Address Confirmed</H2>");
      out.println("<p>");
      out.println("<center>");
      out.println("<table style=\"width:75%\">");
      out.println(
          "<tr><th style=\"text-align:left\">Confirmation UUID:</th><td><input type=\"text\" name=\"ConfirmationUUID\" value=\""
              + confirmationUUID.toString()
              + "\"/></td></tr>");
      out.println(
          "<tr><td colSpan=\"2\" style=\"text-align:center\">You may now <A HRef=\"CFInternetSMWarLoginHtml\">log in</A> to the "
              + clusterDescription
              + " Security Manager</td></tr>");
      out.println("</table>");
      out.println("</center>");
      out.println("</form>");
      out.println("</BODY>");
      out.println("</HTML>");
    } catch (RuntimeException e) {
      throw CFLib.getDefaultExceptionFactory()
          .newRuntimeException(
              getClass(), S_ProcName, "Caught RuntimeException -- " + e.getMessage(), e);
    } finally {
      if (dbSchema != null) {
        try {
          if (schemaObj.isTransactionOpen()) {
            schemaObj.rollback();
          }
        } catch (RuntimeException e) {
        }
        schemaObj.setBackingStore(null);
        CFInternetSchemaPool.getSchemaPool().releaseInstance(dbSchema);
      }
    }
  }
  public static void main(String args[]) {
    final String S_ProcName = "CFInternetSaxMySqlLoaderCLI.main() ";
    initConsoleLog();
    int numArgs = args.length;
    if (numArgs >= 2) {
      String homeDirName = System.getProperty("HOME");
      if (homeDirName == null) {
        homeDirName = System.getProperty("user.home");
        if (homeDirName == null) {
          log.message(S_ProcName + "ERROR: Home directory not set");
          return;
        }
      }
      File homeDir = new File(homeDirName);
      if (!homeDir.exists()) {
        log.message(S_ProcName + "ERROR: Home directory \"" + homeDirName + "\" does not exist");
        return;
      }
      if (!homeDir.isDirectory()) {
        log.message(
            S_ProcName + "ERROR: Home directory \"" + homeDirName + "\" is not a directory");
        return;
      }
      CFInternetConfigurationFile cFInternetConfig = new CFInternetConfigurationFile();
      String cFInternetConfigFileName = homeDir.getPath() + File.separator + ".cfinternetmysqlrc";
      cFInternetConfig.setFileName(cFInternetConfigFileName);
      File cFInternetConfigFile = new File(cFInternetConfigFileName);
      if (!cFInternetConfigFile.exists()) {
        cFInternetConfig.setDbServer("127.0.0.1");
        cFInternetConfig.setDbPort(3306);
        cFInternetConfig.setDbDatabase("CFINet24");
        cFInternetConfig.setDbUserName("root");
        cFInternetConfig.setDbPassword("edit-me-please");
        cFInternetConfig.save();
        log.message(
            S_ProcName
                + "INFO: Created configuration file "
                + cFInternetConfigFileName
                + ", please edit configuration and restart.");
        return;
      }
      if (!cFInternetConfigFile.isFile()) {
        log.message(
            S_ProcName
                + "ERROR: Proposed configuration file "
                + cFInternetConfigFileName
                + " is not a file.");
        return;
      }
      if (!cFInternetConfigFile.canRead()) {
        log.message(
            S_ProcName
                + "ERROR: Permission denied attempting to read configuration file "
                + cFInternetConfigFileName);
        return;
      }
      cFInternetConfig.load();
      boolean fastExit = false;
      CFInternetClientConfigurationFile cFDbTestClientConfig =
          new CFInternetClientConfigurationFile();
      String cFDbTestClientConfigFileName =
          homeDir.getPath() + File.separator + ".cfdbtestclientrc";
      cFDbTestClientConfig.setFileName(cFDbTestClientConfigFileName);
      File cFDbTestClientConfigFile = new File(cFDbTestClientConfigFileName);
      if (!cFDbTestClientConfigFile.exists()) {
        String cFDbTestKeyStoreFileName = homeDir.getPath() + File.separator + ".msscfjceks";
        cFDbTestClientConfig.setKeyStore(cFDbTestKeyStoreFileName);
        InetAddress localHost;
        try {
          localHost = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
          localHost = null;
        }
        if (localHost == null) {
          log.message(S_ProcName + "ERROR: LocalHost is null");
          return;
        }
        String hostName = localHost.getHostName();
        if ((hostName == null) || (hostName.length() <= 0)) {
          log.message("ERROR: LocalHost.HostName is null or empty");
          return;
        }
        String userName = System.getProperty("user.name");
        if ((userName == null) || (userName.length() <= 0)) {
          log.message("ERROR: user.name is null or empty");
          return;
        }
        String deviceName =
            hostName.replaceAll("[^\\w]", "_").toLowerCase()
                + "-"
                + userName.replaceAll("[^\\w]", "_").toLowerCase();
        cFDbTestClientConfig.setDeviceName(deviceName);
        cFDbTestClientConfig.save();
        log.message(
            S_ProcName
                + "INFO: Created CFInternet client configuration file "
                + cFDbTestClientConfigFileName);
        fastExit = true;
      }
      if (!cFDbTestClientConfigFile.isFile()) {
        log.message(
            S_ProcName
                + "ERROR: Proposed client configuration file "
                + cFDbTestClientConfigFileName
                + " is not a file.");
        fastExit = true;
      }
      if (!cFDbTestClientConfigFile.canRead()) {
        log.message(
            S_ProcName
                + "ERROR: Permission denied attempting to read client configuration file "
                + cFDbTestClientConfigFileName);
        fastExit = true;
      }
      cFDbTestClientConfig.load();

      if (fastExit) {
        return;
      }

      // Configure logging
      Properties sysProps = System.getProperties();
      sysProps.setProperty("log4j.rootCategory", "WARN");
      sysProps.setProperty(
          "org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Log4JLogger");

      Logger httpLogger = Logger.getLogger("org.apache.http");
      httpLogger.setLevel(Level.WARN);

      ICFInternetSchema cFInternetSchema = new CFInternetMySqlSchema();
      cFInternetSchema.setConfigurationFile(cFInternetConfig);
      ICFInternetSchemaObj cFInternetSchemaObj = new CFInternetSchemaObj();
      cFInternetSchemaObj.setBackingStore(cFInternetSchema);
      CFInternetSaxLoaderCLI cli = new CFInternetSaxMySqlLoaderCLI();
      CFInternetSaxLoader loader = cli.getSaxLoader();
      loader.setSchemaObj(cFInternetSchemaObj);
      cFInternetSchema.connect();
      String url = args[1];
      if (numArgs >= 5) {
        cli.setClusterName(args[2]);
        cli.setTenantName(args[3]);
        cli.setSecUserName(args[4]);
      } else {
        cli.setClusterName("default");
        cli.setTenantName("system");
        cli.setSecUserName("system");
      }
      loader.setUseCluster(cli.getClusterObj());
      loader.setUseTenant(cli.getTenantObj());
      try {
        cFInternetSchema.beginTransaction();
        cFInternetSchemaObj.setSecCluster(cli.getClusterObj());
        cFInternetSchemaObj.setSecTenant(cli.getTenantObj());
        cFInternetSchemaObj.setSecUser(cli.getSecUserObj());
        cFInternetSchemaObj.setSecSession(cli.getSecSessionObj());
        CFSecurityAuthorization auth = new CFSecurityAuthorization();
        auth.setSecCluster(cFInternetSchemaObj.getSecCluster());
        auth.setSecTenant(cFInternetSchemaObj.getSecTenant());
        auth.setSecSession(cFInternetSchemaObj.getSecSession());
        cFInternetSchemaObj.setAuthorization(auth);
        applyLoaderOptions(loader, args[0]);
        if (numArgs >= 5) {
          cli.evaluateRemainingArgs(args, 5);
        } else {
          cli.evaluateRemainingArgs(args, 2);
        }
        loader.parseFile(url);
        cFInternetSchema.commit();
        cFInternetSchema.disconnect(true);
      } catch (Exception e) {
        log.message(
            S_ProcName + "EXCEPTION: Could not parse XML file \"" + url + "\": " + e.getMessage());
        e.printStackTrace(System.out);
      } catch (Error e) {
        log.message(
            S_ProcName + "ERROR: Could not parse XML file \"" + url + "\": " + e.getMessage());
        e.printStackTrace(System.out);
      } finally {
        if (cFInternetSchema.isConnected()) {
          cFInternetSchema.rollback();
          cFInternetSchema.disconnect(false);
        }
      }
    } else {
      log.message(
          S_ProcName
              + "ERROR: Expected at least two argument specifying the loader options and the name of the XML file to parse.  The first argument may be empty.");
    }
  }