001: /*
002: Copyright (C) 2005 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.hipermail;
034:
035: import java.util.Properties;
036: import java.util.Iterator;
037:
038: import java.security.Security;
039:
040: import javax.mail.AuthenticationFailedException;
041: import javax.mail.NoSuchProviderException;
042: import javax.mail.MessagingException;
043: import javax.mail.internet.ParseException;
044: import javax.mail.SendFailedException;
045: import javax.mail.URLName;
046: import javax.mail.Session;
047: import javax.mail.Address;
048: import javax.mail.Message;
049: import javax.mail.Store;
050: import javax.mail.Transport;
051: import javax.mail.Folder;
052:
053: import com.sun.net.ssl.internal.ssl.Provider;
054:
055: import com.knowgate.debug.DebugFile;
056: import com.knowgate.dataobjs.DB;
057:
058: /**
059: * <p>A wrapper around javax.mail.Store and javax.mail.Transport</p>
060: * @author Sergio Montoro Ten
061: * @version 1.0
062: */
063: public class SessionHandler {
064:
065: private String sInAccountName;
066: private String sInAuthStr;
067: private String sOutAccountName;
068: private String sOutAuthStr;
069: private String sInHostName;
070: private String sOutHostName;
071: private String sMBoxDir;
072: private Properties oProps;
073: private URLName oURLSession;
074: private Session oMailSession;
075: private Session oSmtpSession;
076: private Store oMailStore;
077: private Transport oMailTransport;
078: private boolean bIsStoreConnected;
079: private boolean bIsTransportConnected;
080: private boolean bIncomingSSL;
081: private boolean bOutgoingSSL;
082:
083: // ---------------------------------------------------------------------------
084:
085: /**
086: * Default constructor
087: */
088: public SessionHandler() {
089: bIsStoreConnected = bIsTransportConnected = false;
090: oMailTransport = null;
091: oMailStore = null;
092: oProps = null;
093: sOutAccountName = sOutAuthStr = sInAccountName = sInAuthStr = sInHostName = sOutHostName = sMBoxDir = null;
094: oMailSession = null;
095: bOutgoingSSL = bIncomingSSL = false;
096: }
097:
098: // ---------------------------------------------------------------------------
099:
100: /**
101: * <p>Create instance taking data from a MailAccount</p>
102: * The data of MailAccount stored at k_user_mail table is used for initializing
103: * the connection properties
104: * @param oAccount MailAccount
105: */
106: public SessionHandler(MailAccount oAccount) {
107: String sProtocol;
108: oMailStore = null;
109: oMailTransport = null;
110: bIsStoreConnected = bIsTransportConnected = false;
111: sInAccountName = oAccount.getStringNull(DB.incoming_account,
112: null);
113: sInAuthStr = oAccount.getStringNull(DB.incoming_password, null);
114: sOutAccountName = oAccount.getStringNull(DB.outgoing_account,
115: null);
116: sOutAuthStr = oAccount
117: .getStringNull(DB.outgoing_password, null);
118: sInHostName = oAccount.getStringNull(DB.incoming_server, null);
119: sOutHostName = oAccount.getStringNull(DB.outgoing_server, null);
120: if (!oAccount.isNull(DB.incoming_ssl))
121: bIncomingSSL = (oAccount.getShort(DB.incoming_ssl) == (short) 1);
122: if (!oAccount.isNull(DB.outgoing_ssl))
123: bOutgoingSSL = (oAccount.getShort(DB.outgoing_ssl) == (short) 1);
124:
125: oProps = new Properties();
126:
127: sProtocol = oAccount
128: .getStringNull(DB.incoming_protocol, "pop3");
129: oProps.put("mail.store.protocol", sProtocol);
130: oProps.put("mail." + sProtocol + ".host", oAccount
131: .getStringNull(DB.incoming_server, "localhost"));
132: if (oAccount.isNull(DB.incoming_port))
133: oProps.put("mail." + sProtocol + ".port",
134: (bIncomingSSL ? "995" : "110"));
135: else
136: oProps.put("mail." + sProtocol + ".port", String
137: .valueOf(oAccount.getShort(DB.incoming_port)));
138: if (bIncomingSSL) {
139: oProps.setProperty("mail." + sProtocol
140: + ".socketFactory.class",
141: "javax.net.ssl.SSLSocketFactory");
142: oProps.setProperty("mail." + sProtocol
143: + ".socketFactory.port", oProps.getProperty("mail."
144: + sProtocol + ".port"));
145: oProps.setProperty("mail." + sProtocol
146: + ".socketFactory.fallback", "false");
147: }
148:
149: sProtocol = oAccount
150: .getStringNull(DB.outgoing_protocol, "smtp");
151: oProps.put("mail.transport.protocol", sProtocol);
152: oProps.put("mail." + sProtocol + ".host", oAccount
153: .getStringNull(DB.outgoing_server, "localhost"));
154: if (oAccount.isNull(DB.outgoing_port))
155: oProps.put("mail." + sProtocol + ".port",
156: (bOutgoingSSL ? "465" : "25"));
157: else
158: oProps.put("mail." + sProtocol + ".port", String
159: .valueOf(oAccount.getShort(DB.outgoing_port)));
160: if (bOutgoingSSL) {
161: oProps.setProperty("mail." + sProtocol
162: + ".socketFactory.class",
163: "javax.net.ssl.SSLSocketFactory");
164: oProps.setProperty("mail." + sProtocol
165: + ".socketFactory.port", oProps.getProperty("mail."
166: + sProtocol + ".port"));
167: oProps.setProperty("mail." + sProtocol
168: + ".socketFactory.fallback", "false");
169: }
170:
171: if (!oAccount.isNull(DB.outgoing_account)
172: && !oAccount.isNull(DB.outgoing_password)) {
173: oProps.put("mail." + sProtocol + ".auth", "true");
174: }
175:
176: if (bIncomingSSL || bOutgoingSSL) {
177: Security.addProvider(new Provider());
178: if (bOutgoingSSL)
179: oProps.put("mail."
180: + oAccount.getStringNull(DB.outgoing_protocol,
181: "smtp") + ".starttls.enable", "true");
182: }
183:
184: if (DebugFile.trace) {
185: DebugFile.writeln("new SessionHandler(oAccount="
186: + oAccount.getStringNull(DB.gu_account, "null")
187: + ")");
188: DebugFile.incIdent();
189: Iterator oIter = oProps.keySet().iterator();
190: while (oIter.hasNext()) {
191: String sKey = (String) oIter.next();
192: DebugFile
193: .writeln(sKey + "=" + oProps.getProperty(sKey));
194: } // wend
195: DebugFile.decIdent();
196: }
197: }
198:
199: // ---------------------------------------------------------------------------
200:
201: /**
202: * <p>Create instance taking data from a MailAccount and set local directory for MBOX files</p>
203: * @param oAccount MailAccount
204: * @param sMBoxDirectory String
205: */
206: public SessionHandler(MailAccount oAccount, String sMBoxDirectory) {
207: this (oAccount);
208: sMBoxDir = sMBoxDirectory;
209: }
210:
211: // ---------------------------------------------------------------------------
212:
213: /**
214: * <p>Get column incoming_account of k_user_mail</p>
215: * @return String account name or <b>null</b> if this instance has not been
216: * initialized from a MailAccount object
217: */
218: public String getAccountName() {
219: return sInAccountName;
220: }
221:
222: // ---------------------------------------------------------------------------
223:
224: public void setAccountName(String aAccName) {
225: sInAccountName = aAccName;
226: }
227:
228: // ---------------------------------------------------------------------------
229:
230: /**
231: * <p>Get column incoming_password of k_user_mail</p>
232: * @return String password or <b>null</b> if this instance has not been
233: * initialized from a MailAccount object
234: */
235: public String getAuthStr() {
236: return sInAuthStr;
237: }
238:
239: // ---------------------------------------------------------------------------
240:
241: public void setAuthStr(String aAutStr) {
242: sInAuthStr = aAutStr;
243: }
244:
245: // ---------------------------------------------------------------------------
246:
247: /**
248: * <p>Get column incoming_server of k_user_mail</p>
249: * @return String
250: */
251: public String getHostName() {
252: return sInHostName;
253: }
254:
255: // ---------------------------------------------------------------------------
256:
257: public void setHostName(String sName) {
258: sInHostName = sName;
259: }
260:
261: // ---------------------------------------------------------------------------
262:
263: public String getMBoxDirectory() {
264: return sMBoxDir;
265: }
266:
267: // ---------------------------------------------------------------------------
268:
269: public void setMBoxDirectory(String sDir) {
270: sMBoxDir = sDir;
271: }
272:
273: // ---------------------------------------------------------------------------
274:
275: public Properties getProperties() {
276: return oProps;
277: }
278:
279: // ---------------------------------------------------------------------------
280:
281: public void setProperties(Properties oPropties) {
282: oProps = oPropties;
283: }
284:
285: // ---------------------------------------------------------------------------
286:
287: /**
288: * <p>Get incoming mail server Session</p>
289: * This method calls JavaMail Session.getInstance() method if neccesary,
290: * using properties currently set at this instance and SilentAuthenticator as
291: * Authenticator subclass
292: * @return javax.mail.Session
293: * @throws IllegalStateException
294: * @throws NullPointerException
295: */
296: public Session getSession() throws IllegalStateException {
297: if (DebugFile.trace) {
298: DebugFile.writeln("Begin SessionHandler.getSession()");
299: DebugFile.incIdent();
300: }
301: if (null == oMailSession) {
302: if (null == oProps) {
303: if (DebugFile.trace)
304: DebugFile.decIdent();
305: throw new IllegalStateException(
306: "SessionHandler properties not set");
307: }
308: if (null == sInAccountName) {
309: if (DebugFile.trace)
310: DebugFile.decIdent();
311: throw new NullPointerException(
312: "SessionHandler account name not set");
313: }
314: if (DebugFile.trace)
315: DebugFile.writeln("new SilentAuthenticator("
316: + sInAccountName + ", ...)");
317: SilentAuthenticator oAuth = new SilentAuthenticator(
318: sInAccountName, sInAuthStr);
319: if (DebugFile.trace)
320: DebugFile
321: .writeln("Session.getInstance([Properties],[SilentAuthenticator])");
322: oMailSession = Session.getInstance(oProps, oAuth);
323: }
324: if (DebugFile.trace) {
325: DebugFile.decIdent();
326: DebugFile.writeln("End SessionHandler.getSession() : "
327: + oMailSession);
328: }
329: return oMailSession;
330: } // getSession
331:
332: // ---------------------------------------------------------------------------
333:
334: /**
335: * <p>Get outgoing mail server Session</p>
336: * This method calls JavaMail Session.getInstance() method if neccesary,
337: * using properties currently set at this instance and SilentAuthenticator as
338: * Authenticator subclass
339: * @return javax.mail.Session
340: * @throws IllegalStateException
341: * @throws NullPointerException
342: */
343: public Session getSmtpSession() throws IllegalStateException {
344: if (DebugFile.trace) {
345: DebugFile.writeln("Begin SessionHandler.getSmtpSession()");
346: DebugFile.incIdent();
347: }
348: if (null == oSmtpSession) {
349: if (null == oProps) {
350: if (DebugFile.trace)
351: DebugFile.decIdent();
352: throw new IllegalStateException(
353: "SessionHandler properties not set");
354: }
355: if (null == sOutAccountName) {
356: if (DebugFile.trace)
357: DebugFile.decIdent();
358: throw new NullPointerException(
359: "SessionHandler account name not set");
360: }
361: if (DebugFile.trace)
362: DebugFile.writeln("new SilentAuthenticator("
363: + sOutAccountName + ", ...)");
364: SilentAuthenticator oAuth = new SilentAuthenticator(
365: sOutAccountName, sOutAuthStr);
366: if (DebugFile.trace)
367: DebugFile
368: .writeln("Session.getInstance([Properties],[SilentAuthenticator])");
369: oSmtpSession = Session.getInstance(oProps, oAuth);
370: }
371: if (DebugFile.trace) {
372: DebugFile.decIdent();
373: DebugFile.writeln("End SessionHandler.getSmtpSession() : "
374: + oSmtpSession);
375: }
376: return oSmtpSession;
377: } // getSmtpSession
378:
379: // ---------------------------------------------------------------------------
380:
381: /**
382: * <p>Get Store</p>
383: * This method calls Session.getStore() and Store.connect() if neccesary.
384: * @return javax.mail.Store
385: * @throws NoSuchProviderException
386: * @throws MessagingException
387: */
388: public Store getStore() throws NoSuchProviderException,
389: MessagingException {
390: if (DebugFile.trace) {
391: DebugFile.writeln("Begin SessionHandler.getStore()");
392: DebugFile.incIdent();
393: }
394: if (null == oMailStore) {
395: if (null == sInHostName) {
396: if (DebugFile.trace)
397: DebugFile.decIdent();
398: throw new NullPointerException(
399: "SessionHandler host name not set");
400: }
401: if (DebugFile.trace)
402: DebugFile.writeln("Session.getStore()");
403: oMailStore = getSession().getStore();
404: if (DebugFile.trace)
405: DebugFile.writeln("Store.connect(" + sInHostName + ","
406: + sInAccountName + ", ...)");
407: getStore().connect(sInHostName, sInAccountName, sInAuthStr);
408: bIsStoreConnected = true;
409: }
410: if (DebugFile.trace) {
411: DebugFile.decIdent();
412: DebugFile.writeln("End SessionHandler.getStore() : "
413: + oMailStore);
414: }
415: return oMailStore;
416: } // getStore()
417:
418: // ---------------------------------------------------------------------------
419:
420: /**
421: * <p>Get Transport</p>
422: * This method calls Session.getTransport() and Transport.connect() if neccesary
423: * @return javax.mail.Transport
424: * @throws NoSuchProviderException
425: * @throws MessagingException
426: */
427: public Transport getTransport() throws NoSuchProviderException,
428: MessagingException {
429: if (DebugFile.trace) {
430: DebugFile.writeln("Begin SessionHandler.getTransport()");
431: DebugFile.incIdent();
432: }
433: if (null == oMailTransport) {
434: if (DebugFile.trace)
435: DebugFile.writeln("Session.getTransport()");
436: oMailTransport = getSmtpSession().getTransport();
437: oMailTransport.connect();
438: }
439: if (DebugFile.trace) {
440: DebugFile.decIdent();
441: DebugFile.writeln("End SessionHandler.getTransport() : "
442: + oMailTransport);
443: }
444: return oMailTransport;
445: } // getTransport
446:
447: // ---------------------------------------------------------------------------
448:
449: public Folder getFolder(String sFolderName)
450: throws NoSuchProviderException, MessagingException {
451: getStore();
452: if (null == oMailStore)
453: return null;
454: else {
455: return oMailStore.getFolder(sFolderName);
456: }
457: } // getFolder
458:
459: // ---------------------------------------------------------------------------
460:
461: public URLName getURL() {
462: if (null == oURLSession) {
463: if (DebugFile.trace)
464: DebugFile.writeln("new URLName(jdbc://, " + sInHostName
465: + ", -1, " + sMBoxDir + ", " + sInAccountName
466: + ", ...)");
467: oURLSession = new URLName("jdbc://", sInHostName, -1,
468: sMBoxDir, sInAccountName, sInAuthStr);
469: }
470: return oURLSession;
471: }
472:
473: // ---------------------------------------------------------------------------
474:
475: public boolean isStoreConnected() {
476: return bIsStoreConnected;
477: }
478:
479: // ---------------------------------------------------------------------------
480:
481: public boolean isTransportConnected() {
482: return bIsTransportConnected;
483: }
484:
485: // ---------------------------------------------------------------------------
486:
487: public void sendMessage(Message oMsg)
488: throws NoSuchProviderException, SendFailedException,
489: ParseException, MessagingException, NullPointerException {
490: if (DebugFile.trace) {
491: DebugFile
492: .writeln("Begin SessionHandler.sendMessage([Message])");
493: DebugFile.incIdent();
494: }
495: oMsg.setSentDate(new java.util.Date());
496: if (DebugFile.trace)
497: DebugFile.writeln("Transport.send(Message)");
498: Transport.send(oMsg);
499: if (DebugFile.trace) {
500: DebugFile.decIdent();
501: DebugFile.writeln("End SessionHandler.sendMessage()");
502: }
503: } // sendMessage
504:
505: // ---------------------------------------------------------------------------
506:
507: public void sendMessage(Message oMsg, Address[] aAddrs)
508: throws NoSuchProviderException, SendFailedException,
509: ParseException, MessagingException, NullPointerException {
510: if (DebugFile.trace) {
511: DebugFile
512: .writeln("Begin SessionHandler.sendMessage([Message],Address[])");
513: DebugFile.incIdent();
514: }
515: oMsg.setSentDate(new java.util.Date());
516: if (DebugFile.trace)
517: DebugFile.writeln("Transport.send(Message,Address[])");
518: Transport.send(oMsg, aAddrs);
519: if (DebugFile.trace) {
520: DebugFile.decIdent();
521: DebugFile.writeln("End SessionHandler.sendMessage()");
522: }
523: } // sendMessage
524:
525: // ---------------------------------------------------------------------------
526:
527: public void sendMessage(Message oMsg, Address[] aAdrFrom,
528: Address[] aAdrReply, Address[] aAdrTo, Address[] aAdrCc,
529: Address[] aAdrBcc) throws NoSuchProviderException,
530: SendFailedException, ParseException, MessagingException,
531: NullPointerException {
532: if (DebugFile.trace) {
533: DebugFile
534: .writeln("Begin SessionHandler.sendMessage([Message],Address[],Address[],Address[],Address[],Address[])");
535: DebugFile.incIdent();
536: }
537: oMsg.addFrom(aAdrFrom);
538: if (null == aAdrReply)
539: oMsg.setReplyTo(aAdrReply);
540: else
541: oMsg.setReplyTo(aAdrFrom);
542: if (aAdrTo != null)
543: oMsg.addRecipients(javax.mail.Message.RecipientType.TO,
544: aAdrTo);
545: if (aAdrCc != null)
546: oMsg.addRecipients(javax.mail.Message.RecipientType.CC,
547: aAdrCc);
548: if (aAdrBcc != null)
549: oMsg.addRecipients(javax.mail.Message.RecipientType.BCC,
550: aAdrBcc);
551: oMsg.setSentDate(new java.util.Date());
552: if (DebugFile.trace)
553: DebugFile.writeln("Transport.send(Message)");
554: Transport.send(oMsg);
555: if (DebugFile.trace) {
556: DebugFile.decIdent();
557: DebugFile.writeln("End SessionHandler.sendMessage()");
558: }
559: } // sendMessage
560:
561: // ---------------------------------------------------------------------------
562:
563: public void close() throws MessagingException {
564: if (DebugFile.trace) {
565: DebugFile.writeln("Begin SessionHandler.close()");
566: DebugFile.incIdent();
567: }
568: if (null != oMailStore) {
569: if (isStoreConnected()) {
570: if (DebugFile.trace)
571: DebugFile.writeln("Store.close()");
572: oMailStore.close();
573: }
574: oMailStore = null;
575: }
576: if (null != oMailTransport) {
577: if (isTransportConnected()) {
578: if (DebugFile.trace)
579: DebugFile.writeln("Transport.close()");
580: oMailTransport.close();
581: }
582: oMailTransport = null;
583: }
584: oMailSession = null;
585: oSmtpSession = null;
586: if (DebugFile.trace) {
587: DebugFile.decIdent();
588: DebugFile.writeln("End SessionHandler.close()");
589: }
590: } // close
591:
592: }
|