KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > mail > MailCommandManager


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.mail;
17
18 import java.io.IOException JavaDoc;
19 import java.util.List JavaDoc;
20 import javax.mail.FetchProfile JavaDoc;
21 import javax.mail.Folder JavaDoc;
22 import javax.mail.Message JavaDoc;
23 import javax.mail.MessagingException JavaDoc;
24 import javax.mail.Multipart JavaDoc;
25 import javax.mail.Part JavaDoc;
26 import javax.mail.Store JavaDoc;
27 import javax.mail.UIDFolder JavaDoc;
28 import javax.mail.search.FromStringTerm JavaDoc;
29 import javax.mail.search.OrTerm JavaDoc;
30 import javax.mail.search.SearchTerm JavaDoc;
31 import javax.mail.search.SubjectTerm JavaDoc;
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 /**
40  * Manage invocation of mail commands.
41  *
42  * @author Bernhard Huber
43  * @since 23 October 2002
44  * @version CVS $Id: MailCommandManager.java 30932 2004-07-29 17:35:38Z vgritsenko $
45  */

46 public class MailCommandManager extends AbstractLogEnabled {
47
48     /**
49      * Description of the Field
50      */

51     public final static String JavaDoc DEFAULT_FOLDER_NAME = "INBOX";
52
53     /**
54      * Description of the Field
55      */

56     public final static String JavaDoc DEFAULT_FOLDER_PATTERN = "%";
57
58     /**
59      * Context key specifying the foldername.
60      */

61     public final static String JavaDoc CONTEXT_FOLDER_ENTRY = "folder";
62     /**
63      * Description of the Field
64      */

65     public final static String JavaDoc CONTEXT_UID_ENTRY = "uid";
66     /**
67      * Description of the Field
68      */

69     public final static String JavaDoc CONTEXT_ID_ENTRY = "id";
70     /**
71      * Description of the Field
72      */

73     public final static String JavaDoc CONTEXT_PARTID_ENTRY = "part-id";
74     /**
75      * Description of the Field
76      */

77     public final static String JavaDoc CONTEXT_FOLDER_PATTERN_ENTRY = "folder-pattern";
78     /**
79      * Description of the Field
80      */

81     public final static String JavaDoc CONTEXT_MAX_FOLDER_LEVEL_ENTRY = "max-folder-level";
82
83
84     /**
85      * Creates a new instance of MailHeaderList
86      */

87     public MailCommandManager() { }
88
89
90     /**
91      * Open a javamail folder
92      *
93      *@param f Description of the Parameter
94      *@param mode folder opening mode, use Folder.READ_WRITE, or Folder.READ_ONLY
95      *@exception MessagingException Description of the Exception
96      */

97     public static void openFolder(Folder JavaDoc f, int mode) throws MessagingException JavaDoc {
98         if (!f.isOpen()) {
99             f.open(mode);
100         }
101     }
102
103
104     /**
105      * Close a javamail folder
106      *
107      *@param f Description of the Parameter
108      *@exception MessagingException Description of the Exception
109      */

110     public static void closeFolder(Folder JavaDoc f) throws MessagingException JavaDoc {
111         if (f != null && f.isOpen()) {
112             // fix me : do we need expungeOnExit = true?
113
f.close(false);
114         }
115     }
116
117
118     /**
119      * Open a javamail store
120      *
121      *@param s Description of the Parameter
122      *@exception MessagingException Description of the Exception
123      */

124     public static void openStore(Store JavaDoc s) throws MessagingException JavaDoc {
125         if (!s.isConnected()) {
126             s.connect();
127         }
128     }
129
130
131     /**
132      * Close a javamail store
133      *
134      *@param s Description of the Parameter
135      *@exception MessagingException Description of the Exception
136      */

137     public static void closeStore(Store JavaDoc s) throws MessagingException JavaDoc {
138         if (s != null && s.isConnected()) {
139             s.close();
140         }
141     }
142
143
144     /**
145      * Description of the Method
146      *
147      *@param aList Description of the Parameter
148      *@return Description of the Return Value
149      */

150     public List JavaDoc execute(List JavaDoc aList) {
151         MailCommands folderCommands = new MailCommands(aList);
152         try {
153             folderCommands.execute();
154         } catch (MessagingException JavaDoc me) {
155             // log exception
156
getLogger().error("Cannot execute", me);
157         }
158         return folderCommands.getResults();
159     }
160
161
162     /**
163      * Description of the Method
164      *
165      *@param amfa Description of the Parameter
166      *@return Description of the Return Value
167      */

168     public List JavaDoc execute(AbstractMailCommand amfa) {
169         try {
170             amfa.execute();
171         } catch (MessagingException JavaDoc me) {
172             // log exception
173
getLogger().error("Cannot execute", me);
174         }
175         return amfa.getResults();
176     }
177
178
179     /**
180      * Retrieve folder, and put it as command result.
181      *
182      */

183     public static class MailFolderCatCommand extends AbstractMailCommand implements Contextualizable {
184
185         private Folder JavaDoc aFolder;
186
187
188         /**
189          *Constructor for the MailFolderCommand object
190          */

191         public MailFolderCatCommand() { }
192
193
194         /**
195          * Description of the Method
196          *
197          *@param ctx Description of the Parameter
198          *@exception ContextException Description of the Exception
199          */

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         /**
207          * Description of the Method
208          *
209          *@exception MessagingException Description of the Exception
210          */

211         public void execute() throws MessagingException JavaDoc {
212             MailCommandManager.openFolder(aFolder, Folder.READ_ONLY);
213             addResult(aFolder);
214         }
215     }
216
217
218     /**
219      * Retrieve folder, and put it as command result.
220      *
221      */

222     public static class MailRefreshFolderCommand extends AbstractMailCommand implements Contextualizable {
223
224         private Folder JavaDoc aFolder;
225
226
227         /**
228          *Constructor for the MailFolderCommand object
229          */

230         public MailRefreshFolderCommand() { }
231
232
233         /**
234          * Description of the Method
235          *
236          *@param ctx Description of the Parameter
237          *@exception ContextException Description of the Exception
238          */

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         /**
246          * Description of the Method
247          *
248          *@exception MessagingException Description of the Exception
249          */

250         public void execute() throws MessagingException JavaDoc {
251             MailCommandManager.closeFolder(aFolder);
252             MailCommandManager.openFolder(aFolder, Folder.READ_ONLY);
253             addResult(aFolder);
254         }
255     }
256
257
258     /**
259      * Retrieved headers of all messages of a folder, put
260      * retrieved messages as command result.
261      *
262      */

263     public static class MailListMessagesCommand extends AbstractMailCommand implements Contextualizable {
264
265         private Folder JavaDoc aFolder;
266
267
268         /**
269          *Constructor for the MailAllHeadersCommand object
270          */

271         public MailListMessagesCommand() { }
272
273
274         /**
275          * Description of the Method
276          *
277          *@param ctx Description of the Parameter
278          *@exception ContextException Description of the Exception
279          */

280         public void contextualize(Context ctx) throws ContextException {
281             // try to get the folder object
282
MailContext mctx = (MailContext) ctx;
283             this.aFolder = mctx.getTheFolder(CONTEXT_FOLDER_ENTRY);
284         }
285
286
287         /**
288          * Description of the Method
289          *
290          *@exception MessagingException Description of the Exception
291          */

292         public void execute() throws MessagingException JavaDoc {
293             MailCommandManager.openFolder(aFolder, Folder.READ_ONLY);
294
295             // add folder, too
296
addResult(aFolder);
297
298             Message JavaDoc[] messages = aFolder.getMessages();
299
300             // Use a suitable FetchProfile
301
FetchProfile JavaDoc fp = new FetchProfile JavaDoc();
302             fp.add(FetchProfile.Item.ENVELOPE);
303             fp.add(FetchProfile.Item.FLAGS);
304             fp.add("X-Mailer");
305             aFolder.fetch(messages, fp);
306
307             // add all messages to the result
308
addResult(messages);
309
310         }
311     }
312
313
314     /**
315      * List all subfolders of a folder, put
316      * all retrieved folders as command result.
317      *
318      */

319     public static class MailListFolderCommand extends AbstractMailCommand implements Contextualizable {
320
321         private Folder JavaDoc aFolder;
322         private String JavaDoc folderPattern = MailCommandManager.DEFAULT_FOLDER_PATTERN;
323
324
325         /**
326          *Constructor for the MailFoldersCommand object
327          */

328         public MailListFolderCommand() { }
329
330
331         /**
332          * Gets the folderPattern attribute of the ListFolderCommand object
333          *
334          *@return The folderPattern value
335          */

336         public String JavaDoc getFolderPattern() {
337             return this.folderPattern;
338         }
339
340
341         /**
342          * Description of the Method
343          *
344          *@param ctx Description of the Parameter
345          *@exception ContextException Description of the Exception
346          */

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 JavaDoc) ctx.get("param:" + CONTEXT_FOLDER_PATTERN_ENTRY);
353             } catch (ContextException ce) {
354                 // use default folder pattern
355
this.folderPattern = MailCommandManager.DEFAULT_FOLDER_PATTERN;
356             }
357         }
358
359
360         /**
361          * Description of the Method
362          *
363          *@exception MessagingException Description of the Exception
364          */

