001: /**
002: * Copyright 2002 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: *
013: * Author: Anurag Gupta
014: */package com.sun.portal.netfile.admin.model;
015:
016: // JDK classes
017: import java.text.Collator;
018: import com.sun.portal.log.common.PortalLogger;
019: import java.util.Map;
020: import java.util.HashMap;
021: import java.util.Set;
022: import java.util.LinkedList;
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import java.util.Collection;
026: import java.util.Collections;
027: import java.util.StringTokenizer;
028: import java.util.List;
029: import java.util.ArrayList;
030: import java.security.AccessController;
031:
032: import com.sun.identity.security.DecryptAction;
033:
034: // Servlet classes
035: import javax.servlet.http.HttpServletRequest;
036:
037: // iDS/AMR classes
038: import com.iplanet.sso.SSOTokenManager;
039: import com.iplanet.sso.SSOException;
040: import com.iplanet.sso.SSOToken;
041:
042: //import com.iplanet.am.sdk.AMSchema;
043: //import com.iplanet.am.sdk.AMAttributeSchema;
044: import com.sun.identity.sm.ServiceSchema;
045: import com.sun.identity.sm.ServiceSchemaManager;
046: import com.sun.identity.sm.SchemaType;
047: import com.sun.identity.sm.AttributeSchema;
048:
049: import com.iplanet.am.sdk.AMStoreConnection;
050: import com.iplanet.am.sdk.AMUser;
051: import com.iplanet.am.sdk.AMException;
052: import com.iplanet.am.console.base.model.AMResBundleCacher;
053: import com.iplanet.am.console.user.model.UMUserProfileModelImpl;
054: import com.iplanet.am.console.base.model.AMDisplayTypeConverter;
055: import com.iplanet.am.console.base.model.AMAttrSchemaComparator;
056: import com.iplanet.am.console.base.model.AMConsoleException;
057:
058: // NetFile admin console classes
059: import com.sun.portal.netfile.admin.NetFileAdminModelManager;
060:
061: public class NetFileUserProfileModelImpl extends UMUserProfileModelImpl {
062:
063: private HttpServletRequest req = null;
064: private SSOToken ssoToken = null;
065: private ServiceSchemaManager schemaMgr = null;
066: private ServiceSchema schema = null;
067: private Set dynAttrNames = null;
068: private List attrNames = null;
069: private Map netFileUserAttrs = null;
070: private int currentRow = -1;
071: public static final String ANY_OPTION_SEPARATOR = "|";
072:
073: public NetFileUserProfileModelImpl(HttpServletRequest req, Map map) {
074: super (req, map);
075: //super.initModel(true);
076: resBundle = AMResBundleCacher.getBundle("srapNetFileAdminMsgs",
077: getUserLocale());
078: this .req = req;
079: }
080:
081: public void initModel(String userDN) {
082:
083: //super.initModel(true);
084: this .userDN = userDN;
085: if (userDN == null || userDN.trim().length() == 0) {
086: return;
087: }
088: try {
089: SSOTokenManager ssoTokenManager = SSOTokenManager
090: .getInstance();
091: ssoToken = ssoTokenManager.createSSOToken(req);
092: AMStoreConnection amConn = new AMStoreConnection(ssoToken);
093: curUser = amConn.getUser(userDN);
094: schemaMgr = new ServiceSchemaManager(ssoToken,
095: "srapNetFileService", "1.0");
096: schema = schemaMgr.getSchema(SchemaType.USER);
097: attrNames = getUserAttrNames();
098: // Get the dynamic attribute names
099: ServiceSchema dynSchema = schemaMgr
100: .getSchema(SchemaType.DYNAMIC);
101: dynAttrNames = dynSchema.getAttributeSchemaNames();
102:
103: int size = (attrNames == null) ? 0 : attrNames.size();
104: if (size > 0) {
105: netFileUserAttrs = new HashMap();
106: Iterator iter = attrNames.iterator();
107: while (iter.hasNext()) {
108: String attrName = (String) iter.next();
109: Set attrValue = curUser.getAttribute(attrName);
110: netFileUserAttrs.put(attrName, attrValue);
111: }
112: setAttributeValues(curUser.getAttributes());
113: }
114: } catch (Exception ex) {
115: NetFileAdminModelManager
116: .debugMessage("NetFileUserProfileModelImpl - Unable to initialize model : "
117: + ex);
118: }
119: }
120:
121: public int getSize() {
122: return attrNames == null ? 0 : attrNames.size();
123: }
124:
125: public boolean setCurrentRow(int currRow) {
126: if (netFileUserAttrs == null) {
127: return false;
128: }
129: currentRow = currRow;
130: return currRow >= netFileUserAttrs.size() ? false : true;
131: }
132:
133: public String getAttrName() {
134: return attrNames == null ? null : (String) attrNames
135: .get(currentRow);
136: }
137:
138: public String getAttrLabel(String attrName) {
139: if (schema == null || attrName == null
140: || attrName.trim().length() == 0)
141: return attrName;
142: AttributeSchema attrSchema = schema
143: .getAttributeSchema(attrName);
144: String i18nKey = attrSchema.getI18NKey();
145: String l10name = i18nKey;
146: if (schemaMgr != null) {
147: l10name = getL10NAttributeName(schemaMgr, i18nKey);
148: }
149:
150: return l10name;
151: }
152:
153: public String getAttrLabel() {
154: return getAttrLabel(getAttrName());
155: }
156:
157: public int getAttrType() {
158: int type = AMDisplayTypeConverter.DEFAULT_TYPE;
159: if (schema == null)
160: return type;
161: String attrLabel = getAttrName();
162: AttributeSchema attrSchema = schema
163: .getAttributeSchema(attrLabel);
164: type = AMDisplayTypeConverter.getDisplayType(attrSchema);
165: return type;
166: }
167:
168: public int getAttrSyntax() {
169: int syntax = AMDisplayTypeConverter.DEFAULT_SYNTAX;
170: if (schema == null)
171: return syntax;
172: String attrLabel = getAttrName();
173: AttributeSchema attrSchema = schema
174: .getAttributeSchema(attrLabel);
175: syntax = AMDisplayTypeConverter.getDisplaySyntax(attrSchema);
176: return syntax;
177: }
178:
179: /*
180: * Return true if the current attribute is read-only by the current user
181: */
182: public boolean isReadOnly(String attrName) {
183: AttributeSchema attrSchema = schema
184: .getAttributeSchema(attrName);
185: String any = attrSchema.getAny();
186: Set displayOptions = getDisplayOptions(any);
187: if (displayOptions.isEmpty()
188: || (displayOptions.contains(DISPLAY_ATTRIBUTE))) {
189: return false;
190: } else if (isAdministrator()
191: && displayOptions
192: .contains(ADMINISTRATOR_DISPLAY_ATTRIBUTE)) {
193: return false;
194: }
195: return true;
196: }
197:
198: public boolean isReadOnly() {
199: return isReadOnly(getAttrName());
200: }
201:
202: /*
203: * Returns the attribute value by fetching from Schema
204: */
205: public Set getAttributeValue(String attrName) {
206: if (curUser == null)
207: return null;
208: Set attrValue = null;
209: try {
210: attrValue = curUser.getAttribute(attrName);
211: } catch (Exception ex) {
212: NetFileAdminModelManager
213: .debugError("NetFileUserProfileModelImpl: Unable to get value of "
214: + attrName + ex);
215: }
216: return attrValue;
217: }
218:
219: /*
220: * Returns the value of the Attribute if it is displayable, null otherwise
221: */
222: public Set getAttrValues(String attrName) {
223: return netFileUserAttrs == null ? null : (Set) netFileUserAttrs
224: .get(attrName);
225: }
226:
227: /*
228: * Returns the value of the current attribute in the row.
229: * @ Returns null if the attribute is not displayable.
230: */
231: public Set getAttrValues() {
232: return getAttrValues(getAttrName());
233: }
234:
235: /*
236: * Returns the string value of the attribute by reading from the schema,
237: */
238: public String getAttrStringValue(String attrName,
239: String defaultValue) {
240: Set set = getAttributeValue(attrName);
241: if (null == set) {
242: return defaultValue;
243: }
244: Iterator it = set.iterator();
245: return (it.hasNext()) ? (String) it.next() : defaultValue;
246: }
247:
248: public String getAttrTrueValue() {
249: String trueValue = "true";
250: if (schema == null)
251: return trueValue;
252: String attrLabel = getAttrName();
253: AttributeSchema attrSchema = schema
254: .getAttributeSchema(attrLabel);
255: trueValue = attrSchema.getTrueValue();
256: return trueValue;
257: //return getTrueValue("srapNetFileService", 0, getAttrName());
258: }
259:
260: public String getAttrFalseValue() {
261: String falseValue = "false";
262: if (schema == null)
263: return falseValue;
264: String attrLabel = getAttrName();
265: AttributeSchema attrSchema = schema
266: .getAttributeSchema(attrLabel);
267: falseValue = attrSchema.getFalseValue();
268: return falseValue;
269: }
270:
271: public String[] getAttrChoices() {
272: if (schema == null)
273: return null;
274: String attrLabel = getAttrName();
275: AttributeSchema attrSchema = schema
276: .getAttributeSchema(attrLabel);
277: String[] values = attrSchema.getChoiceValues();
278: return values;
279: }
280:
281: public int getNetFileAttributeIndex(String attr) {
282: return attrNames == null ? -1 : attrNames.indexOf(attr);
283: }
284:
285: public int getNetFileHostsIndex() {
286: return attrNames == null ? -1 : attrNames
287: .indexOf("sunPortalNetFileCommonHostData");
288: }
289:
290: public int getNetFileHostsCount() {
291: Set s = getNetFileHosts();
292: return s == null ? 0 : s.size();
293: }
294:
295: public Set getNetFileHosts() {
296: if (netFileUserAttrs == null) {
297: return new HashSet();
298: }
299: Set netFileHosts = new HashSet();
300: Set s = (Set) netFileUserAttrs
301: .get("sunPortalNetFileCommonHostData");
302: if (s != null) {
303: ArrayList encryptedNetFileHosts = new ArrayList(s);
304: if (!encryptedNetFileHosts.isEmpty()) {
305: Iterator hosts = encryptedNetFileHosts.iterator();
306: while (hosts.hasNext()) {
307: netFileHosts
308: .add(getDecryptedHostInfo((String) hosts
309: .next()));
310: }
311: }
312: }
313:
314: return netFileHosts;
315: }
316:
317: public String getServiceDescription() {
318: return getLocalizedSvcName("srapNetFileService");
319: }
320:
321: public boolean store(Map newMap) {
322: if (curUser == null)
323: return false;
324: HashMap attrMap = new HashMap(1);
325: Set attrNames = newMap.keySet();
326: Iterator iter = attrNames.iterator();
327: String attrName = null;
328: while (iter.hasNext()) {
329: try {
330: attrName = (String) iter.next();
331: Set newValue = (Set) newMap.get(attrName);
332: attrMap.clear();
333: attrMap.put(attrName, newValue);
334: curUser.setAttributes(attrMap);
335: curUser.store();
336: } catch (Exception ex) {
337: NetFileAdminModelManager.debugError(ex + " - "
338: + attrName);
339: continue;
340: }
341: }
342: return true;
343: }
344:
345: /**
346: * Stroes the attribute map for the current user as user attrs.
347: *
348: * @param attrMap The map of name-(Set)values for attrs.
349: */
350: public void storeAttributes(Map userOnlyAttrs, Map customizedAttrs,
351: Set inheritedAttrs) throws AMConsoleException {
352: List errorList = null;
353: /**
354: * Write the attributes without a choice menu. These values will
355: * overwrite any existing value, if and only if the value is different.
356: */
357: Map attrs = new HashMap(userOnlyAttrs.size());
358: Set namesSet = userOnlyAttrs.keySet();
359: Iterator iter = namesSet.iterator();
360: while (iter.hasNext()) {
361: String attrName = (String) iter.next();
362: ;
363: //if (!isAttrReadOnly("srapNetFileService", SCHEMA_TYPE_USER, attrName)) {
364: //if (!isAttrReadOnly(SCHEMA_TYPE_USER, attrName)) {
365: Set attrValues = (Set) userOnlyAttrs.get(attrName);
366: if (attrValues != null) {
367: attrs.put(attrName, attrValues);
368: }
369: //}
370: }
371: try {
372: if (!attrs.isEmpty()) {
373: writeProfile(curUser, attrs, false);
374: }
375: } catch (AMConsoleException ace) {
376: errorList = ace.getErrors();
377: } catch (SSOException ssoe) {
378: NetFileAdminModelManager
379: .debugError("NetFileUserProfileModelImpl: Invalid SSOToken -> "
380: + ssoe);
381: return;
382: }
383:
384: /**
385: * Write the attributes with a choice menu that are customized.
386: * These values will overwrite any existing value even of it is same.
387: */
388: attrs = new HashMap(customizedAttrs.size());
389: namesSet = customizedAttrs.keySet();
390: iter = namesSet.iterator();
391: while (iter.hasNext()) {
392: String attrName = (String) iter.next();
393: ;
394: //if (!isAttrReadOnly("srapNetFileService", SCHEMA_TYPE_USER, attrName)) {
395: //if (!isAttrReadOnly(SCHEMA_TYPE_USER, attrName)) {
396: Set attrValues = (Set) customizedAttrs.get(attrName);
397: if (attrValues != null) {
398: attrs.put(attrName, attrValues);
399: }
400: //}
401: }
402: try {
403: if (!attrs.isEmpty()) {
404: writeProfile(curUser, attrs, true);
405: }
406: } catch (AMConsoleException ace) {
407: errorList = ace.getErrors();
408: } catch (SSOException ssoe) {
409: NetFileAdminModelManager
410: .debugError("NetFileUserProfileModelImpl: Invalid SSOToken -> "
411: + ssoe);
412: return;
413: }
414:
415: /**
416: * Remove the inherited attributes from the user entry. An exception
417: * will be thrown if the attribute doesn't exist in the user entry.
418: */
419: if (!inheritedAttrs.isEmpty()) {
420: if (errorList == null) {
421: errorList = new ArrayList(inheritedAttrs.size());
422: }
423:
424: Set attr = new HashSet(1);
425: iter = inheritedAttrs.iterator();
426: while (iter.hasNext()) {
427: String name = (String) iter.next();
428: attr.add(name);
429: try {
430: curUser.removeAttributes(attr);
431: logger.doLog("attribute.remove", name);
432: } catch (AMException ame) {
433: if (debug.warningEnabled()) {
434: debug.warning("error removing attribute "
435: + name, ame);
436: }
437: errorList.add(name + "-" + ame.getMessage());
438: } catch (SSOException ssoe) {
439: NetFileAdminModelManager
440: .debugError("NetFileUserProfileModelImpl: Invalid SSOToken -> "
441: + ssoe);
442: return;
443: } finally {
444: attr.clear();
445: }
446: }
447: }
448:
449: if (errorList != null && !errorList.isEmpty()) {
450: throw new AMConsoleException(errorList);
451: }
452: }
453:
454: /*
455: * Returns the List of attribute names (String) which should be displayed in the Admin console
456: *
457: */
458:
459: private List getUserAttrNames() {
460: List attrNames = getAttrSchema(SchemaType.USER);
461: List displayableAttrNames = getDisplayableAttrNames(attrNames);
462: return displayableAttrNames;
463: }
464:
465: /*
466: * Return the list of attribute schemas (AMAttributeSchema)
467: */
468:
469: private List getAttrSchema(SchemaType schemaType) {
470: List sortedAttrNames = null;
471: Set attrNames = schema.getAttributeSchemas();
472: sortedAttrNames = sortAttrSchema(attrNames);
473: return sortedAttrNames;
474: }
475:
476: /*
477: * Return the list of attribute names which should be displayed in the admin console
478: */
479:
480: public List getDisplayableAttrNames(List attrNames) {
481: List newAttrNames = new LinkedList();
482: Iterator iter = attrNames.iterator();
483: while (iter.hasNext()) {
484: AttributeSchema attrSchema = (AttributeSchema) iter.next();
485: String attrName = attrSchema.getName();
486: String i18nKey = attrSchema.getI18NKey();
487: if (i18nKey == null || i18nKey.trim().length() == 0) {
488: continue;
489: }
490: String any = attrSchema.getAny();
491: Set displayOptions = getDisplayOptions(any);
492: if (displayOptions.isEmpty()
493: || (displayOptions.contains(DISPLAY_ATTRIBUTE)
494: || displayOptions
495: .contains(READONLY_ATTRIBUTE) || displayOptions
496: .contains(USER_READ_ONLY_ATTRIBUTE))
497: || (isAdministrator() && displayOptions
498: .contains(ADMINISTRATOR_DISPLAY_ATTRIBUTE))) {
499: newAttrNames.add(attrName);
500: }
501: }
502: return newAttrNames;
503: }
504:
505: /*
506: * Parse the "any" string, Get the display options
507: */
508:
509: private Set getDisplayOptions(String anyOptions) {
510: if (anyOptions == null || anyOptions.length() == 0) {
511: return Collections.EMPTY_SET;
512: }
513: StringTokenizer optionTokenizer = new StringTokenizer(
514: anyOptions, ANY_OPTION_SEPARATOR);
515: Set options = new HashSet(optionTokenizer.countTokens());
516: while (optionTokenizer.hasMoreTokens()) {
517: options.add((String) optionTokenizer.nextToken());
518: }
519: return options;
520: }
521:
522: /*
523: * Sort the attribute names in the order it has to be displayed in the admin console
524: */
525:
526: private List sortAttrSchema(Set unordered) {
527: Collator collator = Collator.getInstance(getUserLocale());
528: AMAttrSchemaComparator c = new AMAttrSchemaComparator(collator);
529: List ordered = new ArrayList(unordered);
530: Collections.sort(ordered, c);
531: return ordered;
532: }
533:
534: public String getUserID() {
535: if (userDN != null && userDN.trim().length() != 0) {
536: try {
537: return userDN.substring(userDN.indexOf('=') + 1, userDN
538: .indexOf(','));
539: } catch (Exception ex) {
540: NetFileAdminModelManager
541: .debugError("NetFileUserProfileModelImpl: Unable to get userID -> "
542: + ex);
543: }
544: }
545: return null;
546: }
547:
548: public String getAttributeStatus(String attrName) {
549: if (dynAttrNames.contains(attrName)) {
550: return getSkipValue();
551: }
552: return null;
553: }
554:
555: String getDecryptedHostInfo(String szEncryptedHostInfo) {
556: return (String) AccessController
557: .doPrivileged(new DecryptAction(szEncryptedHostInfo));
558: }
559:
560: }
|