1 package net.suberic.pooka; 2 import javax.mail.*; 3 import javax.mail.internet.*; 4 import net.suberic.pooka.FolderInfo; 5 import net.suberic.pooka.Pooka; 6 import javax.activation.DataHandler ; 7 import java.util.Enumeration ; 8 import java.io.InputStream ; 9 import java.io.ByteArrayInputStream ; 10 import java.io.ByteArrayOutputStream ; 11 12 16 17 public class UIDMimeMessage extends MimeMessage { 18 19 long uid; 20 UIDFolderInfo parent; 21 22 public UIDMimeMessage(UIDFolderInfo parentFolderInfo, long newUid) { 23 super(Pooka.getDefaultSession()); 24 uid = newUid; 25 parent = parentFolderInfo; 26 saved=true; 27 modified=false; 28 } 29 30 public int getSize() throws MessagingException { 31 try { 32 return getMessage().getSize(); 33 } catch (FolderClosedException fce) { 34 int status = parent.getStatus(); 35 if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) { 36 try { 37 parent.openFolder(Folder.READ_WRITE); 38 } catch (MessagingException me) { 39 throw fce; 40 } 41 42 return getMessage().getSize(); 43 44 } else { 45 throw fce; 46 } 47 } 48 } 49 50 protected InputStream getContentStream() throws MessagingException { 51 53 try { 54 InputStream handlerStream = getInputStream(); 55 InternetHeaders tmpHeaders = new InternetHeaders(handlerStream); 56 57 byte[] buf; 58 59 int len; 60 int size = 1024; 61 62 if (handlerStream instanceof ByteArrayInputStream ) { 63 size = handlerStream.available(); 64 buf = new byte[size]; 65 len = handlerStream.read(buf, 0, size); 66 } 67 else { 68 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 69 buf = new byte[size]; 70 while ((len = handlerStream.read(buf, 0, size)) != -1) 71 bos.write(buf, 0, len); 72 buf = bos.toByteArray(); 73 } 74 75 return new ByteArrayInputStream (buf); 76 } catch (java.io.IOException ioe) { 77 throw new MessagingException("Error getting Content Stream", ioe); 78 } 79 80 } 82 83 public synchronized DataHandler getDataHandler() 84 throws MessagingException { 85 try { 86 return getMessage().getDataHandler(); 87 } catch (FolderClosedException fce) { 88 int status = parent.getStatus(); 89 if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) { 90 try { 91 parent.openFolder(Folder.READ_WRITE); 92 } catch (MessagingException me) { 93 throw fce; 94 } 95 96 return getMessage().getDataHandler(); 97 } else { 98 throw fce; 99 } 100 } 101 } 102 103 public String [] getHeader(String name) 104 throws MessagingException { 105 try { 106 return getMessage().getHeader(name); 107 } catch (FolderClosedException fce) { 108 int status = parent.getStatus(); 109 if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) { 110 try { 111 parent.openFolder(Folder.READ_WRITE); 112 } catch (MessagingException me) { 113 throw fce; 114 } 115 116 return getMessage().getHeader(name); 117 118 } else { 119 throw fce; 120 } 121 } 122 } 123 124 public String getHeader(String name, String delimiter) 125 throws MessagingException { 126 try { 127 return getMessage().getHeader(name, delimiter); 128 } catch (FolderClosedException fce) { 129 int status = parent.getStatus(); 130 if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) { 131 try { 132 parent.openFolder(Folder.READ_WRITE); 133 } catch (MessagingException me) { 134 throw fce; 135 } 136 137 return getMessage().getHeader(name, delimiter); 138 139 } else { 140 throw fce; 141 } 142 } 143 } 144 145 public void setHeader(String name, String value) 146 throws MessagingException { 147 try { 148 getMessage().setHeader(name, value); 149 } catch (FolderClosedException fce) { 150 int status = parent.getStatus(); 151 if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) { 152 try { 153 parent.openFolder(Folder.READ_WRITE); 154 } catch (MessagingException me) { 155 throw fce; 156 } 157 158 getMessage().setHeader(name, value); 159 160 } else { 161 throw fce; 162 } 163 } 164 } 165 166 public void addHeader(String name, String value) 167 throws MessagingException { 168 try { 169 getMessage().addHeader(name, value); 170 } catch (FolderClosedException fce) { 171 int status = parent.getStatus(); 172 if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) { 173 try { 174 parent.openFolder(Folder.READ_WRITE); 175 } catch (MessagingException me) { 176 throw fce; 177 } 178 179 getMessage().addHeader(name, value); 180 181 } else { 182 throw fce; 183 } 184 } 185 } 186 187 public void removeHeader(String name) 188 throws MessagingException { 189 try { 190 getMessage().removeHeader(name); 191 } catch (FolderClosedException fce) { 192 int status = parent.getStatus(); 193 if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) { 194 try { 195 parent.openFolder(Folder.READ_WRITE); 196 } catch (MessagingException me) { 197 throw fce; 198 } 199 200 getMessage().removeHeader(name); 201 202 } else { 203 throw fce; 204 } 205 } 206 } 207 208 public Enumeration getAllHeaders() throws MessagingException { 209 try { 210 return getMessage().getAllHeaders(); 211 } catch (FolderClosedException fce) { 212 if (Pooka.isDebug()) 213 System.out.println("debug: caught FolderClosedException."); 214 int status = parent.getStatus(); 215 if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) { 216 if (Pooka.isDebug()) 217 System.out.println("debug: folder should be open. trying to re-open folder."); 218 try { 219 parent.openFolder(Folder.READ_WRITE); 220 } catch (MessagingException me) { 221 throw fce; 222 } 223 224 return getMessage().getAllHeaders(); 225 226 } else { 227 throw fce; 228 } 229 } 230 } 231 232 public Enumeration getMatchingHeaders(String [] names) 233 throws MessagingException { 234 try { 235 return getMessage().getMatchingHeaders(names); 236 } catch (FolderClosedException fce) { 237 int status = parent.getStatus(); 238 if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) { 239 try { 240 parent.openFolder(Folder.READ_WRITE); 241 } catch (MessagingException me) { 242 throw fce; 243 } 244 245 return getMessage().getMatchingHeaders(names); 246 247 } else { 248 throw fce; 249 } 250 } 251 } 252 253 254 261 public Enumeration getNonMatchingHeaders(String [] names) 262 throws MessagingException { 263 try { 264 return getMessage().getNonMatchingHeaders(names); 265 } catch (FolderClosedException fce) { 266 int status = parent.getStatus(); 267 if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) { 268 try { 269 parent.openFolder(Folder.READ_WRITE); 270 } catch (MessagingException me) { 271 throw fce; 272 } 273 274 return getMessage().getNonMatchingHeaders(names); 275 276 } else { 277 throw fce; 278 } 279 } 280 } 281 282 291 public void addHeaderLine(String line) throws MessagingException { 292 try { 293 getMessage().addHeaderLine(line); 294 } catch (FolderClosedException fce) { 295 int status = parent.getStatus(); 296 if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) { 297 try { 298 parent.openFolder(Folder.READ_WRITE); 299 } catch (MessagingException me) { 300 throw fce; 301 } 302 303 getMessage().addHeaderLine(line); 304 305 } else { 306 throw fce; 307 } 308 } 309 } 310 311 318 public Enumeration getAllHeaderLines() throws MessagingException { 319 try { 320 return getMessage().getAllHeaderLines(); 321 } catch (FolderClosedException fce) { 322 int status = parent.getStatus(); 323 if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) { 324 try { 325 parent.openFolder(Folder.READ_WRITE); 326 } catch (MessagingException me) { 327 throw fce; 328 } 329 330 return getMessage().getAllHeaderLines(); 331 332 } else { 333 throw fce; 334 } 335 } 336 } 337 338 345 public Enumeration getMatchingHeaderLines(String [] names) 346 throws MessagingException { 347 try { 348 return getMessage().getMatchingHeaderLines(names); 349 } catch (FolderClosedException fce) { 350 int status = parent.getStatus(); 351 if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) { 352 try { 353 parent.openFolder(Folder.READ_WRITE); 354 } catch (MessagingException me) { 355 throw fce; 356 } 357 358 return getMessage().getMatchingHeaderLines(names); 359 360 } else { 361 throw fce; 362 } 363 } 364 } 365 366 373 public Enumeration getNonMatchingHeaderLines(String [] names) 374 throws MessagingException { 375 try { 376 return getMessage().getNonMatchingHeaderLines(names); 377 } catch (FolderClosedException fce) { 378 int status = parent.getStatus(); 379 if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) { 380 try { 381 parent.openFolder(Folder.READ_WRITE); 382 } catch (MessagingException me) { 383 throw fce; 384 } 385 386 return getMessage().getNonMatchingHeaderLines(names); 387 388 } else { 389 throw fce; 390 } 391 } 392 } 393 394 406 public synchronized Flags getFlags() throws MessagingException { 407 try { 408 Message m = getMessage(); 409 if (m != null) 410 return m.getFlags(); 411 else 412 throw new MessageRemovedException(); 413 } catch (FolderClosedException fce) { 414 int status = parent.getStatus(); 415 if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) { 416 try { 417 parent.openFolder(Folder.READ_WRITE); 418 } catch (MessagingException me) { 419 throw fce; 420 } 421 422 return getMessage().getFlags(); 423 424 } else { 425 throw fce; 426 } 427 } 428 } 429 430 448 public synchronized boolean isSet(Flags.Flag flag) 449 throws MessagingException { 450 try { 451 return getFlags().contains(flag); 452 } catch (FolderClosedException fce) { 453 int status = parent.getStatus(); 454 if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) { 455 try { 456 parent.openFolder(Folder.READ_WRITE); 457 } catch (MessagingException me) { 458 throw fce; 459 } 460 461 return getFlags().contains(flag); 462 463 } else { 464 throw fce; 465 } 466 } 467 } 468 469 480 public synchronized void setFlags(Flags flag, boolean set) 481 throws MessagingException { 482 try { 483 getMessage().setFlags(flag, set); 484 } catch (FolderClosedException fce) { 485 int status = parent.getStatus(); 486 if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) { 487 try { 488 parent.openFolder(Folder.READ_WRITE); 489 } catch (MessagingException me) { 490 throw fce; 491 } 492 493 getMessage().setFlags(flag, set); 494 495 } else { 496 throw fce; 497 } 498 } 499 } 500 501 518 public boolean isExpunged() { 519 try { 520 try { 521 return (getMessage() == null); 522 } catch (FolderClosedException fce) { 523 int status = parent.getStatus(); 524 if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) { 525 try { 526 parent.openFolder(Folder.READ_WRITE); 527 } catch (MessagingException me) { 528 throw fce; 529 } 530 531 return (getMessage() == null); 532 533 } else { 534 throw fce; 535 } 536 } 537 } catch (MessagingException me) { 538 return false; 539 } 540 } 541 542 public void writeTo(java.io.OutputStream os, java.lang.String [] ignoreList) 543 throws java.io.IOException , MessagingException { 544 try { 545 getMessage().writeTo(os, ignoreList); 546 } catch (FolderClosedException fce) { 547 int status = parent.getStatus(); 548 if (status == FolderInfo.CONNECTED || status == FolderInfo.LOST_CONNECTION) { 549 try { 550 parent.openFolder(Folder.READ_WRITE); 551 } catch (MessagingException me) { 552 throw fce; 553 } 554 555 getMessage().writeTo(os, ignoreList); 556 } else { 557 throw fce; 558 } 559 } 560 } 561 562 public long getUID() { 563 return uid; 564 } 565 566 public long getUIDValidity() { 567 return parent.getUIDValidity(); 568 } 569 570 public MimeMessage getMessage() throws MessagingException { 571 MimeMessage returnValue = parent.getRealMessageById(uid); 572 if (returnValue == null) { 573 throw new MessageRemovedException("Message with UID " + uid + " does not exist in Folder " + parent.getFolderID()); 574 } else { 575 return returnValue; 576 } 577 } 578 579 public UIDFolderInfo getParent() { 580 return parent; 581 } 582 583 587 public boolean equals(Object o) { 588 if (o instanceof Message) { 589 try { 590 UIDMimeMessage uidMsg = parent.getUIDMimeMessage((Message) o); 591 if (uidMsg != null) 592 if (uidMsg.getUID() == getUID()) 593 return true; 594 else 595 return false; 596 } catch (MessagingException me) { 597 return false; 598 } 599 } 600 601 return false; 602 } 603 } 604 605 606 607 608 | Popular Tags |