001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.taskadmin.context;
006:
007: import java.util.Iterator;
008: import java.util.Map;
009: import java.util.HashMap;
010: import java.util.Set;
011: import java.util.HashSet;
012: import java.util.TreeSet;
013: import java.util.Locale;
014: import java.util.StringTokenizer;
015: import java.util.Collections;
016: import java.util.logging.Logger;
017: import java.util.logging.Level;
018: import java.util.logging.LogRecord;
019:
020: import javax.servlet.http.HttpServletRequest;
021:
022: import com.sun.portal.taskadmin.TaskAdminException;
023: import com.sun.portal.taskadmin.TaskAdminConstants;
024:
025: import com.sun.portal.desktop.context.ContextError;
026: import com.sun.portal.desktop.context.ConfigContext;
027: import com.sun.portal.desktop.context.PropertiesConfigContext;
028: import com.sun.portal.desktop.ROC;
029:
030: import com.iplanet.am.sdk.AMConstants;
031: import com.iplanet.am.sdk.AMOrganization;
032: import com.iplanet.am.sdk.AMOrganizationalUnit;
033: import com.iplanet.am.sdk.AMFilteredRole;
034: import com.iplanet.am.sdk.AMRole;
035: import com.iplanet.am.sdk.AMTemplate;
036: import com.iplanet.am.sdk.AMException;
037:
038: import com.iplanet.sso.SSOException;
039: import com.iplanet.sso.SSOTokenListener;
040: import com.iplanet.sso.SSOToken;
041:
042: import com.sun.portal.desktop.context.DSAMEConstants;
043: import com.sun.portal.desktop.context.DSAMEMultiPortalConstants;
044:
045: import com.sun.portal.util.ResourceLoader;
046:
047: import com.sun.portal.log.common.PortalLogger;
048:
049: import com.sun.portal.taskadmin.TaskAdminConstants;
050:
051: public class ISTaskAdminContext implements TaskAdminContext {
052: protected static final String LOCALE_STRING = "localeString";
053: protected static final String defaultLocaleString = java.util.Locale
054: .getDefault().toString();
055:
056: protected TaskAdminISConnection conn = null;
057: protected boolean ssoTokenListenerAdded = false;
058: private Set roles = null;
059: protected ConfigContext configContext = null;
060: protected Locale locale = null;
061:
062: private static Logger logger = PortalLogger
063: .getLogger(ISTaskAdminContext.class);
064:
065: public void init(HttpServletRequest req) {
066: try {
067: conn = new TaskAdminISConnection(req);
068: initRoleDNs();
069: initLocale(req);
070: } catch (TaskAdminException tae) {
071: throw new ContextError(
072: "ISTaskAdminContext.init(req):couldn't initialize TaskASdminContext",
073: tae);
074: }
075: }
076:
077: public void init(SSOToken ssoToken, String portalId) {
078: try {
079: conn = new TaskAdminISConnection(ssoToken);
080: initRoleDNs();
081: initLocale();
082: } catch (TaskAdminException tae) {
083: throw new ContextError(
084: "ISTaskAdminContext.init(req):couldn't initialize TaskASdminContext",
085: tae);
086: }
087: }
088:
089: private void initRoleDNs() {
090: roles = new TreeSet();
091:
092: try {
093: String orgDN = conn.getUser().getOrganizationDN();
094: if (orgDN != null && orgDN.length() > 0) {
095: AMOrganizationalUnit orgUnit = conn.getConnection()
096: .getOrganizationalUnit(orgDN);
097: if (isTopLevelAdmin()) {
098: roles.add(orgDN);
099: Set roleDNs = orgUnit
100: .getRoles(AMConstants.SCOPE_ONE);
101: for (Iterator i = roleDNs.iterator(); i.hasNext();) {
102: String roleDN = (String) i.next();
103: if (roleDN != null && roleDN.length() > 0) {
104: roles.add(roleDN);
105: }
106: }
107:
108: Set filteredRoleDNs = orgUnit
109: .getFilteredRoles(AMConstants.SCOPE_ONE);
110: for (Iterator i = filteredRoleDNs.iterator(); i
111: .hasNext();) {
112: String froleDN = (String) i.next();
113: if (froleDN != null && froleDN.length() > 0) {
114: roles.add(froleDN);
115: }
116: }
117: Set suborgDNs = orgUnit
118: .getOrganizations(AMConstants.SCOPE_SUB);
119: for (Iterator i = suborgDNs.iterator(); i.hasNext();) {
120: String suborgDN = (String) i.next();
121: if (suborgDN != null && suborgDN.length() > 0) {
122: roles.add(suborgDN);
123: }
124: }
125:
126: } else {
127:
128: AMOrganization org = conn.getConnection()
129: .getOrganization(orgDN);
130: Set roleDNs = org.searchRoles("*",
131: AMConstants.SCOPE_ONE);
132: for (Iterator i = roleDNs.iterator(); i.hasNext();) {
133: String roleDN = (String) i.next();
134: if (roleDN != null && roleDN.length() > 0) {
135: roles.add(roleDN);
136: }
137: }
138:
139: Set filteredRoleDNs = org.searchFilteredRoles("*",
140: AMConstants.SCOPE_ONE);
141: for (Iterator i = filteredRoleDNs.iterator(); i
142: .hasNext();) {
143: String froleDN = (String) i.next();
144: if (froleDN != null && froleDN.length() > 0) {
145: roles.add(froleDN);
146: }
147: }
148: Set suborgDNs = org.searchSubOrganizations("*",
149: AMConstants.SCOPE_SUB);
150: for (Iterator i = suborgDNs.iterator(); i.hasNext();) {
151: String suborgDN = (String) i.next();
152: if (suborgDN != null && suborgDN.length() > 0) {
153: roles.add(suborgDN);
154: }
155: }
156: }
157: }
158:
159: } catch (AMException ame) {
160: //throw new ContextError(
161: //"ISTaskAdminContext.initRoleDNs(): ", ame
162: //);
163: logger
164: .log(
165: Level.WARNING,
166: "ISTaskAdminContext couldn't intialize the roleDNs.",
167: ame);
168: // return an empty set and not throw error to support realm mode
169: } catch (SSOException ssoe) {
170: //throw new ContextError(
171: // "ISTaskAdminContext.initRoleDNs(): ", ssoe
172: // );
173: // return an empty set and not throw error to support realm mode
174: logger
175: .log(
176: Level.WARNING,
177: "ISTaskAdminContext couldn't intialize the roleDNs.",
178: ssoe);
179: }
180: }
181:
182: private boolean isTopLevelAdmin() {
183: try {
184:
185: Set userRoles = conn.getUser().getRoleDNs();
186: for (Iterator i = userRoles.iterator(); i.hasNext();) {
187: String role = (String) i.next();
188: AMRole amRole = conn.getConnection().getRole(role);
189: int roleType = 0;
190: roleType = amRole.getRoleType();
191: if (roleType == AMRole.TOP_LEVEL_ADMIN_ROLE) {
192: return true;
193: }
194: }
195: } catch (AMException ame) {
196: return false;
197: } catch (SSOException ssoe) {
198: throw new ContextError("TaskAdmin.initNodes(): ", ssoe);
199: }
200: return false;
201: }
202:
203: private boolean isLocaleChanged() {
204: String newLocale = getLocaleString();
205: if ((newLocale != null)
206: && (!newLocale.equals(locale.toString()))) {
207: return true;
208: }
209: return false;
210: }
211:
212: public Locale getLocale() {
213: if (locale == null) {
214: throw new ContextError(
215: "ISTaskAdminContext.getLocale(): not initialized");
216: }
217: if (isLocaleChanged()) {
218: locale = initLocale(getLocaleString());
219: }
220:
221: return locale;
222: }
223:
224: protected Locale initLocale() {
225:
226: locale = initLocale(getLocaleString());
227:
228: // use default if not specified
229: if (locale == null) {
230: locale = Locale.getDefault();
231: }
232:
233: return locale;
234: }
235:
236: protected Locale initLocale(HttpServletRequest req) {
237:
238: locale = initLocale(getLocaleString(req));
239:
240: // use default if not specified
241: if (locale == null) {
242: locale = Locale.getDefault();
243: }
244:
245: return locale;
246: }
247:
248: protected static Locale initLocale(String stringformat) {
249: if (stringformat == null) {
250: return null;
251: }
252:
253: StringTokenizer tokenizer = new StringTokenizer(stringformat,
254: "_");
255: String lang = "";
256: String country = "";
257: String variant = "";
258: if (tokenizer.hasMoreTokens()) {
259: lang = tokenizer.nextToken();
260: }
261: if (tokenizer.hasMoreTokens()) {
262: country = tokenizer.nextToken();
263: }
264: if (tokenizer.hasMoreTokens()) {
265: variant = tokenizer.nextToken();
266: }
267:
268: return new Locale(lang, country, variant);
269: }
270:
271: protected String getLocaleString(HttpServletRequest req) {
272: String localeString = null;
273: if (ROC.containsObject(LOCALE_STRING)) {
274: localeString = (String) ROC.getObject(LOCALE_STRING);
275: } else {
276: localeString = conn
277: .getAttribute(DSAMEConstants.ATTR_LOCALE);
278: if (localeString == null) {
279: // Obtain accept-language locale
280: localeString = com.iplanet.am.util.Locale
281: .getLocaleStringFromAcceptLangHeader(req
282: .getHeader("accept-language"));
283: }
284: if (localeString == null) {
285: // Default locale
286: localeString = defaultLocaleString;
287: }
288: }
289: return localeString;
290:
291: }
292:
293: protected String getLocaleString() {
294: String localeString = localeString = conn
295: .getAttribute(DSAMEConstants.ATTR_LOCALE);
296: if (localeString == null) {
297: // Default locale
298: localeString = defaultLocaleString;
299: }
300: return localeString;
301: }
302:
303: public Set getBaseDNs() {
304: return roles;
305: }
306:
307: public Set getAssignableRoles() {
308: Set assignableRoles = new TreeSet();
309: try {
310: String orgDN = conn.getUser().getOrganizationDN();
311: if (orgDN != null && orgDN.length() > 0) {
312: AMOrganizationalUnit orgUnit = conn.getConnection()
313: .getOrganizationalUnit(orgDN);
314: if (isTopLevelAdmin()) {
315: Set roleDNs = orgUnit
316: .getRoles(AMConstants.SCOPE_ONE);
317: for (Iterator i = roleDNs.iterator(); i.hasNext();) {
318: String roleDN = (String) i.next();
319: if (roleDN != null && roleDN.length() > 0) {
320: assignableRoles.add(roleDN);
321: }
322: }
323: } else {
324: AMOrganization org = conn.getConnection()
325: .getOrganization(orgDN);
326: Set roleDNs = org.searchRoles("*",
327: AMConstants.SCOPE_ONE);
328: for (Iterator i = roleDNs.iterator(); i.hasNext();) {
329: String roleDN = (String) i.next();
330: if (roleDN != null && roleDN.length() > 0) {
331: assignableRoles.add(roleDN);
332: }
333: }
334: }
335: }
336:
337: } catch (AMException ame) {
338: // throw new ContextError(
339: // "ISTaskAdminContext.getAssignableRoles(): ", ame
340: //);
341: logger
342: .log(
343: Level.WARNING,
344: "ISTaskAdminContext couldn't get the assignable roleDNs.",
345: ame);
346: } catch (SSOException ssoe) {
347: // throw new ContextError(
348: // "ISTaskAdminContext.getAssignableRoles(): ", ssoe
349: // );
350: logger
351: .log(
352: Level.WARNING,
353: "ISTaskAdminContext couldn't get the assignable roleDNs.",
354: ssoe);
355: }
356: return assignableRoles;
357: }
358:
359: public String DNToName(String dn) {
360: String name = dn
361: .substring(dn.indexOf("=") + 1, dn.indexOf(","));
362: return name;
363: }
364:
365: protected TaskAdminISConnection getISConnection() {
366: return conn;
367: }
368:
369: public void addSSOTokenListener(SSOTokenListener sl) {
370: SSOToken token = conn.getSSOToken();
371:
372: //
373: // only register as an SSO token listener
374: // once per session
375: //
376:
377: try {
378: if (!ssoTokenListenerAdded) {
379: synchronized (this ) {
380: if (!ssoTokenListenerAdded) {
381: token.addSSOTokenListener(sl);
382: ssoTokenListenerAdded = true;
383: }
384: }
385: }
386: } catch (SSOException ssoe) {
387: throw new ContextError(
388: "TaskAdminContext.addSSOTokenListener(): ", ssoe);
389: }
390: }
391:
392: }
|