com.teasoft.teavote.util.Utilities.java Source code

Java tutorial

Introduction

Here is the source code for com.teasoft.teavote.util.Utilities.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.teasoft.teavote.util;

import com.teasoft.teavote.TeavoteApplication;
import static com.teasoft.teavote.util.PasswordHash.PBKDF2_ALGORITHM;
import java.io.File;
import java.io.PrintWriter;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author Elikem
 */
@Component
public class Utilities {

    @Autowired
    AppProperties prop;

    public boolean backupDB(String dbName, String dbUserName, String dbPassword, String path) throws Exception {
        String executeCmd = "mysqldump -u " + dbUserName + " -p" + dbPassword
                + " --events --routines --triggers -B " + dbName + " -r " + path;
        Process runtimeProcess;
        runtimeProcess = Runtime.getRuntime().exec(executeCmd);
        int processComplete = runtimeProcess.waitFor();
        return processComplete == 0;
    }

    public boolean restoreDB(String dbUserName, String dbPassword, String path) throws Exception {
        //Create a batch file
        String destpath = "restore_command.bat";
        //creating batch file 
        PrintWriter writer = new PrintWriter(destpath);
        String command = "mysql -u" + dbUserName + " -p" + dbPassword + " < \"" + path + "\"";
        writer.println(command);
        writer.close();

        //String executeCmd = "mysql -u" + dbUserName + " -p" + dbPassword + " < " + path;
        Process runtimeProcess;
        runtimeProcess = Runtime.getRuntime().exec(destpath);
        int processComplete = runtimeProcess.waitFor();
        //Delete the bat file
        new File(destpath).delete();
        return processComplete == 0;
    }

    public boolean restoreDB2(String dbUserName, String dbPassword, String path) throws Exception {
        //Create a batch file
        String destpath = "restore_command.bat";
        //creating batch file 
        PrintWriter writer = new PrintWriter(destpath);
        String command = "mysql -u" + dbUserName + " -p" + dbPassword + " < \"" + path + "\"";
        writer.println(command);
        writer.close();

        //String executeCmd = "mysql -u" + dbUserName + " -p" + dbPassword + " < " + path;
        Process runtimeProcess;
        runtimeProcess = Runtime.getRuntime().exec(destpath);
        int processComplete = runtimeProcess.waitFor();
        //Delete the bat file
        new File(destpath).delete();
        return processComplete == 0;
    }

    public String getPassword() {
        String secret = prop.getSecret();
        try {
            String first = PBKDF2_ALGORITHM.substring(0, PBKDF2_ALGORITHM.length() - 2);
            String second = "";
            for (int i = 0, j = 12; i < 4; i++) {
                second += first.substring(j - (i * 4), first.length() - i * 4);
            }
            Key aesKey = new SecretKeySpec(second.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, aesKey);
            for (int i = 0; i < 2; i++) {
                secret = new String(cipher.doFinal(Base64.decodeBase64(secret)));
            }

        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
                | BadPaddingException ex) {
            Logger.getLogger(TeavoteApplication.class.getName()).log(Level.SEVERE, null, ex);
        }
        return secret;
    }
}