KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URLEncoder JavaDoc;
37
38 import javax.mail.*;
39 import javax.mail.internet.*;
40 import javax.activation.*;
41
42 import org.w3c.dom.*;
43 import org.w3c.dom.html.*;
44
45 import com.lutris.appserver.server.httpPresentation.*;
46
47 import scioworks.imap.presentation.base.*;
48 import scioworks.imap.presentation.imapWeb.*;
49 import scioworks.imap.spec.beans.*;
50 import scioworks.imap.spec.ImapWebException;
51 import scioworks.imap.spec.util.*;
52
53 public class mail_list extends BasePO {
54
55   public String JavaDoc handleDefault() throws HttpPresentationException {
56
57
58     // get the input parameter
59
String JavaDoc fFolder = super.getStringParameter(PARAM_folder);
60
61     // set default folder to INBOX
62
if (fFolder.equals("")) {
63       fFolder = "INBOX";
64     }
65
66     mail_listHTML page = (mail_listHTML)m_comms.xmlcFactory.create(mail_listHTML.class);
67
68     HTMLInputElement msgidEle = page.getElementMsgid();
69     HTMLElement fromEle = page.getElementFrom();
70     HTMLElement subjectEle = page.getElementSubject();
71     HTMLElement dateEle = page.getElementDate();
72     HTMLElement sizeEle = page.getElementSize();
73
74     // Get reference to the row to be modified
75
HTMLTableRowElement emailListRow = page.getElementEmailListRow();
76     Node emailListTable = emailListRow.getParentNode();
77
78     // Get reference to the URL to be modified
79
HTMLAnchorElement emailLink = page.getElementEmailLink();
80     String JavaDoc currLink = emailLink.getHref() + "?" +
81                       PARAM_folder + "=" + URLEncoder.encode(fFolder) + "&" +
82                       PARAM_msgid + "=";
83
84     // set the drop down list of target folders for Move
85

86     IWMessage iwMsg = IWMessageFactory.getIWMessage("scioworks.imap.business.beans.IWMessageImpl");
87    
88     try {
89       Vector JavaDoc targetFolders = iwMsg.getFolderNames(super.getImapWebSessionData().getImapStore(),
90                                                   fFolder);
91       targetFolders.insertElementAt("-- Choose Folder --", 0);
92       super.processSelectList(page, page.getElementTargetFolder(),
93                               targetFolders, targetFolders, "-1");
94     
95 /*
96  * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run imapWeb_pres )
97  * The response will be default HTML page
98  * We need to allow imapWeb_pres to be functional
99  */

100     } catch(NullPointerException JavaDoc ex) {
101         return ((mail_listHTML)m_comms.xmlcFactory.create(mail_listHTML.class)).toDocument();
102     } catch (ImapWebException e) {
103       return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(), "", "");
104     }
105     // Remove the id attribute
106
msgidEle.removeAttribute("id");
107     fromEle.removeAttribute("id");
108     subjectEle.removeAttribute("id");
109     dateEle.removeAttribute("id");
110     sizeEle.removeAttribute("id");
111     emailListRow.removeAttribute("id");
112     emailLink.removeAttribute("id");
113
114     // Set the folder attribute
115
page.getElementFolder().setValue(fFolder);
116
117     try {
118       Folder folder = super.getImapWebSessionData().getImapStore().getFolder(fFolder);
119
120       UIDFolder ufolder = (UIDFolder) folder;
121
122       // open the folder for read
123
folder.open(Folder.READ_ONLY);
124
125       //get messages
126
Message[] messages = folder.getMessages();
127
128       long uid;
129       String JavaDoc personalName;
130
131       for (int i=0; i<messages.length; i++) {
132
133         // get the uid
134
uid = ufolder.getUID(messages[i]);
135
136         // set the msgid
137
msgidEle.setValue(Long.toString(uid));
138
139         // display the from address
140
personalName = ((InternetAddress)messages[i].getFrom()[0]).getPersonal();
141         if (personalName == null) {
142           page.setTextFrom((messages[i].getFrom()[0]).toString());
143         } else {
144           page.setTextFrom(personalName);
145         }
146
147         // display the date
148

149   
150  
151         page.setTextDate(TextUtil.singleton().dateFormat(messages[i].getSentDate()));
152
153         // display the size
154
page.setTextSize(TextUtil.singleton().verboseFilesize(messages[i].getSize()));
155
156         // display the subject
157
MessagingUtil messagingUtil = MessagingUtilFactory.getMessagingUtil("scioworks.imap.business.util.MessagingUtilImpl");
158              
159         page.setTextSubject(messagingUtil.formatSubject(messages[i].getSubject()));
160
161         // set the link
162
emailLink.setHref(currLink+uid);
163
164         emailListTable.appendChild(emailListRow.cloneNode(true));
165       }
166
167       // close the folder
168
folder.close(false);
169
170     } catch (MessagingException e) {
171       super.showErrorPage("Error getting messages", e.getMessage(), "", "");
172
173     } finally {
174       emailListTable.removeChild(emailListRow);
175     }
176
177     return page.toDocument();
178   }
179 }
Popular Tags