001: package org.claros.mini.controllers;
002:
003: import java.io.ByteArrayOutputStream;
004: import java.io.ObjectOutputStream;
005: import java.util.ArrayList;
006: import java.util.Locale;
007:
008: import javax.mail.Folder;
009: import javax.mail.Message;
010:
011: import org.apache.commons.codec.binary.Hex;
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014: import org.claros.commons.mail.models.ConnectionMetaHandler;
015: import org.claros.commons.mail.models.ConnectionProfile;
016: import org.claros.commons.mail.models.EmailHeader;
017: import org.claros.commons.mail.protocols.Protocol;
018: import org.claros.commons.mail.protocols.ProtocolFactory;
019: import org.claros.commons.models.AuthProfile;
020: import org.claros.mini.factory.FolderControllerFactory;
021: import org.claros.mini.factory.MailControllerFactory;
022: import org.claros.mini.models.EmailDbItem;
023: import org.claros.mini.models.FolderDbItem;
024: import org.claros.mini.utility.UserPrefConstants;
025: import org.hsqldb.lib.MD5;
026:
027: /**
028: * @author Umut Gokbayrak
029: */
030: public class DbInboxControllerImpl extends InboxControllerBase
031: implements InboxController {
032: private static Log log = LogFactory
033: .getLog(DbInboxControllerImpl.class);
034: private static Locale loc = new Locale("en", "US");
035:
036: /**
037: * @param profile
038: * @param auth
039: * @param handler
040: */
041: public DbInboxControllerImpl(AuthProfile auth,
042: ConnectionProfile profile, ConnectionMetaHandler handler) {
043: super (auth, profile, handler);
044: }
045:
046: /* (non-Javadoc)
047: * @see org.claros.groupware.webmail.controllers.InboxController#checkEmail()
048: */
049: public ConnectionMetaHandler checkEmail() throws Exception {
050: ProtocolFactory factory = new ProtocolFactory(profile, auth,
051: handler);
052: Protocol protocol = factory.getProtocol(null);
053: try {
054: // fetch all messages from the remote pop3 server
055: protocol.disconnect();
056: handler = protocol
057: .connect(org.claros.commons.mail.utility.Constants.CONNECTION_READ_WRITE);
058:
059: ArrayList headers = protocol.fetchAllHeaders();
060: ArrayList toBeDeleted = new ArrayList();
061: if (headers != null) {
062: EmailHeader header = null;
063: for (int i = 0; i < headers.size(); i++) {
064: header = (EmailHeader) headers.get(i);
065: int msgId = header.getMessageId();
066: try {
067: ByteArrayOutputStream bos = new ByteArrayOutputStream();
068: ObjectOutputStream os = new ObjectOutputStream(
069: bos);
070: os.writeObject(header);
071: byte bHeader[] = bos.toByteArray();
072: String md5Header = new String(Hex.encodeHex(MD5
073: .digestBytes(bHeader)))
074: .toUpperCase(loc);
075: // String md5Header = MD5.getHashString(bHeader);
076:
077: MailControllerFactory mailFact = new MailControllerFactory(
078: auth, profile, handler, null);
079: MailController mailCont = mailFact
080: .getMailController();
081: DbMailControllerImpl dbMailCont = (DbMailControllerImpl) mailCont;
082: if (!dbMailCont.mailAlreadyFetched(md5Header)) {
083: Message msg = protocol.getMessage(msgId);
084: if (!msg.getFolder().isOpen()) {
085: msg.getFolder().open(Folder.READ_ONLY);
086: }
087:
088: // find the destionation folderId for the message
089: FolderControllerFactory fcFact = new FolderControllerFactory(
090: auth, profile, handler);
091: FolderController fc = fcFact
092: .getFolderController();
093: FolderDbItem fi = fc.getInboxFolder();
094: String folderId = fi.getId().toString();
095:
096: // if message should be directly deleted it shouldn't be
097: // stored in DB.
098: if (folderId != null) {
099: // create a byte array from the message content.
100: bos = new ByteArrayOutputStream();
101: msg.writeTo(bos);
102: byte bMsg[] = bos.toByteArray();
103:
104: // serialize the message byte array
105: os = new ObjectOutputStream(bos);
106: os.writeObject(bMsg);
107:
108: // create an email db item
109: EmailDbItem item = new EmailDbItem();
110: item.setEmail(bos.toByteArray());
111: item.setUniqueId(md5Header);
112: item.setFolderId(new Long(folderId));
113: item.setUnread(new Boolean(true));
114: item.setUser(auth.getUsername());
115: item.setSize(new Long(bMsg.length));
116:
117: // save the email db item.
118: mailCont.appendEmail(item);
119: }
120: toBeDeleted.add(new Integer(msgId));
121: }
122: } catch (Exception e) {
123: toBeDeleted.add(new Integer(msgId));
124: log.error("Error while processing mail.", e);
125: }
126: }
127: }
128:
129: // fetched messages are deleted if the user requested so.
130: String deleteFetched = UserPrefsController.getUserSetting(
131: auth, UserPrefConstants.deleteFetched);
132: if (deleteFetched != null && deleteFetched.equals("yes")) {
133: if (toBeDeleted.size() > 0) {
134: int ids[] = new int[toBeDeleted.size()];
135: for (int i = 0; i < toBeDeleted.size(); i++) {
136: Integer id = (Integer) toBeDeleted.get(i);
137: ids[i] = id.intValue();
138: }
139: protocol.deleteMessages(ids);
140: }
141: }
142: } finally {
143: protocol.disconnect();
144: }
145: return handler;
146: }
147:
148: }
|