1 32 33 package com.knowgate.hipermail; 34 35 import java.io.OutputStream ; 36 import java.io.IOException ; 37 import java.io.File ; 38 39 import java.sql.Statement ; 40 import java.sql.ResultSet ; 41 import java.sql.SQLException ; 42 43 import javax.mail.MessagingException ; 44 import javax.mail.Multipart ; 45 import javax.mail.BodyPart ; 46 import javax.mail.Part ; 47 48 import javax.mail.internet.MimePart ; 49 import javax.mail.internet.MimeMultipart ; 50 import javax.mail.internet.MimeBodyPart ; 51 52 import java.util.Vector ; 53 54 import com.knowgate.dataobjs.DB; 55 import com.knowgate.debug.DebugFile; 56 57 61 62 public class DBMimeMultipart extends Multipart { 63 private Vector aParts = new Vector (); 64 private Part oParent; 65 66 public DBMimeMultipart(Part oMessage) { 67 oParent = oMessage; 68 } 69 70 72 public Part getParent() { 73 return oParent; 74 } 75 76 78 public void addBodyPart(MimePart part) 79 throws MessagingException { 80 aParts.add(part); 81 } 82 83 85 public int getCount() { 86 return aParts.size(); 87 } 88 89 91 public BodyPart getBodyPart(int index) 92 throws MessagingException { 93 BodyPart oRetVal = null; 94 try { 95 oRetVal = (BodyPart ) aParts.get(index); 96 } 97 catch (ArrayIndexOutOfBoundsException aiob) { 98 throw new MessagingException ("Invalid message part index", aiob); 99 } 100 return oRetVal; 101 } 102 103 105 111 public BodyPart getBodyPart(String cid) 112 throws MessagingException { 113 Object 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 )oPart).getContentID()); 126 if (cid.equals(((MimePart )oPart).getContentID())) 127 break; 128 else 129 oPart = null; 130 } } 133 if (DebugFile.trace) { 134 DebugFile.decIdent(); 135 DebugFile.writeln("End DBMimeMultipart.getBodyPart() :" + oPart); 136 } 137 138 return (BodyPart ) oPart; 139 } 141 143 public void removeBodyPart (int iPart) 144 throws MessagingException , ArrayIndexOutOfBoundsException { 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 oStmt = null; 154 ResultSet oRSet = null; 155 String 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 ("Part not found"); 180 } 181 if (!sDisposition.equals("reference") && !sDisposition.equals("pointer")) { 182 if (DebugFile.trace) DebugFile.decIdent(); 183 throw new MessagingException ("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 oRef = new File (sFileName); 189 if (oRef.exists()) 190 oRef.delete(); 191 } 192 catch (SecurityException se) { 193 if (DebugFile.trace) DebugFile.writeln("SecurityException " + sFileName + " " + se.getMessage()); 194 if (DebugFile.trace) DebugFile.decIdent(); 195 throw new MessagingException ("SecurityException " + sFileName + " " + se.getMessage(), se); 196 } 197 } 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 sqle) { 208 if (oRSet!=null) { try { oRSet.close(); } catch (Exception ignore) {} } 209 if (oStmt!=null) { try { oStmt.close(); } catch (Exception ignore) {} } 210 try { oFldr.getConnection().rollback(); } catch (Exception ignore) {} 211 if (DebugFile.trace) DebugFile.decIdent(); 212 throw new MessagingException (sqle.getMessage(), sqle); 213 } 214 215 if (DebugFile.trace) { 216 DebugFile.decIdent(); 217 DebugFile.writeln("End DBMimeMultipart.removeBodyPart()"); 218 } 219 } 221 223 public void writeTo(OutputStream os) 224 throws IOException , MessagingException { 225 throw new UnsupportedOperationException ("Method writeTo() not implemented for DBMimeMultipart"); 226 } 227 228 230 } 231 | Popular Tags |