KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scioworks > imap > presentation > imapWeb > mailAction


1 /* -----------------------------------------------------------------------------
2  * Copyright (c) 2001, Low Kin Onn
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *
8  * Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * Neither name of the Scioworks Pte. Ltd. nor the names of its contributors
16  * may beused to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * -----------------------------------------------------------------------------
31  */

32
33 package scioworks.imap.presentation.imapWeb;
34
35 import java.util.Vector JavaDoc;
36 import java.io.*;
37
38 import javax.mail.*;
39 import javax.mail.internet.*;
40
41 import org.w3c.dom.*;
42 import org.w3c.dom.html.*;
43 import org.enhydra.xml.xmlc.XMLCUtil;
44
45 import com.lutris.appserver.server.httpPresentation.*;
46 import com.lutris.util.KeywordValueException;
47
48 import scioworks.imap.presentation.base.*;
49 import scioworks.imap.presentation.imapWeb.*;
50 import scioworks.imap.spec.beans.*;
51 import scioworks.imap.spec.ImapWebException;
52 import scioworks.imap.spec.ImapWebConstant;
53
54 public class mailAction extends BasePO {
55
56   private String JavaDoc send() throws HttpPresentationException {
57     // get input parameters
58
String JavaDoc fTo = super.getStringParameter(PARAM_to);
59     String JavaDoc fSubject = super.getStringParameter(PARAM_subject);
60     String JavaDoc fCc = super.getStringParameter(PARAM_cc);
61     String JavaDoc fBcc = super.getStringParameter(PARAM_bcc);
62     String JavaDoc fMessage = super.getStringParameter(PARAM_message);
63     String JavaDoc[] fSaveCopy = super.getStringParameterArray(PARAM_savecopy);
64     String JavaDoc fOrigFolder = super.getStringParameter(PARAM_origFolder);
65     long fOrigMsgid = super.getLongParameter(PARAM_origMsgid);
66
67     boolean saveCopy = (fSaveCopy.length != 0);
68
69     boolean isForwardAttach;
70     if (fOrigMsgid != -1) {
71       isForwardAttach = true;
72     } else {
73       isForwardAttach = false;
74     }
75
76  IWMessage iwMsg = IWMessageFactory.getIWMessage("scioworks.imap.business.beans.IWMessageImpl");
77  
78     try {
79
80       Object JavaDoc obj = super.getComms().sessionData.get(FileUploadSessionData.SESSION_KEY);
81       Vector JavaDoc attachments;
82       if (obj != null) {
83         attachments = ((FileUploadSessionData) obj).getFileList();
84
85       } else {
86         // create empty attachment vector
87
attachments = new Vector JavaDoc();
88       }
89
90       iwMsg.sendMessage(super.getImapWebSessionData().getImapStore(),
91                         super.getImapWebSessionData().getImapSession(),
92                         super.getImapWebSessionData().getImapURL(),
93                         ImapWebConstant.singleton().domain(),
94                         fTo, fSubject, fCc, fBcc, fMessage,
95                         attachments,
96                         true, // send as inline attachments for now
97
saveCopy,
98                         isForwardAttach,
99                         fOrigFolder,
100                         fOrigMsgid);
101
102       // remove the attachment for the session data
103
if (obj != null) {
104         // delete the tmp files
105
String JavaDoc tmpFilename;
106         for (int i=0; i<attachments.size(); i++) {
107           tmpFilename = ((FileUploadSessionData)obj).getTmpFilename(i);
108           File file = new File(tmpFilename);
109           if (!file.delete()) {
110             System.err.println("Cannot delete tmp file: "+ tmpFilename);
111           }
112         }
113         super.getComms().sessionData.remove(FileUploadSessionData.SESSION_KEY);
114       }
115
116       /** @todo : set target url */
117       return showCompletionPage(MSG_OPERATION_COMPLETED,
118                                 "Your email has been sent.", "", "");
119      } catch(NullPointerException JavaDoc ex) {
120    /*
121     * The response will be default HTML page
122     * We need to allow imapWeb_pres to be functional
123     */

124     return super.showCompletionPage(MSG_OPERATION_FAILED,
125                                      "You cannot send massage while runing imapWeb_pres! That was a default HTML page",
126                                      "", "");
127     } catch (ImapWebException e) {
128       // error, show message
129
return showErrorPage(MSG_OPERATION_FAILED, e.getMessage(), "", "");
130
131     } catch (KeywordValueException e) {
132       return showErrorPage(MSG_OPERATION_FAILED, e.getMessage(), "", "");
133     }
134   }
135
136   private String JavaDoc forward() throws HttpPresentationException {
137     // get input parameters
138
String JavaDoc fFolder = super.getStringParameter(PARAM_folder);
139     long fMsgid = super.getLongParameter(PARAM_msgid);
140     String JavaDoc fForwardType = super.getStringParameter(PARAM_forwardType);
141
142     boolean isForwardInline;
143     if (fForwardType.equalsIgnoreCase("INLINE")) {
144       isForwardInline = true;
145     } else {
146       isForwardInline = false;
147     }
148
149     boolean quoteOrig = true;
150
151 IWMessage iwMsg = IWMessageFactory.getIWMessage("scioworks.imap.business.beans.IWMessageImpl");
152  
153     try {
154       Message forwardMsg = iwMsg.forwardMessage(super.getImapWebSessionData().getImapSession(),
155                                      super.getImapWebSessionData().getImapStore(),
156                                      fFolder, fMsgid, isForwardInline);
157
158       composeHTML page = (composeHTML)m_comms.xmlcFactory.create(composeHTML.class);
159
160       // reset the session data
161
Object JavaDoc obj = getComms().sessionData.get(FileUploadSessionData.SESSION_KEY);
162       if (obj != null) {
163         obj = null;
164       }
165
166       if (isForwardInline) {
167         ((HTMLInputElement)page.getElementOrigFolder()).setValue("-1");
168         ((HTMLInputElement)page.getElementOrigMsgid()).setValue("-1");
169       } else {
170         ((HTMLInputElement)page.getElementOrigFolder()).setValue(fFolder);
171         ((HTMLInputElement)page.getElementOrigMsgid()).setValue(Long.toString(fMsgid));
172       }
173
174       // set the 'to'
175
page.getElementTo().setValue(InternetAddress.toString(forwardMsg.getAllRecipients()));
176       // set the 'subject'
177
page.getElementSubject().setValue(forwardMsg.getSubject());
178
179       HTMLTextAreaElement messageEle = page.getElementMessage();
180
181  
182     
183     String JavaDoc quote = iwMsg.getMessageBody(forwardMsg);
184     
185     
186
187     
188     
189       if (XMLCUtil.findFirstText(messageEle) != null) {
190         XMLCUtil.findFirstText(messageEle).setData(quote);
191       } else {
192         messageEle.appendChild(page.createTextNode(quote));
193       }
194
195       return page.toDocument();
196
197      } catch(NullPointerException JavaDoc ex) {
198    /*
199     * The response will be default HTML page
200     * We need to allow imapWeb_pres to be functional
201     */

202     return super.showCompletionPage(MSG_OPERATION_FAILED,
203                                      "You cannot forward massage while runing imapWeb_pres! That was a default HTML page",
204                                      "", "");
205     } catch (MessagingException e) {
206       return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(),
207                                  "", "");
208
209     } catch (ImapWebException e) {
210       return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(),
211                                  "", "");
212
213     } catch (KeywordValueException e) {
214       return super.showErrorPage(MSG_OPERATION_FAILED,
215                                  "Problem getting session data from session. " + e.getMessage(),
216                                  "", "");
217     }
218   }
219
220   private String JavaDoc reply(boolean replyAll) throws HttpPresentationException {
221     // get input parameters
222
String JavaDoc fFolder = super.getStringParameter(PARAM_folder);
223     long fMsgid = super.getLongParameter(PARAM_msgid);
224
225     boolean quoteOrig = true;
226
227   IWMessage iwMsg = IWMessageFactory.getIWMessage("scioworks.imap.business.beans.IWMessageImpl");
228  
229     try {
230       Message replyMsg = iwMsg.replyMessage(super.getImapWebSessionData().getImapSession(),
231                                      super.getImapWebSessionData().getImapStore(),
232                                      fFolder, fMsgid, replyAll, quoteOrig);
233
234       composeHTML page = (composeHTML)m_comms.xmlcFactory.create(composeHTML.class);
235
236       // reset the session data
237
Object JavaDoc obj = getComms().sessionData.get(FileUploadSessionData.SESSION_KEY);
238       if (obj != null) {
239         obj = null;
240       }
241
242       // set the 'to'
243
page.getElementTo().setValue(InternetAddress.toString(replyMsg.getAllRecipients()));
244       // set the 'subject'
245
page.getElementSubject().setValue(replyMsg.getSubject());
246
247       // set the message box, if quoteOrig is true
248
if (quoteOrig) {
249         HTMLTextAreaElement messageEle = page.getElementMessage();
250
251
252         String JavaDoc quote = iwMsg.getMessageBody(replyMsg);
253
254
255         if (XMLCUtil.findFirstText(messageEle) != null) {
256           XMLCUtil.findFirstText(messageEle).setData(quote);
257         } else {
258           messageEle.appendChild(page.createTextNode(quote));
259         }
260       }
261
262       return page.toDocument();
263      } catch(NullPointerException JavaDoc ex) {
264    /*
265     * The response will be default HTML page
266     * We need to allow imapWeb_pres to be functional
267     */

268     return super.showCompletionPage(MSG_OPERATION_FAILED,
269                                      "You cannot reply massage while runing imapWeb_pres! That was a default HTML page",
270                                      "", "");
271     
272     } catch (MessagingException e) {
273       return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(),
274                                  "", "");
275
276     } catch (ImapWebException e) {
277       return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(),
278                                  "", "");
279
280     } catch (KeywordValueException e) {
281       return super.showErrorPage(MSG_OPERATION_FAILED,
282                                  "Problem getting session data from session. " + e.getMessage(),
283                                  "", "");
284     }
285   }
286
287   private String JavaDoc delete() throws HttpPresentationException {
288     // get input parameters
289
String JavaDoc fFolder = super.getStringParameter(PARAM_folder);
290     long[] fMsgidArr = super.getLongParameterArray(PARAM_msgid);
291
292     try {
293       
294    IWMessage iwMsg = IWMessageFactory.getIWMessage("scioworks.imap.business.beans.IWMessageImpl");
295     
296       
297       iwMsg.deleteMessage(super.getImapWebSessionData().getImapStore(),
298                           fFolder, fMsgidArr);
299
300       /** @todo : set target url */
301       return super.showCompletionPage(MSG_OPERATION_COMPLETED,
302                                      "The selected messages has been deleted.",
303                                      "", "");
304     } catch(NullPointerException JavaDoc ex) {
305    /*
306     * The response will be default HTML page
307     * We need to allow imapWeb_pres to be functional
308     */

309     return super.showCompletionPage(MSG_OPERATION_FAILED,
310                                      "You cannot delete massage while runing imapWeb_pres! That was a default HTML page",
311                                      "", "");
312     
313     } catch (ImapWebException e) {
314       return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(),
315                                  "", "");
316     }
317
318   }
319
320   private String JavaDoc move() throws HttpPresentationException {
321     // get input parameters
322
String JavaDoc fFolder = super.getStringParameter(PARAM_folder);
323     long[] fMsgidArr = super.getLongParameterArray(PARAM_msgid);
324     String JavaDoc fTargetFolder = super.getStringParameter(PARAM_targetFolder);
325
326     try {
327  IWMessage iwMsg = IWMessageFactory.getIWMessage("scioworks.imap.business.beans.IWMessageImpl");
328  
329       iwMsg.moveMessage(super.getImapWebSessionData().getImapStore(),
330                         fFolder, fMsgidArr, fTargetFolder);
331
332       /** @todo : set target url */
333       return super.showCompletionPage(MSG_OPERATION_COMPLETED,
334                                      "The selected messages has been moved.",
335                                      "", "");
336      } catch(NullPointerException JavaDoc ex) {
337    /*
338     * The response will be default HTML page
339     * We need to allow imapWeb_pres to be functional
340     */

341     return super.showCompletionPage(MSG_OPERATION_FAILED,
342                                      "You cannot move massage while runing imapWeb_pres! That was a default HTML page",
343                                      "", "");
344     } catch (ImapWebException e) {
345       return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(),
346                                  "", "");
347     }
348   }
349
350   private String JavaDoc downloadAttachment() throws HttpPresentationException {
351     // get input parameters
352
String JavaDoc fFolder = super.getStringParameter(PARAM_folder);
353     long fMsgid = super.getLongParameter(PARAM_msgid);
354     String JavaDoc fPart = super.getStringParameter(PARAM_part);
355
356     try {
357        IWMessage iwMsg = IWMessageFactory.getIWMessage("scioworks.imap.business.beans.IWMessageImpl");
358    
359       Part part = iwMsg.getMessagePart(super.getImapWebSessionData().getImapSession(),
360                                        super.getImapWebSessionData().getImapStore(),
361                                        fFolder, fMsgid, fPart);
362
363       // get binary stream to retrieve BLOB data
364
InputStream is = part.getInputStream();
365
366       // Get the buffer size suggested by JDBC
367
byte[] buffer = new byte[1024];
368       int length = -1;
369
370       this.getComms().response.setContentType(part.getContentType());
371       this.getComms().response.setHeader("Content-disposition",
372                                          "filename=" + part.getFileName());
373
374       // Pipe the input stream to output stream
375
while ((length = is.read(buffer)) != -1) {
376         this.getComms().response.getOutputStream().write(buffer, 0, length);
377       }
378
379       // Close input stream
380
is.close();
381
382       // Flush the output
383
this.getComms().response.flush();
384
385       return "?";
386
387      } catch(NullPointerException JavaDoc ex) {
388    /*
389     * The response will be default HTML page
390     * We need to allow imapWeb_pres to be functional
391     */

392     return super.showCompletionPage(MSG_OPERATION_FAILED,
393                                      "You cannot download attachment while runing imapWeb_pres! That was a default HTML page",
394                                      "", "");
395     } catch (IOException e) {
396       return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(),
397                                  "", "");
398     } catch (MessagingException e) {
399       return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(),
400                                  "", "");
401
402     } catch (ImapWebException e) {
403       return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(),
404                                  "", "");
405     }
406   }
407
408   private String JavaDoc createFolder() throws HttpPresentationException {
409     // get input parameter
410
String JavaDoc fName = super.getStringParameter(PARAM_name);
411
412     try {
413   IWMessage iwMsg = IWMessageFactory.getIWMessage("scioworks.imap.business.beans.IWMessageImpl");
414   
415       iwMsg.createFolder(super.getImapWebSessionData().getImapStore(),
416                          fName);
417
418       /** @todo : set target url */
419       return super.showCompletionPage(MSG_OPERATION_COMPLETED,
420                                      "Folder created.","", "");
421    } catch(NullPointerException JavaDoc ex) {
422    /*
423     * The response will be default HTML page
424     * We need to allow imapWeb_pres to be functional
425     */

426     return super.showCompletionPage(MSG_OPERATION_FAILED,
427                                      "You cannot create folder while runing imapWeb_pres! That was a default HTML page",
428                                      "", "");
429     } catch (ImapWebException e) {
430       return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(),
431                                  "", "");
432     }
433   }
434
435   private String JavaDoc renameFolder() throws HttpPresentationException {
436     // get input parameter
437
String JavaDoc fFolder = super.getStringParameter(PARAM_folder);
438     String JavaDoc fName = super.getStringParameter(PARAM_name);
439
440     try {
441   IWMessage iwMsg = IWMessageFactory.getIWMessage("scioworks.imap.business.beans.IWMessageImpl");
442   
443       iwMsg.renameFolder(super.getImapWebSessionData().getImapStore(),
444                          fFolder, fName);
445
446       /** @todo : set target url */
447       return super.showCompletionPage(MSG_OPERATION_COMPLETED,
448                                      "Folder renamed.",
449                                      "", "");
450     } catch (ImapWebException e) {
451       return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(),
452                                  "", "");
453     }
454   }
455
456   private String JavaDoc removeFolder() throws HttpPresentationException {
457     // get input parameter
458
String JavaDoc fFolder = super.getStringParameter(PARAM_folder);
459
460     try {
461    IWMessage iwMsg = IWMessageFactory.getIWMessage("scioworks.imap.business.beans.IWMessageImpl");
462     
463       iwMsg.removeFolder(super.getImapWebSessionData().getImapStore(),
464                          fFolder);
465
466       /** @todo : set target url */
467       return super.showCompletionPage(MSG_OPERATION_COMPLETED,
468                                      "Folder removed.","", "");
469      } catch(NullPointerException JavaDoc ex) {
470    /*
471     * The response will be default HTML page
472     * We need to allow imapWeb_pres to be functional
473     */

474     return super.showCompletionPage(MSG_OPERATION_FAILED,
475                                      "You cannot remove folder while runing imapWeb_pres! That was a default HTML page",
476                                      "", "");
477     } catch (ImapWebException e) {
478       return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(),
479                                  "", "");
480     }
481   }
482
483   public String JavaDoc handleDefault()
484       throws HttpPresentationException {
485
486     String JavaDoc event = getStringParameter(PARAM_event);
487     String JavaDoc returnHTML = null;
488
489     if (event.equals(EVENT_send)) {
490       returnHTML = send();
491
492     } else if (event.equals(EVENT_reply)) {
493       returnHTML = reply(false);
494
495     } else if (event.equals(EVENT_replyAll)) {
496       returnHTML = reply(true);
497
498     } else if (event.equals(EVENT_delete)) {
499       returnHTML = delete();
500
501     } else if (event.equals(EVENT_forward)) {
502       returnHTML = forward();
503
504     } else if (event.equals(EVENT_downloadAttachment)) {
505       returnHTML = downloadAttachment();
506
507     } else if (event.equals(EVENT_move)) {
508       returnHTML = move();
509
510     } else if (event.equals(EVENT_createFolder)) {
511       returnHTML = createFolder();
512
513     } else if (event.equals(EVENT_renameFolder)) {
514       returnHTML = renameFolder();
515
516     } else if (event.equals(EVENT_removeFolder)) {
517       returnHTML = removeFolder();
518
519     } else {
520       return showErrorPage(ERROR_UNDEFINE_EVENT,
521                            "Event " + event + " is undefined",
522                            "", "");
523     }
524
525     return returnHTML;
526   }
527
528 }
Popular Tags