001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package echo2example.email;
031:
032: import java.util.Properties;
033: import java.util.ResourceBundle;
034:
035: import javax.mail.AuthenticationFailedException;
036: import javax.mail.MessagingException;
037: import javax.mail.Session;
038: import javax.mail.Store;
039:
040: import nextapp.echo2.app.ApplicationInstance;
041: import nextapp.echo2.app.Window;
042:
043: /**
044: * Email Application Instance.
045: */
046: public class EmailApp extends ApplicationInstance {
047:
048: /**
049: * Flag indicating whether we should use fake e-mail data. Enabling this
050: * flag will cause the <code>FauxStore</code> e-mail store to be used and
051: * will disable sending of messages.
052: */
053: public static final boolean FAUX_MODE;
054: public static final String MAIL_DOMAIN, RECEIVE_MAIL_SERVER,
055: RECEIVE_PROTOCOL, SEND_MAIL_SERVER, SEND_MAIL_PORT;
056: public static final int MESSAGES_PER_PAGE;
057:
058: static {
059: // Static initializer to retrieve information from configuration properties file.
060: ResourceBundle config = ResourceBundle
061: .getBundle("/echo2example/email/Configuration");
062: FAUX_MODE = "true".equals(config.getString("FauxMode"));
063: MAIL_DOMAIN = config.getString("MailDomain");
064: RECEIVE_MAIL_SERVER = config.getString("ReceiveMailServer");
065: RECEIVE_PROTOCOL = config.getString("ReceiveProtocol");
066: SEND_MAIL_SERVER = config.getString("SendMailServer");
067: SEND_MAIL_PORT = config.getString("SendMailPort");
068: MESSAGES_PER_PAGE = Integer.parseInt(config
069: .getString("MessagesPerPage"));
070: }
071:
072: /**
073: * The user name of the currently logged in user. This property
074: * will be null when no user is logged in.
075: */
076: private String emailAddress;
077:
078: /**
079: * The <code>javax.mail.Store</code> used to retrieve messages from the mail server.
080: * See the Sun JavaMail API Specification for details.
081: */
082: private Store store;
083:
084: /**
085: * The <code>javax.mail.Session</code> used to connect to the mail server.
086: * See the Sun JavaMail API Specification for details.
087: */
088: private Session mailSession;
089:
090: /**
091: * Convenience method to return the active email application as a
092: * <code>EmailApp</code>.
093: *
094: * @return the active <code>EmailApp</code>
095: */
096: public static EmailApp getApp() {
097: return (EmailApp) getActive();
098: }
099:
100: /**
101: * Connects to the mail server with the given e-mail address and
102: * password. Displays the <code>MailScreen</code> on success.
103: *
104: * @param emailAddress e-mail address
105: * @param password the password
106: * @return true if the application was able to connect to the
107: * server using the specified information, false if not.
108: */
109: public boolean connect(String emailAddress, String password) {
110: Properties properties = System.getProperties();
111: if (!FAUX_MODE) {
112: properties.put("mail.smtp.host", SEND_MAIL_SERVER);
113: properties.put("mail.smtp.port", SEND_MAIL_PORT);
114: }
115: try {
116: mailSession = Session.getDefaultInstance(properties, null);
117: store = mailSession.getStore(RECEIVE_PROTOCOL);
118: store.connect(RECEIVE_MAIL_SERVER, emailAddress, password);
119:
120: // Store user name.
121: this .emailAddress = emailAddress;
122:
123: // Display MailScreen.
124: MailScreen mailScreen = new MailScreen();
125: mailScreen.setStore(store);
126: getDefaultWindow().setContent(mailScreen);
127: } catch (AuthenticationFailedException ex) {
128: // Return false to indicate the user was not successfully authenticated.
129: return false;
130: } catch (MessagingException ex) {
131: processFatalException(ex);
132: }
133:
134: // Return indicating that the user was successfully authenticated.
135: return true;
136: }
137:
138: /**
139: * Disconnects the session with the mail server and displays
140: * the authentication screen.
141: */
142: public void disconnect() {
143: if (store != null) {
144: try {
145: store.close();
146: } catch (MessagingException ex) {
147: // Do nothing.
148: }
149: store = null;
150: }
151:
152: emailAddress = null;
153: getDefaultWindow().setContent(new LoginScreen());
154: }
155:
156: /**
157: * Returns the email address of the active user, or null if none.
158: *
159: * @return the email address
160: */
161: public String getEmailAddress() {
162: return emailAddress;
163: }
164:
165: /**
166: * Returns the active JavaMail <code>Session</code> object.
167: *
168: * @return the <code>Session</code>
169: */
170: public Session getMailSession() {
171: return mailSession;
172: }
173:
174: /**
175: * @see nextapp.echo2.app.ApplicationInstance#init()
176: */
177: public Window init() {
178: setStyleSheet(Styles.DEFAULT_STYLE_SHEET);
179: Window window = new Window();
180: window.setTitle(Messages.getString("Application.Title.Window"));
181: window.setContent(new LoginScreen());
182: return window;
183: }
184:
185: /**
186: * Handles a fatal exception.
187: * This method is invoked when a component of the application
188: * encounters a fatal error that can not be resolved. This
189: * method will log off any currently logged in user and
190: * display an error dialog screen.
191: *
192: * @param ex the fatal exception
193: */
194: public void processFatalException(Exception ex) {
195: ex.printStackTrace();
196: disconnect();
197: MessageDialog messageDialog = new MessageDialog(Messages
198: .getString("FatalException.Title"), ex.toString(),
199: MessageDialog.TYPE_ERROR, MessageDialog.CONTROLS_OK);
200: getDefaultWindow().getContent().add(messageDialog);
201: }
202: }
|