/* * 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 gridmanualprocessor; import javax.swing.*; import java.io.*; import java.util.HashSet; import java.security.MessageDigest; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.net.URL; import java.net.URLConnection; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Properties; /** * * @author admin */ public class GridManualProcess extends javax.swing.JFrame { /** * Creates new form GridManualProcess */ public GridManualProcess() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); TxtFilePath = new javax.swing.JTextField(); BSelectFolder = new javax.swing.JButton(); BRunProgram = new javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); TAUniqueCount = new javax.swing.JTextArea(); jScrollPane2 = new javax.swing.JScrollPane(); TADownloadedReport = new javax.swing.JTextArea(); jLabel3 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); jScrollPane3 = new javax.swing.JScrollPane(); TAUniqueReport = new javax.swing.JTextArea(); jScrollPane4 = new javax.swing.JScrollPane(); TAUnknownReport = new javax.swing.JTextArea(); jLabel4 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jMenu2 = new javax.swing.JMenu(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Grid Manual Processor"); setBackground(java.awt.Color.red); setResizable(false); jLabel1.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N jLabel1.setText("File Path:"); TxtFilePath.setEditable(false); BSelectFolder.setText("Select Folder"); BSelectFolder.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BSelectFolderActionPerformed(evt); } }); BRunProgram.setText("Run Program"); BRunProgram.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BRunProgramActionPerformed(evt); } }); jLabel2.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N jLabel2.setText("Downloaded Report:"); TAUniqueCount.setEditable(false); TAUniqueCount.setColumns(20); TAUniqueCount.setRows(5); jScrollPane1.setViewportView(TAUniqueCount); TADownloadedReport.setEditable(false); TADownloadedReport.setColumns(20); TADownloadedReport.setRows(5); jScrollPane2.setViewportView(TADownloadedReport); jLabel3.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N jLabel3.setText("Unique Report:"); jLabel6.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N jLabel6.setText("Unknown Report:"); TAUniqueReport.setColumns(20); TAUniqueReport.setRows(5); jScrollPane3.setViewportView(TAUniqueReport); TAUnknownReport.setColumns(20); TAUnknownReport.setRows(5); jScrollPane4.setViewportView(TAUnknownReport); jLabel4.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N jLabel4.setText("Summary Report:"); jButton1.setText("Sha1 List"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Daily Report"); jMenu1.setText("File"); jMenuBar1.add(jMenu1); jMenu2.setText("Edit"); jMenuBar1.add(jMenu2); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(20, 20, 20) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup() .addGap(8, 8, 8) .addComponent(jLabel3))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel6) .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))) .addComponent(TxtFilePath, javax.swing.GroupLayout.PREFERRED_SIZE, 394, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel4) .addComponent(jScrollPane1) .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(BSelectFolder) .addGap(18, 18, 18) .addComponent(BRunProgram))) .addContainerGap(21, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(TxtFilePath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(BSelectFolder) .addComponent(BRunProgram)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel6)) .addGroup(layout.createSequentialGroup() .addGap(18, 18, 18) .addComponent(jLabel3)))) .addGroup(layout.createSequentialGroup() .addGap(52, 52, 52) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(jLabel4)))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup() .addComponent(jScrollPane1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton2) .addGap(12, 12, 12))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void BSelectFolderActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: FCFolderPath = new JFileChooser(); FCFolderPath.setCurrentDirectory(new java.io.File("D:\\")); FCFolderPath.setDialogTitle("Folder Path"); FCFolderPath.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int result = FCFolderPath.showDialog(this,"Select Folder"); if (result == JFileChooser.APPROVE_OPTION) { File file = FCFolderPath.getSelectedFile(); TxtFilePath.setText(file.getAbsolutePath()); } } private void BRunProgramActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String filepaths = TxtFilePath.getText(); String PWord = "PWord"; String DBUrl = "DBUrl"; String UName = "UName"; String SiteInitial = "SiteInitial"; String OJTNumber = "OJTNumber"; int downloadCount = 0; if ("".equals(TxtFilePath.getText())) { JOptionPane.showMessageDialog(null, "PLEASE CHOOSE A FOLDER PATH","MESSAGE ALERT",JOptionPane.ERROR_MESSAGE); } else { try { DeduplicateFiles(filepaths); //Reporting(filepaths, UName, PWord, SiteInitial, OJTNumber); // int DLCount = DuplicateCount + UniqueCount; // TAUniqueCount.append("Dounloaded:"+ DLCount + "\n"); QueryACL(filepaths, DBUrl, UName, PWord, SiteInitial, OJTNumber); downloadCount = UniqueCount + DuplicateCount; TAUniqueCount.append("Total Downloaded Files: "+ downloadCount +"\n"); //MyUniqueReporting(); JOptionPane.showMessageDialog(null, "COMPLETED","MANUAL PROCESS",JOptionPane.INFORMATION_MESSAGE); } catch(Exception e) { } } } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } public static byte[] createChecksum(String filename) throws Exception { MessageDigest complete; try (InputStream fis = new FileInputStream(filename)) { byte[] buffer = new byte[1024]; complete = MessageDigest.getInstance("MD5"); int numRead; do { numRead = fis.read(buffer); if (numRead > 0) { complete.update(buffer, 0, numRead); } } while (numRead != -1); } return complete.digest(); } public String DeduplicateFiles(String myFolderLocation) { try { HashSet<String> newset = new HashSet<>(); File folder = new File(myFolderLocation); //Directory where the files are located File[] listOfFiles = folder.listFiles(); for (File listOfFile : listOfFiles) { //Enumerate the files inside the folder if (listOfFile.isFile()) { String myMD5 = getMD5Checksum(listOfFile.getPath()); if (newset.contains(myMD5)) { DuplicateCount++; // System.out.println(listOfFile.getName() + "," + myMD5 + ", DUPLICATE"); TADownloadedReport.append(listOfFile.toPath().getParent().getFileName()+","+listOfFile.getName() + "," + myMD5 + ", \n"); File myFilePath = new File(listOfFile.getPath()); myFilePath.delete(); } else { UniqueCount++; //System.out.println(listOfFile.isDirectory() + "," + myMD5 + ", UNIQUE"); TADownloadedReport.append(listOfFile.toPath().getParent().getFileName()+","+listOfFile.getName() + "," + myMD5 + ", \n"); TAUniqueReport.append(listOfFile.toPath().getParent().getFileName()+","+listOfFile.getName() + "," + myMD5 + "\n"); newset.add(myMD5); } } else if (listOfFile.isDirectory()) { System.out.println("Directory " + listOfFile.getName()); } } } catch (Exception e) { } return null; } public static String getMD5Checksum(String filename) throws Exception { byte[] b = createChecksum(filename); String result = ""; for (int i = 0; i < b.length; i++) { result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1); } return result; } public static String MyUniqueReporting() { DateFormat NgayonAraw = new SimpleDateFormat("yyyy-MM-dd"); Date Araw = new Date(); PrintWriter MyUniqueReport; try { MyUniqueReport = new PrintWriter(new FileOutputStream(NgayonAraw.format(Araw)+"-UniqueReport.txt",true)); int downloadCount = UniqueCount + DuplicateCount; int yieldCount = downloadCount - (DuplicateCount + KnownInACL + totalProvenanceBreakdown); MyUniqueReport.println("Folder Name: , Total Download Files: , Total Unique Files:"); MyUniqueReport.println(downloadCount+yieldCount); MyUniqueReport.close(); } catch (FileNotFoundException exc) { JOptionPane.showMessageDialog(null,"File not Found."); } return null; } public String QueryACL(String myFolderLocation, String myDBUrl, String myUName, String myPWord, String mySiteInitial, String myOJTNumber) { try { File folder = new File(myFolderLocation); //Directory where the files are located File[] listOfFiles = folder.listFiles(); for (File listOfFile : listOfFiles) { //Enumerate the files inside the folder if (listOfFile.isFile()) { String myMD5 = getMD5Checksum(listOfFile.getPath()); URL uri = new URL("https://gacl.trendmicro.com/rs/level-0/files/isKnownGood/" + myMD5); URLConnection ec = uri.openConnection(); StringBuilder a; try (BufferedReader in = new BufferedReader(new InputStreamReader(ec.getInputStream(), "UTF-8"))) { String inputLine; a = new StringBuilder(); while ((inputLine = in.readLine()) != null) { a.append(inputLine); } } if (a.toString().isEmpty()) { //Result is UNKNOWN UnknownInACL++; //System.out.println(myMD5 + ", UNKNOWN IN GRID"); TAUnknownReport.append(listOfFile.toPath().getParent().getFileName()+","+listOfFile.getName() + "," + myMD5 + ", \n"); // DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // Date date = new Date(); // // try { // String myDriver = "com.mysql.jdbc.Driver"; // Class.forName(myDriver); // // Connection conn = DriverManager.getConnection(myDBUrl, myUName, myPWord); // Statement st = conn.createStatement(); // String query = "INSERT INTO TBL_MAIN " // + "(MD5, Checkin_Date, Site_Initial, OJT_Number)" // + "VALUES ('" + myMD5 + "', '" + dateFormat.format(date) + "', '" // + mySiteInitial + "', '" + myOJTNumber + "')"; // // st.executeUpdate(query); // st.close(); // // } catch (Exception e) { // System.err.println("Got an exception! "); // System.err.println(e.getMessage()); // } //WRITE IN LOG FILE // FileWriter FWrite = new FileWriter(sdf.format(date) + "-UnknownHashes.txt", true); // BufferedWriter out = new BufferedWriter(FWrite); // out.write(listOfFiles[i].getName() + " - " + myMD5 + "\r\n"); // out.close(); } else { //Result is KNOWN KnownInACL++; System.out.println(myMD5 + ", KNOWN"); File myFilePath = new File(listOfFile.getPath()); myFilePath.delete(); } } else if (listOfFile.isDirectory()) { System.out.println("Directory " + listOfFile.getName()); } } } catch (Exception e) { } return null; } public String Reporting(String myDBUrl, String myUName, String myPWord, String mySiteInitial, String myOJTNumber) { int downloadCount = UniqueCount + DuplicateCount; int yieldCount = downloadCount - (DuplicateCount + KnownInACL + totalProvenanceBreakdown); // TAUniqueCount.append("Total Downloaded Files: "+ downloadCount +"\nTotal Unique Files: "+UniqueCount+"\nTotal Unknown Files: "+yieldCount); return null; } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(GridManualProcess.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> /* Create and display the form */ // java.awt.EventQueue.invokeLater(() -> { // new GridManualProcess().setVisible(true); // }); java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { new GridManualProcess().setVisible(true); } }); } public static Date date = new Date(); public static SimpleDateFormat TodayDate = new SimpleDateFormat("yyyy-MM-dd"); public static Integer UniqueCount = 0, DuplicateCount = 0, KnownInACL = 0, UnknownInACL = 0; public static Integer totalProvenanceBreakdown = 0, total_provenance = 0; public static String siteInitial = ""; private javax.swing.JFileChooser FCFolderPath; // Variables declaration - do not modify private javax.swing.JButton BRunProgram; private javax.swing.JButton BSelectFolder; private javax.swing.JTextArea TADownloadedReport; private javax.swing.JTextArea TAUniqueCount; private javax.swing.JTextArea TAUniqueReport; private javax.swing.JTextArea TAUnknownReport; private javax.swing.JTextField TxtFilePath; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel6; private javax.swing.JMenu jMenu1; private javax.swing.JMenu jMenu2; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JScrollPane jScrollPane4; // End of variables declaration }