Exemplo n.º 1
0
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    LOG.info("Try to install MyCollab");
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Expires", "-1");
    PrintWriter out = response.getWriter();
    String sitename = request.getParameter("sitename");
    String serverAddress = request.getParameter("serverAddress");
    String databaseName = request.getParameter("databaseName");
    String dbUserName = request.getParameter("dbUserName");
    String dbPassword = request.getParameter("dbPassword");
    String databaseServer = request.getParameter("databaseServer");
    String smtpUserName = request.getParameter("smtpUserName");
    String smtpPassword = request.getParameter("smtpPassword");
    String smtpHost = request.getParameter("smtpHost");
    String smtpPort = request.getParameter("smtpPort");
    String tls = request.getParameter("tls");
    String ssl = request.getParameter("ssl");

    String dbUrl =
        String.format(
            "jdbc:mysql://%s/%s?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&rewriteBatchedStatements=true&useCompression=true&useServerPrepStmts=false",
            databaseServer, databaseName);

    try {
      Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
      LOG.error("Can not load mysql driver", e);
      return;
    }

    try (Connection connection = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
      LOG.debug("Check database config");
      connection.getMetaData();
    } catch (Exception e) {
      String rootCause = (e.getCause() == null) ? e.getMessage() : e.getCause().getMessage();
      out.write(
          "Cannot establish connection to database. Make sure your inputs are correct. Root cause is "
              + rootCause);
      LOG.error("Can not connect database", e);
      return;
    }

    int mailServerPort;
    try {
      mailServerPort = Integer.parseInt(smtpPort);
    } catch (Exception e) {
      mailServerPort = 0;
    }

    boolean isStartTls = Boolean.parseBoolean(tls);
    boolean isSsl = Boolean.parseBoolean(ssl);
    try {
      InstallUtils.checkSMTPConfig(
          smtpHost, mailServerPort, smtpUserName, smtpPassword, true, isStartTls, isSsl);
    } catch (UserInvalidInputException e) {
      LOG.warn("Cannot authenticate mail server successfully. Make sure your inputs are correct.");
    }

    VelocityContext templateContext = new VelocityContext();
    templateContext.put("sitename", sitename);
    templateContext.put("serveraddress", serverAddress);
    templateContext.put("dbUrl", dbUrl);
    templateContext.put("dbUser", dbUserName);
    templateContext.put("dbPassword", dbPassword);
    templateContext.put("smtpAddress", smtpHost);
    templateContext.put("smtpPort", mailServerPort + "");
    templateContext.put("smtpUserName", smtpUserName);
    templateContext.put("smtpPassword", smtpPassword);
    templateContext.put("smtpTLSEnable", tls);
    templateContext.put("smtpSSLEnable", ssl);

    File confFolder =
        FileUtils.getDesireFile(System.getProperty("user.dir"), "conf", "src/main/conf");
    if (confFolder == null) {
      throw new MyCollabException("The conf folder is not existed");
    }

    try {
      File templateFile = new File(confFolder, "mycollab.properties.template");
      FileReader templateReader = new FileReader(templateFile);

      StringWriter writer = new StringWriter();

      VelocityEngine engine = new VelocityEngine();
      engine.evaluate(templateContext, writer, "log task", templateReader);

      FileOutputStream outStream =
          new FileOutputStream(new File(confFolder, "mycollab.properties"));
      outStream.write(writer.toString().getBytes());
      outStream.flush();
      outStream.close();

      while (waitFlag) {
        try {
          Thread.sleep(5000);
        } catch (InterruptedException e) {
          throw new MyCollabException(e);
        }
      }

    } catch (Exception e) {
      LOG.error("Error while set up MyCollab", e);
      out.write(
          "Can not write the settings to the file system. You should check our knowledge base system or contact us at [email protected] to solve this issue.");
      return;
    }
  }