001: /**
002: * $Id: NotesMailSSOAdapter.java,v 1.13 2005/09/21 11:10:27 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.ssoadapter.impl;
014:
015: import com.sun.ssoadapter.SSOAdapter;
016: import com.sun.ssoadapter.AbstractSSOAdapter;
017: import com.sun.ssoadapter.SSOAdapterException;
018:
019: import com.sun.ssoadapter.SSOAdapterLogger;
020:
021: import com.iplanet.sso.SSOToken;
022: import com.iplanet.sso.SSOTokenEvent;
023: import com.iplanet.sso.SSOException;
024:
025: import javax.mail.*;
026: import javax.mail.internet.*;
027: import javax.servlet.http.*;
028: import java.util.*;
029: import java.net.*;
030: import java.io.*;
031:
032: import java.util.logging.Level;
033: import java.util.logging.Logger;
034:
035: /**
036: * This class implements MailSSOHelper and MailApplicationHelper functionality
037: * specific to the Sun One Portal and Mail services.
038: * <p>Specific features include:
039: * <ul>
040: * <li>Support for username/password style authentication.</li>
041: * <li>Ability to generate web application URLs for the following:
042: * <ul>
043: * <li>Messenger Express</li>
044: * <li>MAP JSP Mail application. The URL generated in this
045: * case will specify a configuration index via the
046: * query string parameter: "mi". </li>
047: * </ul>
048: * </li>
049: * </ul>
050: * <p>At the present time, username/password is stored in the clear.
051: * <p>This MailSSOHelper implementation consumes the following
052: * Configuration properties:
053: * <ul>
054: * <li><b>protocol</b>: Required value. Must be either "imap" or "pop3".
055: * <li><b>uid</b>: Required value. Username (uid) of imap user.
056: * <li><b>password</b>: Required value. Password of imap user.
057: * <li><b>host</b>: Required value. Name of host providing IMAP service.
058: * <li><b>port</b>: Optional value. Port number of IMAP server. Defaults to "143"
059: * when protocol is "imap", and "110" when protocol is "pop3".
060: * <li><b>smtpServer</b>: Optional value. Specifies name of outgoing mail server.
061: * Defaults to value of host property.
062: * <li><b>clientProtocol</b>: Protocol to specify within URLs that activate
063: * web application functionality. Defaults to "http".
064: * <li><b>clientPort</b>: Port to specify within URLs that that activate web
065: * application functionality. Defaults to "80".
066: * <li><b>jspContextPath</b>: The "request context path" to use when forming a URL
067: * that activates MAP JSP application functionality.
068: * Defaults to request.getContextPath().
069: * <li><b>jspLaunch</b>: The document path to use when forming a URL
070: * that activates MAP JSP application functionality.
071: * </ul>
072: *
073: * @author John Saare
074: * @version 1.0
075: * @see com.sun.ssoadapter.SSOAdapter
076: * @see com.sun.ssoadapter.SSOAdapterFactory
077: *
078: */
079:
080: public class NotesMailSSOAdapter extends JavaMailSSOAdapter {
081:
082: //
083: // Mail Store
084: //
085: protected javax.mail.Store mailStore = null;
086:
087: //
088: // Mail Session
089: //
090: protected javax.mail.Session mailSession = null;
091:
092: private static Logger logger = SSOAdapterLogger
093: .getLogger("com.sun.portal.ssoadapter.impl");
094:
095: /**
096: * Initialize and validate NotesMailSSOAdapter
097: *
098: * @param adapterName Used to identify the SSOAdapter
099: * @param token Used to identify the user on who's behalf the request is
100: * being processed.
101: * @param adapterProperties Contains the adapter information that will drive
102: * the operation of this instance of an SSOAdapter.
103: */
104: public void init(String adapterName, SSOToken token,
105: Properties adapterProperties, List userPropertiesList,
106: List encodedProperteisList, Locale locale)
107: throws SSOAdapterException {
108:
109: super .init(adapterName, token, adapterProperties,
110: userPropertiesList, encodedProperteisList, locale);
111:
112: if (logger.isLoggable(Level.INFO)) {
113: Properties dp = new Properties();
114: dp.putAll(adapterProperties);
115: dp.remove(PROP_PASSWORD_NAME);
116:
117: String[] param = new String[5];
118: param[0] = adapterName;
119: param[1] = (String) dp.toString();
120: param[2] = identifier;
121: param[3] = userPropertiesList.toString();
122: param[4] = encodedProperteisList.toString();
123:
124: logger.log(Level.INFO, "PSSA_CSSI0001", param);
125: }
126:
127: if (adapterProperties.getProperty("validate", "false").equals(
128: "true")) {
129: try {
130: validate();
131: } catch (ValidationException ve) {
132: throw new SSOAdapterException(ve.getLocalizedMessage(
133: "ssoadapter", locale), true);
134: }
135: }
136:
137: }
138:
139: /**
140: * Validates configuration.
141: */
142: public void validate() throws ValidationException {
143:
144: if (logger.isLoggable(Level.INFO)) {
145: logger.log(Level.INFO, "PSSA_CSSI0003", new String[] {
146: adapterName, identifier });
147: }
148:
149: String protocolString = adapterProperties
150: .getProperty(PROP_PROTOCOL_NAME);
151: if (protocolString == null
152: || (!protocolString.equals("imap") && !protocolString
153: .equals("pop3"))) {
154: ValidationException ve = new ValidationException();
155: ve.setErrorMessageID("invalidProtocol");
156: throw ve;
157: }
158:
159: String portString = adapterProperties
160: .getProperty(PROP_PORT_NAME);
161: if ((portString != null) && (portString.length() > 0)) {
162: try {
163: int portInt = Integer.parseInt(portString);
164: } catch (Exception e) {
165: ValidationException ve = new ValidationException();
166: ve.setErrorMessageID("invalidPort");
167: throw ve;
168: }
169: }
170:
171: String hostString = adapterProperties
172: .getProperty(PROP_HOST_NAME);
173: if (hostString == null) {
174: ValidationException ve = new ValidationException();
175: ve.setErrorMessageID("missingHost");
176: throw ve;
177: }
178:
179: String uidString = adapterProperties.getProperty(PROP_UID_NAME);
180: if (uidString == null) {
181: ValidationException ve = new ValidationException();
182: ve.setErrorMessageID("missingUid");
183: throw ve;
184: }
185:
186: String passwordString = adapterProperties
187: .getProperty(PROP_PASSWORD_NAME);
188: if (passwordString == null) {
189: ValidationException ve = new ValidationException();
190: ve.setErrorMessageID("missingPassword");
191: throw ve;
192: }
193:
194: String clientPortString = adapterProperties
195: .getProperty("clientPort");
196: if ((clientPortString != null)
197: && (clientPortString.length() > 0)) {
198: try {
199: int portInt = Integer.parseInt(clientPortString);
200: } catch (Exception e) {
201: ValidationException ve = new ValidationException();
202: ve.setErrorMessageID("invalidClientPort");
203: throw ve;
204: }
205: }
206:
207: }
208:
209: /**
210: * Adapter specific Connection.
211: */
212: public Object getConnection() {
213: Object obj = null;
214:
215: try {
216: obj = getStore();
217: } catch (Exception e) {
218: if (logger.isLoggable(Level.INFO)) {
219: logger.log(Level.INFO, "PSSA_CSSI0031", new String[] {
220: adapterName, identifier });
221: logger.log(Level.INFO, "PSSA_CSSI0032", e);
222: }
223: return null;
224: }
225:
226: if (obj != null) {
227: if (logger.isLoggable(Level.INFO)) {
228: logger.log(Level.INFO, "PSSA_CSSI0033", new String[] {
229: adapterName, identifier });
230: }
231: }
232:
233: return obj;
234: }
235:
236: /**
237: * Tests service availability.
238: */
239: public boolean isAvailable() {
240: if (mailStore != null && mailStore.isConnected()) {
241: if (logger.isLoggable(Level.INFO)) {
242: logger.log(Level.INFO, "PSSA_CSSI0034", new String[] {
243: adapterName, identifier });
244: }
245: return true;
246: } else {
247: return false;
248: }
249: }
250:
251: /**
252: * Returns a JavaMail Store object.
253: */
254: public javax.mail.Store getStore() throws Exception {
255: if (isAvailable()) {
256: return mailStore;
257: }
258:
259: Session mailSession = getSession();
260:
261: if (mailSession == null) {
262: return null;
263: }
264:
265: String protocolString = adapterProperties
266: .getProperty(PROP_PROTOCOL_NAME);
267: mailStore = mailSession.getStore(protocolString);
268:
269: String portString = adapterProperties.getProperty(
270: PROP_PORT_NAME, protocolString.equals("pop3") ? "110"
271: : "143");
272:
273: if (logger.isLoggable(Level.INFO)) {
274: logger.log(Level.INFO, "PSSA_CSSI0039", new String[] {
275: adapterName, portString, identifier });
276: }
277:
278: mailStore.connect(
279: adapterProperties.getProperty(PROP_HOST_NAME), Integer
280: .parseInt(portString), adapterProperties
281: .getProperty(PROP_UID_NAME), adapterProperties
282: .getProperty(PROP_PASSWORD_NAME));
283:
284: return mailStore;
285: }
286:
287: /**
288: * Returns a JavaMail Session object.
289: */
290: public javax.mail.Session getSession() throws Exception {
291: if (mailSession != null) {
292: if (logger.isLoggable(Level.INFO)) {
293: logger.log(Level.INFO, "PSSA_CSSI0035", new String[] {
294: adapterName, identifier });
295: }
296: return mailSession;
297: }
298:
299: // check for mandatory properties, if they are null then there's
300: // no point in trying to establish a connection
301: //
302: String host = adapterProperties.getProperty(PROP_HOST_NAME);
303:
304: if (host == null) {
305: if (logger.isLoggable(Level.INFO)) {
306: logger.log(Level.INFO, "PSSA_CSSI0036", new String[] {
307: adapterName, identifier });
308: }
309: return null;
310: }
311:
312: String smtpPort = adapterProperties.getProperty("smtpPort");
313: Properties props = new Properties();
314:
315: props.put("mail.store.pkgs", "com.sun.mail");
316: props.put("mail.transport.protocol", "smtp");
317: props.put("mail.imap.fetchsize", "16384");
318: props.put("mail.smtp.host", adapterProperties.getProperty(
319: "smtpServer", host));
320:
321: if (smtpPort != null) {
322: props.put("mail.smtp.port", smtpPort);
323: }
324:
325: if (logger.isLoggable(Level.INFO)) {
326: String[] param = new String[3];
327: param[0] = adapterName;
328: param[1] = (String) props.toString();
329: param[2] = identifier;
330: logger.log(Level.INFO, "PSSA_CSSI0038", param);
331: }
332:
333: mailSession = javax.mail.Session.getInstance(props, null);
334:
335: return mailSession;
336: }
337:
338: /**
339: * Adapter specific Connection termination.
340: *
341: * @return true if the connection was terminated successfully.
342: */
343: public boolean closeConnection() {
344: boolean retval = true;
345:
346: try {
347: mailStore.close();
348: mailStore = null;
349: mailSession = null;
350: } catch (Exception e) {
351: retval = false;
352: }
353:
354: if (logger.isLoggable(Level.INFO)) {
355: logger.log(Level.INFO, "PSSA_CSSI0005", new String[] {
356: adapterName, identifier });
357: }
358:
359: return retval;
360: }
361:
362: /**
363: * Implements SSOTokenListener "ssoTokenChanged" method.
364: *
365: * The following are possible SSO token event types:
366: * <ul>
367: * <li>SSO_TOKEN_IDLE_TIMEOUT
368: * <li>SSO_TOKEN_MAX_TIMEOUT
369: * <li>SSO_TOKEN_DESTROY
370: * </ul>
371: *
372: * The event getType() method is used to ensure that one of the
373: * three types above are the basis for this event. If getType()
374: * returns a type not listed above, then an SSOException is thrown.
375: *
376: * @param evt SSOTokenEvent
377: */
378: public void ssoTokenChanged(SSOTokenEvent evt) {
379:
380: try {
381: int evtType = evt.getType();
382:
383: if (mailStore != null) {
384: mailStore.close();
385: }
386:
387: mailStore = null;
388: mailSession = null;
389: } catch (Exception e) {
390: if (logger.isLoggable(Level.WARNING)) {
391: logger.log(Level.WARNING, "PSSA_CSSI0006", e);
392: }
393: return;
394: }
395:
396: if (logger.isLoggable(Level.INFO)) {
397: logger.log(Level.INFO, "PSSA_CSSI0002", new String[] {
398: adapterName, identifier });
399: }
400: }
401:
402: }
|