001: /**
002: * $Id: WabpSSOAdapter.java,v 1.13 2005/09/21 11:10:32 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 java.util.List;
016: import java.util.Locale;
017: import java.util.Properties;
018: import java.util.logging.Level;
019: import java.util.logging.Logger;
020: import java.util.logging.LogRecord;
021:
022: import com.sun.ssoadapter.SSOAdapter;
023: import com.sun.ssoadapter.AbstractSSOAdapter;
024: import com.sun.ssoadapter.SSOAdapterException;
025:
026: import com.sun.ssoadapter.SSOAdapterLogger;
027:
028: import com.sun.addressbook.ABStore;
029: import com.sun.addressbook.ABStoreException;
030: import com.sun.addressbook.ABSession;
031: import com.iplanet.sso.SSOToken;
032: import com.iplanet.sso.SSOTokenEvent;
033: import com.iplanet.sso.SSOException;
034: import com.iplanet.am.sdk.AMStoreConnection;
035: import com.iplanet.am.sdk.AMUser;
036: import com.iplanet.am.sdk.AMException;
037:
038: /**
039: * <p>
040: * This class is responsible for retrieving a user's Address Book from
041: * the Address Book Server using the Java Address Book API (JABAPI).
042: * </p>
043: *
044: * <p>
045: * WABP: Web Address Book Protocol
046: * </p>
047: *
048: * <p>
049: * This SSOAdapter implementation consumes the following Configuration
050: * properties:
051: * <ul>
052: * <li> Required Properties </li>
053: * <ul>
054: * <li> <b>uid</b> : username (uid) of address book user.
055: * <li> <b>password</b> : password of address book user.
056: * <li> <b>host</b> : address book server host.
057: * <li> <b>port</b> : address book server port. defaults to "80".
058: * <li> <b>protocol</b> : address book server protocol. defaults to "http".
059: * <li> <b>uwcContext</b> : address book server context uri. defaults
060: * to "uwc".
061: * </ul>
062: * <li> Optional Properties </li>
063: * </ul>
064: *
065: * <p>
066: * URL format:
067: *
068: * <protocol>://<host>:<port>/<uwcContext>/wabp/login.wabp?
069: * username=<uid>@<domain>&password=<password>
070: *
071: * </p>
072: *
073: *
074: *@author Dean Polla
075: * created May 24, 2004
076: *@version 1.0
077: *@see com.sun.ssoadapter.SSOAdapter
078: *@see com.sun.ssoadapter.SSOAdapterFactory
079: */
080:
081: public class WabpSSOAdapter extends AbstractSSOAdapter {
082:
083: // jabapi address book store
084: protected ABStore abStore = null;
085:
086: // jabapi address book session
087: protected ABSession abSession = null;
088:
089: // wabp defaults
090: //
091: protected static String WABP_SERVICE_CLASS = "com.sun.addressbook.wabp.WabpABStore";
092: protected static String WABP_CONTEXT_URI = "wabp";
093: protected static String WABP_LOGIN_CMD = "login.wabp";
094: protected static String WABP_USER_NAME = "username";
095: protected static String WABP_USER_PASSWORD = "password";
096:
097: // sso adapter template defaults
098: //
099: protected static final String CLIENT_DEFAULT_PORT = "80";
100: protected static final String CLIENT_DEFAULT_PROTOCOL = "http";
101:
102: private static Logger logger = SSOAdapterLogger
103: .getLogger("com.sun.portal.ssoadapter.impl");
104:
105: /**
106: * Initialize and validate
107: *
108: *@param adapterName Used to identify the SSOAdapter
109: *@param token Used to identify the user on who's behalf the
110: * request is being processed.
111: *@param adapterProperties Contains the adapter information that will drive
112: * the operation of this instance of an SSOAdapter.
113: */
114: public void init(String adapterName, SSOToken token,
115: Properties adapterProperties, List userPropertiesList,
116: List encodedProperteisList, Locale locale)
117: throws SSOAdapterException {
118:
119: super .init(adapterName, token, adapterProperties,
120: userPropertiesList, encodedProperteisList, locale);
121:
122: if (logger.isLoggable(Level.INFO)) {
123: Properties dp = new Properties();
124: dp.putAll(adapterProperties);
125: dp.remove(PROP_PASSWORD_NAME);
126: dp.remove("proxyAdminPassword");
127:
128: String[] param = new String[5];
129: param[0] = adapterName;
130: param[1] = (String) dp.toString();
131: param[2] = identifier;
132: param[3] = userPropertiesList.toString();
133: param[4] = encodedProperteisList.toString();
134:
135: logger.log(Level.INFO, "PSSA_CSSI0001", param);
136: }
137:
138: if (adapterProperties.getProperty("validate", "false").equals(
139: "true")) {
140: try {
141: validate();
142: } catch (ValidationException ve) {
143: throw new SSOAdapterException(ve.getLocalizedMessage(
144: "ssoadapter", locale), true);
145: }
146: }
147: }
148:
149: public void validate() throws ValidationException {
150: }
151:
152: /**
153: * Adapter specific Connection.
154: *
155: *@return The connection value
156: */
157: public Object getConnection() {
158: Object obj = null;
159:
160: try {
161: obj = getABStore();
162: } catch (Exception e) {
163: if (logger.isLoggable(Level.INFO)) {
164: logger.log(Level.INFO, "PSSA_CSSI0050", new String[] {
165: adapterName, identifier });
166: logger.log(Level.INFO, "PSSA_CSSI0051", e);
167: }
168: return null;
169: }
170:
171: if (obj != null) {
172: if (logger.isLoggable(Level.INFO)) {
173: logger.log(Level.INFO, "PSSA_CSSI0052", new String[] {
174: adapterName, identifier });
175: }
176: }
177:
178: return obj;
179: }
180:
181: /*
182: * This method is responsible for retrieving the <code>ABStore</code>
183: * from the Address Book Server via the <code>WABP_SERVICE_CLASS</code>
184: *
185: * @return ABStore
186: * @throws Exception
187: */
188: public ABStore getABStore() throws Exception {
189:
190: // check to see if the ABStore is available
191: if (isAvailable()) {
192: return abStore;
193: }
194:
195: // check for mandatory properties, if they are null then there's
196: // no point in trying to establish a connection
197: //
198: String host = adapterProperties.getProperty(PROP_HOST_NAME);
199:
200: if (host == null) {
201: if (logger.isLoggable(Level.INFO)) {
202: logger.log(Level.INFO, "PSSA_CSSI0056", new String[] {
203: adapterName, identifier });
204: }
205: return null;
206: }
207:
208: abSession = getABSession();
209: abStore = abSession.getABStore(WABP_SERVICE_CLASS);
210: abStore.connect();
211:
212: return abStore;
213: }
214:
215: /*
216: * This method is responsible for retrieving the <code>ABSession</code>
217: * from the Address Book Server.
218: *
219: * @return ABSession
220: * @throws Exception
221: */
222: public ABSession getABSession() throws Exception {
223:
224: if (abSession != null) {
225: if (logger.isLoggable(Level.INFO)) {
226: logger.log(Level.INFO, "PSSA_CSSI0053", new String[] {
227: adapterName, identifier });
228: }
229: return abSession;
230: }
231:
232: Properties props = new Properties();
233: String host = adapterProperties.getProperty("host");
234: String defport = adapterProperties.getProperty("port",
235: CLIENT_DEFAULT_PORT);
236: String port = adapterProperties.getProperty("clientPort",
237: defport);
238: String protocol = adapterProperties.getProperty("protocol",
239: "http");
240: String contexturi = adapterProperties.getProperty("uwcContext");
241: String domain = adapterProperties.getProperty("domain");
242: String enableProxyAuth = adapterProperties.getProperty(
243: "enableProxyAuth", "false");
244: String uid = null;
245: String password = null;
246:
247: if ((domain != null) && (domain.length() > 0)) {
248: uid = uid + "@" + domain;
249: }
250:
251: if (host != null) {
252: props.put("ab.host", host);
253: }
254: if (port != null) {
255: props.put("ab.port", port);
256: }
257: if (protocol != null) {
258: props.put("ab.protocol", protocol);
259: }
260: if (contexturi != null) {
261: props.put("ab.contextURI", contexturi);
262: }
263:
264: if (enableProxyAuth != null) {
265: props.put("ab.enableProxyAuth", enableProxyAuth);
266:
267: if (enableProxyAuth.equalsIgnoreCase("true")) {
268: if (logger.isLoggable(Level.INFO)) {
269: logger.log(Level.INFO, "PSSA_CSSI0054",
270: new String[] { adapterName, identifier });
271: }
272:
273: SSOToken tok = getSSOToken();
274: AMStoreConnection amsc = null;
275: AMUser auser = null;
276: String usrAttr = null;
277:
278: if (tok != null) {
279: usrAttr = adapterProperties.getProperty(
280: "userAttribute", "uid");
281:
282: try {
283: amsc = new AMStoreConnection(tok);
284: auser = amsc.getUser(tok.getPrincipal()
285: .getName());
286: uid = auser.getStringAttribute(usrAttr);
287: if (uid != null) {
288: props.put("ab.userName", uid);
289: }
290: } catch (SSOException ssoe) {
291: if (logger.isLoggable(Level.SEVERE)) {
292: LogRecord rec = new LogRecord(Level.INFO,
293: "PSSA_CSSI0004");
294: String[] param = { adapterName, usrAttr,
295: identifier };
296: rec.setParameters(param);
297: rec.setThrown(ssoe);
298: rec.setLoggerName(logger.getName());
299: logger.log(rec);
300: }
301: } catch (AMException ame) {
302: if (logger.isLoggable(Level.SEVERE)) {
303: LogRecord rec = new LogRecord(Level.INFO,
304: "PSSA_CSSI0004");
305: String[] param = { adapterName, usrAttr,
306: identifier };
307: rec.setParameters(param);
308: rec.setThrown(ame);
309: rec.setLoggerName(logger.getName());
310: logger.log(rec);
311: }
312: }
313: }
314:
315: String proxyAdminUid = adapterProperties
316: .getProperty("proxyAdminUid");
317: String proxyAdminPassword = adapterProperties
318: .getProperty("proxyAdminPassword");
319:
320: if (proxyAdminUid != null) {
321: props.put("ab.proxyAdminUid", proxyAdminUid);
322: }
323: if (proxyAdminPassword != null) {
324: props.put("ab.proxyAdminPassword",
325: proxyAdminPassword);
326: }
327:
328: } else {
329: uid = adapterProperties.getProperty("uid");
330: if (uid != null) {
331: if ((domain != null) && (domain.length() > 0)) {
332: uid = uid + "@" + domain;
333: }
334: props.put("ab.userName", uid);
335: }
336: password = adapterProperties.getProperty("password");
337: }
338: }
339:
340: if (logger.isLoggable(Level.INFO)) {
341: String[] param = new String[3];
342: param[0] = adapterName;
343: param[1] = (String) props.toString();
344: param[2] = identifier;
345: logger.log(Level.INFO, "PSSA_CSSI0055", param);
346: }
347:
348: if (password != null) {
349: props.put("ab.userPassword", password);
350: }
351:
352: abSession = ABSession.getInstance(props);
353:
354: return abSession;
355: }
356:
357: /**
358: * Tests service availability.
359: *
360: *@return The available value
361: */
362: public boolean isAvailable() {
363: try {
364: if (abStore != null && abStore.isConnected()) {
365: if (logger.isLoggable(Level.INFO)) {
366: logger.log(Level.INFO, "PSSA_CSSI0057",
367: new String[] { adapterName, identifier });
368: }
369: return true;
370: } else {
371: return false;
372: }
373: } catch (ABStoreException abse) {
374: if (logger.isLoggable(Level.SEVERE)) {
375: logger.log(Level.SEVERE, "PSSA_CSSI0058", abse);
376: }
377: return false;
378: }
379: }
380:
381: /**
382: * Adapter specific Connection termination.
383: *
384: *@return true if the connection was terminated successfully.
385: */
386: public boolean closeConnection() {
387: boolean retval = true;
388:
389: try {
390: abStore.disconnect();
391: abStore = null;
392: abSession = null;
393: } catch (Exception e) {
394: retval = false;
395: }
396:
397: if (logger.isLoggable(Level.INFO)) {
398: logger.log(Level.INFO, "PSSA_CSSI0005", new String[] {
399: adapterName, identifier });
400: }
401:
402: return retval;
403: }
404:
405: /**
406: * Implements SSOTokenListener "ssoTokenChanged" method. The following are
407: * possible SSO token event types:
408: * <ul>
409: * <li> SSO_TOKEN_IDLE_TIMEOUT
410: * <li> SSO_TOKEN_MAX_TIMEOUT
411: * <li> SSO_TOKEN_DESTROY
412: * </ul>
413: * The event getType() method is used to ensure that one of the three types
414: * above are the basis for this event. If getType() returns a type not listed
415: * above, then an SSOException is thrown.
416: *
417: *@param evt SSOTokenEvent
418: */
419: public void ssoTokenChanged(SSOTokenEvent evt) {
420:
421: try {
422:
423: if (abStore != null) {
424: abStore.disconnect();
425: }
426:
427: abStore = null;
428: abSession = null;
429: } catch (Exception e) {
430: if (logger.isLoggable(Level.WARNING)) {
431: logger.log(Level.WARNING, "PSSA_CSSI0006", e);
432: }
433: return;
434: }
435:
436: if (logger.isLoggable(Level.INFO)) {
437: logger.log(Level.INFO, "PSSA_CSSI0002", new String[] {
438: adapterName, identifier });
439: }
440:
441: }
442:
443: }
|