1 package net.suberic.pooka; 2 import java.util.*; 3 import java.io.IOException ; 4 import javax.mail.internet.*; 5 import javax.mail.*; 6 7 8 13 class AttachmentBundle { 14 private Attachment textPart = null; 15 private Vector allAttachments = new Vector(); 16 Vector attachmentsAndTextPart = null; 17 InternetHeaders headers = null; 18 Vector headerLines = null; 19 20 23 AttachmentBundle() { 24 } 25 26 29 AttachmentBundle(MimePart m) throws MessagingException { 30 setHeaderSource(m); 31 } 32 33 38 synchronized void addAll(AttachmentBundle subBundle) { 39 if (subBundle.textPart != null) 40 subBundle.textPart.setHeaderSource(subBundle); 41 42 if (textPart == null) { 43 textPart = subBundle.textPart; 44 } else if (textPart instanceof net.suberic.pooka.crypto.CryptoAttachment) { 45 if (subBundle.textPart != null) { 46 textPart = subBundle.textPart; 47 } 48 } else if (subBundle.textPart != null) { 49 allAttachments.add(subBundle.textPart); 50 } 51 52 allAttachments.addAll(subBundle.allAttachments); 53 } 54 55 59 synchronized void addAttachment(Attachment newAttach) { 60 addAttachment(newAttach, newAttach.getMimeType(), true); 61 } 62 63 70 synchronized void addAttachment(Attachment newAttach, boolean allowTextPart) { 71 addAttachment(newAttach, newAttach.getMimeType(), allowTextPart); 72 } 73 74 77 synchronized void removeAttachment(Attachment delAttach) { 78 if (textPart == delAttach) { 79 textPart = null; 80 Iterator it = allAttachments.iterator(); 81 while (textPart == null && it.hasNext()) { 82 Attachment at = (Attachment) it.next(); 83 if (at instanceof AlternativeAttachment || at.getMimeType().match("text/")) { 84 textPart = at; 85 } 86 } 87 } else { 88 allAttachments.remove(delAttach); 89 } 90 } 91 92 95 synchronized void addAttachment(Attachment newAttach, ContentType ct) { 96 addAttachment(newAttach, ct, true); 97 } 98 99 106 synchronized void addAttachment(Attachment newAttach, ContentType ct, boolean allowTextPart) { 107 if (! allowTextPart) { 108 allAttachments.add(newAttach); 109 } else { 110 if ((textPart == null || textPart instanceof net.suberic.pooka.crypto.CryptoAttachment) && (newAttach instanceof AlternativeAttachment || ct.match("text/*"))) { 111 textPart = newAttach; 112 } else if (textPart == null && newAttach instanceof net.suberic.pooka.crypto.CryptoAttachment) { 113 textPart = newAttach; 114 allAttachments.add(newAttach); 115 } else { 116 allAttachments.add(newAttach); 117 } 118 } 119 } 120 121 125 public String getTextPart(boolean withHeaders, boolean showFullHeaders, int maxLength, String truncationMessage) throws IOException { 126 127 StringBuffer retVal = new StringBuffer (); 128 129 if (withHeaders) 130 retVal.append(getHeaderInformation(showFullHeaders, false)); 131 132 String text = null; 133 if (textPart != null) { 134 text = textPart.getText(withHeaders, showFullHeaders, maxLength, truncationMessage); 135 } 136 137 if (text != null) { 138 retVal.append(text); 139 return retVal.toString(); 140 } else 141 return null; 142 } 143 144 148 public String getHtmlPart(boolean withHeaders, boolean showFullHeaders, int maxLength, String truncationMessage) throws IOException { 149 StringBuffer retVal = new StringBuffer (); 150 151 retVal.append("<html><body>"); 152 153 if (withHeaders) 154 retVal.append(getHeaderInformation(showFullHeaders, true)); 155 156 if (textPart != null) 157 retVal.append(textPart.getHtml(withHeaders, showFullHeaders, maxLength, truncationMessage)); 158 159 retVal.append("</body></html>"); 160 return retVal.toString(); 161 } 162 163 167 public Vector getAttachments() { 168 return new Vector(allAttachments); 169 } 170 171 177 public Vector getAttachments(int maxLength) { 178 if (textPart != null && textPart.getSize() >= maxLength) { 179 if (attachmentsAndTextPart != null) 180 return attachmentsAndTextPart; 181 else { 182 attachmentsAndTextPart = new Vector(); 183 attachmentsAndTextPart.add(textPart); 184 attachmentsAndTextPart.addAll(allAttachments); 185 return new Vector(attachmentsAndTextPart); 186 } 187 } else 188 return new Vector(allAttachments); 189 } 190 191 194 public Vector getAttachmentsAndTextPart() { 195 if (attachmentsAndTextPart != null) 196 return new Vector(attachmentsAndTextPart); 197 else { 198 attachmentsAndTextPart = new Vector(); 199 attachmentsAndTextPart.add(textPart); 200 attachmentsAndTextPart.addAll(allAttachments); 201 return new Vector(attachmentsAndTextPart); 202 } 203 } 204 205 209 public String getTextAndTextInlines(String separator, boolean withHeaders, boolean showFullHeaders, int maxLength, String truncationMessage) throws IOException { 210 StringBuffer returnValue = new StringBuffer (); 211 212 if (withHeaders) 213 returnValue.append(getHeaderInformation(showFullHeaders, false)); 214 215 if (textPart != null) 216 returnValue.append(textPart.getText(withHeaders, showFullHeaders, maxLength, truncationMessage)); 217 218 if (allAttachments != null && allAttachments.size() > 0) { 219 for (int i = 0; i < allAttachments.size() ; i++) { 220 Attachment attach = (Attachment) allAttachments.elementAt(i); 221 if (attach.isPlainText()) { 222 returnValue.append(separator); 223 returnValue.append(attach.getText(withHeaders, showFullHeaders, maxLength, truncationMessage)); 224 } 225 } 226 } 227 228 return returnValue.toString(); 229 } 230 231 232 236 public String getHtmlAndTextInlines(String separator, boolean withHeaders, boolean showFullHeaders, int maxLength, String truncationMessage) throws IOException { 237 StringBuffer returnValue = new StringBuffer (); 238 239 returnValue.append("<html><body>"); 240 241 if (withHeaders) 242 returnValue.append(getHeaderInformation(showFullHeaders, true)); 243 244 if (textPart != null) 245 returnValue.append(textPart.getHtml(withHeaders, showFullHeaders, maxLength, truncationMessage)); 246 247 if (allAttachments != null && allAttachments.size() > 0) { 248 for (int i = 0; i < allAttachments.size() ; i++) { 249 Attachment attach = (Attachment) allAttachments.elementAt(i); 250 if (attach.isPlainText()) { 251 returnValue.append(separator); 252 returnValue.append(attach.getText(withHeaders, showFullHeaders, maxLength, truncationMessage)); 253 } 254 } 255 } 256 257 returnValue.append("</body></html>"); 258 return returnValue.toString(); 259 } 260 261 265 public void setHeaderSource(MimePart headerSource) throws MessagingException { 266 headers = parseHeaders(headerSource.getAllHeaders()); 267 headerLines = parseHeaderLines(headerSource.getAllHeaderLines()); 268 } 269 270 273 public StringBuffer getHeaderInformation (boolean showFullHeaders, boolean useHtml) { 274 if (headers != null) { 275 StringBuffer headerText = new StringBuffer (); 276 277 if (showFullHeaders) { 278 if (useHtml) { 279 Enumeration allHdrs = headers.getAllHeaders(); 280 while (allHdrs.hasMoreElements()) { 281 Header nextHeader = (Header) allHdrs.nextElement(); 282 headerText.append("<b>" + nextHeader.getName() + ":</b> "); 283 headerText.append(MailUtilities.escapeHtml(nextHeader.getValue())); 284 285 headerText.append("<br>\n"); 286 } 287 } else { 288 Enumeration allHdrs = headers.getAllHeaderLines(); 289 while (allHdrs.hasMoreElements()) { 290 headerText.append(MailUtilities.decodeText((String ) allHdrs.nextElement())); 291 headerText.append('\n'); 292 } 293 } 294 } else { 295 StringTokenizer tokens = new StringTokenizer(Pooka.getProperty("MessageWindow.Header.DefaultHeaders", "From:To:CC:Date:Subject"), ":"); 296 String hdrLabel,currentHeader = null; 297 String hdrValue = null; 298 299 while (tokens.hasMoreTokens()) { 300 currentHeader=tokens.nextToken(); 301 hdrLabel = Pooka.getProperty("MessageWindow.Header." + currentHeader + ".label", currentHeader); 302 hdrValue = MailUtilities.decodeText((String ) headers.getHeader(Pooka.getProperty("MessageWindow.Header." + currentHeader + ".MIMEHeader", currentHeader), ":")); 303 if (hdrValue != null) { 304 if (useHtml) { 305 headerText.append("<b>" + hdrLabel + ":</b> "); 306 headerText.append(MailUtilities.escapeHtml(hdrValue)); 307 308 headerText.append("<br>\n"); 309 } else { 310 headerText.append(hdrLabel + ": "); 311 headerText.append(hdrValue); 312 313 headerText.append("\n"); 314 } 315 } 316 } 317 } 318 if (useHtml) { 319 String separator = Pooka.getProperty("MessageWindow.htmlSeparator", "<hr><br>"); 320 headerText.append(separator); 321 } else { 322 String separator = Pooka.getProperty("MessageWindow.separator", ""); 323 if (separator.equals("")) 324 headerText.append("\n\n"); 325 else 326 headerText.append(separator); 327 } 328 329 return headerText; 330 } else { 331 return new StringBuffer (); 332 } 333 } 334 335 338 private InternetHeaders parseHeaders(Enumeration pHeaders) { 339 InternetHeaders retVal = new InternetHeaders(); 340 while (pHeaders.hasMoreElements()) { 341 Header hdr = (Header) pHeaders.nextElement(); 342 retVal.addHeader(hdr.getName(), hdr.getValue()); 343 } 344 return retVal; 345 } 346 347 350 private Vector parseHeaderLines(Enumeration pHeaderLines) { 351 Vector retVal = new Vector(); 352 while (pHeaderLines.hasMoreElements()) 353 retVal.add(pHeaderLines.nextElement()); 354 return retVal; 355 } 356 357 360 public boolean containsHtml() { 361 if (textPart != null) { 362 if (textPart instanceof AlternativeAttachment) { 363 return true; 364 } else { 365 return textPart.getMimeType().match("text/html"); 366 } 367 } else 368 return false; 369 } 370 371 375 public boolean isHtml() { 376 if (textPart != null) { 377 if (textPart instanceof AlternativeAttachment) 378 return false; 379 else 380 return (textPart.getMimeType().match("text/html")); 381 } else 382 return false; 383 } 384 } 385 | Popular Tags |