1 21 22 27 28 package com.sun.mail.pop3; 29 30 import javax.mail.*; 31 import javax.mail.internet.*; 32 import javax.mail.event.*; 33 import java.io.InputStream ; 34 import java.io.BufferedInputStream ; 35 import java.io.ByteArrayInputStream ; 36 import java.io.IOException ; 37 import java.io.EOFException ; 38 import java.util.Vector ; 39 import java.util.StringTokenizer ; 40 import java.lang.reflect.Constructor ; 41 42 import com.sun.mail.util.LineInputStream; 43 44 53 public class POP3Folder extends Folder { 54 55 private String name; 56 private Protocol port; 57 private int total; 58 private int size; 59 private boolean exists = false; 60 private boolean opened = false; 61 private Vector message_cache; 62 private boolean doneUidl = false; 63 64 POP3Folder(POP3Store store, String name) { 65 super(store); 66 this.name = name; 67 if (name.equalsIgnoreCase("INBOX")) 68 exists = true; 69 } 70 71 public String getName() { 72 return name; 73 } 74 75 public String getFullName() { 76 return name; 77 } 78 79 public Folder getParent() { 80 return new DefaultFolder((POP3Store)store); 81 } 82 83 89 public boolean exists() { 90 return exists; 91 } 92 93 99 public Folder[] list(String pattern) throws MessagingException { 100 throw new MessagingException("not a directory"); 101 } 102 103 108 public char getSeparator() { 109 return '\0'; 110 } 111 112 117 public int getType() { 118 return HOLDS_MESSAGES; 119 } 120 121 127 public boolean create(int type) throws MessagingException { 128 return false; 129 } 130 131 137 public boolean hasNewMessages() throws MessagingException { 138 return false; } 140 141 147 public Folder getFolder(String name) throws MessagingException { 148 throw new MessagingException("not a directory"); 149 } 150 151 158 public boolean delete(boolean recurse) throws MessagingException { 159 throw new MethodNotSupportedException("delete"); 160 } 161 162 168 public boolean renameTo(Folder f) throws MessagingException { 169 throw new MethodNotSupportedException("renameTo"); 170 } 171 172 180 public synchronized void open(int mode) throws MessagingException { 181 checkClosed(); 182 if (!exists) 183 throw new FolderNotFoundException(this, "folder is not INBOX"); 184 185 try { 186 port = ((POP3Store)store).getPort(this); 187 Status s = port.stat(); 188 total = s.total; 189 size = s.size; 190 this.mode = mode; 191 opened = true; 192 } catch (IOException ioex) { 193 try { 194 if (port != null) 195 port.quit(); 196 } catch (IOException ioex2) { 197 } finally { 199 port = null; 200 ((POP3Store)store).closePort(this); 201 } 202 throw new MessagingException("Open failed", ioex); 203 } 204 205 message_cache = new Vector (total); 207 message_cache.setSize(total); 208 doneUidl = false; 209 210 notifyConnectionListeners(ConnectionEvent.OPENED); 211 } 212 213 public synchronized void close(boolean expunge) throws MessagingException { 214 checkOpen(); 215 216 try { 217 226 if (((POP3Store)store).rsetBeforeQuit) 227 port.rset(); 228 if (expunge && mode == READ_WRITE) { 229 POP3Message m; 231 for (int i = 0; i < message_cache.size(); i++) { 232 if ((m = (POP3Message)message_cache.elementAt(i)) != null) { 233 if (m.isSet(Flags.Flag.DELETED)) 234 try { 235 port.dele(i + 1); 236 } catch (IOException ioex) { 237 throw new MessagingException( 238 "Exception deleting messages during close", 239 ioex); 240 } 241 } 242 } 243 } 244 245 port.quit(); 246 } catch (IOException ex) { 247 } finally { 249 port = null; 250 ((POP3Store)store).closePort(this); 251 message_cache = null; 252 opened = false; 253 notifyConnectionListeners(ConnectionEvent.CLOSED); 254 } 255 } 256 257 public boolean isOpen() { 258 if (!opened) 259 return false; 260 if (store.isConnected()) 261 return true; 262 try { 263 close(false); 264 } catch (MessagingException ex) { } 265 return false; 266 } 267 268 274 public Flags getPermanentFlags() { 275 return new Flags(); } 277 278 283 public int getMessageCount() throws MessagingException { 284 if (!opened) 285 return -1; 286 checkReadable(); 287 return total; 288 } 289 290 public synchronized Message getMessage(int msgno) 291 throws MessagingException { 292 checkOpen(); 293 294 POP3Message m; 295 296 if ((m = (POP3Message)message_cache.elementAt(msgno-1)) == null) { 298 m = createMessage(this, msgno); 299 message_cache.setElementAt(m, msgno-1); 300 } 301 return m; 302 } 303 304 protected POP3Message createMessage(Folder f, int msgno) 305 throws MessagingException { 306 POP3Message m = null; 307 Constructor cons = ((POP3Store)store).messageConstructor; 308 if (cons != null) { 309 try { 310 Object [] o = { this, new Integer (msgno) }; 311 m = (POP3Message)cons.newInstance(o); 312 } catch (Exception ex) { 313 } 315 } 316 if (m == null) 317 m = new POP3Message(this, msgno); 318 return m; 319 } 320 321 327 public void appendMessages(Message[] msgs) throws MessagingException { 328 throw new MethodNotSupportedException("Append not supported"); 329 } 330 331 340 public Message[] expunge() throws MessagingException { 341 throw new MethodNotSupportedException("Expunge not supported"); 342 } 343 344 353 public synchronized void fetch(Message[] msgs, FetchProfile fp) 354 throws MessagingException { 355 checkReadable(); 356 if (!doneUidl && fp.contains(UIDFolder.FetchProfileItem.UID)) { 357 365 String [] uids = new String [message_cache.size()]; 366 try { 367 if (!port.uidl(uids)) 368 return; 369 } catch (EOFException eex) { 370 close(false); 371 throw new FolderClosedException(this, eex.toString()); 372 } catch (IOException ex) { 373 throw new MessagingException("error getting UIDL", ex); 374 } 375 for (int i = 0; i < uids.length; i++) { 376 if (uids[i] == null) 377 continue; 378 POP3Message m = (POP3Message)getMessage(i + 1); 379 m.uid = uids[i]; 380 } 381 doneUidl = true; } 383 if (fp.contains(FetchProfile.Item.ENVELOPE)) { 384 for (int i = 0; i < msgs.length; i++) { 385 try { 386 POP3Message msg = (POP3Message)msgs[i]; 387 msg.getHeader(""); 389 msg.getSize(); 391 } catch (MessageRemovedException mex) { 392 } 394 } 395 } 396 } 397 398 405 public synchronized String getUID(Message msg) throws MessagingException { 406 checkOpen(); 407 POP3Message m = (POP3Message)msg; 408 try { 409 if (m.uid == POP3Message.UNKNOWN) 410 m.uid = port.uidl(m.getMessageNumber()); 411 return m.uid; 412 } catch (EOFException eex) { 413 close(false); 414 throw new FolderClosedException(this, eex.toString()); 415 } catch (IOException ex) { 416 throw new MessagingException("error getting UIDL", ex); 417 } 418 } 419 420 427 public int getSize() throws MessagingException { 428 checkOpen(); 429 return size; 430 } 431 432 441 public synchronized int[] getSizes() throws MessagingException { 442 checkOpen(); 443 int sizes[] = new int[total]; 444 InputStream is = null; 445 LineInputStream lis = null; 446 try { 447 is = port.list(); 448 lis = new LineInputStream(is); 449 String line; 450 while ((line = lis.readLine()) != null) { 451 try { 452 StringTokenizer st = new StringTokenizer (line); 453 int msgnum = Integer.parseInt(st.nextToken()); 454 int size = Integer.parseInt(st.nextToken()); 455 if (msgnum > 0 && msgnum <= total) 456 sizes[msgnum - 1] = size; 457 } catch (Exception e) { 458 } 459 } 460 } catch (IOException ex) { 461 } finally { 463 try { 464 if (lis != null) 465 lis.close(); 466 } catch (IOException cex) { } 467 try { 468 if (is != null) 469 is.close(); 470 } catch (IOException cex) { } 471 } 472 return sizes; 473 } 474 475 482 public synchronized InputStream listCommand() 483 throws MessagingException, IOException { 484 checkOpen(); 485 return port.list(); 486 } 487 488 491 protected void finalize() throws Throwable { 492 super.finalize(); 493 close(false); 494 } 495 496 497 void checkOpen() throws IllegalStateException { 498 if (!opened) 499 throw new IllegalStateException ("Folder is not Open"); 500 } 501 502 503 void checkClosed() throws IllegalStateException { 504 if (opened) 505 throw new IllegalStateException ("Folder is Open"); 506 } 507 508 509 void checkReadable() throws IllegalStateException { 510 if (!opened || (mode != READ_ONLY && mode != READ_WRITE)) 511 throw new IllegalStateException ("Folder is not Readable"); 512 } 513 514 515 void checkWritable() throws IllegalStateException { 516 if (!opened || mode != READ_WRITE) 517 throw new IllegalStateException ("Folder is not Writable"); 518 } 519 520 525 Protocol getProtocol() throws MessagingException { 526 checkOpen(); 527 return port; 528 } 529 530 533 protected void notifyMessageChangedListeners(int type, Message m) { 534 super.notifyMessageChangedListeners(type, m); 535 } 536 } 537 | Popular Tags |