com.cisco.iwe.services.util.EmailMonitor.java Source code

Java tutorial

Introduction

Here is the source code for com.cisco.iwe.services.util.EmailMonitor.java

Source

/*****************************************************************************************************
 * ClassName    : EmailMonitor
 * Description  : Monitors the Inbox and reads the email, parses it and provide JSON response.
 * Known Bugs   : None
 * Modification Log
 * Date                 Author                      
 * -----------------------------------------
 * 16-Dec-2015          Lokesh Thadani (Accenture Limited)       
                 MyExpenses MailServerConnectivity
 ****************************************************************************************************/
package com.cisco.iwe.services.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Properties;
import java.util.Scanner;
import javax.mail.Authenticator;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeBodyPart;
import org.apache.log4j.Logger;
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
import org.json.JSONException;
import org.json.JSONObject;
import com.aspose.ocr.IRecognizedText;
import com.aspose.ocr.ImageStream;
import com.aspose.ocr.License;
import com.aspose.ocr.OcrEngine;
import com.cisco.iwe.services.util.ExceptionConstants;
import com.cisco.iwe.services.util.DataBaseUtil;

public class EmailMonitor {

    private static Logger logger;
    public static boolean monitorEmail = true;
    private static Properties prop = null;

    static {
        prop = new Properties();
        try {
            prop.load(EmailMonitor.class.getClassLoader().getResourceAsStream(EmailParseConstants.propertiesFile));
        } catch (IOException ioexception) {
            logger.error("Exception!! " + ioexception);
        }
        logger = Logger.getLogger(EmailMonitor.class);
    }

    /**This method returns the login credentials for MailServer Connectivity.**/

    private String[] getEmailCredens() {

        String[] mailCredens = new String[2];
        String emailId = null;
        String emailPwd = null;
        emailId = prop.getProperty(EmailParseConstants.eMailId);
        emailPwd = prop.getProperty(EmailParseConstants.eMailPwd);
        mailCredens[0] = emailId;
        mailCredens[1] = emailPwd;
        return mailCredens;
    }

    /**
     * This method returns the corresponding JSON response.'Success = true' in case the Mail contents get stored in the database successfully. 'Success = false' in case of any errors 
     **/

