0001: /*
0002: Copyright (C) 2004 Know Gate S.L. All rights reserved.
0003: C/Oņa, 107 1š2 28050 Madrid (Spain)
0004:
0005: Redistribution and use in source and binary forms, with or without
0006: modification, are permitted provided that the following conditions
0007: are met:
0008:
0009: 1. Redistributions of source code must retain the above copyright
0010: notice, this list of conditions and the following disclaimer.
0011:
0012: 2. The end-user documentation included with the redistribution,
0013: if any, must include the following acknowledgment:
0014: "This product includes software parts from hipergate
0015: (http://www.hipergate.org/)."
0016: Alternately, this acknowledgment may appear in the software itself,
0017: if and wherever such third-party acknowledgments normally appear.
0018:
0019: 3. The name hipergate must not be used to endorse or promote products
0020: derived from this software without prior written permission.
0021: Products derived from this software may not be called hipergate,
0022: nor may hipergate appear in their name, without prior written
0023: permission.
0024:
0025: This library is distributed in the hope that it will be useful,
0026: but WITHOUT ANY WARRANTY; without even the implied warranty of
0027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
0028:
0029: You should have received a copy of hipergate License with this code;
0030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
0031: */
0032:
0033: package com.knowgate.hipermail;
0034:
0035: import com.knowgate.debug.DebugFile;
0036: import com.knowgate.dataobjs.DB;
0037: import com.knowgate.dataobjs.DBBind;
0038: import com.knowgate.dataobjs.DBSubset;
0039: import com.knowgate.jdc.JDCConnection;
0040: import com.knowgate.misc.Gadgets;
0041: import com.knowgate.misc.MD5;
0042: import com.knowgate.dfs.StreamPipe;
0043: import com.knowgate.dfs.ByteArrayDataSource;
0044:
0045: import java.io.File;
0046: import java.io.StringBufferInputStream;
0047: import java.io.IOException;
0048: import java.io.InputStream;
0049: import java.io.OutputStream;
0050: import java.io.ByteArrayInputStream;
0051: import java.io.ByteArrayOutputStream;
0052: import java.io.UnsupportedEncodingException;
0053:
0054: import java.math.BigDecimal;
0055:
0056: import java.sql.Blob;
0057: import java.sql.ResultSet;
0058: import java.sql.CallableStatement;
0059: import java.sql.PreparedStatement;
0060: import java.sql.Statement;
0061: import java.sql.SQLException;
0062: import java.sql.Timestamp;
0063: import java.sql.Types;
0064:
0065: import java.util.Date;
0066: import java.util.Properties;
0067: import java.util.Enumeration;
0068: import java.util.LinkedList;
0069: import java.util.HashMap;
0070: import java.util.Iterator;
0071:
0072: import java.math.BigDecimal;
0073:
0074: import java.text.SimpleDateFormat;
0075:
0076: import java.net.URL;
0077:
0078: import javax.activation.DataHandler;
0079: import javax.activation.FileDataSource;
0080:
0081: import javax.mail.BodyPart;
0082: import javax.mail.Address;
0083: import javax.mail.Part;
0084: import javax.mail.Message;
0085: import javax.mail.Flags;
0086: import javax.mail.Folder;
0087: import javax.mail.Multipart;
0088: import javax.mail.Session;
0089: import javax.mail.internet.AddressException;
0090: import javax.mail.internet.InternetAddress;
0091: import javax.mail.internet.MimeMessage;
0092: import javax.mail.internet.MimeBodyPart;
0093: import javax.mail.internet.InternetAddress;
0094: import javax.mail.internet.ParseException;
0095: import javax.mail.internet.MimePart;
0096: import javax.mail.internet.MimeMultipart;
0097: import javax.mail.MessagingException;
0098: import javax.mail.FolderClosedException;
0099:
0100: import com.sun.mail.smtp.SMTPMessage;
0101:
0102: import org.htmlparser.Parser;
0103: import org.htmlparser.util.NodeList;
0104: import org.htmlparser.util.NodeIterator;
0105: import org.htmlparser.util.ParserException;
0106: import org.htmlparser.tags.ImageTag;
0107: import org.htmlparser.beans.StringBean;
0108: import org.htmlparser.filters.TagNameFilter;
0109:
0110: import org.apache.oro.text.regex.Pattern;
0111: import org.apache.oro.text.regex.PatternMatcher;
0112: import org.apache.oro.text.regex.PatternCompiler;
0113: import org.apache.oro.text.regex.StringSubstitution;
0114: import org.apache.oro.text.regex.Perl5Matcher;
0115: import org.apache.oro.text.regex.Perl5Compiler;
0116: import org.apache.oro.text.regex.MalformedPatternException;
0117: import org.apache.oro.text.regex.Util;
0118:
0119: import javax.mail.internet.MimeUtility;
0120:
0121: /**
0122: * MIME messages stored at database BLOB columns or MBOX files
0123: * @author Sergio Montoro Ten
0124: * @version 2.2
0125: */
0126:
0127: public class DBMimeMessage extends MimeMessage implements MimePart,
0128: Part {
0129:
0130: private String sGuid;
0131: private Folder oFolder;
0132: private Address[] aAddrs;
0133: private HashMap oHeaders;
0134:
0135: private static PatternMatcher oMatcher = new Perl5Matcher();
0136: private static PatternCompiler oCompiler = new Perl5Compiler();
0137:
0138: /**
0139: * Create an empty message
0140: * @param oMailSession
0141: */
0142:
0143: public DBMimeMessage(Session oMailSession) {
0144: super (oMailSession);
0145: sGuid = null;
0146: oFolder = null;
0147: oHeaders = null;
0148: }
0149:
0150: /**
0151: * Create DBMimeMessage from a MimeMessage and assign a new GUID
0152: * @param oMsg MimeMessage
0153: * @throws MessagingException
0154: */
0155: public DBMimeMessage(MimeMessage oMsg) throws MessagingException {
0156: super (oMsg);
0157: sGuid = Gadgets.generateUUID();
0158: oHeaders = null;
0159: }
0160:
0161: /**
0162: * Create DBMimeMessage from an InputStream and assign a new GUID
0163: * @param oMailSession Session
0164: * @param oInStrm InputStream
0165: * @throws MessagingException
0166: */
0167: public DBMimeMessage(Session oMailSession, InputStream oInStrm)
0168: throws MessagingException {
0169: super (oMailSession, oInStrm);
0170: sGuid = Gadgets.generateUUID();
0171: oHeaders = null;
0172: }
0173:
0174: /**
0175: * Create DBMimeMessage from an InputStream, set folder and assign a new GUID
0176: * @param Folder oFldr
0177: * @param oInStrm InputStream
0178: * @throws MessagingException
0179: */
0180: public DBMimeMessage(Folder oFldr, InputStream oInStrm)
0181: throws MessagingException, ClassCastException {
0182: super (((DBStore) oFldr.getStore()).getSession(), oInStrm);
0183: setFolder(oFldr);
0184: sGuid = Gadgets.generateUUID();
0185: oHeaders = null;
0186: }
0187:
0188: /**
0189: * Create DBMimeMessage from a MimeMessage, set folder and assign a new GUID
0190: * @param oFldr Folder
0191: * @param MimeMessage oMsg
0192: * @throws MessagingException
0193: */
0194: public DBMimeMessage(Folder oFldr, MimeMessage oMsg)
0195: throws MessagingException {
0196: super (oMsg);
0197: setFolder(oFldr);
0198: sGuid = Gadgets.generateUUID();
0199: oHeaders = null;
0200: }
0201:
0202: /**
0203: * <p>Create DBMimeMessage from another DBMimeMessage</p>
0204: * GUID of this message is set to be the same as that of oMsg
0205: * @param oFldr Folder
0206: * @param MimeMessage oMsg
0207: * @throws MessagingException
0208: */
0209: public DBMimeMessage(Folder oFldr, DBMimeMessage oMsg)
0210: throws MessagingException {
0211: super (oMsg);
0212: setFolder(oFldr);
0213: sGuid = oMsg.getMessageGuid();
0214: oHeaders = null;
0215: }
0216:
0217: /**
0218: * Create empty message at the given folder
0219: * @param oFldr Folder
0220: * @param sMsgGuid String Message GUID
0221: * @throws MessagingException
0222: */
0223: public DBMimeMessage(Folder oFldr, String sMsgGuid)
0224: throws MessagingException {
0225: super (((DBStore) oFldr.getStore()).getSession());
0226: sGuid = sMsgGuid;
0227: setFolder(oFldr);
0228: oHeaders = null;
0229: }
0230:
0231: // ---------------------------------------------------------------------------
0232:
0233: /**
0234: * Get message folder
0235: * @return Folder
0236: */
0237: public Folder getFolder() {
0238: if (oFolder == null)
0239: return super .getFolder();
0240: else
0241: return oFolder;
0242: }
0243:
0244: // ---------------------------------------------------------------------------
0245:
0246: /**
0247: * Set message folder
0248: * @param oFldr Folder
0249: */
0250: public void setFolder(Folder oFldr) {
0251: oFolder = oFldr;
0252: }
0253:
0254: // ---------------------------------------------------------------------------
0255:
0256: /**
0257: * <p>Get message GUID</p>
0258: * If message had no previous GUID then a new one is assigned
0259: * @return String
0260: */
0261: public String getMessageGuid() {
0262: if (DebugFile.trace) {
0263: DebugFile.writeln("Begin DBMimeMessage.getMessageGuid()");
0264: DebugFile.incIdent();
0265: }
0266:
0267: if (null == sGuid) {
0268: if (DebugFile.trace)
0269: DebugFile
0270: .writeln("previous message GUID is null, assigning a new one");
0271: sGuid = Gadgets.generateUUID();
0272: }
0273:
0274: if (DebugFile.trace) {
0275: DebugFile.decIdent();
0276: DebugFile.writeln("End DBMimeMessage.getMessageGuid() : "
0277: + sGuid);
0278: }
0279: return sGuid;
0280: }
0281:
0282: // ---------------------------------------------------------------------------
0283:
0284: /**
0285: * Set message GUID
0286: * @param sId String
0287: */
0288: public void setMessageGuid(String sId) {
0289: sGuid = sId;
0290: }
0291:
0292: // ---------------------------------------------------------------------------
0293:
0294: /**
0295: * <p>Get message flags</p>
0296: * Message flags are readed from k_mime_msgs table at the database
0297: * @return Flags
0298: * @throws MessagingException
0299: */
0300: public Flags getFlags() throws MessagingException {
0301: Object oFlag;
0302:
0303: if (oFolder == null)
0304: return super .getFlags();
0305: else {
0306: Flags oRetVal = null;
0307: Statement oStmt = null;
0308: ResultSet oRSet = null;
0309: try {
0310: Flags.Flag[] aFlags = new Flags.Flag[] {
0311: Flags.Flag.RECENT, Flags.Flag.ANSWERED,
0312: Flags.Flag.DELETED, Flags.Flag.DRAFT,
0313: Flags.Flag.FLAGGED, Flags.Flag.SEEN };
0314: oStmt = ((DBFolder) oFolder).getConnection()
0315: .createStatement(ResultSet.TYPE_FORWARD_ONLY,
0316: ResultSet.CONCUR_READ_ONLY);
0317: oRSet = oStmt.executeQuery("SELECT " + DB.bo_recent
0318: + "," + DB.bo_answered + "," + DB.bo_deleted
0319: + "," + DB.bo_draft + "," + DB.bo_flagged + ","
0320: + DB.bo_recent + "," + DB.bo_seen + " FROM "
0321: + DB.k_mime_msgs + " WHERE " + DB.gu_mimemsg
0322: + "='" + getMessageGuid() + "'");
0323: if (oRSet.next()) {
0324: oRetVal = new Flags();
0325: for (int f = 1; f <= 6; f++) {
0326: oFlag = oRSet.getObject(f);
0327: if (!oRSet.wasNull()) {
0328: if (oFlag.getClass().equals(Short.TYPE)) {
0329: if (((Short) oFlag).shortValue() == (short) 1)
0330: oRetVal.add(aFlags[f - 1]);
0331: } else {
0332: if (Integer.parseInt(oFlag.toString()) != 0)
0333: oRetVal.add(aFlags[f - 1]);
0334: }
0335: }
0336: } // next (f)
0337: }
0338: oRSet.close();
0339: oRSet = null;
0340: oStmt.close();
0341: oStmt = null;
0342: return oRetVal;
0343: } catch (SQLException sqle) {
0344: if (oStmt != null) {
0345: try {
0346: oStmt.close();
0347: } catch (Exception ignore) {
0348: }
0349: }
0350: if (oRSet != null) {
0351: try {
0352: oRSet.close();
0353: } catch (Exception ignore) {
0354: }
0355: }
0356: }
0357: }
0358: return null;
0359: } // getFlags
0360:
0361: // ---------------------------------------------------------------------------
0362:
0363: /**
0364: * <p>Get message recipients</p>
0365: * This method read recipients from a message stored at k_inet_addrs table
0366: * or if message is not already stored at k_inet_addrs then it delegates
0367: * behaviour to parent class MimMessage.getAllRecipients()
0368: * @return If this message is stored at the database then this method returns
0369: * an array of DBInetAddr objects. If this message has not been stored yet then
0370: * this method returns an array of javax.mail.internet.InternetAddress objects
0371: * @throws MessagingException
0372: * @throws NullPointerException
0373: * @throws IllegalArgumentException
0374: */
0375: public Address[] getAllRecipients() throws MessagingException,
0376: NullPointerException, IllegalArgumentException {
0377: DBSubset oAddrs;
0378: int iAddrs;
0379:
0380: if (DebugFile.trace) {
0381: DebugFile.writeln("Begin DBMimeMessage.getAllRecipients()");
0382: DebugFile.incIdent();
0383: }
0384:
0385: if (oFolder == null) {
0386: if (DebugFile.trace) {
0387: DebugFile
0388: .writeln("Message is not stored at any Folder or Folder is closed");
0389: DebugFile.decIdent();
0390: }
0391: return super .getAllRecipients();
0392: } else {
0393: if (oFolder.getClass().getName().equals(
0394: "com.knowgate.hipermail.DBFolder")) {
0395:
0396: if (((DBFolder) oFolder).getConnection() == null) {
0397: if (DebugFile.trace)
0398: DebugFile.decIdent();
0399: throw new MessagingException(
0400: "DBMimeMessage.getAllRecipients() not connected to the database");
0401: }
0402:
0403: oAddrs = new DBSubset(DB.k_inet_addrs, DB.gu_mimemsg
0404: + "," + DB.id_message + "," + DB.tx_email + ","
0405: + DB.tx_personal + "," + DB.tp_recipient + ","
0406: + DB.gu_user + "," + DB.gu_contact + ","
0407: + DB.gu_company, DB.gu_mimemsg + "=?", 10);
0408: try {
0409: iAddrs = oAddrs.load(((DBFolder) oFolder)
0410: .getConnection(), new Object[] { sGuid });
0411: } catch (SQLException sqle) {
0412: if (DebugFile.trace)
0413: DebugFile.decIdent();
0414: throw new MessagingException(sqle.getMessage(),
0415: sqle);
0416: }
0417:
0418: if (iAddrs > 0) {
0419: aAddrs = new DBInetAddr[iAddrs];
0420: for (int a = 0; a < iAddrs; a++) {
0421: aAddrs[a] = new DBInetAddr(oAddrs.getString(0,
0422: a), // gu_mimemsg
0423: oAddrs.getString(1, a), // id_message
0424: oAddrs.getString(2, a), // tx_email
0425: oAddrs.getStringNull(3, a, null), // tx_personal
0426: oAddrs.getString(4, a), // tp_recipient
0427: oAddrs.getStringNull(5, a, null), // gu_user
0428: oAddrs.getStringNull(6, a, null), // gu_contact
0429: oAddrs.getStringNull(7, a, null)); // gu_company
0430: } // next
0431: } // fi (iAddrs)
0432: else {
0433: aAddrs = null;
0434: }
0435: } // fi (oFolder.getClass() == com.knowgate.hipergate.DBFolder)
0436: else {
0437: DebugFile.writeln("message Folder type is "
0438: + oFolder.getClass().getName());
0439: if (DebugFile.trace)
0440: DebugFile.decIdent();
0441: aAddrs = super .getAllRecipients();
0442: } // fi(oFolder instanceof DBFolder)
0443: } // fi (oFolder)
0444:
0445: if (DebugFile.trace) {
0446: DebugFile.decIdent();
0447: DebugFile.writeln("End DBMimeMessage.getAllRecipients()");
0448: }
0449:
0450: return aAddrs;
0451: } // getAllRecipients
0452:
0453: // ---------------------------------------------------------------------------
0454:
0455: /**
0456: * <p>Get recipients of a particular type</p>
0457: * This method first calls getAllRecipients() and then filters retrieved recipients by their type.
0458: * @param cTpRecipient javax.mail.Message.RecipientType
0459: * @return Address[]
0460: * @throws MessagingException
0461: */
0462: public Address[] getRecipients(Message.RecipientType cTpRecipient)
0463: throws MessagingException {
0464: int a;
0465: int iRecipients = 0;
0466: DBInetAddr[] aRecipients = null;
0467: DBInetAddr oAdr;
0468:
0469: if (oFolder == null) {
0470: return super .getRecipients(cTpRecipient);
0471: }
0472:
0473: if (aAddrs == null)
0474: getAllRecipients();
0475:
0476: if (aAddrs != null) {
0477: for (a = 0; a < aAddrs.length; a++) {
0478: oAdr = ((DBInetAddr) aAddrs[a]);
0479: if ((oAdr.getStringNull(DB.tp_recipient, "")
0480: .equalsIgnoreCase("to") && Message.RecipientType.TO
0481: .equals(cTpRecipient))
0482: || (oAdr.getStringNull(DB.tp_recipient, "")
0483: .equalsIgnoreCase("cc") && Message.RecipientType.CC
0484: .equals(cTpRecipient))
0485: || (oAdr.getStringNull(DB.tp_recipient, "")
0486: .equalsIgnoreCase("bcc") && Message.RecipientType.BCC
0487: .equals(cTpRecipient)))
0488:
0489: iRecipients++;
0490: } // next
0491:
0492: aRecipients = new DBInetAddr[iRecipients];
0493:
0494: int iRecipient = 0;
0495:
0496: for (a = 0; a < aAddrs.length; a++) {
0497: oAdr = ((DBInetAddr) aAddrs[a]);
0498: if ((oAdr.getStringNull(DB.tp_recipient, "")
0499: .equalsIgnoreCase("to") && Message.RecipientType.TO
0500: .equals(cTpRecipient))
0501: || (oAdr.getStringNull(DB.tp_recipient, "")
0502: .equalsIgnoreCase("cc") && Message.RecipientType.CC
0503: .equals(cTpRecipient))
0504: || (oAdr.getStringNull(DB.tp_recipient, "")
0505: .equalsIgnoreCase("bcc") && Message.RecipientType.BCC
0506: .equals(cTpRecipient)))
0507:
0508: aRecipients[iRecipient++] = (DBInetAddr) aAddrs[a];
0509: } // next
0510: }
0511:
0512: return aRecipients;
0513: } // getRecipients
0514:
0515: // ---------------------------------------------------------------------------
0516:
0517: public DBInetAddr getFromRecipient() throws MessagingException {
0518:
0519: DBInetAddr oFrom = null;
0520:
0521: if (aAddrs == null)
0522: getAllRecipients();
0523:
0524: if (aAddrs != null) {
0525: for (int a = 0; a < aAddrs.length && oFrom == null; a++) {
0526:
0527: if (((DBInetAddr) (aAddrs[a])).getStringNull(
0528: DB.tp_recipient, "").equals("from"))
0529: oFrom = (DBInetAddr) (aAddrs[a]);
0530: } // next
0531:
0532: } // fi
0533:
0534: return oFrom;
0535: } // getFrom
0536:
0537: // ---------------------------------------------------------------------------
0538:
0539: private void cacheHeaders() throws SQLException {
0540:
0541: if (DebugFile.trace) {
0542: DebugFile.writeln("Begin DBMimeMessage.cacheHeaders()");
0543: DebugFile.incIdent();
0544: }
0545:
0546: PreparedStatement oStmt = null;
0547: ResultSet oRSet = null;
0548:
0549: oStmt = ((DBFolder) oFolder).getConnection()
0550: .prepareStatement(
0551: "SELECT " + DB.id_type + "," + DB.tx_subject
0552: + "," + DB.id_message + ","
0553: + DB.len_mimemsg + "," + DB.tx_md5
0554: + "," + DB.de_mimemsg + ","
0555: + DB.tx_encoding + "," + DB.dt_sent
0556: + "," + DB.dt_received + ","
0557: + DB.dt_readed + "," + DB.bo_spam + ","
0558: + DB.id_compression + ","
0559: + DB.id_priority + " FROM "
0560: + DB.k_mime_msgs + " WHERE "
0561: + DB.gu_mimemsg + "=?",
0562: ResultSet.TYPE_FORWARD_ONLY,
0563: ResultSet.CONCUR_READ_ONLY);
0564: oStmt.setString(1, getMessageGuid());
0565: oRSet = oStmt.executeQuery();
0566:
0567: if (oRSet.next()) {
0568: oHeaders = new HashMap(23);
0569: oHeaders.put("Content-Type", oRSet.getString(1));
0570: oHeaders.put("Subject", oRSet.getString(2));
0571: oHeaders.put("Message-ID", oRSet.getString(3));
0572: oHeaders.put("Date", oRSet.getDate(8));
0573: oHeaders.put("Date-Received", oRSet.getDate(9));
0574: oHeaders.put("Date-Readed", oRSet.getDate(10));
0575: oHeaders.put("X-Spam-Flag", oRSet.getObject(11));
0576: oHeaders.put("Compression", oRSet.getString(12));
0577: oHeaders.put("X-Priority", oRSet.getString(13));
0578: }
0579: oRSet.close();
0580: oStmt.close();
0581:
0582: if (DebugFile.trace) {
0583: DebugFile.decIdent();
0584: DebugFile.writeln("End DBMimeMessage.cacheHeaders()");
0585: }
0586: }
0587:
0588: // ---------------------------------------------------------------------------
0589:
0590: public String getContentType() throws MessagingException {
0591:
0592: if (DebugFile.trace) {
0593: DebugFile.writeln("Begin DBMimeMessage.getContentType()");
0594: DebugFile.incIdent();
0595: }
0596:
0597: String sRetVal;
0598:
0599: if (oFolder == null) {
0600: if (DebugFile.trace) {
0601: DebugFile
0602: .writeln("Message is not stored at any Folder or Folder is closed");
0603: }
0604: sRetVal = super .getContentType();
0605: } else {
0606: try {
0607: if (null == oHeaders)
0608: cacheHeaders();
0609: if (null == oHeaders)
0610: sRetVal = super .getContentType();
0611: else
0612: sRetVal = (String) oHeaders.get("Content-Type");
0613: } catch (SQLException sqle) {
0614: throw new MessagingException(sqle.getMessage(), sqle);
0615: }
0616: }
0617:
0618: if (DebugFile.trace) {
0619: DebugFile.decIdent();
0620: DebugFile.writeln("End DBMimeMessage.getContentType() : "
0621: + sRetVal);
0622: }
0623: return sRetVal;
0624: }
0625:
0626: // ---------------------------------------------------------------------------
0627:
0628: public Date getSentDate() throws MessagingException {
0629:
0630: if (DebugFile.trace) {
0631: DebugFile.writeln("Begin DBMimeMessage.getSentDate()");
0632: DebugFile.incIdent();
0633: }
0634:
0635: Date dtRetVal;
0636:
0637: if (oFolder == null) {
0638: if (DebugFile.trace) {
0639: DebugFile
0640: .writeln("Message is not stored at any Folder or Folder is closed");
0641: }
0642: dtRetVal = super .getSentDate();
0643: } else {
0644: try {
0645: if (null == oHeaders)
0646: cacheHeaders();
0647: if (null == oHeaders)
0648: dtRetVal = super .getSentDate();
0649: else
0650: dtRetVal = (java.util.Date) oHeaders.get("Date");
0651: } catch (SQLException sqle) {
0652: throw new MessagingException(sqle.getMessage(), sqle);
0653: }
0654: }
0655:
0656: if (DebugFile.trace) {
0657: DebugFile.decIdent();
0658: if (dtRetVal == null)
0659: DebugFile
0660: .writeln("End DBMimeMessage.getSentDate() : null");
0661: else
0662: DebugFile.writeln("End DBMimeMessage.getSentDate() : "
0663: + dtRetVal.toString());
0664: }
0665: return dtRetVal;
0666: }
0667:
0668: // ---------------------------------------------------------------------------
0669:
0670: public String getSubject() throws MessagingException {
0671:
0672: if (DebugFile.trace) {
0673: DebugFile.writeln("Begin DBMimeMessage.getSubject()");
0674: DebugFile.incIdent();
0675: }
0676:
0677: String sRetVal;
0678:
0679: if (oFolder == null) {
0680: if (DebugFile.trace) {
0681: DebugFile
0682: .writeln("Message is not stored at any Folder or Folder is closed");
0683: }
0684: sRetVal = super .getSubject();
0685: } else {
0686: try {
0687: if (null == oHeaders)
0688: cacheHeaders();
0689: if (null == oHeaders)
0690: sRetVal = super .getSubject();
0691: else
0692: sRetVal = (String) oHeaders.get("Subject");
0693: } catch (SQLException sqle) {
0694: throw new MessagingException(sqle.getMessage(), sqle);
0695: }
0696: }
0697:
0698: if (DebugFile.trace) {
0699: DebugFile.decIdent();
0700: DebugFile.writeln("End DBMimeMessage.getSubject() : "
0701: + sRetVal);
0702: }
0703: return sRetVal;
0704: }
0705:
0706: // ---------------------------------------------------------------------------
0707:
0708: public MimePart getMessageBody() throws ParseException,
0709: MessagingException, IOException {
0710:
0711: MimePart oRetVal = null;
0712:
0713: if (DebugFile.trace) {
0714: DebugFile
0715: .writeln("Begin DBMimeMessage.getMessageBody([MimeMessage])");
0716: DebugFile.incIdent();
0717: }
0718:
0719: Object oContent = getContent();
0720:
0721: if (DebugFile.trace) {
0722: if (null == oContent)
0723: DebugFile.writeln("message content is null");
0724: else
0725: DebugFile.writeln("message content class is "
0726: + oContent.getClass().getName());
0727: }
0728:
0729: String sContentClass = oContent.getClass().getName();
0730:
0731: if (sContentClass.equals("javax.mail.internet.MimeMultipart")) {
0732:
0733: MimeMultipart oParts = (MimeMultipart) oContent;
0734: int iParts = oParts.getCount();
0735: MimePart oPart;
0736: String sType, sPrevType, sNextType;
0737:
0738: for (int p = 0; p < iParts; p++) {
0739:
0740: oPart = (MimeBodyPart) oParts.getBodyPart(0);
0741:
0742: sType = oPart.getContentType().toUpperCase();
0743:
0744: if (p < iParts - 1)
0745: sNextType = ((MimeBodyPart) oParts
0746: .getBodyPart(p + 1)).getContentType()
0747: .toUpperCase();
0748: else
0749: sNextType = "";
0750:
0751: if (p > 0 && iParts > 1)
0752: sPrevType = ((MimeBodyPart) oParts
0753: .getBodyPart(p - 1)).getContentType()
0754: .toUpperCase();
0755: else
0756: sPrevType = "";
0757:
0758: // If a message has a dual content both text and html ignore the text and show only HTML
0759: if ((iParts <= 1)
0760: && (sType.startsWith("TEXT/PLAIN") || sType
0761: .startsWith("TEXT/HTML"))) {
0762: if (DebugFile.trace)
0763: DebugFile.writeln("parts="
0764: + String.valueOf(iParts)
0765: + ", content-type="
0766: + oPart.getContentType());
0767: oRetVal = oPart;
0768: break;
0769: } else if (((p == 0) && (iParts > 1)
0770: && sType.startsWith("TEXT/PLAIN") && sNextType
0771: .startsWith("TEXT/HTML"))) {
0772: if (DebugFile.trace)
0773: DebugFile.writeln("parts="
0774: + String.valueOf(iParts)
0775: + ", part=0, content-type="
0776: + oPart.getContentType()
0777: + ", next-type=" + sNextType);
0778: oRetVal = ((MimeBodyPart) oParts.getBodyPart(p + 1));
0779: break;
0780: } else if ((p == 1) && sType.startsWith("TEXT/PLAIN")
0781: && sPrevType.startsWith("TEXT/HTML")) {
0782: if (DebugFile.trace)
0783: DebugFile.writeln("parts="
0784: + String.valueOf(iParts)
0785: + ", part=1, content-type="
0786: + oPart.getContentType()
0787: + ", prev-type=" + sPrevType);
0788: oRetVal = ((MimeBodyPart) oParts.getBodyPart(p - 1));
0789: break;
0790: } else {
0791: oRetVal = DBMimePart.getMessagePart(oPart, p);
0792: }
0793: } // next (p)
0794: } else if (sContentClass.equals("java.lang.String")) {
0795: oRetVal = new MimeBodyPart();
0796: oRetVal.setText((String) oContent);
0797: } else {
0798: throw new MessagingException("Unparsed Mime Content "
0799: + oContent.getClass().getName());
0800: }
0801:
0802: if (null == oRetVal) {
0803: oRetVal = new MimeBodyPart();
0804: oRetVal.setText("");
0805: }
0806:
0807: if (DebugFile.trace) {
0808: DebugFile.decIdent();
0809: DebugFile.writeln("End DBMimeMessage.getMessageBody() : "
0810: + oRetVal.getContentType());
0811: }
0812:
0813: return oRetVal;
0814: } // getMessageBody
0815:
0816: // ---------------------------------------------------------------------------
0817:
0818: public void setFlag(Flags.Flag oFlg, boolean bFlg)
0819: throws MessagingException {
0820: String sColunm;
0821:
0822: super .setFlag(oFlg, bFlg);
0823:
0824: if (oFlg.equals(Flags.Flag.ANSWERED))
0825: sColunm = DB.bo_answered;
0826: else if (oFlg.equals(Flags.Flag.DELETED))
0827: sColunm = DB.bo_deleted;
0828: else if (oFlg.equals(Flags.Flag.DRAFT))
0829: sColunm = DB.bo_draft;
0830: else if (oFlg.equals(Flags.Flag.FLAGGED))
0831: sColunm = DB.bo_flagged;
0832: else if (oFlg.equals(Flags.Flag.RECENT))
0833: sColunm = DB.bo_recent;
0834: else if (oFlg.equals(Flags.Flag.SEEN))
0835: sColunm = DB.bo_seen;
0836: else
0837: sColunm = null;
0838:
0839: if (null != sColunm && oFolder instanceof DBFolder) {
0840: JDCConnection oConn = null;
0841: PreparedStatement oUpdt = null;
0842: try {
0843: oConn = ((DBFolder) oFolder).getConnection();
0844: String sSQL = "UPDATE " + DB.k_mime_msgs + " SET "
0845: + sColunm + "=" + (bFlg ? "1" : "0")
0846: + " WHERE " + DB.gu_mimemsg + "='"
0847: + getMessageGuid() + "'";
0848: if (DebugFile.trace)
0849: DebugFile.writeln("Connection.prepareStatement("
0850: + sSQL + ")");
0851: oUpdt = oConn.prepareStatement(sSQL);
0852: oUpdt.executeUpdate();
0853: oUpdt.close();
0854: oUpdt = null;
0855: oConn.commit();
0856: oConn = null;
0857: } catch (SQLException e) {
0858: if (null != oConn) {
0859: try {
0860: oConn.rollback();
0861: } catch (Exception ignore) {
0862: }
0863: }
0864: if (null != oUpdt) {
0865: try {
0866: oUpdt.close();
0867: } catch (Exception ignore) {
0868: }
0869: }
0870: if (DebugFile.trace)
0871: DebugFile.decIdent();
0872: throw new MessagingException(e.getMessage(), e);
0873: }
0874: }
0875: } // setFlag
0876:
0877: // ---------------------------------------------------------------------------
0878:
0879: public void saveChanges() throws MessagingException {
0880:
0881: if (DebugFile.trace) {
0882: DebugFile.writeln("Begin DBMimeMessage.saveChanges()");
0883: DebugFile.incIdent();
0884: }
0885:
0886: Flags oFlgs = getFlags();
0887:
0888: if (oFolder instanceof DBFolder) {
0889: JDCConnection oConn = null;
0890: try {
0891: oConn.commit();
0892: oConn = null;
0893: } catch (SQLException e) {
0894: if (null != oConn) {
0895: try {
0896: oConn.rollback();
0897: } catch (Exception ignore) {
0898: }
0899: }
0900: if (DebugFile.trace)
0901: DebugFile.decIdent();
0902: throw new MessagingException(e.getMessage(), e);
0903: }
0904: } else {
0905: super .saveChanges();
0906: }
0907:
0908: if (DebugFile.trace) {
0909: DebugFile.decIdent();
0910: DebugFile.writeln("End DBMimeMessage.saveChanges()");
0911: }
0912: }
0913:
0914: // ---------------------------------------------------------------------------
0915:
0916: /**
0917: * Get message parts as an array of DBMimePart objects
0918: * @return DBMimeMultiPart if this message folder is of type DBFolder
0919: * or another type of Object if this message folder is another subclass of javax.mail.Folder
0920: * such as POP3Folder.
0921: * @throws MessagingException
0922: * @throws IOException
0923: * @throws NullPointerException If this message Folder is <b>null</b>
0924: */
0925: public Multipart getParts() throws MessagingException, IOException,
0926: NullPointerException {
0927:
0928: if (DebugFile.trace) {
0929: DebugFile.writeln("Begin DBMimeMessage.getParts()");
0930: DebugFile.incIdent();
0931: }
0932:
0933: if (oFolder == null) {
0934: if (DebugFile.trace)
0935: DebugFile.decIdent();
0936: throw new NullPointerException(
0937: "DBMimeMessage.getContent() : Folder for message cannot be null");
0938: }
0939:
0940: if (DebugFile.trace)
0941: DebugFile.writeln("Folder type "
0942: + oFolder.getClass().getName());
0943:
0944: Multipart oRetVal;
0945:
0946: if (oFolder.getClass().getName().equals(
0947: "com.knowgate.hipermail.DBFolder")) {
0948:
0949: if (sGuid == null) {
0950: if (DebugFile.trace)
0951: DebugFile.decIdent();
0952: throw new NullPointerException(
0953: "DBMimeMessage.getContent() : message GUID cannot be null");
0954: }
0955:
0956: PreparedStatement oStmt = null;
0957: ResultSet oRSet = null;
0958: DBMimeMultipart oMultiPart = new DBMimeMultipart(
0959: (Part) this );
0960:
0961: try {
0962: if (DebugFile.trace) {
0963: DebugFile
0964: .writeln("Connection.prepareStatement(SELECT id_part,id_content,id_disposition,len_part,de_part,tx_md5,id_encoding,file_name,id_type FROM "
0965: + DB.k_mime_parts
0966: + " WHERE "
0967: + DB.gu_mimemsg
0968: + "='"
0969: + sGuid
0970: + "')");
0971: }
0972:
0973: oStmt = ((DBFolder) oFolder)
0974: .getConnection()
0975: .prepareStatement(
0976: "SELECT id_part,id_content,id_disposition,len_part,de_part,tx_md5,id_encoding,file_name,id_type FROM "
0977: + DB.k_mime_parts
0978: + " WHERE "
0979: + DB.gu_mimemsg + "=?",
0980: ResultSet.TYPE_FORWARD_ONLY,
0981: ResultSet.CONCUR_READ_ONLY);
0982: oStmt.setString(1, sGuid);
0983:
0984: oRSet = oStmt.executeQuery();
0985:
0986: while (oRSet.next()) {
0987: if (DebugFile.trace)
0988: DebugFile
0989: .writeln("DBMimeMultipart.addBodyPart("
0990: + sGuid
0991: + ","
0992: + String.valueOf(oRSet
0993: .getInt(1))
0994: + ","
0995: + oRSet.getString(2)
0996: + ","
0997: + oRSet.getString(9)
0998: + ","
0999: + oRSet.getString(6)
1000: + ","
1001: + oRSet.getString(5)
1002: + ","
1003: + oRSet.getString(3)
1004: + ","
1005: + oRSet.getString(7)
1006: + ","
1007: + oRSet.getString(8)
1008: + ","
1009: + String.valueOf(oRSet
1010: .getInt(4)));
1011:
1012: MimePart oPart = new DBMimePart(oMultiPart, oRSet
1013: .getInt(1), oRSet.getString(2), oRSet
1014: .getString(9), oRSet.getString(6), oRSet
1015: .getString(5), oRSet.getString(3), oRSet
1016: .getString(7), oRSet.getString(8), oRSet
1017: .getInt(4));
1018:
1019: oMultiPart.addBodyPart(oPart);
1020: }
1021:
1022: oRSet.close();
1023: oRSet = null;
1024: oStmt.close();
1025: oStmt = null;
1026:
1027: } catch (SQLException sqle) {
1028: try {
1029: if (oRSet != null)
1030: oRSet.close();
1031: } catch (Exception e) {
1032: }
1033: try {
1034: if (oStmt != null)
1035: oStmt.close();
1036: } catch (Exception e) {
1037: }
1038: throw new MessagingException(sqle.getMessage(), sqle);
1039: }
1040: oRetVal = oMultiPart;
1041: } else {
1042: oRetVal = (MimeMultipart) super .getContent();
1043: }
1044:
1045: if (DebugFile.trace) {
1046: DebugFile.decIdent();
1047: DebugFile.writeln("End DBMimeMessage.getParts() : "
1048: + (oRetVal == null ? "null" : oRetVal.getClass()
1049: .getName()));
1050: }
1051:
1052: return oRetVal;
1053: } // getParts
1054:
1055: // ---------------------------------------------------------------------------
1056:
1057: public MimePart getBody() throws ParseException,
1058: MessagingException, IOException {
1059:
1060: MimePart oRetVal = null;
1061:
1062: if (DebugFile.trace) {
1063: DebugFile
1064: .writeln("Begin DBMimeMessage.getBody([MimeMessage])");
1065: DebugFile.incIdent();
1066: }
1067:
1068: Object oContent = null;
1069:
1070: try {
1071: oContent = super .getContent();
1072: } catch (Exception xcpt) {
1073: DebugFile.decIdent();
1074: throw new ParseException(
1075: "MimeMessage.getContent() ParseException cause "
1076: + xcpt.getClass().getName()
1077: + " "
1078: + (xcpt.getMessage() == null ? "" : xcpt
1079: .getMessage()));
1080: }
1081:
1082: if (DebugFile.trace) {
1083: if (null == oContent)
1084: DebugFile.writeln("message content is null");
1085: else
1086: DebugFile.writeln("message content class is "
1087: + oContent.getClass().getName());
1088: }
1089:
1090: String sContentClass = oContent.getClass().getName();
1091:
1092: if (sContentClass.equals("javax.mail.internet.MimeMultipart")) {
1093:
1094: MimeMultipart oParts = (MimeMultipart) oContent;
1095: int iParts = oParts.getCount();
1096: MimePart oPart;
1097: String sType, sPrevType, sNextType;
1098:
1099: for (int p = 0; p < iParts; p++) {
1100:
1101: oPart = (MimePart) oParts.getBodyPart(0);
1102:
1103: sType = oPart.getContentType().toUpperCase();
1104:
1105: if (p < iParts - 1)
1106: sNextType = ((MimeBodyPart) oParts
1107: .getBodyPart(p + 1)).getContentType()
1108: .toUpperCase();
1109: else
1110: sNextType = "";
1111:
1112: if (p > 0 && iParts > 1)
1113: sPrevType = ((MimeBodyPart) oParts
1114: .getBodyPart(p - 1)).getContentType()
1115: .toUpperCase();
1116: else
1117: sPrevType = "";
1118:
1119: // If a message has a dual content both text and html ignore the text and show only HTML
1120: if ((iParts <= 1)
1121: && (sType.startsWith("TEXT/PLAIN") || sType
1122: .startsWith("TEXT/HTML"))) {
1123: if (DebugFile.trace)
1124: DebugFile.writeln("parts="
1125: + String.valueOf(iParts)
1126: + ", content-type="
1127: + oPart.getContentType());
1128: oRetVal = oPart;
1129: break;
1130: } else if (((p == 0) && (iParts > 1)
1131: && sType.startsWith("TEXT/PLAIN") && sNextType
1132: .startsWith("TEXT/HTML"))) {
1133: if (DebugFile.trace)
1134: DebugFile.writeln("parts="
1135: + String.valueOf(iParts)
1136: + ", part=0, content-type="
1137: + oPart.getContentType()
1138: + ", next-type=" + sNextType);
1139: oRetVal = ((MimeBodyPart) oParts.getBodyPart(p + 1));
1140: break;
1141: } else if ((p == 1) && sType.startsWith("TEXT/PLAIN")
1142: && sPrevType.startsWith("TEXT/HTML")) {
1143: if (DebugFile.trace)
1144: DebugFile.writeln("parts="
1145: + String.valueOf(iParts)
1146: + ", part=1, content-type="
1147: + oPart.getContentType()
1148: + ", prev-type=" + sPrevType);
1149: oRetVal = ((MimeBodyPart) oParts.getBodyPart(p - 1));
1150: break;
1151: } else {
1152: oRetVal = DBMimePart.getMessagePart(oPart, p);
1153: }
1154: } // next (p)
1155: } else if (sContentClass.equals("java.lang.String")) {
1156: oRetVal = new MimeBodyPart();
1157: oRetVal.setText((String) oContent);
1158: } else if (oContent instanceof InputStream) {
1159: // This branch is reached when the content-type is not recognized
1160: // (usually with a com.sun.mail.util.SharedByteArrayInputStream)
1161: // Decode content as an ISO-8859-1 string
1162: if (DebugFile.trace)
1163: DebugFile
1164: .writeln("No data handler found for Content-Type, decoding as ISO-8859-1 string");
1165: InputStream oInStrm = (InputStream) oContent;
1166: ByteArrayOutputStream oBaStrm = new ByteArrayOutputStream();
1167: StreamPipe oPipe = new StreamPipe();
1168: oPipe.between(oInStrm, oBaStrm);
1169: oRetVal = new MimeBodyPart();
1170: oRetVal.setText(oBaStrm.toString("ISO8859_1"));
1171: } else {
1172: throw new MessagingException("Unparsed Mime Content "
1173: + oContent.getClass().getName());
1174: }
1175:
1176: if (null == oRetVal) {
1177: oRetVal = new MimeBodyPart();
1178: oRetVal.setText("");
1179: }
1180:
1181: if (DebugFile.trace) {
1182: DebugFile.decIdent();
1183: DebugFile.writeln("End DBMimeMessage.getBody() : "
1184: + oRetVal.getContentType());
1185: }
1186:
1187: return oRetVal;
1188: } // getBody
1189:
1190: // ---------------------------------------------------------------------------
1191:
1192: /**
1193: * Get message body text into a StringBuffer
1194: * @param oBuffer StringBuffer
1195: * @throws MessagingException
1196: * @throws IOException
1197: * @throws ClassCastException
1198: */
1199: public void getText(StringBuffer oBuffer)
1200: throws MessagingException, IOException, ClassCastException {
1201:
1202: if (DebugFile.trace) {
1203: DebugFile.writeln("Begin DBMimeMessage.getText()");
1204: DebugFile.incIdent();
1205: }
1206:
1207: if (null == oFolder) {
1208: Multipart oParts = (Multipart) super .getContent();
1209:
1210: if (DebugFile.trace)
1211: DebugFile
1212: .writeln("MimeBodyPart = MimeMultipart.getBodyPart(0)");
1213:
1214: BodyPart oPart0 = oParts.getBodyPart(0);
1215:
1216: if (DebugFile.trace) {
1217: if (null == oPart0)
1218: DebugFile.writeln("part 0 is null");
1219: else
1220: DebugFile.writeln("part 0 is "
1221: + oPart0.getClass().getName());
1222: }
1223: DBMimePart.parseMimePart(oBuffer, null, null,
1224: getMessageID() != null ? getMessageID()
1225: : getContentID(), (MimePart) oPart0, 0);
1226: } else {
1227: InputStream oInStrm;
1228: PreparedStatement oStmt = null;
1229: ResultSet oRSet = null;
1230: MimeBodyPart oBody = null;
1231: String sFolderNm = null;
1232: String sType = "multipart/";
1233:
1234: try {
1235: sFolderNm = ((DBFolder) oFolder).getCategory()
1236: .getStringNull(DB.nm_category, null);
1237: if (getMessageGuid() != null) {
1238: oStmt = ((DBFolder) oFolder).getConnection()
1239: .prepareStatement(
1240: "SELECT " + DB.id_type + ","
1241: + DB.by_content + " FROM "
1242: + DB.k_mime_msgs
1243: + " WHERE " + DB.gu_mimemsg
1244: + "=?");
1245: oStmt.setString(1, getMessageGuid());
1246: } else {
1247: oStmt = ((DBFolder) oFolder).getConnection()
1248: .prepareStatement(
1249: "SELECT " + DB.id_type + ","
1250: + DB.by_content + " FROM "
1251: + DB.k_mime_msgs
1252: + " WHERE " + DB.id_message
1253: + "=? AND "
1254: + DB.gu_category + "=?");
1255: oStmt.setString(1, getMessageID());
1256: oStmt.setString(2, ((DBFolder) oFolder)
1257: .getCategory().getString(DB.gu_category));
1258: }
1259: oRSet = oStmt.executeQuery();
1260: if (oRSet.next()) {
1261: sType = oRSet.getString(1);
1262: oInStrm = oRSet.getBinaryStream(2);
1263: if (!oRSet.wasNull()) {
1264: oBody = new MimeBodyPart(oInStrm);
1265: oInStrm.close();
1266: }
1267: }
1268: oRSet.close();
1269: oRSet = null;
1270: oStmt.close();
1271: oStmt = null;
1272: } catch (SQLException sqle) {
1273: if (oRSet != null) {
1274: try {
1275: oRSet.close();
1276: } catch (Exception ignore) {
1277: }
1278: }
1279: if (oStmt != null) {
1280: try {
1281: oStmt.close();
1282: } catch (Exception ignore) {
1283: }
1284: }
1285: throw new MessagingException(sqle.getMessage(), sqle);
1286: }
1287: if (oBody != null) {
1288: if (sType.startsWith("text/"))
1289: oBuffer.append(oBody.getContent());
1290: else
1291: DBMimePart.parseMimePart(oBuffer, null, sFolderNm,
1292: getMessageID() != null ? getMessageID()
1293: : getContentID(), oBody, 0);
1294: }
1295: }
1296:
1297: if (DebugFile.trace) {
1298: DebugFile.decIdent();
1299: DebugFile.writeln("End DBMimeMessage.getText() : "
1300: + String.valueOf(oBuffer.length()));
1301: }
1302: } // getText
1303:
1304: // ---------------------------------------------------------------------------
1305:
1306: public String getText() throws MessagingException, IOException {
1307: StringBuffer oStrBuff = new StringBuffer(16000);
1308: getText(oStrBuff);
1309: return oStrBuff.toString();
1310: }
1311:
1312: // ---------------------------------------------------------------------------
1313:
1314: public void getTextPlain(StringBuffer oBuffer)
1315: throws MessagingException, IOException {
1316:
1317: if (DebugFile.trace) {
1318: DebugFile.writeln("Begin DBMimeMessage.getTextPlain()");
1319: DebugFile.incIdent();
1320: }
1321: boolean bHasPlainTextVersion = false;
1322:
1323: if (getContentType().startsWith("text/plain")) {
1324: getText(oBuffer);
1325: } else if (getContentType().startsWith("text/html")) {
1326: StringBuffer oHtmlBuff = new StringBuffer();
1327: getText(oHtmlBuff);
1328: Parser oPrsr = Parser.createParser(oHtmlBuff.toString(),
1329: getEncoding());
1330: StringBean oStrBn = new StringBean();
1331: try {
1332: oPrsr.visitAllNodesWith(oStrBn);
1333: } catch (ParserException pe) {
1334: throw new MessagingException(pe.getMessage(), pe);
1335: }
1336: // Code for HTML parser 1.4
1337: // oStrBn.setInputHTML(oHtmlBuff.toString());
1338: oBuffer.append(oStrBn.getStrings());
1339: } else {
1340: if (DebugFile.trace)
1341: DebugFile
1342: .writeln("Multipart = DBMimeMessage.getParts()");
1343:
1344: Multipart oParts = getParts();
1345:
1346: final int iParts = oParts.getCount();
1347:
1348: MimePart oPart;
1349:
1350: int p;
1351: for (p = 0; p < iParts && !bHasPlainTextVersion; p++) {
1352: oPart = (MimePart) oParts.getBodyPart(p);
1353:
1354: String sType = oPart.getContentType();
1355: if (null != sType)
1356: sType = sType.toLowerCase();
1357: String sDisp = oPart.getDisposition();
1358: if (null == sDisp)
1359: sDisp = "inline";
1360: else if (sDisp.length() == 0)
1361: sDisp = "inline";
1362:
1363: if (DebugFile.trace)
1364: DebugFile.writeln("scanning part "
1365: + String.valueOf(p)
1366: + sDisp
1367: + " "
1368: + sType.replace('\r', ' ').replace('\n',
1369: ' '));
1370:
1371: if (sType.startsWith("text/plain")
1372: && sDisp.equalsIgnoreCase("inline")) {
1373: bHasPlainTextVersion = true;
1374: DBMimePart.parseMimePart(oBuffer, null, getFolder()
1375: .getName(),
1376: getMessageID() != null ? getMessageID()
1377: : getContentID(), oPart, p);
1378: }
1379: }
1380:
1381: if (DebugFile.trace) {
1382: if (bHasPlainTextVersion)
1383: DebugFile
1384: .writeln("MimeMultipart has plain text version at part "
1385: + String.valueOf(p));
1386: else
1387: DebugFile
1388: .writeln("MimeMultipart has no plain text version, converting part 0 from HTML");
1389: }
1390:
1391: if (!bHasPlainTextVersion) {
1392: oPart = (MimeBodyPart) oParts.getBodyPart(0);
1393: StringBuffer oHtml = new StringBuffer();
1394: DBMimePart.parseMimePart(oHtml, null, getFolder()
1395: .getName(),
1396: getMessageID() != null ? getMessageID()
1397: : getContentID(), oPart, 0);
1398:
1399: Parser oPrsr = Parser.createParser(oHtml.toString(),
1400: getEncoding());
1401: StringBean oStrBn = new StringBean();
1402:
1403: try {
1404: oPrsr.visitAllNodesWith(oStrBn);
1405: } catch (ParserException pe) {
1406: throw new MessagingException(pe.getMessage(), pe);
1407: }
1408:
1409: // Code for HTML parser 1.4
1410: // oSB.setInputHTML(oHtml.toString());
1411:
1412: String sStrs = oStrBn.getStrings();
1413:
1414: if (DebugFile.trace) {
1415: DebugFile.writeln("StringBean.getStrings(");
1416: if (null != sStrs)
1417: DebugFile.write(sStrs);
1418: else
1419: DebugFile.write("null");
1420: DebugFile.writeln(")");
1421: }
1422: oBuffer.append(sStrs);
1423: } // fi (!bHasPlainTextVersion)
1424: }
1425:
1426: if (DebugFile.trace) {
1427: DebugFile.decIdent();
1428: DebugFile.writeln("End DBMimeMessage.getTextPlain() : "
1429: + String.valueOf(oBuffer.length()));
1430: }
1431: }
1432:
1433: // ---------------------------------------------------------------------------
1434:
1435: public void writeTo(OutputStream oOutStrm) throws IOException,
1436: FolderClosedException, MessagingException {
1437:
1438: if (DebugFile.trace) {
1439: DebugFile
1440: .writeln("Begin DBMimeMessage.writeTo([OutputStream])");
1441: DebugFile.incIdent();
1442: }
1443:
1444: if (getFolder() == null) {
1445: DebugFile.decIdent();
1446: throw new MessagingException("No folder for message");
1447: }
1448:
1449: DBFolder oDBF = (DBFolder) getFolder();
1450:
1451: if ((oDBF.getType() & DBFolder.MODE_MBOX) != 0) {
1452: if (oDBF.getConnection() == null) {
1453: if (DebugFile.trace)
1454: DebugFile.decIdent();
1455: throw new FolderClosedException(oDBF,
1456: "Folder is closed");
1457: }
1458:
1459: PreparedStatement oStmt = null;
1460: ResultSet oRSet = null;
1461: BigDecimal oPos = null;
1462: int iLen = 0;
1463:
1464: try {
1465: oStmt = oDBF.getConnection().prepareStatement(
1466: "SELECT " + DB.nu_position + ","
1467: + DB.len_mimemsg + " FROM "
1468: + DB.k_mime_msgs + " WHERE "
1469: + DB.gu_mimemsg + "=?",
1470: ResultSet.TYPE_FORWARD_ONLY,
1471: ResultSet.CONCUR_READ_ONLY);
1472: oStmt.setString(1, getMessageGuid());
1473: oRSet = oStmt.executeQuery();
1474: boolean bFound = oRSet.next();
1475: if (bFound) {
1476: oPos = oRSet.getBigDecimal(1);
1477: iLen = oRSet.getInt(2);
1478: }
1479: oRSet.close();
1480: oRSet = null;
1481: oStmt.close();
1482: oStmt = null;
1483:
1484: if (!bFound) {
1485: if (DebugFile.trace)
1486: DebugFile.writeln("MimeMessage.writeTo("
1487: + oOutStrm.getClass().getName() + ")");
1488: super .writeTo(oOutStrm);
1489: if (DebugFile.trace) {
1490: DebugFile.decIdent();
1491: DebugFile
1492: .writeln("End DBMimeMessage.writeTo()");
1493: }
1494: return;
1495: } // fi (!bFound)
1496: } catch (SQLException sqle) {
1497: if (oRSet != null) {
1498: try {
1499: oRSet.close();
1500: } catch (Exception ignore) {
1501: }
1502: }
1503: if (oStmt != null) {
1504: try {
1505: oStmt.close();
1506: } catch (Exception ignore) {
1507: }
1508: }
1509: }
1510:
1511: File oFile = oDBF.getFile();
1512: MboxFile oMBox = new MboxFile(oFile, MboxFile.READ_ONLY);
1513:
1514: InputStream oInStrm = oMBox.getMessageAsStream(oPos
1515: .longValue(), iLen);
1516: StreamPipe oPipe = new StreamPipe();
1517: oPipe.between(oInStrm, oOutStrm);
1518: oInStrm.close();
1519: oMBox.close();
1520: } else {
1521: Multipart oDBParts = getParts();
1522: MimeMultipart oMimeParts = new MimeMultipart();
1523:
1524: for (int p = 0; p < oDBParts.getCount(); p++) {
1525: oMimeParts.addBodyPart(oDBParts.getBodyPart(p));
1526: super .setContent(oMimeParts);
1527: }
1528: super .writeTo(oOutStrm);
1529: }
1530: if (DebugFile.trace) {
1531: DebugFile.decIdent();
1532: DebugFile.writeln("End DBMimeMessage.writeTo()");
1533: }
1534: }
1535:
1536: // ----------------------------------------------------------------------------------------
1537:
1538: public String tagBodyHtml() throws java.io.IOException,
1539: javax.mail.MessagingException {
1540:
1541: if (com.knowgate.debug.DebugFile.trace) {
1542: com.knowgate.debug.DebugFile
1543: .writeln("Begin DBMimeMessage.tagBodyHtml()");
1544: com.knowgate.debug.DebugFile.incIdent();
1545: }
1546:
1547: StringBuffer oText = new StringBuffer();
1548:
1549: oText
1550: .append("<P><BR>------------------------------------------------------------<BR>");
1551:
1552: com.knowgate.hipermail.DBInetAddr oAddr;
1553:
1554: oAddr = (DBInetAddr) this .getFromRecipient();
1555: oText.append("<B>From:</B> <A HREF=\"mailto:"
1556: + oAddr.getAddress() + "\"><");
1557: if (oAddr.getPersonal() != null)
1558: oText.append(oAddr.getPersonal());
1559: else
1560: oText.append(oAddr.getAddress());
1561: oText.append("></A><BR>");
1562:
1563: aAddrs = this .getRecipients(Message.RecipientType.TO);
1564: if (aAddrs != null) {
1565: if (aAddrs.length > 0) {
1566: oText.append("<B>To:</B> ");
1567: for (int a = 0; a < aAddrs.length; a++) {
1568: oAddr = (DBInetAddr) aAddrs[a];
1569: oText.append("<<A HREF=\"mailto:"
1570: + oAddr.getAddress() + "\">");
1571: if (oAddr.getPersonal() != null)
1572: oText.append(oAddr.getPersonal());
1573: else
1574: oText.append(oAddr.getAddress());
1575: oText.append("</A>>; ");
1576: } // next
1577: oText.append("<BR>");
1578: }
1579: } // fi (aAddrs.TO)
1580:
1581: aAddrs = this .getRecipients(Message.RecipientType.CC);
1582: if (aAddrs != null) {
1583: if (aAddrs.length > 0) {
1584: oText.append("<B>CC:</B> ");
1585: for (int a = 0; a < aAddrs.length; a++) {
1586: oAddr = (DBInetAddr) aAddrs[a];
1587: oText.append("<<A HREF=\"mailto:"
1588: + oAddr.getAddress() + "\">");
1589: if (oAddr.getPersonal() != null)
1590: oText.append(oAddr.getPersonal());
1591: else
1592: oText.append(oAddr.getAddress());
1593: oText.append("</A>>; ");
1594: } // next
1595: oText.append("<BR>");
1596: }
1597: } // fi (aAddrs.CC)
1598:
1599: aAddrs = this .getRecipients(Message.RecipientType.BCC);
1600: if (aAddrs != null) {
1601: if (aAddrs.length > 0) {
1602: oText.append("<B>BCC:</B> ");
1603: for (int a = 0; a < aAddrs.length; a++) {
1604: oAddr = (DBInetAddr) aAddrs[a];
1605: oText.append("<<A HREF=\"mailto:"
1606: + oAddr.getAddress() + "\">");
1607: if (oAddr.getPersonal() != null)
1608: oText.append(oAddr.getPersonal());
1609: else
1610: oText.append(oAddr.getAddress());
1611: oText.append("</A>>; ");
1612: } // next
1613: oText.append("<BR>");
1614: }
1615: } // fi (aAddrs.CCO)
1616:
1617: aAddrs = null;
1618:
1619: Date dtSent = this .getSentDate();
1620: if (dtSent != null) {
1621: SimpleDateFormat dtFmt = new SimpleDateFormat(
1622: "yyyy-MM-dd HH:mm:ss");
1623:
1624: oText.append("<B>Sent:</B> " + dtFmt.format(dtSent)
1625: + "<BR>");
1626: }
1627:
1628: oText.append("</P><BR>");
1629:
1630: this .getTextPlain(oText);
1631:
1632: if (DebugFile.trace) {
1633: DebugFile.decIdent();
1634: DebugFile.writeln("End DBMimeMessage.tagBodyHtml()");
1635: }
1636:
1637: return oText.toString();
1638: } // tagBodyHtml
1639:
1640: // ----------------------------------------------------------------------------------------
1641:
1642: public String tagBodyPlain() throws java.io.IOException,
1643: javax.mail.MessagingException {
1644:
1645: if (com.knowgate.debug.DebugFile.trace) {
1646: com.knowgate.debug.DebugFile
1647: .writeln("Begin DBMimeMessage.tagBodyPlain()");
1648: com.knowgate.debug.DebugFile.incIdent();
1649: }
1650:
1651: StringBuffer oText = new StringBuffer();
1652:
1653: oText
1654: .append("\n------------------------------------------------------------\n");
1655:
1656: com.knowgate.hipermail.DBInetAddr oAddr;
1657:
1658: oAddr = (DBInetAddr) this .getFromRecipient();
1659: oText.append("From: " + oAddr.getAddress());
1660:
1661: javax.mail.Address[] aAddrs;
1662:
1663: aAddrs = this .getRecipients(MimeMessage.RecipientType.TO);
1664: if (aAddrs != null) {
1665: if (aAddrs.length > 0) {
1666: oText.append("To: ");
1667: for (int a = 0; a < aAddrs.length; a++) {
1668: oAddr = (DBInetAddr) aAddrs[a];
1669: oText.append(oAddr.getAddress());
1670: } // next
1671: oText.append("\n");
1672: }
1673: } // fi (aAddrs.TO)
1674:
1675: aAddrs = this .getRecipients(MimeMessage.RecipientType.CC);
1676: if (aAddrs != null) {
1677: if (aAddrs.length > 0) {
1678: oText.append("CC: ");
1679: for (int a = 0; a < aAddrs.length; a++) {
1680: oAddr = (DBInetAddr) aAddrs[a];
1681: oText.append(oAddr.getAddress());
1682: } // next
1683: oText.append("\n");
1684: }
1685: } // fi (aAddrs.CC)
1686:
1687: aAddrs = this .getRecipients(MimeMessage.RecipientType.BCC);
1688: if (aAddrs != null) {
1689: if (aAddrs.length > 0) {
1690: oText.append("BCC: ");
1691: for (int a = 0; a < aAddrs.length; a++) {
1692: oAddr = (DBInetAddr) aAddrs[a];
1693: oText.append(oAddr.getAddress());
1694: } // next
1695: oText.append("\n");
1696: }
1697: } // fi (aAddrs.CCO)
1698:
1699: java.util.Date dtSent = this .getSentDate();
1700: if (dtSent != null) {
1701: java.text.SimpleDateFormat dtFmt = new java.text.SimpleDateFormat(
1702: "yyyy-MM-dd HH:mm:ss");
1703:
1704: oText.append("Sent: " + dtFmt.format(dtSent) + "\n");
1705: }
1706:
1707: oText.append("\n\n");
1708:
1709: this .getTextPlain(oText);
1710:
1711: if (com.knowgate.debug.DebugFile.trace) {
1712: com.knowgate.debug.DebugFile.decIdent();
1713: com.knowgate.debug.DebugFile
1714: .writeln("End DBMimeMessage.tagBodyPlain()");
1715: }
1716:
1717: return oText.toString();
1718: } // tagBodyPlain
1719:
1720: // ---------------------------------------------------------------------------
1721:
1722: /**
1723: * <p>Create an SMTPMessage object from given components</p>
1724: * Depending on what is inside, message structure is as follows :<br>
1725: * <table>
1726: * <tr><td><b>Format/Attachments</b></td><td><b>No</b></td><td><b>Yes</b></td></tr>
1727: * <tr><td><b>plain</b></td><td><b>text/plain</b></td><td><b>multipart/mixed [text/plain, {attachment}]</b></td></tr>
1728: * <tr><td><b>html without images</b></td><td><b>multipart/alternative [text/plain, text/html]</b></td><td><b>multipart/mixed [multipart/alternative [text/plain, text/html], {attachment}]</b></td></tr>
1729: * <tr><td><b>html with images</b></td><td><b>multipart/alternative [text/plain, multipart/related[text/html, {image}]]</b></td><td><b>multipart/mixed [multipart/alternative [text/plain, multipart/related[text/html, {image}]], {attachment}]</b></td></tr>
1730: * </table>
1731: * @param oMailSession Session
1732: * @param sSubject String Message subject or <b>null</b>
1733: * @param sBody String Message text body or <b>null</b>
1734: * @param sId String Contend-ID for message or <b>null</b>
1735: * @param sContentType String should be either "plain" or "html"
1736: * @return SMTPMessage
1737: * @throws IOException
1738: * @throws MessagingException
1739: * @throws SecurityException
1740: * @throws IllegalArgumentException if sContentType is not "plain" or "html"
1741: */
1742: public SMTPMessage composeFinalMessage(Session oMailSession,
1743: String sSubject, String sBody, String sId,
1744: String sContentType) throws IOException,
1745: MessagingException, IllegalArgumentException,
1746: SecurityException {
1747:
1748: if (DebugFile.trace) {
1749: DebugFile
1750: .writeln("Begin DBMimeMessage.composeFinalMessage([Session],"
1751: + sSubject
1752: + ",...,"
1753: + sId
1754: + ","
1755: + sContentType + ")");
1756: }
1757:
1758: if (!"html".equalsIgnoreCase(sContentType)
1759: && !"plain".equalsIgnoreCase(sContentType))
1760: throw new IllegalArgumentException(
1761: "Content Type must be either plain or html but it is "
1762: + sContentType);
1763:
1764: if (DebugFile.trace)
1765: DebugFile.incIdent();
1766:
1767: // If message has no body then send it always as plain text even if marked as HTML
1768: if (null == sBody) {
1769: sBody = "";
1770: sContentType = "plain";
1771: } else if (sBody.length() == 0) {
1772: sContentType = "plain";
1773: }
1774:
1775: SMTPMessage oSentMessage = new SMTPMessage(oMailSession);
1776:
1777: Multipart oDraftParts = getParts();
1778: final int iDraftParts = oDraftParts.getCount();
1779:
1780: if (DebugFile.trace)
1781: DebugFile.writeln("Multipart.getCount() = "
1782: + String.valueOf(iDraftParts));
1783:
1784: MimeBodyPart oMsgPlainText = new MimeBodyPart();
1785: MimeMultipart oSentMsgParts = new MimeMultipart("mixed");
1786:
1787: if (sContentType.equalsIgnoreCase("html")) {
1788:
1789: MimeMultipart oHtmlRelated = new MimeMultipart("related");
1790: MimeMultipart oTextHtmlAlt = new MimeMultipart(
1791: "alternative");
1792:
1793: // ************************************************************************
1794: // Replace image CIDs
1795:
1796: HashMap oDocumentImages = new HashMap(23);
1797:
1798: StringSubstitution oSrcSubs = new StringSubstitution();
1799:
1800: String sText = "";
1801:
1802: Parser oPrsr = Parser.createParser(sBody, getEncoding());
1803:
1804: String sCid, sSrc;
1805:
1806: try {
1807:
1808: // ****************************
1809: // Extract plain text from HTML
1810: if (DebugFile.trace)
1811: DebugFile.writeln("new StringBean()");
1812:
1813: StringBean oStrBn = new StringBean();
1814:
1815: try {
1816: oPrsr.visitAllNodesWith(oStrBn);
1817: } catch (ParserException pe) {
1818: throw new MessagingException(pe.getMessage(), pe);
1819: }
1820:
1821: sText = oStrBn.getStrings();
1822:
1823: oStrBn = null;
1824:
1825: // *******************************
1826: // Set plain text alternative part
1827:
1828: oMsgPlainText.setDisposition("inline");
1829: oMsgPlainText.setContent(sText,
1830: "text/plain; charset=utf-8");
1831: if (DebugFile.trace)
1832: DebugFile
1833: .writeln("MimeBodyPart(multipart/alternative).addBodyPart(text/plain)");
1834: oTextHtmlAlt.addBodyPart(oMsgPlainText);
1835:
1836: // ****************************************
1837: // Iterate images from HTML ad replace CIDs
1838:
1839: NodeList oCollectionList = new NodeList();
1840: TagNameFilter oImgFilter = new TagNameFilter("IMG");
1841: for (NodeIterator e = oPrsr.elements(); e
1842: .hasMoreNodes();)
1843: e.nextNode().collectInto(oCollectionList,
1844: oImgFilter);
1845:
1846: final int nImgs = oCollectionList.size();
1847:
1848: if (DebugFile.trace)
1849: DebugFile.writeln("NodeList.size() = "
1850: + String.valueOf(nImgs));
1851:
1852: for (int i = 0; i < nImgs; i++) {
1853:
1854: sSrc = ((ImageTag) oCollectionList.elementAt(i))
1855: .extractImageLocn();
1856:
1857: // Keep a reference to every related image name so that the same image is not included twice in the message
1858: if (!oDocumentImages.containsKey(sSrc)) {
1859:
1860: // Find last slash from image url
1861: int iSlash = sSrc.lastIndexOf('/');
1862:
1863: // Take image name
1864: if (iSlash >= 0) {
1865: while (sSrc.charAt(iSlash) == '/') {
1866: if (++iSlash == sSrc.length())
1867: break;
1868: }
1869: sCid = sSrc.substring(iSlash);
1870: } else {
1871: sCid = sSrc;
1872: }
1873:
1874: //String sUid = Gadgets.generateUUID();
1875: //sCid = sUid.substring(0,12)+"$"+sUid.substring(12,20)+"$"+sUid.substring(20,28)+"@hipergate.org";
1876:
1877: if (DebugFile.trace)
1878: DebugFile.writeln("HashMap.put(" + sSrc
1879: + "," + sCid + ")");
1880:
1881: oDocumentImages.put(sSrc, sCid);
1882: } // fi (!oDocumentImages.containsKey(sSrc))
1883:
1884: try {
1885: Pattern oPattern = oCompiler.compile(sSrc,
1886: Perl5Compiler.SINGLELINE_MASK);
1887: oSrcSubs.setSubstitution("cid:"
1888: + oDocumentImages.get(sSrc));
1889: if (DebugFile.trace)
1890: DebugFile
1891: .writeln("Util.substitute([PatternMatcher],"
1892: + sSrc
1893: + ",cid:"
1894: + oDocumentImages.get(sSrc)
1895: + ",...)");
1896: sBody = Util.substitute(oMatcher, oPattern,
1897: oSrcSubs, sBody);
1898: } catch (MalformedPatternException neverthrown) {
1899: }
1900:
1901: } // next
1902: } catch (ParserException pe) {
1903: if (DebugFile.trace) {
1904: DebugFile
1905: .writeln("org.htmlparser.util.ParserException "
1906: + pe.getMessage());
1907: }
1908: }
1909: // End replace image CIDs
1910: // ************************************************************************
1911:
1912: // ************************************************************************
1913: // Add HTML related images
1914:
1915: if (oDocumentImages.isEmpty()) {
1916: // Set HTML part
1917: MimeBodyPart oMsgHtml = new MimeBodyPart();
1918: oMsgHtml.setDisposition("inline");
1919: oMsgHtml.setContent(sBody, "text/html; charset=utf-8");
1920: oTextHtmlAlt.addBodyPart(oMsgHtml);
1921: } else {
1922:
1923: // Set HTML text related part
1924:
1925: MimeBodyPart oMsgHtmlText = new MimeBodyPart();
1926: oMsgHtmlText.setDisposition("inline");
1927: oMsgHtmlText.setContent(sBody,
1928: "text/html; charset=utf-8");
1929: if (DebugFile.trace)
1930: DebugFile
1931: .writeln("MimeBodyPart(multipart/related).addBodyPart(text/html)");
1932: oHtmlRelated.addBodyPart(oMsgHtmlText);
1933:
1934: // Set HTML text related inline images
1935:
1936: Iterator oImgs = oDocumentImages.keySet().iterator();
1937:
1938: while (oImgs.hasNext()) {
1939: BodyPart oImgBodyPart = new MimeBodyPart();
1940:
1941: sSrc = (String) oImgs.next();
1942: sCid = (String) oDocumentImages.get(sSrc);
1943:
1944: if (sSrc.startsWith("www."))
1945: sSrc = "http://" + sSrc;
1946:
1947: if (sSrc.startsWith("http://")
1948: || sSrc.startsWith("https://")) {
1949: oImgBodyPart.setDataHandler(new DataHandler(
1950: new URL(sSrc)));
1951: } else {
1952: oImgBodyPart.setDataHandler(new DataHandler(
1953: new FileDataSource(sSrc)));
1954: }
1955:
1956: oImgBodyPart.setDisposition("inline");
1957: oImgBodyPart.setHeader("Content-ID", sCid);
1958: oImgBodyPart.setFileName(sCid);
1959:
1960: // Add image to multi-part
1961: if (DebugFile.trace)
1962: DebugFile
1963: .writeln("MimeBodyPart(multipart/related).addBodyPart("
1964: + sCid + ")");
1965: oHtmlRelated.addBodyPart(oImgBodyPart);
1966: } // wend
1967:
1968: // Set html text alternative part (html text + inline images)
1969: MimeBodyPart oTextHtmlRelated = new MimeBodyPart();
1970: oTextHtmlRelated.setContent(oHtmlRelated);
1971: if (DebugFile.trace)
1972: DebugFile
1973: .writeln("MimeBodyPart(multipart/alternative).addBodyPart(multipart/related)");
1974: oTextHtmlAlt.addBodyPart(oTextHtmlRelated);
1975: }
1976:
1977: // ************************************************************************
1978: // Create message to be sent and add main text body to it
1979:
1980: if (0 == iDraftParts) {
1981: oSentMessage.setContent(oTextHtmlAlt);
1982: } else {
1983: MimeBodyPart oMixedPart = new MimeBodyPart();
1984: oMixedPart.setContent(oTextHtmlAlt);
1985: oSentMsgParts.addBodyPart(oMixedPart);
1986: }
1987:
1988: } else { // (sContentType=="plain")
1989:
1990: // *************************************************
1991: // If this is a plain text message just add the text
1992:
1993: if (0 == iDraftParts) {
1994: oSentMessage.setText(sBody, "utf-8");
1995: } else {
1996: oMsgPlainText.setDisposition("inline");
1997: oMsgPlainText.setContent(sBody,
1998: "text/plain; charset=utf-8");
1999: if (DebugFile.trace)
2000: DebugFile
2001: .writeln("MimeBodyPart(multipart/mixed).addBodyPart(text/plain)");
2002: oSentMsgParts.addBodyPart(oMsgPlainText);
2003: }
2004: }
2005: // fi (sContentType=="html")
2006:
2007: // ************************************************************************
2008: // Add attachments to message to be sent
2009:
2010: if (iDraftParts > 0) {
2011:
2012: for (int p = 0; p < iDraftParts; p++) {
2013: DBMimePart oPart = (DBMimePart) oDraftParts
2014: .getBodyPart(p);
2015:
2016: String sDisposition = oPart.getDisposition();
2017: if (sDisposition == null)
2018: sDisposition = "inline";
2019: else if (sDisposition.equals("reference")
2020: || sDisposition.equals("pointer"))
2021: sDisposition = "attachment";
2022:
2023: int iSize = oPart.getSize();
2024: if (iSize <= 0)
2025: iSize = 4000;
2026: InputStream oInStrm = oPart.getInputStream();
2027: ByteArrayOutputStream oByStrm = new java.io.ByteArrayOutputStream(
2028: iSize);
2029: new StreamPipe().between(oInStrm, oByStrm);
2030: oInStrm.close();
2031:
2032: if (DebugFile.trace)
2033: DebugFile.writeln("part " + String.valueOf(p)
2034: + " size is "
2035: + String.valueOf(oByStrm.size()));
2036:
2037: ByteArrayDataSource oDataSrc = new ByteArrayDataSource(
2038: oByStrm.toByteArray(), oPart.getContentType());
2039: MimeBodyPart oAttachment = new MimeBodyPart();
2040: oAttachment.setDisposition(sDisposition);
2041: if (sDisposition.equals("attachment"))
2042: if (null == oPart.getDescription())
2043: oAttachment.setFileName(oPart.getFileName());
2044: else
2045: oAttachment.setFileName(oPart.getDescription());
2046: oAttachment.setHeader("Content-Transfer-Encoding",
2047: "base64");
2048: oAttachment.setDataHandler(new DataHandler(oDataSrc));
2049: oSentMsgParts.addBodyPart(oAttachment);
2050: } // next
2051: oSentMessage.setContent(oSentMsgParts);
2052: } // fi (iDraftParts>0)
2053:
2054: if (null != sSubject)
2055: oSentMessage.setSubject(sSubject);
2056:
2057: if (sId != null)
2058: if (sId.trim().length() > 0)
2059: oSentMessage.setContentID(sId);
2060:
2061: if (DebugFile.trace) {
2062: DebugFile.decIdent();
2063: DebugFile
2064: .writeln("End DBMimeMessage.composeFinalMessage()");
2065: }
2066:
2067: return oSentMessage;
2068: } // composeFinalMessage
2069:
2070: // ---------------------------------------------------------------------------
2071:
2072: /**
2073: * <p>Delete message from database</p>
2074: * This method calls stored procedure k_sp_del_mime_msg<br>
2075: * @param oConn JDBC database connection
2076: * @param sFolderId Folder GUID (k_mime_msgs.gu_category)
2077: * @param sMimeMsgId Message GUID (k_mime_msgs.gu_mimemsg)
2078: * @throws SQLException
2079: */
2080:
2081: public static void delete(JDCConnection oConn, String sFolderId,
2082: String sMimeMsgId) throws SQLException, IOException {
2083: Statement oStmt;
2084: CallableStatement oCall;
2085:
2086: if (DebugFile.trace) {
2087: DebugFile
2088: .writeln("Begin DBMimeMessage.delete([Connection], "
2089: + sMimeMsgId + ")");
2090: DebugFile.incIdent();
2091: }
2092:
2093: oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
2094: ResultSet.CONCUR_READ_ONLY);
2095: ResultSet oRSet = oStmt.executeQuery("SELECT " + DB.file_name
2096: + " FROM " + DB.k_mime_parts + " WHERE "
2097: + DB.gu_mimemsg + "='" + sMimeMsgId + "' AND "
2098: + DB.id_disposition + "='reference'");
2099:
2100: while (oRSet.next()) {
2101: String sFileName = oRSet.getString(1);
2102: if (!oRSet.wasNull()) {
2103: try {
2104: File oRef = new File(sFileName);
2105: oRef.delete();
2106: } catch (SecurityException se) {
2107: if (DebugFile.trace)
2108: DebugFile.writeln("SecurityException "
2109: + sFileName + " " + se.getMessage());
2110: }
2111: }
2112: } // wend
2113:
2114: oRSet.close();
2115: oRSet = null;
2116: oStmt.close();
2117: oStmt = null;
2118:
2119: if (oConn.getDataBaseProduct() == JDCConnection.DBMS_POSTGRESQL) {
2120: oStmt = oConn.createStatement();
2121: oStmt.executeQuery("SELECT k_sp_del_mime_msg('"
2122: + sMimeMsgId + "')");
2123: oStmt.close();
2124: } else {
2125: oCall = oConn.prepareCall("{ call k_sp_del_mime_msg(?) }");
2126: oCall.setString(1, sMimeMsgId);
2127: oCall.execute();
2128: oCall.close();
2129: }
2130:
2131: if (DebugFile.trace) {
2132: DebugFile.decIdent();
2133: DebugFile.writeln("End DBMimeMessage.delete()");
2134: }
2135: } // delete
2136:
2137: // ---------------------------------------------------------------------------
2138:
2139: public static String source(MimeMessage oMsg, String sEncoding)
2140: throws MessagingException, UnsupportedEncodingException,
2141: IOException {
2142:
2143: if (DebugFile.trace) {
2144: DebugFile
2145: .writeln("Begin DBMimeMessage.source([MimeMessage], "
2146: + sEncoding + ")");
2147: DebugFile.incIdent();
2148: }
2149:
2150: final int iSize = oMsg.getSize();
2151: if (DebugFile.trace)
2152: DebugFile.writeln("size=" + String.valueOf(iSize));
2153: ByteArrayOutputStream byOutStrm = new ByteArrayOutputStream(
2154: iSize > 0 ? iSize : 16000);
2155: oMsg.writeTo(byOutStrm);
2156: String sSrc = byOutStrm.toString(sEncoding);
2157: byOutStrm.close();
2158:
2159: if (DebugFile.trace) {
2160: DebugFile.decIdent();
2161: if (null == sSrc)
2162: DebugFile.writeln("End DBMimeMessage.source() : null");
2163: else
2164: DebugFile.writeln("End DBMimeMessage.source() : "
2165: + String.valueOf(sSrc.length()));
2166: }
2167: return sSrc;
2168: } // source
2169:
2170: // ---------------------------------------------------------------------------
2171:
2172: public static String getGuidFromId(JDCConnection oConn,
2173: String sMsgId) throws SQLException {
2174: String sMsgGuid;
2175:
2176: switch (oConn.getDataBaseProduct()) {
2177: case JDCConnection.DBMS_POSTGRESQL:
2178: PreparedStatement oStmt = oConn
2179: .prepareStatement("SELECT k_sp_get_mime_msg(?)");
2180: oStmt.setString(1, sMsgId);
2181: ResultSet oRSet = oStmt.executeQuery();
2182: oRSet.next();
2183: sMsgGuid = oRSet.getString(1);
2184: oRSet.close();
2185: oRSet = null;
2186: oStmt.close();
2187: oStmt = null;
2188: break;
2189: default:
2190: CallableStatement oCall = oConn
2191: .prepareCall("{ call k_sp_get_mime_msg(?,?) }");
2192: oCall.setString(1, sMsgId);
2193: oCall.registerOutParameter(2, Types.CHAR);
2194: oCall.execute();
2195: sMsgGuid = oCall.getString(2);
2196: if (sMsgGuid != null)
2197: sMsgGuid = sMsgGuid.trim();
2198: oCall.close();
2199: oCall = null;
2200: }
2201: return sMsgGuid;
2202: }
2203:
2204: // ===========================================================================
2205: public static final short ClassId = 822;
2206: }
|