001: /*
002: * %W% %G% Sun Microsystems
003: *
004: * Copyright 2000 Sun Microsystems, Inc. All rights reserved.
005: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
006: */
007:
008: package com.sun.portal.wireless.taglibs.ab;
009:
010: import java.util.*;
011: import java.util.logging.Logger;
012: import java.util.logging.Level;
013:
014: import javax.servlet.*;
015: import javax.servlet.http.*;
016: import javax.servlet.jsp.*;
017:
018: import com.sun.portal.wireless.taglibs.base.Context;
019: import com.sun.portal.wireless.taglibs.base.Util;
020: import com.sun.portal.wireless.util.AppConfigConstants;
021: import com.sun.portal.log.common.PortalLogger;
022:
023: import javax.mail.Store;
024:
025: import com.iplanet.sso.*;
026:
027: import com.sun.ssoadapter.SSOAdapter;
028: import com.sun.ssoadapter.SSOAdapterSession;
029: import com.sun.ssoadapter.config.Configuration;
030: import com.sun.ssoadapter.config.ConfigurationFactory;
031:
032: import com.sun.addressbook.Entry;
033: import com.sun.addressbook.Element;
034: import com.sun.addressbook.Group;
035:
036: /**
037: * ABContext - track state across Address book requests.
038: *
039: * This class implements a simple context mechanism
040: * that may be used for tracking user state across
041: * multiple requests.
042: *
043: * @author Mihir Sambhus
044: * @version 1.0
045: * @see Context, ContextCache, ABContextCache
046:
047: */
048:
049: public class ABContext extends Context {
050:
051: public static final String DESCENDING = "dsc";
052:
053: public static final String ASCENDING = "asc";
054:
055: protected String sortBy = null;
056: protected String sortOrder = null;
057:
058: protected List entries = null;
059: protected List groupmembers = null;
060: protected List memberchoices = null;
061: protected List contacts = null;
062:
063: protected int numEntries = 0;
064: protected int numGroupmembers = 0;
065: protected int numMemberchoices = 0;
066: protected int numContacts = 0;
067:
068: protected ContactDB contactdb = null;
069:
070: public static final String CONTEXT_CLASS_NAME = "com.sun.portal.wireless.taglibs.ab.ABContext";
071: public static final String CONTEXT_CACHE_CLASS_NAME = "com.sun.portal.wireless.taglibs.ab.ABContextCache";
072: public static final String CONTEXT_TYPE = "abcontext";
073: public static final String SSO_CONFIG_TYPE = "AB-TYPE";
074:
075: private static final boolean SUCCESS = true;
076: private static final boolean FAIL = false;
077: private static final String PIM_CDO = "cdo";
078: private static final String PIM_INTERFACE_TYPE = "ab.pimInterfaceType";
079:
080: protected ConfigurationFactory configFactory = null;
081: protected Configuration appConfig = null;
082:
083: protected String pabLang = null;
084:
085: // Create a logger for this class
086: private static Logger debugLogger = PortalLogger
087: .getLogger(ABContext.class);
088:
089: /**
090: * Initialize the Address Book context
091: *
092: * @param session where to save the context.
093: * @exception Exception
094: */
095: public void init(HttpServletRequest request, SSOToken session,
096: SSOAdapter ssoAdapter) throws Exception {
097:
098: super .init(request, session, "SunMobileAppABService",
099: ssoAdapter);
100:
101: //
102: // Read "per-account" application preferences...
103: //
104: configFactory = ConfigurationFactory.getInstance(
105: AppConfigConstants.AB_APP_SERVICE_NAME,
106: AppConfigConstants.AB_APP_ATTR_CONFIGURATIONS_NAME,
107: AppConfigConstants.AB_APP_ATTR_TEMPLATES_NAME);
108:
109: try {
110: String configName = ssoAdapter.getName();
111: appConfig = configFactory.readConfiguration(configName,
112: true, new SSOAdapterSession(request));
113: if (appConfig == null) {
114: int i = configName.indexOf('_');
115: if (i >= 0) {
116: appConfig = configFactory.readConfiguration(
117: configName.substring(0, i), true,
118: new SSOAdapterSession(request));
119: }
120: if (appConfig == null) {
121: appConfig = new Configuration();
122: }
123: }
124: } catch (Exception e) {
125: appConfig = new Configuration();
126: }
127:
128: //
129: // Load the default properties for Address Book
130: //
131: sortBy = appConfig.getProperty("sortBy", "cn");
132: sortOrder = appConfig.getProperty("sortOrder", ASCENDING);
133:
134: // Identify and get this user's address book and corresponding
135: // contactdb object
136: if (this .contactdb == null) {
137: /**
138: * Adding try - catch block as a fix for 6327956 (Mail Menu throws NPE....)
139: *
140: * If the AddressBook is not available then new ContactDB(...) will throw JspException and if
141: * this JspException is not handled properly it will get thrown to the container resulting in Server Error.
142: *
143: * As a fix, instead of propogating the JspException directly to the container throw the Exception itself
144: * so that it gets handled appropriately in doStartTag method of ContextTag.java
145: */
146: try {
147: contactdb = new ContactDB(ssoAdapter, request, this );
148: } catch (Exception e) {
149: throw new Exception(
150: "com.sun.portal.wireless.taglibs.ab.ABContext::new ContactDB() failed. "
151: + e);
152: }
153: }
154:
155: }
156:
157: /**
158: * Returns a ABContext for this session.
159: *
160: * @param request
161: * @param pageContext
162: */
163: public static ABContext getContext(PageContext pageContext)
164: throws Exception {
165:
166: return (ABContext) Context.getContext(pageContext,
167: CONTEXT_CLASS_NAME, CONTEXT_CACHE_CLASS_NAME,
168: CONTEXT_TYPE);
169: }
170:
171: // access methods
172:
173: /**
174: * Get the contactdb object, which contains methods to access LDAP server
175: *
176: * @return the contactdb object
177: */
178: public ContactDB getContactDB() {
179: return contactdb;
180: }
181:
182: /**
183: * Set the contactdb object, which contains methods to access LDAP serve
184: *
185: * @param contactdb the ContactDB object
186: */
187: public void setContactDB(ContactDB contactdb) {
188: this .contactdb = contactdb;
189: }
190:
191: /**
192: * Get the Collection of Address Book Elements
193: *
194: * @return the elements List
195: */
196: public List getEntries() {
197: return entries;
198: }
199:
200: /**
201: * Set the Collection of Address Book Elements
202: *
203: * @param res the Address Book elements List
204: */
205: public void setEntries(List res) {
206: this .entries = res;
207: }
208:
209: /**
210: * Get the Element bean from the collection located at the index provided
211: *
212: * @param entryid Index of the entry bean to retrieve from collection
213: * @return the appropriate element
214: */
215: public Element getEntry(int entryid) {
216: if (entries == null) {
217: return null;
218: }
219: Element element = (Element) entries.get(entryid);
220: return element;
221: }
222:
223: /**
224: * Returns whether the entries collection is empty or not.
225: *
226: * @return true if the entries collection is empty
227: * false otherwise
228: */
229: public boolean isEntriesEmpty() {
230: if (this .numEntries > 0) {
231: return false;
232: } else {
233: return true;
234: }
235: }
236:
237: /**
238: * Get the number of elements in the entries collection.
239: *
240: * @return numEntries int
241: *
242: */
243: public int getNumEntries() {
244: return numEntries;
245: }
246:
247: /**
248: * Set the number of elements in the entries collection.
249: *
250: * @param numEntries int
251: *
252: */
253: public void setNumEntries(int numEntries) {
254: this .numEntries = numEntries;
255: }
256:
257: /**
258: * Get the Collection of Group members
259: *
260: * @return the groupmembers List
261: */
262: public List getGroupmembers() {
263: return groupmembers;
264: }
265:
266: /**
267: * Set the Collection of Group members
268: *
269: * @param res the groupmembers List
270: */
271: public void setGroupmembers(List res) {
272: this .groupmembers = res;
273: }
274:
275: /**
276: * Get the Element bean from the collection located at the index provided
277: *
278: * @param entryid Index of the entry bean to retrieve from collection
279: * @return the appropriate element
280: */
281: public Element getGroupmember(int entryid) {
282: if (groupmembers == null) {
283: return null;
284: }
285: Element element = (Element) groupmembers.get(entryid);
286: return element;
287: }
288:
289: /**
290: * Returns whether the groupmembers collection is empty or not
291: *
292: * @return true if the groupmembers collection is empty
293: * false otherwise
294: */
295: public boolean isGroupmembersEmpty() {
296: if (this .numGroupmembers > 0) {
297: return false;
298: } else {
299: return true;
300: }
301: }
302:
303: /**
304: * Get the number of elements in the groupmembers collection
305: *
306: * @return numGroupmembers int
307: *
308: */
309: public int getNumGroupmembers() {
310: return numGroupmembers;
311: }
312:
313: /**
314: * Set the number of elements in the groupmembers collection
315: *
316: * @param numGroupmembers int - number of elements in the group members coll.
317: *
318: */
319: public void setNumGroupmembers(int numGroupmembers) {
320: this .numGroupmembers = numGroupmembers;
321: }
322:
323: /**
324: * Get the Collection of Entries to add to a group
325: *
326: * @return the memberchoices List
327: */
328: public List getMemberchoices() {
329: return memberchoices;
330: }
331:
332: /**
333: * Set the Collection of Entries to add to a group
334: *
335: * @param res the memberchoices List
336: */
337: public void setMemberchoices(List res) {
338: this .memberchoices = res;
339: }
340:
341: /**
342: * Get the Element bean from the collection located at the index provided
343: *
344: * @param entryid Index of the entry bean to retrieve from collection
345: * @return the appropriate element
346: */
347: public Element getMemberchoice(int entryid) {
348: if (memberchoices == null) {
349: return null;
350: }
351: Element element = (Element) memberchoices.get(entryid);
352: return element;
353: }
354:
355: /**
356: * Returns whether the memberchoices collection is empty or not
357: *
358: * @return true if the memberchoices collection is empty
359: * false otherwise
360: */
361: public boolean isMemberchoicesEmpty() {
362: if (this .numMemberchoices > 0) {
363: return false;
364: } else {
365: return true;
366: }
367: }
368:
369: /**
370: * Get the number of elements in the memberchoices collection
371: *
372: * @return memberchoices int
373: *
374: */
375: public int getNumMemberchoices() {
376: return numMemberchoices;
377: }
378:
379: /**
380: * Set the number of elements in the memberchoices collection
381: *
382: * @param numMemberchoices int - number of elements in the memberchoices coll.
383: *
384: */
385: public void setNumMemberchoices(int numMemberchoices) {
386: this .numMemberchoices = numMemberchoices;
387: }
388:
389: /**
390: * Get the Collection of all contacts from a group or top level after
391: * recursively expanding all groups
392: *
393: * @return the contacts List
394: */
395: public List getContacts() {
396: return contacts;
397: }
398:
399: /**
400: * Set the Collection of all contacts from a group or top level after
401: * recursively expanding all groups
402: *
403: * @param res the contacts List
404: */
405: public void setContacts(List res) {
406: this .contacts = res;
407: }
408:
409: /**
410: * Get the Element bean from the collection located at the index provided
411: *
412: * @param entryid Index of the entry bean to retrieve from collection
413: * @return the appropriate element
414: */
415: public Element getContact(int entryid) {
416: if (contacts == null) {
417: return null;
418: }
419: Element element = (Element) contacts.get(entryid);
420: return element;
421: }
422:
423: /**
424: * Returns whether the contacts collection is empty or not
425: *
426: * @return true if the contacts collection is empty
427: * false otherwise
428: */
429: public boolean isContactsEmpty() {
430: if (this .numContacts > 0) {
431: return false;
432: } else {
433: return true;
434: }
435: }
436:
437: /**
438: * Get the number of elements in the contacts collection
439: *
440: * @return numContacts int
441: *
442: */
443: public int getNumContacts() {
444: return numContacts;
445: }
446:
447: /**
448: * Set the number of elements in the contacts collection
449: *
450: * @param numContacts int - number of elements in the contacts coll.
451: *
452: */
453: public void setNumContacts(int numContacts) {
454: this .numContacts = numContacts;
455: }
456:
457: /**
458: * Get the User specified locale
459: *
460: * @return pabLang String
461: *
462: */
463: public String getPabLang() {
464: return pabLang;
465: }
466:
467: /**
468: * Get the User specified sort field
469: *
470: * @return sortBy String
471: *
472: */
473: public String getSortBy() {
474: return sortBy;
475: }
476:
477: /**
478: * Get the User specified sort direction
479: *
480: * @return sortOrder String
481: *
482: */
483: public String getSortOrder() {
484: return sortOrder;
485: }
486:
487: /**
488: * Are Groups supported by this Address Book
489: * Groups are supported by Sun ONE, Lotus Notes and Webdav Exchange.
490: * Currently groups are not supported by CDO Exchange. So returning
491: * false if sso adapter is of type CDO Exchange.
492: *
493: * @return isGroupSupported Boolean
494: */
495: public boolean isGroupSupported() {
496: String pimInterfaceType = getContactDB().getABStore()
497: .getSession().getProperty(PIM_INTERFACE_TYPE);
498: debugLogger.log(Level.FINER, "PSMA_CSPWTA0001",
499: pimInterfaceType);
500: if (pimInterfaceType != null
501: && pimInterfaceType.equalsIgnoreCase(PIM_CDO)) {
502: return false;
503: } else {
504: return true;
505: }
506: }
507:
508: /*
509: * Cleanup
510: */
511: public void release() {
512: pabLang = null;
513: contactdb.release();
514: contactdb = null;
515: }
516:
517: /**
518: * Handle SessionChanged events from the Portal Session.
519: * Cleanup the context when the session goes invalid.
520: *
521: * @param se session change event
522: */
523: public void ssoTokenChanged(SSOTokenEvent evt) {
524: try {
525: int evtType = evt.getType();
526:
527: if (evtType != evt.SSO_TOKEN_DESTROY
528: && evtType != evt.SSO_TOKEN_IDLE_TIMEOUT
529: && evtType != evt.SSO_TOKEN_MAX_TIMEOUT) {
530: return;
531: }
532:
533: if (contactdb != null)
534: contactdb.release();
535:
536: } catch (Exception e) {
537: }
538:
539: super.ssoTokenChanged(evt);
540: }
541:
542: }
|