KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.*;
36 import java.net.URLEncoder JavaDoc;
37 import java.util.Vector JavaDoc;
38
39 import javax.mail.*;
40 import javax.mail.internet.*;
41 import javax.activation.*;
42
43 import org.w3c.dom.*;
44 import org.w3c.dom.html.*;
45
46 import com.lutris.appserver.server.httpPresentation.*;
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.util.*;
53
54 public class view_mail extends BasePO {
55
56   public String JavaDoc handleDefault() throws HttpPresentationException {
57
58     String JavaDoc fFolder = super.getStringParameter(PARAM_folder);
59     long fMsgid = super.getLongParameter(PARAM_msgid);
60
61     try {
62
63   
64   
65     IWMessage iwMsg = IWMessageFactory.getIWMessage("scioworks.imap.business.beans.IWMessageImpl");
66  
67       Message msg = iwMsg.getMessage(super.getImapWebSessionData().getImapStore(),
68                                      fFolder, fMsgid);
69     // create the page
70
view_mailHTML page = (view_mailHTML)m_comms.xmlcFactory.create(view_mailHTML.class);
71
72      
73       // set the hidden field values
74
((HTMLInputElement) page.getElementFolder1()).setValue(fFolder);
75       ((HTMLInputElement) page.getElementMsgid1()).setValue(Long.toString(fMsgid));
76
77       ((HTMLInputElement) page.getElementFolder2()).setValue(fFolder);
78       ((HTMLInputElement) page.getElementMsgid2()).setValue(Long.toString(fMsgid));
79
80       ((HTMLInputElement) page.getElementFolder3()).setValue(fFolder);
81       ((HTMLInputElement) page.getElementMsgid3()).setValue(Long.toString(fMsgid));
82
83       // set the drop down list of target folders for Move
84
Vector JavaDoc targetFolders = iwMsg.getFolderNames(super.getImapWebSessionData().getImapStore(),
85                                                   fFolder);
86       targetFolders.insertElementAt("-- Choose Folder --", 0);
87       super.processSelectList(page, page.getElementTargetFolder(),
88                               targetFolders, targetFolders, "-1");
89
90       page.getElementDate().removeAttribute("id");
91       page.getElementTo().removeAttribute("id");
92       page.getElementFrom().removeAttribute("id");
93       page.getElementSubject().removeAttribute("id");
94       page.getElementEmailText().removeAttribute("id");
95
96       // get reference to the message row
97
Node messageRow = page.getElementMessageRow();
98       Node messageRowParent = messageRow.getParentNode();
99
100       // set the email header and details
101
page.setTextDate(msg.getSentDate().toString());
102       page.setTextTo(InternetAddress.toString(msg.getAllRecipients()));
103     
104       MessagingUtil messagingUtil = MessagingUtilFactory.getMessagingUtil("scioworks.imap.business.util.MessagingUtilImpl");
105   
106       page.setTextSubject(messagingUtil.formatSubject(msg.getSubject()));
107       page.setTextFrom(msg.getFrom()[0].toString());
108       page.setTextEmailText(messagingUtil.formatBodyText(iwMsg.getMessageBody(msg)));
109
110       messageRowParent.appendChild(messageRow.cloneNode(true));
111
112       // set the attachments
113
HTMLElement filenameEle = page.getElementFilename();
114       HTMLAnchorElement downloadLinkEle = page.getElementDownloadLink();
115
116       filenameEle.removeAttribute("id");
117       downloadLinkEle.removeAttribute("id");
118
119       // Get reference to the row to be modified
120
HTMLTableRowElement attachmentListRow = page.getElementAttachmentListRow();
121       Node attachmentListTable = attachmentListRow.getParentNode();
122
123       String JavaDoc currLink = downloadLinkEle.getHref() + "?" +
124                         PARAM_event + "=" + EVENT_downloadAttachment + "&" +
125                         PARAM_folder + "=" + URLEncoder.encode(fFolder) + "&" +
126                         PARAM_msgid + "=" + Long.toString(fMsgid) + "&" +
127                         PARAM_part + "=" ;
128
129       if (msg.isMimeType("multipart/*")) {
130         Multipart mp = (Multipart)msg.getContent();
131         for (int i=1; i<mp.getCount(); i++) {
132           processEmail(page, mp.getBodyPart(i), currLink+Integer.toString(i));
133         }
134         attachmentListTable.removeChild(attachmentListRow);
135       } else {
136         super.removeElement(page, (Element)attachmentListTable);
137       }
138
139       messageRowParent.removeChild(messageRow);
140
141       return page.toDocument();
142    
143    } catch(NullPointerException JavaDoc ex) {
144    /*
145     * The response will be default HTML page
146     * We need to allow imapWeb_pres to be functional
147     */

148      return ((view_mailHTML)m_comms.xmlcFactory.create(view_mailHTML.class)).toDocument();
149    
150    
151     } catch (ImapWebException e) {
152       return super.showErrorPage("Error getting message", e.getMessage(), "", "");
153
154     } catch (MessagingException e) {
155       e.printStackTrace();
156       return super.showErrorPage("Error getting messages", e.getMessage(), "", "");
157
158     } catch (IOException e) {
159       e.printStackTrace();
160       return super.showErrorPage("Error getting message", e.getMessage(), "", "");
161     }
162   }
163
164   public void processEmail(view_mailHTML page, Part part, String JavaDoc partId)
165       throws HttpPresentationException, MessagingException, IOException, ImapWebException {
166
167     if (part.isMimeType("multipart/*")) {
168
169       Multipart mp = ((Multipart) part.getContent());
170       int count = mp.getCount();
171       for (int i = 1; i<mp.getCount(); i++) {
172         processEmail(page, mp.getBodyPart(i), partId + "." + i);
173       }
174
175     } else if (part.isMimeType("message/rfc822")) {
176
177
178       // get reference to the message row
179
Node messageRow = page.getElementMessageRow();
180       Node messageRowParent = messageRow.getParentNode();
181
182       Message msg = (Message) part.getContent();
183       page.setTextDate(msg.getSentDate().toString());
184       page.setTextTo(InternetAddress.toString(msg.getAllRecipients()));
185       MessagingUtil messagingUtil = MessagingUtilFactory.getMessagingUtil("scioworks.imap.business.util.MessagingUtilImpl");
186   
187       page.setTextSubject(messagingUtil.formatSubject(msg.getSubject()));
188       page.setTextFrom(msg.getFrom()[0].toString());
189      
190  IWMessage iwMsg = IWMessageFactory.getIWMessage("scioworks.imap.business.beans.IWMessageImpl");
191  
192       page.setTextEmailText(messagingUtil.formatBodyText(iwMsg.getMessageBody(msg)));
193
194       messageRowParent.appendChild(messageRow.cloneNode(true));
195
196       if (msg.isMimeType("multipart/*")) {
197         Multipart mp = (Multipart)msg.getContent();
198         for (int i=1; i<mp.getCount(); i++) {
199           processEmail(page, mp.getBodyPart(i), partId + "." + i);
200         }
201       }
202
203     } else {
204       // set the attachments
205
HTMLElement filenameEle = page.getElementFilename();
206       HTMLAnchorElement downloadLinkEle = page.getElementDownloadLink();
207
208       // Get reference to the row to be modified
209
HTMLTableRowElement attachmentListRow = page.getElementAttachmentListRow();
210       Node attachmentListTable = attachmentListRow.getParentNode();
211
212      page.setTextFilename(part.getFileName());
213      downloadLinkEle.setHref(partId);
214
215      attachmentListTable.appendChild(attachmentListRow.cloneNode(true));
216
217     }
218   }
219
220 }
Popular Tags