001: /**
002: * $Id: FetchTag.java,v 1.16 2005/09/21 10:52:28 dg154973 Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and iPlanet
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.wireless.taglibs.mail;
014:
015: import javax.servlet.jsp.*;
016: import javax.servlet.jsp.tagext.*;
017: import com.sun.portal.wireless.taglibs.base.*;
018: import com.sun.portal.log.common.PortalLogger;
019:
020: import java.util.*;
021: import java.util.logging.Logger;
022: import java.util.logging.Level;
023: import java.util.logging.LogRecord;
024: import java.text.*;
025:
026: import javax.mail.*;
027: import javax.mail.internet.*;
028: import javax.mail.search.*;
029:
030: /**
031: *
032: * @version 1.0
033: */
034: public class FetchTag extends CommandTag {
035: private static Logger logger = PortalLogger
036: .getLogger(FetchTag.class);
037: private static final String SHARED_FOLDERS = "Shared Folders";
038: private static final String SHARED_FOLDERS_NAMESPACE = "Shared Folders/User";
039: String collection = null;
040: String id = null;
041: String sd = null;
042: String sb = null;
043: MailContext mc;
044:
045: public boolean execute() throws JspException {
046:
047: try {
048: mc = MailContext.getContext(pageContext);
049: } catch (Exception e) {
050: throw new JspException(this .getClass().getName()
051: + ".execute(): Couldn't get mail context: "
052: + e.getMessage());
053: }
054:
055: try {
056: if (collection.equals("messages")) {
057: doMsgsFetch();
058: } else if (collection.equals("folders")) {
059: doFoldersFetch();
060: } else if (collection.equals("lines")) {
061: doLinesFetch();
062: } else if (collection.equals("presetmsgs")) {
063: mc.doPresetMessagesFetch();
064: } else {
065: logger.log(Level.FINER, "PSMA_CSPWTM0006");
066: release();
067: mc.setErrorCode("");
068: return false;
069: }
070: } catch (MessagingException me) {
071: if (logger.isLoggable(Level.FINER)) {
072: LogRecord record = new LogRecord(Level.FINER,
073: "PSMA_CSPWTM0007");
074: record.setParameters(new Object[] { collection });
075: record.setThrown(me);
076: record.setLoggerName(logger.getName());
077: logger.log(record);
078: }
079: return false;
080: }
081:
082: release();
083: return true;
084: }
085:
086: public void release() {
087: super .release();
088: collection = null;
089: id = null;
090: sd = null;
091: sb = null;
092: }
093:
094: public void setCollection(String str) {
095: collection = str;
096: }
097:
098: public void setSb(String str) {
099: sb = evalAttribute(str);
100: }
101:
102: public void setSd(String str) {
103: sd = evalAttribute(str);
104: }
105:
106: public void setid(String str) {
107: id = evalAttribute(str);
108: }
109:
110: private void doFoldersFetch() throws JspException,
111: MessagingException {
112: Folder tmp = null;
113: try {
114: if (id == null || id.length() == 0) { // Get it from the top
115: Store store = mc.getMailStore();
116: tmp = store.getDefaultFolder();
117: } else {
118: Store store = mc.getMailStore();
119:
120: // This is a Shared Folders fetch, use the namespace. Otherwise,
121: // javamail will throws a FolderNotFoundException.
122: if (id.equals(SHARED_FOLDERS)) {
123: id = SHARED_FOLDERS_NAMESPACE;
124: }
125: tmp = store.getFolder(id);
126: }
127: } catch (MessagingException e) {
128: throw new JspException(this .getClass().getName()
129: + ".doFoldersFetch() failed: " + e.toString());
130: }
131:
132: if (tmp != null) {
133: if (!tmp.exists()) {
134: // name specified by "id" does not actually exist on the server.
135: mc.setErrorCode("MAIL_005");
136: throw new MessagingException("Folder (" + id
137: + ") does not exist!");
138: }
139: Folder[] f = tmp.list();
140: FolderBean[] fdrs = MailUtils.toFolderBeanArray(f);
141: MailUtils.setSortDirection(sd);
142: sortArray(fdrs, "folders");
143: mc.setFoldersCollection(fdrs);
144: } else {
145: throw new JspException(this .getClass().getName()
146: + ": Currentfolder is null.");
147: }
148: }
149:
150: private void doLinesFetch() throws JspException {
151: int i = 0;
152: i = Integer.parseInt(id);
153:
154: MsgBean[] msgs = mc.getMsgsCollection();
155:
156: // make sure we have a messages collection first
157: //
158: if (msgs == null) {
159: throw new JspException(this .getClass().getName()
160: + ".doLinesFetch(): No messages collection");
161: }
162:
163: // do some bounds sanity check
164: if (i < 0)
165: i = 0;
166:
167: if (i > (msgs.length - 1))
168: i = msgs.length - 1;
169:
170: mc.setMsg(msgs[i]);
171: mc.setMsgIdx(i);
172:
173: int lineLength = mc.getLineLength();
174: LineBean[] lines;
175:
176: if (lineLength == 0)
177: lines = msgs[i].toLines();
178: else
179: lines = msgs[i].toLines(lineLength);
180:
181: mc.setLinesCollection(lines);
182: }
183:
184: private void doMsgsFetch() throws JspException, MessagingException {
185: Folder tmp = null;
186:
187: if (id != null && id.length() > 0) {
188: try {
189: Store store = mc.getMailStore();
190: tmp = store.getFolder(id);
191: } catch (MessagingException e) {
192: throw new JspException(this .getClass().getName()
193: + ".doMsgsFetch(): failed: " + e.toString());
194: }
195:
196: if (!tmp.exists()) {
197: mc.setErrorCode("MAIL_005");
198: throw new MessagingException("Folder (" + id
199: + ") does not exist!");
200: }
201:
202: Folder curFolder = mc.getCurrentFolder().getFolder();
203: String newName = tmp.getFullName();
204: String oldName = curFolder.getFullName();
205:
206: if (newName.equals(oldName)) {
207: // new folder is the same as the current folder
208: // reuse the current folder object.
209: tmp = curFolder;
210: } else {
211: // new folder is different then current folder,
212: // close current folder and set new folder as
213: // current in context and continue.
214: try {
215: if (curFolder.isOpen())
216: curFolder.close(true);
217: } catch (MessagingException e) {
218: // This isn't a failure error condition, just means we were
219: // unable to close the current folder. Just catch the
220: // exception so it isn't propagated up the stack.
221: }
222:
223: mc.setCurrentFolder(new FolderBean(tmp));
224:
225: // Reset the current Mailview. (-1) will set
226: // it to the allView.
227: mc.setCurrentView("-1");
228: }
229: } else {
230: tmp = mc.getCurrentFolder().getFolder();
231: }
232:
233: if (tmp != null) {
234: MsgBean[] msgs = null;
235:
236: // Check to see if folder is open.
237: // if it isn't then open it
238: if (!tmp.isOpen()) {
239: try {
240: tmp.open(Folder.READ_WRITE);
241: } catch (MessagingException e) {
242: // could be a read only shared folder
243: try {
244: tmp.open(Folder.READ_ONLY);
245: mc.setCurrentFolder(new FolderBean(tmp));
246: } catch (MessagingException me) {
247: mc.setErrorCode("MAIL_006");
248: throw me;
249: }
250: }
251: }
252:
253: // Get all non-deleted messages
254:
255: ViewBean view = mc.getCurrentViewBean();
256: try {
257: Message notDeleted[] = tmp.search(view.getRules());
258: msgs = MailUtils.toBeanArray(notDeleted, mc.getFrom(),
259: mc.getTimeZone(), pageContext);
260: } catch (MessagingException e) {
261: mc.setErrorCode("MAIL_007");
262: throw e;
263: }
264:
265: MailUtils.setSortDirection(sd);
266: sortArray(msgs, sb);
267: mc.setMsgsCollection(msgs);
268: } else {
269: // This should never ever happen, but just in case...
270: throw new JspException(this .getClass().getName()
271: + ".doMsgsFetch(): Specified folder is null.");
272: }
273: }
274:
275: private void sortArray(Object[] in, String field)
276: throws JspException {
277: // Set a default sort by field in case its empty
278: //
279: if (field == null) {
280: field = "inbox";
281: }
282:
283: List arr = Arrays.asList(in);
284: Collator collate = Collator.getInstance(mc.getLocale());
285:
286: Comparator comp = null;
287:
288: if (field.equals("inbox")) {
289: MailUtils.inboxOrder(in);
290: } else if (field.equals("subject")) {
291: comp = new MailUtils.MsgSubjectComparator(collate);
292: MailUtils.prefetch((MsgBean[]) in, "Subject");
293: Arrays.sort(in, comp);
294: } else if (field.equals("folders")) {
295: comp = new MailUtils.FolderComparator(collate);
296: Arrays.sort(in, comp);
297: } else if (field.equals("from")) {
298: comp = new MailUtils.MsgFromComparator(collate);
299: MailUtils.prefetch((MsgBean[]) in, "From");
300: Arrays.sort(in, comp);
301: } else if (field.equals("date")) {
302: comp = new MailUtils.MsgDateComparator();
303: MailUtils.prefetch((MsgBean[]) in, "Date");
304: Arrays.sort(in, comp);
305: } else if (field.equals("id")) {
306: comp = new MailUtils.MsgIdComparator();
307: Arrays.sort(in, comp);
308: } else if (field.equals("seen")) {
309: comp = new MailUtils.MsgSeenComparator();
310: Arrays.sort(in, comp);
311: } else if (field.equals("recent")) {
312: comp = new MailUtils.MsgRecentComparator();
313: Arrays.sort(in, comp);
314: } else {
315: throw new JspException("Unknown sort field specified:"
316: + field);
317: }
318: }
319:
320: }
|