001: /*
002: * Copyright (c) 2001 - 2005 ivata limited.
003: * All rights reserved.
004: * -----------------------------------------------------------------------------
005: * ivata groupware may be redistributed under the GNU General Public
006: * License as published by the Free Software Foundation;
007: * version 2 of the License.
008: *
009: * These programs are free software; you can redistribute them and/or
010: * modify them under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; version 2 of the License.
012: *
013: * These programs are distributed in the hope that they will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: *
017: * See the GNU General Public License in the file LICENSE.txt for more
018: * details.
019: *
020: * If you would like a copy of the GNU General Public License write to
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place - Suite 330
024: * Boston, MA 02111-1307, USA.
025: *
026: *
027: * To arrange commercial support and licensing, contact ivata at
028: * http://www.ivata.com/contact.jsp
029: * -----------------------------------------------------------------------------
030: * $Log: MailSession.java,v $
031: * Revision 1.4 2005/10/02 14:08:59 colinmacleod
032: * Added/improved log4j logging.
033: *
034: * Revision 1.3 2005/09/14 16:16:52 colinmacleod
035: * Removed unused local and class variables.
036: * Added serialVersionUID.
037: *
038: * Revision 1.2 2005/04/09 17:20:01 colinmacleod
039: * Changed copyright text to GPL v2 explicitly.
040: *
041: * Revision 1.1.1.1 2005/03/10 17:51:16 colinmacleod
042: * Restructured ivata op around Hibernate/PicoContainer.
043: * Renamed ivata groupware.
044: *
045: * Revision 1.6 2004/11/12 18:16:08 colinmacleod
046: * Ordered imports.
047: *
048: * Revision 1.5 2004/11/12 15:57:25 colinmacleod
049: * Removed dependencies on SSLEXT.
050: * Moved Persistence classes to ivata masks.
051: *
052: * Revision 1.4 2004/07/13 19:48:12 colinmacleod
053: * Moved project to POJOs from EJBs.
054: * Applied PicoContainer to services layer (replacing session EJBs).
055: * Applied Hibernate to persistence layer (replacing entity EJBs).
056: *
057: * Revision 1.3 2004/03/21 21:16:39 colinmacleod
058: * Shortened name to ivata op.
059: *
060: * Revision 1.2 2004/02/01 22:07:32 colinmacleod
061: * Added full names to author tags
062: *
063: * Revision 1.1.1.1 2004/01/27 20:59:58 colinmacleod
064: * Moved ivata openportal to SourceForge..
065: *
066: * Revision 1.2 2003/10/15 14:11:33 colin
067: * fixing for XDoclet
068: *
069: * Revision 1.1 2003/02/24 19:09:24 colin
070: * moved to business
071: *
072: * Revision 1.3 2003/02/04 17:39:22 colin
073: * copyright notice
074: *
075: * Revision 1.2 2002/09/09 11:00:38 colin
076: * made session classes serializable :-)
077: *
078: * Revision 1.3 2002/09/09 08:44:52 colin
079: * made new mail session class to abstract the non-serializable JavaMail session
080: *
081: * Revision 1.1 2002/09/09 08:26:37 colin
082: * first version of MailSession logs into session everytime
083: * -----------------------------------------------------------------------------
084: */
085: package com.ivata.groupware.business.mail.session;
086:
087: import org.apache.log4j.Logger;
088:
089: import java.security.NoSuchProviderException;
090: import java.util.Properties;
091:
092: import javax.mail.MessagingException;
093: import javax.mail.Session;
094: import javax.mail.Store;
095: import javax.naming.AuthenticationException;
096:
097: import org.picocontainer.PicoContainer;
098:
099: import com.ivata.groupware.admin.security.server.AbstractSecuritySession;
100: import com.ivata.groupware.admin.security.server.SecurityServerException;
101: import com.ivata.groupware.admin.security.user.UserDO;
102: import com.ivata.mask.util.SystemException;
103:
104: /**
105: * <p><code>javax.mail.Session</code> is not serializable and cannot
106: * be passed from client to server. This class stores the login details
107: * an provides a new session instance when required, by logging in
108: * again.</p>
109: *
110: * @since 2002-09-08
111: * @author Colin MacLeod
112: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
113: * @version $Revision: 1.4 $
114: */
115: public class MailSession extends AbstractSecuritySession {
116: /**
117: * Logger for this class.
118: */
119: private static final Logger logger = Logger
120: .getLogger(MailSession.class);
121:
122: /**
123: * Serialization version (for <code>Serializable</code> interface).
124: */
125: private static final long serialVersionUID = 1L;
126:
127: /**
128: * <p>Contains all of the properties necessary to log into the
129: * <code>javax.mail.Session</code>.</p>
130: */
131: private Properties mailProperties = null;
132:
133: /**
134: * <p>Used to autheticate this mail session, whenever the
135: * <code>javax.mail.Session</code> is recreated.</p>
136: */
137: public MailAuthenticator authenticator;
138:
139: /**
140: * Construct a new mail session.
141: */
142: public MailSession(PicoContainer container, UserDO user)
143: throws SystemException {
144: super (container, user);
145: }
146:
147: /**
148: * <p>Logs into the mail session for the first time. This stores the
149: * user name and password so that the session can be continued again
150: * later.</p>
151: *
152: * @userName name of the user to log in.
153: * @param password clear-text user's password to log into the mail
154: * server.
155: * @param mailProperties all of the mail properties necessary to login
156: * to the system.
157: * @throws AuthenticationException thrown by JavaMail if the user cannot
158: * login.
159: * @return newly created mail session.
160: */
161: public Session login(final String password,
162: final Properties mailProperties)
163: throws SecurityServerException, NoSuchProviderException,
164: MessagingException {
165: if (logger.isDebugEnabled()) {
166: logger.debug("login(String password = " + password
167: + ", Properties mailProperties = " + mailProperties
168: + ") - start");
169: }
170:
171: authenticator = new MailAuthenticator(mailProperties
172: .getProperty("mail.user"), password);
173: this .mailProperties = mailProperties;
174:
175: Session returnSession = getJavaMailSession();
176: if (logger.isDebugEnabled()) {
177: logger
178: .debug("login(String, Properties) - end - return value = "
179: + returnSession);
180: }
181: return returnSession;
182: }
183:
184: /**
185: * <p>Return the current mail session as a
186: * <code>javax.mail.Session</code> instance. This involves logging in
187: * again.</p>
188: *
189: * @throws AuthenticationException thrown by JavaMail if the user cannot login.
190: * @throws NoSuchProviderException thrown by JavaMail if the user cannot login.
191: * @throws MessagingException thrown by JavaMail if the user cannot login.
192: * @return the current mail session as a
193: * <code>javax.mail.Session</code> instance.
194: */
195: public Session getJavaMailSession() throws SecurityServerException,
196: NoSuchProviderException, MessagingException {
197: if (logger.isDebugEnabled()) {
198: logger.debug("getJavaMailSession() - start");
199: }
200:
201: Session javaSession = Session.getInstance(mailProperties,
202: authenticator);
203: Store store = javaSession.getStore("imap");
204: if (!store.isConnected()) {
205: store.connect();
206: }
207: store.close();
208:
209: if (logger.isDebugEnabled()) {
210: logger.debug("getJavaMailSession() - end - return value = "
211: + javaSession);
212: }
213: return javaSession;
214: }
215: }
|