KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > hipermail > DBMimeMultipart


1 /*
2   Copyright (C) 2004 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.hipermail;
34
35 import java.io.OutputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.File JavaDoc;
38
39 import java.sql.Statement JavaDoc;
40 import java.sql.ResultSet JavaDoc;
41 import java.sql.SQLException JavaDoc;
42
43 import javax.mail.MessagingException JavaDoc;
44 import javax.mail.Multipart JavaDoc;
45 import javax.mail.BodyPart JavaDoc;
46 import javax.mail.Part JavaDoc;
47
48 import javax.mail.internet.MimePart JavaDoc;
49 import javax.mail.internet.MimeMultipart JavaDoc;
50 import javax.mail.internet.MimeBodyPart JavaDoc;
51
52 import java.util.Vector JavaDoc;
53
54 import com.knowgate.dataobjs.DB;
55 import com.knowgate.debug.DebugFile;
56
57 /**
58  * @author Sergio Montoro Ten
59  * @version 1.0
60  */

61
62 public class DBMimeMultipart extends Multipart JavaDoc {
63   private Vector JavaDoc aParts = new Vector JavaDoc();
64   private Part JavaDoc oParent;
65
66   public DBMimeMultipart(Part JavaDoc oMessage) {
67     oParent = oMessage;
68   }
69
70   // ---------------------------------------------------------------------------
71

72   public Part JavaDoc getParent() {
73     return oParent;
74   }
75
76   // ---------------------------------------------------------------------------
77

78   public void addBodyPart(MimePart JavaDoc part)
79     throws MessagingException JavaDoc {
80     aParts.add(part);
81   }
82
83   // ---------------------------------------------------------------------------
84

85   public int getCount() {
86     return aParts.size();
87   }
88
89   // ---------------------------------------------------------------------------
90

91   public BodyPart JavaDoc getBodyPart(int index)
92     throws MessagingException JavaDoc {
93     BodyPart JavaDoc oRetVal = null;
94     try {
95       oRetVal = (BodyPart JavaDoc) aParts.get(index);
96     }
97     catch (ArrayIndexOutOfBoundsException JavaDoc aiob) {
98       throw new MessagingException JavaDoc("Invalid message part index", aiob);
99     }
100     return oRetVal;
101   }
102
103   // ---------------------------------------------------------------------------
104

105   /**
106    * <p>Get body part given its Content-Id</p>
107    * @param cid String Content-Id
108    * @return BodyPart or <b>null</b> if no body part with such Content-Id is found
109    * @throws MessagingException
110    */

111   public BodyPart JavaDoc getBodyPart(String JavaDoc cid)
112     throws MessagingException JavaDoc {
113     Object JavaDoc oPart = null;
114
115     if (DebugFile.trace) {
116       DebugFile.writeln("Begin DBMimeMultipart.getBodyPart("+cid+")");
117       DebugFile.incIdent();
118     }
119
120     if (cid!=null) {
121       final int iParts = aParts.size();
122       if (DebugFile.trace) DebugFile.writeln("MimeMultiPart has "+String.valueOf(iParts)+" parts");
123       for (int p=0; p<iParts; p++) {
124         oPart = aParts.get(p);
125         if (DebugFile.trace) DebugFile.writeln("Checking part "+String.valueOf(p)+" with Content-Id "+((MimePart JavaDoc)oPart).getContentID());
126         if (cid.equals(((MimePart JavaDoc)oPart).getContentID()))
127           break;
128         else
129           oPart = null;
130       } // next
131
} // fi (cid != null)
132

133     if (DebugFile.trace) {
134       DebugFile.decIdent();
135       DebugFile.writeln("End DBMimeMultipart.getBodyPart() :" + oPart);
136     }
137
138     return (BodyPart JavaDoc) oPart;
139   } // getBodyPart
140

141   // ---------------------------------------------------------------------------
142

143   public void removeBodyPart (int iPart)
144     throws MessagingException JavaDoc, ArrayIndexOutOfBoundsException JavaDoc {
145
146     if (DebugFile.trace) {
147       DebugFile.writeln("Begin DBMimeMultipart.removeBodyPart("+String.valueOf(iPart)+")");
148       DebugFile.incIdent();
149     }
150
151     DBMimeMessage oMsg = (DBMimeMessage) getParent();
152     DBFolder oFldr = ((DBFolder)oMsg.getFolder());
153     Statement JavaDoc oStmt = null;
154     ResultSet JavaDoc oRSet = null;
155     String JavaDoc sDisposition = null, sFileName = null;
156     boolean bFound;
157
158     try {
159       oStmt = oFldr.getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
160
161       if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(SELECT " + DB.id_disposition + "," + DB.file_name + " FROM " + DB.k_mime_parts + " WHERE " + DB.gu_mimemsg + "='" + oMsg.getMessageGuid() + "' AND " + DB.id_part + "=" + String.valueOf(iPart)+ ")");
162
163       oRSet = oStmt.executeQuery("SELECT " + DB.id_disposition + "," + DB.file_name + " FROM " + DB.k_mime_parts + " WHERE " + DB.gu_mimemsg + "='" + oMsg.getMessageGuid() + "' AND " + DB.id_part + "=" + String.valueOf(iPart));
164       bFound = oRSet.next();
165
166       if (bFound) {
167         sDisposition = oRSet.getString(1);
168         if (oRSet.wasNull()) sDisposition = "inline";
169         sFileName = oRSet.getString(2);
170       }
171
172       oRSet.close();
173       oRSet = null;
174       oStmt.close();
175       oStmt = null;
176
177       if (!bFound) {
178         if (DebugFile.trace) DebugFile.decIdent();
179         throw new MessagingException JavaDoc("Part not found");
180       }
181       if (!sDisposition.equals("reference") && !sDisposition.equals("pointer")) {
182         if (DebugFile.trace) DebugFile.decIdent();
183         throw new MessagingException JavaDoc("Only parts with reference or pointer disposition can be removed from a message");
184       }
185       else {
186         if (sDisposition.equals("reference")) {
187           try {
188           File JavaDoc oRef = new File JavaDoc(sFileName);
189           if (oRef.exists())
190             oRef.delete();
191           }
192           catch (SecurityException JavaDoc se) {
193             if (DebugFile.trace) DebugFile.writeln("SecurityException " + sFileName + " " + se.getMessage());
194             if (DebugFile.trace) DebugFile.decIdent();
195             throw new MessagingException JavaDoc("SecurityException " + sFileName + " " + se.getMessage(), se);
196           }
197         } // fi (reference)
198

199         oStmt = oFldr.getConnection().createStatement();
200         if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_mime_parts + " WHERE " + DB.gu_mimemsg + "='" + oMsg.getMessageGuid() + "' AND " + DB.id_part + "=" + String.valueOf(iPart)+")");
201         oStmt.executeUpdate("DELETE FROM " + DB.k_mime_parts + " WHERE " + DB.gu_mimemsg + "='" + oMsg.getMessageGuid() + "' AND " + DB.id_part + "=" + String.valueOf(iPart));
202         oStmt.close();
203         oStmt = null;
204         oFldr.getConnection().commit();
205       }
206     }
207     catch (SQLException JavaDoc sqle) {
208       if (oRSet!=null) { try { oRSet.close(); } catch (Exception JavaDoc ignore) {} }
209       if (oStmt!=null) { try { oStmt.close(); } catch (Exception JavaDoc ignore) {} }
210       try { oFldr.getConnection().rollback(); } catch (Exception JavaDoc ignore) {}
211       if (DebugFile.trace) DebugFile.decIdent();
212       throw new MessagingException JavaDoc (sqle.getMessage(), sqle);
213     }
214
215     if (DebugFile.trace) {
216       DebugFile.decIdent();
217       DebugFile.writeln("End DBMimeMultipart.removeBodyPart()");
218     }
219   } // removeBodyPart
220

221   // ---------------------------------------------------------------------------
222

223   public void writeTo(OutputStream JavaDoc os)
224     throws IOException JavaDoc, MessagingException JavaDoc {
225     throw new UnsupportedOperationException JavaDoc("Method writeTo() not implemented for DBMimeMultipart");
226   }
227
228   // ---------------------------------------------------------------------------
229

230 }
231
Popular Tags