001: /*
002: Copyright (C) 2004 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.hipermail;
034:
035: import java.io.OutputStream;
036: import java.io.IOException;
037: import java.io.File;
038:
039: import java.sql.Statement;
040: import java.sql.ResultSet;
041: import java.sql.SQLException;
042:
043: import javax.mail.MessagingException;
044: import javax.mail.Multipart;
045: import javax.mail.BodyPart;
046: import javax.mail.Part;
047:
048: import javax.mail.internet.MimePart;
049: import javax.mail.internet.MimeMultipart;
050: import javax.mail.internet.MimeBodyPart;
051:
052: import java.util.Vector;
053:
054: import com.knowgate.dataobjs.DB;
055: import com.knowgate.debug.DebugFile;
056:
057: /**
058: * @author Sergio Montoro Ten
059: * @version 1.0
060: */
061:
062: public class DBMimeMultipart extends Multipart {
063: private Vector aParts = new Vector();
064: private Part oParent;
065:
066: public DBMimeMultipart(Part oMessage) {
067: oParent = oMessage;
068: }
069:
070: // ---------------------------------------------------------------------------
071:
072: public Part getParent() {
073: return oParent;
074: }
075:
076: // ---------------------------------------------------------------------------
077:
078: public void addBodyPart(MimePart part) throws MessagingException {
079: aParts.add(part);
080: }
081:
082: // ---------------------------------------------------------------------------
083:
084: public int getCount() {
085: return aParts.size();
086: }
087:
088: // ---------------------------------------------------------------------------
089:
090: public BodyPart getBodyPart(int index) throws MessagingException {
091: BodyPart oRetVal = null;
092: try {
093: oRetVal = (BodyPart) aParts.get(index);
094: } catch (ArrayIndexOutOfBoundsException aiob) {
095: throw new MessagingException("Invalid message part index",
096: aiob);
097: }
098: return oRetVal;
099: }
100:
101: // ---------------------------------------------------------------------------
102:
103: /**
104: * <p>Get body part given its Content-Id</p>
105: * @param cid String Content-Id
106: * @return BodyPart or <b>null</b> if no body part with such Content-Id is found
107: * @throws MessagingException
108: */
109: public BodyPart getBodyPart(String cid) throws MessagingException {
110: Object oPart = null;
111:
112: if (DebugFile.trace) {
113: DebugFile.writeln("Begin DBMimeMultipart.getBodyPart("
114: + cid + ")");
115: DebugFile.incIdent();
116: }
117:
118: if (cid != null) {
119: final int iParts = aParts.size();
120: if (DebugFile.trace)
121: DebugFile.writeln("MimeMultiPart has "
122: + String.valueOf(iParts) + " parts");
123: for (int p = 0; p < iParts; p++) {
124: oPart = aParts.get(p);
125: if (DebugFile.trace)
126: DebugFile.writeln("Checking part "
127: + String.valueOf(p) + " with Content-Id "
128: + ((MimePart) oPart).getContentID());
129: if (cid.equals(((MimePart) oPart).getContentID()))
130: break;
131: else
132: oPart = null;
133: } // next
134: } // fi (cid != null)
135:
136: if (DebugFile.trace) {
137: DebugFile.decIdent();
138: DebugFile.writeln("End DBMimeMultipart.getBodyPart() :"
139: + oPart);
140: }
141:
142: return (BodyPart) oPart;
143: } // getBodyPart
144:
145: // ---------------------------------------------------------------------------
146:
147: public void removeBodyPart(int iPart) throws MessagingException,
148: ArrayIndexOutOfBoundsException {
149:
150: if (DebugFile.trace) {
151: DebugFile.writeln("Begin DBMimeMultipart.removeBodyPart("
152: + String.valueOf(iPart) + ")");
153: DebugFile.incIdent();
154: }
155:
156: DBMimeMessage oMsg = (DBMimeMessage) getParent();
157: DBFolder oFldr = ((DBFolder) oMsg.getFolder());
158: Statement oStmt = null;
159: ResultSet oRSet = null;
160: String sDisposition = null, sFileName = null;
161: boolean bFound;
162:
163: try {
164: oStmt = oFldr.getConnection().createStatement(
165: ResultSet.TYPE_FORWARD_ONLY,
166: ResultSet.CONCUR_READ_ONLY);
167:
168: if (DebugFile.trace)
169: DebugFile.writeln("Statement.executeQuery(SELECT "
170: + DB.id_disposition + "," + DB.file_name
171: + " FROM " + DB.k_mime_parts + " WHERE "
172: + DB.gu_mimemsg + "='" + oMsg.getMessageGuid()
173: + "' AND " + DB.id_part + "="
174: + String.valueOf(iPart) + ")");
175:
176: oRSet = oStmt.executeQuery("SELECT " + DB.id_disposition
177: + "," + DB.file_name + " FROM " + DB.k_mime_parts
178: + " WHERE " + DB.gu_mimemsg + "='"
179: + oMsg.getMessageGuid() + "' AND " + DB.id_part
180: + "=" + String.valueOf(iPart));
181: bFound = oRSet.next();
182:
183: if (bFound) {
184: sDisposition = oRSet.getString(1);
185: if (oRSet.wasNull())
186: sDisposition = "inline";
187: sFileName = oRSet.getString(2);
188: }
189:
190: oRSet.close();
191: oRSet = null;
192: oStmt.close();
193: oStmt = null;
194:
195: if (!bFound) {
196: if (DebugFile.trace)
197: DebugFile.decIdent();
198: throw new MessagingException("Part not found");
199: }
200: if (!sDisposition.equals("reference")
201: && !sDisposition.equals("pointer")) {
202: if (DebugFile.trace)
203: DebugFile.decIdent();
204: throw new MessagingException(
205: "Only parts with reference or pointer disposition can be removed from a message");
206: } else {
207: if (sDisposition.equals("reference")) {
208: try {
209: File oRef = new File(sFileName);
210: if (oRef.exists())
211: oRef.delete();
212: } catch (SecurityException se) {
213: if (DebugFile.trace)
214: DebugFile
215: .writeln("SecurityException "
216: + sFileName + " "
217: + se.getMessage());
218: if (DebugFile.trace)
219: DebugFile.decIdent();
220: throw new MessagingException(
221: "SecurityException " + sFileName + " "
222: + se.getMessage(), se);
223: }
224: } // fi (reference)
225:
226: oStmt = oFldr.getConnection().createStatement();
227: if (DebugFile.trace)
228: DebugFile
229: .writeln("Statement.executeUpdate(DELETE FROM "
230: + DB.k_mime_parts
231: + " WHERE "
232: + DB.gu_mimemsg
233: + "='"
234: + oMsg.getMessageGuid()
235: + "' AND "
236: + DB.id_part
237: + "="
238: + String.valueOf(iPart) + ")");
239: oStmt.executeUpdate("DELETE FROM " + DB.k_mime_parts
240: + " WHERE " + DB.gu_mimemsg + "='"
241: + oMsg.getMessageGuid() + "' AND " + DB.id_part
242: + "=" + String.valueOf(iPart));
243: oStmt.close();
244: oStmt = null;
245: oFldr.getConnection().commit();
246: }
247: } catch (SQLException sqle) {
248: if (oRSet != null) {
249: try {
250: oRSet.close();
251: } catch (Exception ignore) {
252: }
253: }
254: if (oStmt != null) {
255: try {
256: oStmt.close();
257: } catch (Exception ignore) {
258: }
259: }
260: try {
261: oFldr.getConnection().rollback();
262: } catch (Exception ignore) {
263: }
264: if (DebugFile.trace)
265: DebugFile.decIdent();
266: throw new MessagingException(sqle.getMessage(), sqle);
267: }
268:
269: if (DebugFile.trace) {
270: DebugFile.decIdent();
271: DebugFile.writeln("End DBMimeMultipart.removeBodyPart()");
272: }
273: } // removeBodyPart
274:
275: // ---------------------------------------------------------------------------
276:
277: public void writeTo(OutputStream os) throws IOException,
278: MessagingException {
279: throw new UnsupportedOperationException(
280: "Method writeTo() not implemented for DBMimeMultipart");
281: }
282:
283: // ---------------------------------------------------------------------------
284:
285: }
|