emailportlet.java

来自「jetspeed源代码」· Java 代码 · 共 1,760 行 · 第 1/5 页

JAVA
1,760
字号
                        .valueOf(msgeIndexnum));

            } else
            {

                //check if msgeIndex is > than the actual message size
                //if it is, reset msgeIndex
                try
                {
                    int msgIndex = Integer.parseInt(msgeIndexstring);
                    if (msgIndex > msgeIndexnum)
                    {
                        setPortletSession(data, context, "msgeIndex", null);
                    }

                } catch (NumberFormatException ne)
                {
                    setPortletSession(data, context, "msgeIndex", String
                            .valueOf(msgeIndexnum));
                }

            }

            boolean withAttachment = true;
            for (int i = 0; i < vAscmessages.size(); i++)
            {
                Hashtable ht = (Hashtable) vAscmessages.get(i);
                String s = (String) ht.get("hasAttachment");
                Message message = (Message) ht.get("message");

                if (s.equals(""))
                {
                    withAttachment = false;
                } else
                    withAttachment = true;

                DBInsert(data, message, withAttachment);

            }
            
            descendingOrder(data, context, vAscmessages, start_index,
                    range_per_page, msgectr);

        } catch (NoSuchProviderException np)
        {
            log.error("Please check email paramters... Protocol(imap/pop3) is case-sensitive.");
            log.error(np);
        } catch (MessagingException ms)
        {
            log.error(ms);
        } catch (IOException io)
        {
            log.error(io);
        }
    }

    /*-----------------------------------------------------------------------------------------*
     *  Retrieves message id from message : RJPY
     *-----------------------------------------------------------------------------------------*/
    public String getMessageId(Message message)
    {
        String messageid = "";
        try
        {
            // Retrieve message id from headers
            Enumeration e = message.getAllHeaders();
            while (messageid.equals(""))
            {
                Header header = (Header) e.nextElement();
                if (header.getName().equals("Message-ID"))
                {
                    messageid = header.getValue();
                }
            }
        } catch (Exception e)
        {

            log.error("Error in getMessageId()", e);
        }
        return messageid;
        // end Retrieve
    }

    /*-----------------------------------------------------------------------------------------*
     *  Inserts message and attachment detail to database : RJPY
     *-----------------------------------------------------------------------------------------*/

    public void DBInsert(RunData data, Message message, boolean withAttachment)
    {
        try
        {
            log.info("[RJPY] Trying to write into DB...");

            String messageid = getMessageId(message);
            Criteria crit = new Criteria();
            crit.add(EmailInboxPeer.MESSAGE_ID, messageid);
            //Vector vEmail = EmailInboxPeer.doSelect(crit);
            List vEmail = EmailInboxPeer.doSelect(crit); //tdk2.2 version
            if (vEmail.size() == 0)
            {

                log.info("[RJPY] This email is not yet in the DB...");
                String filename = "";
                InputStream is = null;
                //PreparedStatement ps = con.prepareStatement(sql2);

                int size = 0;

                // Get Attachment
                if (withAttachment)
                {

                    Object obj = message.getContent();
                    Multipart mpart = (Multipart) obj;

                    for (int j = 0, n = mpart.getCount(); j < n; j++)
                    {
                        Part part = mpart.getBodyPart(j);
                        String disposition = part.getDisposition();
                        if ((disposition != null)
                                && ((disposition
                                        .equalsIgnoreCase(Part.ATTACHMENT)) || (disposition
                                        .equals(Part.INLINE))))
                        {
                            if (part.getFileName() != null)
                            {
                                log.info("*** Attachment name: "
                                        + part.getFileName());
                                if (part.getContent() instanceof String)
                                {
                                    byte[] b = ((String) part.getContent())
                                            .getBytes();
                                    is = new ByteArrayInputStream(b);
                                } else
                                {
                                    is = part.getInputStream();
                                }
                                filename = part.getFileName();
                                size = part.getSize();
                            }
                        }
                    } //for
                } // if with attachment

                if (is != null)
                {
                    // ps.executeUpdate();
                    log.info("sure");
                    EmailInbox emailInbox = new EmailInbox();
                    emailInbox.setMessageId(messageid);
                    emailInbox.setReadflag(0);
                    emailInbox.setFilename(filename);

                    byte[] b = new byte[size];
                    int bytes = is.read(b);
                    while (bytes != -1)
                    {
                        bytes = is.read(b);
                    }
                    emailInbox.setAttachment(b);
                    EmailInboxPeer.doInsert(emailInbox);
                } else
                {
                    //EmailInboxPeer.executeStatement(sql);
                    EmailInbox emailInbox = new EmailInbox();
                    emailInbox.setMessageId(messageid);
                    emailInbox.setReadflag(0);
                    EmailInboxPeer.doInsert(emailInbox);
                }
                //ps.close();
            } //if email doesnt exist

        } catch (Exception ex)
        {

            log.error("Error in DBInsert()", ex);

        }

    }

    // recent messages dislayed on top
    public void descendingOrder(RunData data, Context context,
            Vector inMessages, int start_index, int range_per_page, int msgectr)
    {
        Vector vDescending = new Vector();
        int msgSize = inMessages.size();
        String msgeIndex1 = (String)getPortletSession(data, context, "msgeIndex");
        int msgIndex = 0;
        try
        {
            //check if msgeIndex is > than the actual message size
            //if it is, reset msgeIndex
            msgIndex = Integer.parseInt(msgeIndex1);
            if (msgIndex > msgSize)
            {
                setPortletSession(data, context, "msgeIndex", null);
            }

        } catch (NumberFormatException ne)
        {
            msgIndex = msgSize;
            setPortletSession(data, context, "msgeIndex", String
                    .valueOf(msgSize));
        }

        //display 10 messages per page (descending order)
        for (int j = start_index; ((j <= range_per_page) && (j < msgectr)); j++)
        {
            vDescending.add(inMessages.get(msgIndex - 1));
            msgIndex--;
        }
        context.put("inMessages", vDescending);

    }

    //check if message is to be move to a new line
    public String convertMessage(String msg) throws Exception
    {
        log.info("convert message");
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < msg.length() - 1; i++)
        {
            char c = msg.charAt(i);
            if (c == '\n')
            {
                if (((msg.charAt(i + 1)) != '<') && (msg.charAt(i + 1) != ' '))
                {
                    log.info("new line");
                    sb.append("<br>");
                } else
                {
                    sb.append(c);
                }
            } else
            {
                sb.append(c);
            }
        }
        String returnString = sb.toString();
        return returnString;
    }

    // exclude displaying html tags in replying or forwarding message
    // there are still instances that html tags may be included/displayed in
    // reply/forward message.
    // only those tags below were checked... other than those, tags may be
    // displayed in some cases.

    public String checkFormat(String msg) throws Exception
    {
        log.info("check format ");
        int startIndex = 0;
        int end = 0;

        String testSubstring = null;

        for (int i = 0; i < msg.length() - 6; i++)
        {
            String sub = msg.substring(i, i + 2);
            if (sub.equals("<A"))
            {
                startIndex = i;
                end = msg.indexOf(">", startIndex);
                StringBuffer sb = new StringBuffer(msg);
                sb.replace(startIndex, end + 1, " ");
                msg = sb.toString();
            }
        }

        for (int i = 0; i < msg.length() - 7; i++)
        {
            String sub = msg.substring(i, i + 7);
            if (sub.equals("style=\""))
            {
                startIndex = i;
                end = msg.indexOf(">", startIndex);
                StringBuffer sb = new StringBuffer(msg);
                sb.replace(startIndex, end + 1, " ");
                msg = sb.toString();
            }
        }

        for (int j = 0; j < msg.length() - 6; j++)
        {
            String sub = msg.substring(j, j + 6);
            if (sub.equals("href=\""))
            {
                startIndex = j;
                end = msg.indexOf(">", startIndex);
                StringBuffer sb = new StringBuffer(msg);
                sb.replace(startIndex, end + 1, " ");
                msg = sb.toString();
            }
        }
        return msg;
    }

    //set flag as OLD
    public void setMessageflag(Message message) throws Exception
    {
        log.info("set message flag");

        String msgeId = getMessageId(message);

        Criteria cr = new Criteria();
        cr.add(EmailInboxPeer.MESSAGE_ID, msgeId);

        //Vector vMsge = EmailInboxPeer.doSelect(cr);
        List vMsge = EmailInboxPeer.doSelect(cr); //tdk2.2 version
        EmailInbox eMsge = (EmailInbox) vMsge.get(0);
        eMsge.setReadflag(1);
        EmailInboxPeer.doUpdate(eMsge);
    }

    public void doNext(RunData data, Context context) throws Exception
    {

        //view next page
        log.info("doNext");
        String start_index1 = (String)getPortletSession(data, context, "start_index");

        String range_per_page1 = (String)getPortletSession(data, context,
                "range_per_page");
        String cur_page1 = (String)getPortletSession(data, context, "cur_page");
        String msgeIndex1 = (String)getPortletSession(data, context, "msgeIndex");

        Hashtable userInfo = this.getEmailUserInfo(data, context);
        String user = (String) userInfo.get("username");
        String pass = (String) userInfo.get("password");

        int start_index = Integer.parseInt(start_index1);
        int range_per_page = Integer.parseInt(range_per_page1);
        int cur_page = Integer.parseInt(cur_page1);
        int msgeIndex = Integer.parseInt(msgeIndex1);

        start_index = (range_per_page + 1);
        range_per_page = (range_per_page + maxPerPage);
        msgeIndex = msgeIndex - 10;
        log.info("msgeIndex in doNext " + msgeIndex);
        setPortletSession(data, context, "start_index", String
                .valueOf(start_index));

        setPortletSession(data, context, "range_per_page", String
                .valueOf(range_per_page));
        setPortletSession(data, context, "msgeIndex", String
                .valueOf(msgeIndex));
        cur_page = cur_page + 1;
        setPortletSession(data, context, "cur_page", String.valueOf(cur_page));

        setPortletSession(data, context, "inboxMessages", "yes");

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?