001    package org.ujmp.mail;
002    
003    import java.io.IOException;
004    import java.util.Enumeration;
005    
006    import javax.mail.Address;
007    import javax.mail.Flags;
008    import javax.mail.Message;
009    import javax.mail.MessagingException;
010    
011    import org.ujmp.core.Matrix;
012    import org.ujmp.core.listmatrix.DefaultListMatrix;
013    import org.ujmp.core.listmatrix.ListMatrix;
014    import org.ujmp.core.mapmatrix.DefaultMapMatrix;
015    
016    public class MessageMatrix extends DefaultMapMatrix<Object, Object> {
017            private static final long serialVersionUID = 4973660519646290182L;
018    
019            public MessageMatrix(Message m) throws IOException, MessagingException {
020                    put("Content", m.getContent());
021                    put("ContentType", m.getContentType());
022                    put("Description", m.getDescription());
023                    put("Disposition", m.getDisposition());
024                    put("FileName", m.getFileName());
025                    put("SentDate", m.getSentDate());
026                    put("Subject", m.getSubject());
027                    put("Folder", m.getFolder());
028                    put("ReceivedDate", m.getReceivedDate());
029    
030                    Flags flags = m.getFlags();
031                    Matrix flagMatrix = new DefaultListMatrix<String>(flags.getUserFlags());
032                    put("Flags", flagMatrix);
033    
034                    Enumeration<?> headers = m.getAllHeaders();
035                    ListMatrix<Object> headerMatrix = new DefaultListMatrix<Object>();
036                    while (headers.hasMoreElements()) {
037                            headerMatrix.add(headers.nextElement());
038                    }
039                    put("AllHeaders", headerMatrix);
040    
041                    Address[] recipients = m.getAllRecipients();
042                    ListMatrix<String> recipientsMatrix = new DefaultListMatrix<String>();
043                    for (Address a : recipients) {
044                            recipientsMatrix.add("" + a);
045                    }
046                    put("AllRecipients", recipientsMatrix);
047    
048                    Address[] from = m.getFrom();
049                    ListMatrix<String> fromMatrix = new DefaultListMatrix<String>();
050                    for (Address a : from) {
051                            fromMatrix.add("" + a);
052                    }
053                    put("From", fromMatrix);
054    
055                    Address[] replyTo = m.getReplyTo();
056                    ListMatrix<String> replyToMatrix = new DefaultListMatrix<String>();
057                    for (Address a : replyTo) {
058                            replyToMatrix.add("" + a);
059                    }
060                    put("ReplyTo", replyToMatrix);
061    
062            }
063    }