365         public void execute() throws MessagingException JavaDoc {
366             // spec say: folder list can be invoked on closed folder MailCommandManager.openFolder(aFolder,Folder.READ_ONLY);
367
//addResult(aFolder);
368
Folder JavaDoc[] 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     /**
379      * Retrieved a message (envelope plus content) of a folder by its uid, put
380      * retrieved message as command result.
381      *
382      */

383     public static class MailCatMessageByUIDCommand extends AbstractMailCommand implements Contextualizable {
384
385         private int msgUID = 1;
386         private Folder JavaDoc aFolder;
387
388
389         /**
390          *Constructor for the MailMessageByUIDCommand object
391          */

392         public MailCatMessageByUIDCommand() { }
393
394
395         /**
396          * Description of the Method
397          *
398          *@param ctx Description of the Parameter
399          *@exception ContextException Description of the Exception
400          */

401         public void contextualize(Context ctx) throws ContextException {
402             MailContext mctx = (MailContext) ctx;
403             this.aFolder = mctx.getTheFolder(CONTEXT_FOLDER_ENTRY);
404
405             Integer JavaDoc i = (Integer JavaDoc) ctx.get("param-integer:" + CONTEXT_UID_ENTRY);
406             if (i == null) {
407                 String JavaDoc 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         /**
415          * Description of the Method
416          *
417          *@exception MessagingException Description of the Exception
418          */

419         public void execute() throws MessagingException JavaDoc {
420             UIDFolder JavaDoc uidFolder = (UIDFolder JavaDoc) aFolder;
421             MailCommandManager.openFolder(aFolder, Folder.READ_ONLY);
422
423             // add folder, too
424
addResult(aFolder);
425
426             Message JavaDoc msg = uidFolder.getMessageByUID(msgUID);
427             addResult(msg);
428         }
429     }
430
431
432     /**
433      * Retrieved a message (envelope plus content) of a folder by its id, put
434      * retrieved message as command result.
435      *
436      */

437     public static class MailCatMessageByIdCommand extends AbstractMailCommand implements Contextualizable {
438
439         private int msgId = 1;
440         private Folder JavaDoc aFolder;
441
442
443         /**
444          *Constructor for the MailMessageByIdCommand object
445          */

446         public MailCatMessageByIdCommand() { }
447
448
449         /**
450          * Description of the Method
451          *
452          *@param ctx Description of the Parameter
453          *@exception ContextException Description of the Exception
454          */

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 JavaDoc i = (Integer JavaDoc) ctx.get("param-integer:" + CONTEXT_ID_ENTRY);
461                 this.msgId = i.intValue();
462             } catch (ContextException ce) {
463                 String JavaDoc message = "Missing mandatory context entry " + String.valueOf(CONTEXT_ID_ENTRY);
464                 throw new ContextException(message);
465             }
466         }
467
468
469         /**
470          * Description of the Method
471          *
472          *@exception MessagingException Description of the Exception
473          */

474         public void execute() throws MessagingException JavaDoc {
475             MailCommandManager.openFolder(aFolder, Folder.READ_ONLY);
476
477             // add folder, too
478
addResult(aFolder);
479
480             Message JavaDoc msg = aFolder.getMessage(msgId);
481             addResult(msg);
482         }
483     }
484
485
486     /**
487      * Retrieved a message part by its part id, specifying the message by id, put
488      * retrieved part as command result.
489      *
490      */

491     public static class MailCatAttachmentMessageByIdCommand extends AbstractMailCommand implements Contextualizable {
492
493         private int msgId = -1;
494         private int partId = -1;
495         private Folder JavaDoc aFolder;
496
497
498         /**
499          *Constructor for the MailCatAttachmentMessageByIdCommand object
500          */

501         public MailCatAttachmentMessageByIdCommand() { }
502
503
504         /**
505          * Description of the Method
506          *
507          *@param ctx Description of the Parameter
508          *@exception ContextException Description of the Exception
509          */

510         public void contextualize(Context ctx) throws ContextException {
511             MailContext mctx = (MailContext) ctx;
512             this.aFolder = mctx.getTheFolder(CONTEXT_FOLDER_ENTRY);
513
514             Integer JavaDoc i = (Integer JavaDoc) ctx.get("param-integer:" + CONTEXT_ID_ENTRY);
515             if (i == null) {
516                 String JavaDoc 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 JavaDoc) ctx.get("param-integer:" + CONTEXT_PARTID_ENTRY);
522             if (i == null) {
523                 String JavaDoc 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         /**
531          * Description of the Method
532          *
533          *@exception MessagingException Description of the Exception
534          */

535         public void execute() throws MessagingException JavaDoc {
536             MailCommandManager.openFolder(aFolder, Folder.READ_ONLY);
537
538             // add folder, too
539
addResult(aFolder);
540
541             // get the message
542
Message JavaDoc msg = aFolder.getMessage(msgId);
543
544             if (msg == null) {
545                 String JavaDoc message = "Cannot get message for id " + String.valueOf(msgId);
546                 getLogger().warn(message);
547                 return;
548             }
549             try {
550                 Part JavaDoc part = null;
551                 Object JavaDoc objRef = msg.getContent();
552                 if (!(objRef instanceof Multipart JavaDoc)) {
553                     String JavaDoc message = "Message of id " + String.valueOf(msgId) + " is not a multipart message!";
554                     getLogger().warn(message);
555                     return;
556                 }
557                 Multipart JavaDoc multipart = (Multipart JavaDoc) objRef;
558                 int numParts = multipart.getCount();
559
560                 if (partId < numParts) {
561                     part = multipart.getBodyPart(partId);
562                 } else {
563                     String JavaDoc 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 JavaDoc ioe) {
568                 String JavaDoc message = "Cannot get content of " +
569                         "message for id " + String.valueOf(msgId);
570                 throw new MessagingException JavaDoc(message, ioe);
571             }
572         }
573     }
574
575
576     /**
577      * Description of the Class
578      *
579      */

580     public static class MailSearchMessagesCommand extends AbstractMailCommand implements Contextualizable {
581         private Folder JavaDoc aFolder;
582         private SearchTerm JavaDoc searchTerm;
583
584
585         /**
586          * Description of the Method
587          *
588          *@param ctx Description of the Parameter
589          *@exception ContextException Description of the Exception
590          */

591         public void contextualize(Context ctx) throws ContextException {
592             MailContext mctx = (MailContext) ctx;
593             this.aFolder = mctx.getTheFolder(CONTEXT_FOLDER_ENTRY);
594
595             String JavaDoc searchString = (String JavaDoc) ctx.get("param:" + "search");
596             if (searchString == null) {
597                 searchString = "";
598             }
599             searchTerm = new OrTerm JavaDoc(
600                     new SubjectTerm JavaDoc(searchString),
601                     new FromStringTerm JavaDoc(searchString));
602
603             // build searchTerm from searchTermString
604

605             /* proposed searchTermString syntax
606               {header}:comp-op:{value} & {header}:comp-op:{value} | .....
607               eg. subject:eq:cocoon & date::MM/DD/2002
608               header:com-op:
609                 subject:[cont|ncont]:
610                 sender:[cont|ncont]:
611                 body:[cont|ncont]:
612                 date:[is|nis|before|after]
613                 status:[is|nis]:[Read|New|Replied|Forwarded]
614                 to:[cont|ncont]:
615                 cc:
616                 age-in-days:[is|nis|gt|lt]:
617                 reply-to:[cont|ncont]:
618             */

619         }
620
621
622         /**
623          * Description of the Method
624          *
625          *@exception MessagingException Description of the Exception
626          */

627         public void execute() throws MessagingException JavaDoc {
628             MailCommandManager.openFolder(aFolder, Folder.READ_ONLY);
629
630             // add folder, too
631
addResult(aFolder);
632
633             Message JavaDoc[] msgs = aFolder.search(searchTerm);
634             addResult(msgs);
635         }
636     }
637 }
638
639
Popular Tags