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