    public String monitorEmailAndLoadDB() {
        License license = new License();
        license.setLicense(EmailParseConstants.ocrLicenseFile);
        Store emailStore = null;
        Folder folder = null;
        Properties props = new Properties();
        logger.info("EmailMonitor monitorEmailAndLoadDB Enter (+)");
        // Setting session and Store information
        // MailServerConnectivity - get the email credentials based on the environment
        String[] mailCredens = getEmailCredens();
        final String username = mailCredens[0];
        final String password = mailCredens[1];
        logger.info("monitorEmailAndLoadDB : Email ID : " + username);

        try {
            logger.info("EmailMonitor.monitorEmailAndLoadDB get the mail server properties");
            props.put(EmailParseConstants.emailAuthKey, "true");
            props.put(EmailParseConstants.emailHostKey, prop.getProperty(EmailParseConstants.emailHost));
            props.put(EmailParseConstants.emailPortKey, prop.getProperty(EmailParseConstants.emailPort));
            props.put(EmailParseConstants.emailTlsKey, "true");

            logger.info("EmailMonitor.monitorEmailAndLoadDB create the session object with mail server properties");
            Session session = Session.getDefaultInstance(props, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });
            // Prod-MailServerConnectivity - create the POP3 store object and
            // connect with the pop server
            logger.info("monitorEmailAndLoadDB : create the POP3 store object");
            emailStore = (Store) session.getStore(prop.getProperty(EmailParseConstants.emailType));
            logger.info("monitorEmailAndLoadDB : Connecting to Store :" + emailStore.toString());
            emailStore.connect(prop.getProperty(EmailParseConstants.emailHost),
                    Integer.parseInt(prop.getProperty(EmailParseConstants.emailPort)), username, password);
            logger.info("monitorEmailAndLoadDB : Connection Status:" + emailStore.isConnected());

            // create the folder object
            folder = emailStore.getFolder(prop.getProperty(EmailParseConstants.emailFolder));
            // Check if Inbox exists
            if (!folder.exists()) {
                logger.error("monitorEmailAndLoadDB : No INBOX exists...");
                System.exit(0);
            }
            // Open inbox and read messages
            logger.info("monitorEmailAndLoadDB : Connected to Folder");
            folder.open(Folder.READ_WRITE);

            // retrieve the messages from the folder in an array and process it
            Message[] msgArr = folder.getMessages();
            // Read each message and delete the same once data is stored in DB
            logger.info("monitorEmailAndLoadDB : Message length::::" + msgArr.length);

            SimpleDateFormat sdf2 = new SimpleDateFormat(EmailParseConstants.dateFormat);

            Date sent = null;
            String emailContent = null;
            String contentType = null;
            // for (int i = 0; i < msg.length; i++) {
            for (int i = msgArr.length - 1; i > msgArr.length - 2; i--) {
                Message message = msgArr[i];
                if (!message.isSet(Flags.Flag.SEEN)) {
                    try {
                        sent = msgArr[i].getSentDate();
                        contentType = message.getContentType();
                        String fileType = null;
                        byte[] byteArr = null;
                        String validAttachments = EmailParseConstants.validAttachmentTypes;
                        if (contentType.contains("multipart")) {
                            Multipart multiPart = (Multipart) message.getContent();
                            int numberOfParts = multiPart.getCount();
                            for (int partCount = 0; partCount < numberOfParts; partCount++) {
                                MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
                                InputStream inStream = (InputStream) part.getInputStream();
                                ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                                int nRead;
                                byte[] data = new byte[16384];
                                while ((nRead = inStream.read(data, 0, data.length)) != -1) {
                                    buffer.write(data, 0, nRead);
                                }
                                buffer.flush();
                                byteArr = buffer.toByteArray();
                                if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
                                    fileType = part.getFileName().substring(part.getFileName().lastIndexOf("."),
                                            part.getFileName().length());
                                    String fileDir = part.getFileName();
                                    if (validAttachments.contains(fileType)) {
                                        part.saveFile(fileDir);
                                        saveAttachmentAndText(message.getFrom()[0].toString(), message.getSubject(),
                                                byteArr, emailContent.getBytes(), fileType, sent,
                                                fileType.equalsIgnoreCase(".PDF") ? scanPDF(fileDir)
                                                        : scanImage(fileDir).toString());
                                        deleteFile(fileDir);
                                    } else {
                                        sendNotification();
                                    }

                                } else {
                                    // this part may be the message content
                                    emailContent = part.getContent().toString();
                                }
                            }
                        } else if (contentType.contains("text/plain") || contentType.contains("text/html")) {
                            Object content = message.getContent();
                            if (content != null) {
                                emailContent = content.toString();
                            }
                        }
                        message.setFlag(Flags.Flag.DELETED, false);
                        logger.info(
                                "monitorEmailAndLoadDB : loadSuccess : Mail Parsed for : " + message.getSubject());
                        logger.info("monitorEmailAndLoadDB : loadSuccess : Created at : " + sdf2.format(sent));
                        logger.info("Message deleted");
                    } catch (IOException e) {
                        logger.error("IO Exception in email monitoring: " + e);
                        logger.error(
                                "IO Exception in email monitoring message: " + Arrays.toString(e.getStackTrace()));
                    } catch (SQLException sexp) {
                        logger.error("SQLException Occurred GetSpogDetails-db2 :", sexp);
                        buildErrorJson(ExceptionConstants.sqlErrCode, ExceptionConstants.sqlErrMsg);
                    } catch (Exception e) {
                        logger.error("Unknown Exception in email monitoring: " + e);
                        logger.error("Unknown Exception in email monitoring message: "
                                + Arrays.toString(e.getStackTrace()));
                    }
                }
            }

