001: /* -----------------------------------------------------------------------------
002: * Copyright (c) 2001, Low Kin Onn
003: * All rights reserved.
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: *
008: * Redistributions of source code must retain the above copyright notice, this
009: * list of conditions and the following disclaimer.
010: *
011: * Redistributions in binary form must reproduce the above copyright notice,
012: * this list of conditions and the following disclaimer in the documentation
013: * and/or other materials provided with the distribution.
014: *
015: * Neither name of the Scioworks Pte. Ltd. nor the names of its contributors
016: * may beused to endorse or promote products derived from this software without
017: * specific prior written permission.
018: *
019: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
020: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
021: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
022: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
023: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
024: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
025: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
026: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
027: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
028: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: *
030: * -----------------------------------------------------------------------------
031: */
032:
033: package scioworks.imap.presentation.imapWeb;
034:
035: import java.util.Vector;
036: import java.net.URLEncoder;
037:
038: import javax.mail.*;
039: import javax.mail.internet.*;
040: import javax.activation.*;
041:
042: import org.w3c.dom.*;
043: import org.w3c.dom.html.*;
044:
045: import com.lutris.appserver.server.httpPresentation.*;
046:
047: import scioworks.imap.presentation.base.*;
048: import scioworks.imap.presentation.imapWeb.*;
049: import scioworks.imap.spec.beans.*;
050: import scioworks.imap.spec.ImapWebException;
051: import scioworks.imap.spec.util.*;
052:
053: public class mail_list extends BasePO {
054:
055: public String handleDefault() throws HttpPresentationException {
056:
057: // get the input parameter
058: String fFolder = super .getStringParameter(PARAM_folder);
059:
060: // set default folder to INBOX
061: if (fFolder.equals("")) {
062: fFolder = "INBOX";
063: }
064:
065: mail_listHTML page = (mail_listHTML) m_comms.xmlcFactory
066: .create(mail_listHTML.class);
067:
068: HTMLInputElement msgidEle = page.getElementMsgid();
069: HTMLElement fromEle = page.getElementFrom();
070: HTMLElement subjectEle = page.getElementSubject();
071: HTMLElement dateEle = page.getElementDate();
072: HTMLElement sizeEle = page.getElementSize();
073:
074: // Get reference to the row to be modified
075: HTMLTableRowElement emailListRow = page
076: .getElementEmailListRow();
077: Node emailListTable = emailListRow.getParentNode();
078:
079: // Get reference to the URL to be modified
080: HTMLAnchorElement emailLink = page.getElementEmailLink();
081: String currLink = emailLink.getHref() + "?" + PARAM_folder
082: + "=" + URLEncoder.encode(fFolder) + "&" + PARAM_msgid
083: + "=";
084:
085: // set the drop down list of target folders for Move
086:
087: IWMessage iwMsg = IWMessageFactory
088: .getIWMessage("scioworks.imap.business.beans.IWMessageImpl");
089:
090: try {
091: Vector targetFolders = iwMsg.getFolderNames(super
092: .getImapWebSessionData().getImapStore(), fFolder);
093: targetFolders.insertElementAt("-- Choose Folder --", 0);
094: super .processSelectList(page,
095: page.getElementTargetFolder(), targetFolders,
096: targetFolders, "-1");
097:
098: /*
099: * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run imapWeb_pres )
100: * The response will be default HTML page
101: * We need to allow imapWeb_pres to be functional
102: */
103: } catch (NullPointerException ex) {
104: return ((mail_listHTML) m_comms.xmlcFactory
105: .create(mail_listHTML.class)).toDocument();
106: } catch (ImapWebException e) {
107: return super .showErrorPage(MSG_OPERATION_FAILED, e
108: .getMessage(), "", "");
109: }
110: // Remove the id attribute
111: msgidEle.removeAttribute("id");
112: fromEle.removeAttribute("id");
113: subjectEle.removeAttribute("id");
114: dateEle.removeAttribute("id");
115: sizeEle.removeAttribute("id");
116: emailListRow.removeAttribute("id");
117: emailLink.removeAttribute("id");
118:
119: // Set the folder attribute
120: page.getElementFolder().setValue(fFolder);
121:
122: try {
123: Folder folder = super .getImapWebSessionData()
124: .getImapStore().getFolder(fFolder);
125:
126: UIDFolder ufolder = (UIDFolder) folder;
127:
128: // open the folder for read
129: folder.open(Folder.READ_ONLY);
130:
131: //get messages
132: Message[] messages = folder.getMessages();
133:
134: long uid;
135: String personalName;
136:
137: for (int i = 0; i < messages.length; i++) {
138:
139: // get the uid
140: uid = ufolder.getUID(messages[i]);
141:
142: // set the msgid
143: msgidEle.setValue(Long.toString(uid));
144:
145: // display the from address
146: personalName = ((InternetAddress) messages[i].getFrom()[0])
147: .getPersonal();
148: if (personalName == null) {
149: page.setTextFrom((messages[i].getFrom()[0])
150: .toString());
151: } else {
152: page.setTextFrom(personalName);
153: }
154:
155: // display the date
156:
157: page.setTextDate(TextUtil.singleton().dateFormat(
158: messages[i].getSentDate()));
159:
160: // display the size
161: page.setTextSize(TextUtil.singleton().verboseFilesize(
162: messages[i].getSize()));
163:
164: // display the subject
165: MessagingUtil messagingUtil = MessagingUtilFactory
166: .getMessagingUtil("scioworks.imap.business.util.MessagingUtilImpl");
167:
168: page.setTextSubject(messagingUtil
169: .formatSubject(messages[i].getSubject()));
170:
171: // set the link
172: emailLink.setHref(currLink + uid);
173:
174: emailListTable
175: .appendChild(emailListRow.cloneNode(true));
176: }
177:
178: // close the folder
179: folder.close(false);
180:
181: } catch (MessagingException e) {
182: super .showErrorPage("Error getting messages", e
183: .getMessage(), "", "");
184:
185: } finally {
186: emailListTable.removeChild(emailListRow);
187: }
188:
189: return page.toDocument();
190: }
191: }
|