001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: DatabaseMailQueue.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.mail.dam;
009:
010: import com.uwyn.rife.database.Datasource;
011: import com.uwyn.rife.database.DbQueryManager;
012: import com.uwyn.rife.database.exceptions.DatabaseException;
013: import com.uwyn.rife.database.querymanagers.generic.GenericQueryManager;
014: import com.uwyn.rife.database.querymanagers.generic.GenericQueryManagerFactory;
015: import com.uwyn.rife.mail.Email;
016: import com.uwyn.rife.mail.MailQueueManager;
017: import com.uwyn.rife.mail.exceptions.InstallMailQueueErrorException;
018: import com.uwyn.rife.mail.exceptions.MailQueueManagerException;
019: import com.uwyn.rife.mail.exceptions.RemoveMailQueueErrorException;
020: import com.uwyn.rife.site.ConstrainedBean;
021: import com.uwyn.rife.tools.Base64;
022: import java.io.ByteArrayOutputStream;
023: import java.io.IOException;
024: import javax.mail.MessagingException;
025: import javax.mail.internet.MimeMessage;
026:
027: public class DatabaseMailQueue extends DbQueryManager implements
028: MailQueueManager {
029: protected GenericQueryManager<Email> mManager = null;
030:
031: protected DatabaseMailQueue(Datasource datasource) {
032: super (datasource);
033:
034: mManager = GenericQueryManagerFactory.getInstance(datasource,
035: Email.class);
036: }
037:
038: public boolean install() throws MailQueueManagerException {
039: try {
040: mManager.install();
041: } catch (DatabaseException e) {
042: throw new InstallMailQueueErrorException(e);
043: }
044:
045: return true;
046: }
047:
048: public boolean remove() throws MailQueueManagerException {
049: try {
050: mManager.remove();
051: } catch (DatabaseException e) {
052: throw new RemoveMailQueueErrorException(e);
053: }
054:
055: return true;
056: }
057:
058: public boolean queue(Email email) throws MailQueueManagerException {
059: if (null == email) {
060: return false;
061: }
062:
063: // store the email in the database if it's not already in the queue
064: email.addConstraint(new ConstrainedBean().unique("fromAddress",
065: "toAddresses", "ccAddresses", "bccAddresses",
066: "subject", "body"));
067:
068: if (!email.validate(mManager)) {
069: return false;
070: }
071:
072: try {
073: mManager.save(email);
074: } catch (DatabaseException e) {
075: throw new MailQueueManagerException(e);
076: }
077:
078: return true;
079: }
080:
081: public boolean queue(MimeMessage email)
082: throws MailQueueManagerException {
083: if (null == email) {
084: return false;
085: }
086:
087: // converting the email to a base-64 encoded string
088: ByteArrayOutputStream out = new ByteArrayOutputStream();
089: try {
090: email.writeTo(out);
091: } catch (MessagingException e) {
092: throw new MailQueueManagerException(e);
093: } catch (IOException e) {
094: throw new MailQueueManagerException(e);
095: }
096: String body = Base64.encodeToString(out.toByteArray(), false);
097:
098: // construct a temporary email message with the encoded javamail message
099: Email tmp_email = new Email().from(MimeMessage.class.getName())
100: .to(MimeMessage.class.getName()).subject(
101: MimeMessage.class.getName()).body(body);
102:
103: // store the email in the database if it's not already in the queue
104: tmp_email.addConstraint(new ConstrainedBean().unique(
105: "fromAddress", "toAddresses", "subject", "body"));
106:
107: if (!tmp_email.validate(mManager)) {
108: return false;
109: }
110:
111: try {
112: mManager.save(tmp_email);
113: } catch (DatabaseException e) {
114: throw new MailQueueManagerException(e);
115: }
116:
117: return true;
118: }
119: }
|