            // Close folder and store
            folder.close(true);
            emailStore.close();

        } catch (NoSuchProviderException e) {
            logger.error("monitorEmailAndLoadDB : NoSuchProviderException in email monitoring: " + e);
            logger.error("monitorEmailAndLoadDB : NoSuchProviderException in email monitoring message: "
                    + Arrays.toString(e.getStackTrace()));
        } catch (MessagingException e) {
            logger.error("monitorEmailAndLoadDB : MessagingException in email monitoring: " + e);
            logger.error("monitorEmailAndLoadDB : MessagingException in email monitoring message: "
                    + Arrays.toString(e.getStackTrace()));
        } finally {
            if (folder != null && folder.isOpen()) {
                // Close folder and store
                try {
                    folder.close(true);
                    emailStore.close();
                } catch (MessagingException e) {
                    logger.error("monitorEmailAndLoadDB : MessagingException in email monitoring: " + e);
                    logger.error("monitorEmailAndLoadDB : MessagingException in email monitoring: "
                            + Arrays.toString(e.getStackTrace()));
                }
            }
        }
        logger.info("EmailMonitor monitorEmailAndLoadDB Exit (-)");
        return buildSuccessJson().toString();
    }

    /**
     * 
     * @param from
     * @param subject
     * @param mailAttachment
     * @param MailText
     * @param fileType
     * @param sent
     * @throws Exception
     */
    /** This method saves the email contents in the database  **/
    public static void saveAttachmentAndText(String from, String subject, byte[] mailAttachment, byte[] MailText,
            String fileType, Date sent, String pdfText) throws Exception {

        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            String query = EmailParseConstants.saveQuery;
            conn = DataBaseUtil.getDevConnection();
            // DataBaseUtil.getConnection(jndiName+"_"+System.getProperty("cisco.life"));
            conn.setAutoCommit(false);
            stmt = conn.prepareStatement(query);
            stmt.setString(1, from);
            stmt.setString(2, subject);
            stmt.setBinaryStream(3, new ByteArrayInputStream(mailAttachment), mailAttachment.length);
            stmt.setBinaryStream(4, new ByteArrayInputStream(MailText), MailText.length);
            stmt.setString(5, fileType);
            stmt.setTimestamp(6, new Timestamp(sent.getTime()));
            stmt.executeUpdate();
        } finally {
            try {
                if (stmt != null) {
                }
            } finally {
                if (conn != null) {
                    conn.close();
                }
            }
        }

    }

    /**
     * @param is
     * @return
     */
    public static String convertStreamToString(InputStream is) {
        return new Scanner(is).useDelimiter("\\A").next();
    }

    /**
     * 
     * @param errorCode
     * @param errorMessage
     * @return
     */
    private String buildErrorJson(String errorCode, String errorMessage) {
        JSONObject responseJson = new JSONObject();
        try {
            responseJson.put("success", false);
            responseJson.put("errorCode", errorCode);
            responseJson.put("errorMessage", errorMessage);
        } catch (JSONException exp) {
            // logger.error("ERROR While Building the Error Json"+exp.getMessage());
        }
        return responseJson.toString();
    }

    /**
     * 
     * @return
     */
    private JSONObject buildSuccessJson() {
        JSONObject responseJson = new JSONObject();
        try {
            responseJson.put("success", true);
        } catch (JSONException exp) {
            logger.error("ERROR While Building the Success Json" + exp.getMessage());
        }
        return responseJson;
    }

    /**
     * 
     * @throws Exception
     */
    /* This sends notification to the user via mail in case the user has uploaded an expense receipt type apart from .jpg,.jpeg,.bmp,.png,.txt,.doc,.pdf formats. */
    public void sendNotification() throws Exception {
        CallableStatement stmt = null;
        Connection conn = null;
        try {
            conn = DataBaseUtil.getDevConnection();
            stmt = conn.prepareCall(EmailParseConstants.sendNotificationQuery);
            stmt.setString(1, EmailParseConstants.genericMailAccount);
            stmt.setString(2, EmailParseConstants.emailHostName);
            stmt.setString(3, EmailParseConstants.mailPortNumber);
            stmt.executeUpdate();
        } finally {
            try {
                if (stmt != null) {
                }
            } finally {
                if (conn != null) {
                    conn.close();
                }
            }
        }
    }

    /**
     * 
     * @param fileDir
     * @return
     */
    /* This method is used to scan the uploaded expense receipt in .pdf format and extract the text embedded in it. */
    public String scanPDF(String fileDir) {
        PDFParser parser;
        String parsedText = null;
        PDFTextStripper pdfStripper = null;
        PDDocument pdDoc = null;
        COSDocument cosDoc = null;
        File file = new File(fileDir);
        if (!file.isFile()) {
            System.err.println("File " + fileDir + " does not exist.");
            return null;
        }
        try {
            parser = new PDFParser(new FileInputStream(file));
        } catch (IOException e) {
            System.err.println("Unable to open PDF Parser. " + e.getMessage());
            return null;
        }
        try {
            parser.parse();
            cosDoc = parser.getDocument();
            pdfStripper = new PDFTextStripper();
            pdDoc = new PDDocument(cosDoc);
            pdfStripper.setStartPage(1);
            pdfStripper.setEndPage(pdDoc.getNumberOfPages());
            parsedText = pdfStripper.getText(pdDoc);
        } catch (Exception e) {
            System.err.println("An exception occured in parsing the PDF Document." + e.getMessage());
        } finally {
            try {
                if (cosDoc != null)
                    cosDoc.close();
                if (pdDoc != null)
                    pdDoc.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return parsedText;
    }

    /**
     * 
     * @param fileDir
     * @return
     */
    public IRecognizedText scanImage(String fileDir) {
        // String imagePath = "C:\\Sampleocr - Copy.bmp";

        // Create an instance of OcrEngine
        OcrEngine ocr = new OcrEngine();

        // Set image file
        ocr.setImage(ImageStream.fromFile(fileDir));

        // Perform OCR and get extracted text
        try {
            if (ocr.process()) {
                System.out.println("\rImage text -> " + ocr.getText());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ocr.getText();
    }

    /**
     * 
     * @param fileDir
     */
    public void deleteFile(String fileDir) {
        try {
            File file = new File(fileDir);
            if (file.delete()) {
                System.out.println(file.getName() + " is deleted!");
            } else {
                System.out.println("Delete operation is failed.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 
     * @param args
     */
    public static void main(String[] args) {

        EmailMonitor obj = new EmailMonitor();
        String response = obj.monitorEmailAndLoadDB();
        System.out.println("response ==" + response);
    }

}