001: package com.sun.portal.netlet.util;
002:
003: import java.util.ArrayList;
004: import java.util.Collections;
005: import java.util.HashMap;
006: import java.util.HashSet;
007: import java.util.Iterator;
008: import java.util.List;
009: import java.util.Map;
010: import java.util.Set;
011:
012: import com.iplanet.am.sdk.AMException;
013: import com.iplanet.am.sdk.AMStoreConnection;
014: import com.iplanet.am.sdk.AMUser;
015: import com.iplanet.sso.SSOException;
016: import com.iplanet.sso.SSOToken;
017: import com.sun.identity.policy.PolicyEvaluator;
018: import com.sun.identity.policy.PolicyException;
019:
020: /**
021: * To be used only by Netlet.
022: *
023: * @modified bs126381
024: */
025: public class UserAttributes implements NetletConstants {
026:
027: private SSOToken ssoToken = null;
028:
029: private AMUser user = null;
030:
031: private Map userAttributes;
032:
033: private boolean applyDefault;
034:
035: public UserAttributes(SSOToken token) {
036: try {
037: ssoToken = token;
038: AMStoreConnection connection = new AMStoreConnection(
039: ssoToken);
040: user = connection.getUser(token.getPrincipal().getName());
041: userAttributes = user
042: .getServiceAttributes(NetletConstants.NETLET_SERVICE_NAME);
043: applyDefault = false;
044: } catch (SSOException ssoe) {
045: applyDefault = true;
046: } catch (AMException dpe) {
047: applyDefault = true;
048: }
049: }
050:
051: public boolean isPolicyAssigned() {
052: return true;
053: /*boolean policyAssigned = false;
054: try {
055: PolicyEvaluator policyEval = new PolicyEvaluator(
056: NetletConstants.NETLET_SERVICE_NAME);
057: policyAssigned = policyEval
058: .isAllowed(ssoToken, "", NetletConstants.NETLET_POLICY,
059: Collections.EMPTY_MAP);
060: } catch (PolicyException pe) {
061: policyAssigned = false;
062: } catch (SSOException ssoe) {
063: policyAssigned = false;
064: }
065: return policyAssigned;*/
066: }
067:
068: public boolean isServiceAssigned() {
069: boolean serviceAssigned = false;
070: try {
071: Set vals = null;
072: vals = user.getAssignedServices();
073: if ((vals == null) || (vals.isEmpty())) {
074: serviceAssigned = false;
075: }
076: Iterator iter = vals.iterator();
077: while (iter.hasNext()) {
078: String serviceName = (String) iter.next();
079: if (NetletConstants.NETLET_SERVICE_NAME
080: .equalsIgnoreCase(serviceName)) {
081: serviceAssigned = true;
082: break;
083: }
084: }
085: } catch (Exception ex) {
086: serviceAssigned = false;
087: }
088: return serviceAssigned;
089: }
090:
091: public boolean isAllowed() {
092: return (isServiceAssigned() && isPolicyAssigned());
093: }
094:
095: public String getString(String name, String defaultValue) {
096: return applyDefault ? defaultValue : AttributeExtractor
097: .getString(userAttributes, name, defaultValue);
098: }
099:
100: public String getString(String name) {
101: return getString(name, "");
102: }
103:
104: public void setString(String name, String value) {
105: HashSet hs = new HashSet(1);
106: hs.add((Object) value);
107: Map changedMap = new HashMap();
108: changedMap.put(name, hs);
109: try {
110: user.setAttributes(changedMap);
111: user.store();
112: } catch (SSOException ssoe) {
113: } catch (AMException ame) {
114: }
115: }
116:
117: public int getInt(String name, int defaultValue) {
118: return applyDefault ? defaultValue : AttributeExtractor.getInt(
119: userAttributes, name, defaultValue);
120: }
121:
122: public int getInt(String name) {
123: return getInt(name, -1);
124: }
125:
126: public void setInt(String name, int value) {
127: String val = "" + value;
128: HashSet hs = new HashSet(1);
129: hs.add((Object) val);
130: Map changedMap = new HashMap();
131: changedMap.put(name, hs);
132: try {
133: user.setAttributes(changedMap);
134: user.store();
135: } catch (SSOException ssoe) {
136: } catch (AMException ame) {
137: }
138: }
139:
140: public boolean getBoolean(String name, boolean defaultValue) {
141: return applyDefault ? defaultValue : AttributeExtractor
142: .getBoolean(userAttributes, name, defaultValue);
143: }
144:
145: public boolean getBoolean(String name) {
146: return getBoolean(name, false);
147: }
148:
149: public void setBoolean(String name, boolean value) {
150: String val = "" + value;
151: HashSet hs = new HashSet(1);
152: hs.add((Object) val);
153: Map changedMap = new HashMap();
154: changedMap.put(name, hs);
155: try {
156: user.setAttributes(changedMap);
157: user.store();
158: } catch (SSOException ssoe) {
159: } catch (AMException ame) {
160: }
161: }
162:
163: public List getStringList(String name) {
164: return applyDefault ? new ArrayList() : AttributeExtractor
165: .getStringList(userAttributes, name);
166: }
167:
168: public void setStringList(String name, List value) {
169: HashSet hs = new HashSet(value);
170: Map changedMap = new HashMap();
171: changedMap.put(name, hs);
172: try {
173: user.setAttributes(changedMap);
174: user.store();
175: } catch (SSOException ssoe) {
176: } catch (AMException ame) {
177: }
178: }
179:
180: public String getUserAttribute(String name, String defaultValue) {
181: try {
182: Set set = user.getAttribute(name);
183: if (set != null) {
184: Iterator it = set.iterator();
185: return (it.hasNext()) ? (String) it.next()
186: : defaultValue;
187: }
188: } catch (SSOException ssoe) {
189: return defaultValue;
190: } catch (AMException ame) {
191: return defaultValue;
192: }
193: return defaultValue;
194: }
195:
196: public String getUserAttribute(String name) {
197: return getUserAttribute(name, "");
198: }
199: }
|