1 16 package org.apache.cocoon.mail; 17 18 import java.io.IOException ; 19 import java.util.List ; 20 import javax.mail.FetchProfile ; 21 import javax.mail.Folder ; 22 import javax.mail.Message ; 23 import javax.mail.MessagingException ; 24 import javax.mail.Multipart ; 25 import javax.mail.Part ; 26 import javax.mail.Store ; 27 import javax.mail.UIDFolder ; 28 import javax.mail.search.FromStringTerm ; 29 import javax.mail.search.OrTerm ; 30 import javax.mail.search.SearchTerm ; 31 import javax.mail.search.SubjectTerm ; 32 import org.apache.avalon.framework.context.Context; 33 import org.apache.avalon.framework.context.ContextException; 34 import org.apache.avalon.framework.context.Contextualizable; 35 import org.apache.avalon.framework.logger.AbstractLogEnabled; 36 import org.apache.cocoon.mail.command.AbstractMailCommand; 37 import org.apache.cocoon.mail.command.MailCommands; 38 39 46 public class MailCommandManager extends AbstractLogEnabled { 47 48 51 public final static String DEFAULT_FOLDER_NAME = "INBOX"; 52 53 56 public final static String DEFAULT_FOLDER_PATTERN = "%"; 57 58 61 public final static String CONTEXT_FOLDER_ENTRY = "folder"; 62 65 public final static String CONTEXT_UID_ENTRY = "uid"; 66 69 public final static String CONTEXT_ID_ENTRY = "id"; 70 73 public final static String CONTEXT_PARTID_ENTRY = "part-id"; 74 77 public final static String CONTEXT_FOLDER_PATTERN_ENTRY = "folder-pattern"; 78 81 public final static String CONTEXT_MAX_FOLDER_LEVEL_ENTRY = "max-folder-level"; 82 83 84 87 public MailCommandManager() { } 88 89 90 97 public static void openFolder(Folder f, int mode) throws MessagingException { 98 if (!f.isOpen()) { 99 f.open(mode); 100 } 101 } 102 103 104 110 public static void closeFolder(Folder f) throws MessagingException { 111 if (f != null && f.isOpen()) { 112 f.close(false); 114 } 115 } 116 117 118 124 public static void openStore(Store s) throws MessagingException { 125 if (!s.isConnected()) { 126 s.connect(); 127 } 128 } 129 130 131 137 public static void closeStore(Store s) throws MessagingException { 138 if (s != null && s.isConnected()) { 139 s.close(); 140 } 141 } 142 143 144 150 public List execute(List aList) { 151 MailCommands folderCommands = new MailCommands(aList); 152 try { 153 folderCommands.execute(); 154 } catch (MessagingException me) { 155 getLogger().error("Cannot execute", me); 157 } 158 return folderCommands.getResults(); 159 } 160 161 162 168 public List execute(AbstractMailCommand amfa) { 169 try { 170 amfa.execute(); 171 } catch (MessagingException me) { 172 getLogger().error("Cannot execute", me); 174 } 175 return amfa.getResults(); 176 } 177 178 179 183 public static class MailFolderCatCommand extends AbstractMailCommand implements Contextualizable { 184 185 private Folder aFolder; 186 187 188 191 public MailFolderCatCommand() { } 192 193 194 200 public void contextualize(Context ctx) throws ContextException { 201 MailContext mctx = (MailContext) ctx; 202 this.aFolder = mctx.getTheFolder(CONTEXT_FOLDER_ENTRY); 203 } 204 205 206 211 public void execute() throws MessagingException { 212 MailCommandManager.openFolder(aFolder, Folder.READ_ONLY); 213 addResult(aFolder); 214 } 215 } 216 217 218 222 public static class MailRefreshFolderCommand extends AbstractMailCommand implements Contextualizable { 223 224 private Folder aFolder; 225 226 227 230 public MailRefreshFolderCommand() { } 231 232 233 239 public void contextualize(Context ctx) throws ContextException { 240 MailContext mctx = (MailContext) ctx; 241 this.aFolder = mctx.getTheFolder(CONTEXT_FOLDER_ENTRY); 242 } 243 244 245 250 public void execute() throws MessagingException { 251 MailCommandManager.closeFolder(aFolder); 252 MailCommandManager.openFolder(aFolder, Folder.READ_ONLY); 253 addResult(aFolder); 254 } 255 } 256 257 258 263 public static class MailListMessagesCommand extends AbstractMailCommand implements Contextualizable { 264 265 private Folder aFolder; 266 267 268 271 public MailListMessagesCommand() { } 272 273 274 280 public void contextualize(Context ctx) throws ContextException { 281 MailContext mctx = (MailContext) ctx; 283 this.aFolder = mctx.getTheFolder(CONTEXT_FOLDER_ENTRY); 284 } 285 286 287 292 public void execute() throws MessagingException { 293 MailCommandManager.openFolder(aFolder, Folder.READ_ONLY); 294 295 addResult(aFolder); 297 298 Message [] messages = aFolder.getMessages(); 299 300 FetchProfile fp = new FetchProfile (); 302 fp.add(FetchProfile.Item.ENVELOPE); 303 fp.add(FetchProfile.Item.FLAGS); 304 fp.add("X-Mailer"); 305 aFolder.fetch(messages, fp); 306 307 addResult(messages); 309 310 } 311 } 312 313 314 319 public static class MailListFolderCommand extends AbstractMailCommand implements Contextualizable { 320 321 private Folder aFolder; 322 private String folderPattern = MailCommandManager.DEFAULT_FOLDER_PATTERN; 323 324 325 328 public MailListFolderCommand() { } 329 330 331 336 public String getFolderPattern() { 337 return this.folderPattern; 338 } 339 340 341 347 public void contextualize(Context ctx) throws ContextException { 348 MailContext mctx = (MailContext) ctx; 349 this.aFolder = mctx.getTheFolder(CONTEXT_FOLDER_ENTRY); 350 351 try { 352 this.folderPattern = (String ) ctx.get("param:" + CONTEXT_FOLDER_PATTERN_ENTRY); 353 } catch (ContextException ce) { 354 this.folderPattern = MailCommandManager.DEFAULT_FOLDER_PATTERN; 356 } 357 } 358 359 360 365 public void execute() throws MessagingException { 366 Folder [] subFolders = aFolder.list(this.folderPattern); 369 getLogger().debug("Adding " + String.valueOf(subFolders.length) + " subFolders "); 370 for (int i = 0; i < subFolders.length; i++) { 371 getLogger().debug("subFolder " + String.valueOf(i) + " name " + subFolders[i].getFullName()); 372 } 373 addResult(subFolders); 374 } 375 } 376 377 378 383 public static class MailCatMessageByUIDCommand extends AbstractMailCommand implements Contextualizable { 384 385 private int msgUID = 1; 386 private Folder aFolder; 387 388 389 392 public MailCatMessageByUIDCommand() { } 393 394 395 401 public void contextualize(Context ctx) throws ContextException { 402 MailContext mctx = (MailContext) ctx; 403 this.aFolder = mctx.getTheFolder(CONTEXT_FOLDER_ENTRY); 404 405 Integer i = (Integer ) ctx.get("param-integer:" + CONTEXT_UID_ENTRY); 406 if (i == null) { 407 String message = "Missing mandatory context entry " + String.valueOf(CONTEXT_UID_ENTRY); 408 throw new ContextException(message); 409 } 410 this.msgUID = i.intValue(); 411 } 412 413 414 419 public void execute() throws MessagingException { 420 UIDFolder uidFolder = (UIDFolder ) aFolder; 421 MailCommandManager.openFolder(aFolder, Folder.READ_ONLY); 422 423 addResult(aFolder); 425 426 Message msg = uidFolder.getMessageByUID(msgUID); 427 addResult(msg); 428 } 429 } 430 431 432 437 public static class MailCatMessageByIdCommand extends AbstractMailCommand implements Contextualizable { 438 439 private int msgId = 1; 440 private Folder aFolder; 441 442 443 446 public MailCatMessageByIdCommand() { } 447 448 449 455 public void contextualize(Context ctx) throws ContextException { 456 MailContext mctx = (MailContext) ctx; 457 this.aFolder = mctx.getTheFolder(CONTEXT_FOLDER_ENTRY); 458 459 try { 460 Integer i = (Integer ) ctx.get("param-integer:" + CONTEXT_ID_ENTRY); 461 this.msgId = i.intValue(); 462 } catch (ContextException ce) { 463 String message = "Missing mandatory context entry " + String.valueOf(CONTEXT_ID_ENTRY); 464 throw new ContextException(message); 465 } 466 } 467 468 469 474 public void execute() throws MessagingException { 475 MailCommandManager.openFolder(aFolder, Folder.READ_ONLY); 476 477 addResult(aFolder); 479 480 Message msg = aFolder.getMessage(msgId); 481 addResult(msg); 482 } 483 } 484 485 486 491 public static class MailCatAttachmentMessageByIdCommand extends AbstractMailCommand implements Contextualizable { 492 493 private int msgId = -1; 494 private int partId = -1; 495 private Folder aFolder; 496 497 498 501 public MailCatAttachmentMessageByIdCommand() { } 502 503 504 510 public void contextualize(Context ctx) throws ContextException { 511 MailContext mctx = (MailContext) ctx; 512 this.aFolder = mctx.getTheFolder(CONTEXT_FOLDER_ENTRY); 513 514 Integer i = (Integer ) ctx.get("param-integer:" + CONTEXT_ID_ENTRY); 515 if (i == null) { 516 String message = "Missing mandatory context entry " + String.valueOf(CONTEXT_ID_ENTRY); 517 throw new ContextException(message); 518 } 519 this.msgId = i.intValue(); 520 521 i = (Integer ) ctx.get("param-integer:" + CONTEXT_PARTID_ENTRY); 522 if (i == null) { 523 String message = "Missing mandatory context entry " + String.valueOf(CONTEXT_PARTID_ENTRY); 524 throw new ContextException(message); 525 } 526 this.partId = i.intValue(); 527 } 528 529 530 535 public void execute() throws MessagingException { 536 MailCommandManager.openFolder(aFolder, Folder.READ_ONLY); 537 538 addResult(aFolder); 540 541 Message msg = aFolder.getMessage(msgId); 543 544 if (msg == null) { 545 String message = "Cannot get message for id " + String.valueOf(msgId); 546 getLogger().warn(message); 547 return; 548 } 549 try { 550 Part part = null; 551 Object objRef = msg.getContent(); 552 if (!(objRef instanceof Multipart )) { 553 String message = "Message of id " + String.valueOf(msgId) + " is not a multipart message!"; 554 getLogger().warn(message); 555 return; 556 } 557 Multipart multipart = (Multipart ) objRef; 558 int numParts = multipart.getCount(); 559 560 if (partId < numParts) { 561 part = multipart.getBodyPart(partId); 562 } else { 563 String message = "Invalid part id " + String.valueOf(this.partId) + " of message id " + String.valueOf(this.msgId); 564 getLogger().warn(message); 565 } 566 addResult(part); 567 } catch (IOException ioe) { 568 String message = "Cannot get content of " + 569 "message for id " + String.valueOf(msgId); 570 throw new MessagingException (message, ioe); 571 } 572 } 573 } 574 575 576 580 public static class MailSearchMessagesCommand extends AbstractMailCommand implements Contextualizable { 581 private Folder aFolder; 582 private SearchTerm searchTerm; 583 584 585 591 public void contextualize(Context ctx) throws ContextException { 592 MailContext mctx = (MailContext) ctx; 593 this.aFolder = mctx.getTheFolder(CONTEXT_FOLDER_ENTRY); 594 595 String searchString = (String ) ctx.get("param:" + "search"); 596 if (searchString == null) { 597 searchString = ""; 598 } 599 searchTerm = new OrTerm ( 600 new SubjectTerm (searchString), 601 new FromStringTerm (searchString)); 602 603 605 619 } 620 621 622 627 public void execute() throws MessagingException { 628 MailCommandManager.openFolder(aFolder, Folder.READ_ONLY); 629 630 addResult(aFolder); 632 633 Message [] msgs = aFolder.search(searchTerm); 634 addResult(msgs); 635 } 636 } 637 } 638 639 | Popular Tags |