001: /**
002: * $Id: POPCollectTag.java,v 1.11 2005/09/21 10:52:50 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.http.*;
016: import javax.servlet.jsp.*;
017: import javax.servlet.jsp.tagext.*;
018:
019: import javax.mail.*;
020:
021: import com.sun.mobile.util.*;
022: import com.sun.portal.wireless.util.*;
023:
024: import com.sun.portal.wireless.taglibs.base.*;
025: import com.sun.portal.log.common.PortalLogger;
026:
027: import com.sun.ssoadapter.SSOAdapter;
028: import com.sun.ssoadapter.SSOAdapterFactory;
029: import com.sun.ssoadapter.SSOAdapterException;
030: import com.sun.ssoadapter.SSOAdapterSession;
031:
032: import com.sun.ssoadapter.config.Configuration;
033: import com.sun.ssoadapter.config.ConfigurationFactory;
034: import com.sun.ssoadapter.config.ConfigurationException;
035:
036: import java.util.logging.Logger;
037: import java.util.logging.Level;
038:
039: /**
040: *
041: * POPCollectTag - execute a POP3 mail collection
042: *
043: * @version 1.0
044: * @see com.sun.portal.wireless.taglibs.base.CommandTag
045: */
046: public class POPCollectTag extends CommandTag {
047: private static Logger logger = PortalLogger
048: .getLogger(POPCollectTag.class);
049:
050: private String configName = null;
051:
052: public boolean execute() throws JspException {
053:
054: MailContext mc;
055: try {
056: mc = MailContext.getContext(pageContext);
057: } catch (Exception e) {
058: throw new JspException(this .getClass().getName()
059: + ".execute(): Couldn't get mail context: "
060: + e.getMessage());
061: }
062:
063: // Reset error code
064: mc.setErrorCode("");
065:
066: SSOAdapterFactory factory = SSOAdapterFactory.getInstance();
067: SSOAdapter popSSOAdapter = null;
068: try {
069: popSSOAdapter = factory.getSSOAdapter(configName,
070: new SSOAdapterSession(
071: (HttpServletRequest) pageContext
072: .getRequest(), null, null,
073: AppConfigConstants.MAP_INSTANCE_NAME));
074:
075: } catch (SSOAdapterException e) {
076: logger.log(Level.FINE, "PSMA_CSPWTM0031", e);
077: release();
078: return false;
079: }
080:
081: // Destination folder is INBOX
082: // The destination Folder doesn't have to be opened for copyMessages().
083: Folder dfolder = null;
084: try {
085: Store store = mc.getMailStore();
086: dfolder = store.getFolder("INBOX");
087: if (!dfolder.exists())
088: dfolder.create(Folder.HOLDS_MESSAGES);
089:
090: } catch (MessagingException e) {
091: logger.log(Level.FINE, "PSMA_CSPWTM0032", e);
092: release();
093: return false;
094: }
095:
096: // Open POP3 INBOX and copy all the messages to the destination folder
097: try {
098: Store popStore = (Store) popSSOAdapter.getConnection();
099: Folder popInbox = popStore.getFolder("INBOX");
100: if (!popInbox.isOpen())
101: popInbox.open(Folder.READ_WRITE);
102:
103: Message msgs[] = popInbox.getMessages();
104: popInbox.copyMessages(msgs, dfolder);
105:
106: // The expunge() method is not supported by the POP3 provider. To delete
107: // messages, close the folder with the expunge flag set to true.
108: boolean expunge = false;
109:
110: String popDelete = popSSOAdapter.getProperties()
111: .getProperty("popDelete");
112: if (popDelete != null && popDelete.equalsIgnoreCase("true")) {
113: popInbox.setFlags(msgs, new Flags(Flags.Flag.DELETED),
114: true);
115: expunge = true;
116: }
117:
118: popInbox.close(expunge);
119: popStore.close();
120:
121: } catch (Exception e) {
122: logger.log(Level.FINE, "PSMA_CSPWTM0033", e);
123: release();
124: return false;
125: }
126:
127: release();
128: return true;
129: }
130:
131: /**
132: * Cleanup
133: */
134:
135: public void release() {
136: super .release();
137: configName = null;
138: }
139:
140: /**
141: *
142: * Set the config attribute
143: *
144: * @param set the config attribute
145: */
146:
147: public void setConfig(String name) {
148: configName = evalAttribute(name);
149: }
150:
151: }
|