001: /*
002: * Copyright 2004 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: package com.sun.portal.taskadmin.context;
014:
015: import java.util.Iterator;
016: import java.util.TreeSet;
017: import java.util.Set;
018: import java.util.Map;
019:
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpServletResponse;
022:
023: import com.sun.portal.taskadmin.TaskAdminException;
024: import com.sun.portal.taskadmin.TaskAdminConstants;
025:
026: import com.sun.portal.desktop.context.ContextError;
027: import com.sun.portal.desktop.context.DPContext;
028: import com.sun.portal.desktop.context.DSAMEAdminDPContext;
029: import com.sun.portal.desktop.context.DSAMEConstants;
030: import com.sun.portal.desktop.dp.DPError;
031: import com.sun.portal.desktop.dp.xml.XMLDPFactory;
032:
033: import com.iplanet.am.sdk.AMStoreConnection;
034: import com.iplanet.am.sdk.AMObject;
035: import com.iplanet.am.sdk.AMUser;
036: import com.iplanet.am.sdk.AMException;
037: import com.iplanet.am.sdk.AMConstants;
038:
039: import com.sun.identity.sm.ServiceManager;
040: import com.sun.identity.sm.ServiceSchema;
041: import com.sun.identity.sm.ServiceSchemaManager;
042: import com.sun.identity.sm.SMSException;
043:
044: import com.iplanet.sso.SSOTokenManager;
045: import com.iplanet.sso.SSOToken;
046: import com.iplanet.sso.SSOException;
047:
048: /**
049: * This class provides the implementation for Desktop Content
050: * Management
051: */
052:
053: public class TaskAdminISConnection {
054: // lazy instantiation: use getXXX() to access
055: private static SSOTokenManager _tokenMgr = null;
056:
057: protected AMStoreConnection connection = null;
058: protected String userDN = null;
059: protected AMUser user = null;
060: protected SSOToken ssoToken = null;
061: protected String sid = null;
062: protected ServiceManager serviceManager = null;
063:
064: public TaskAdminISConnection(HttpServletRequest httpreq)
065: throws TaskAdminException {
066: this (getSSOToken(httpreq));
067: }
068:
069: public TaskAdminISConnection(SSOToken token)
070: throws TaskAdminException {
071: ssoToken = token;
072: try {
073: connection = new AMStoreConnection(token);
074: ;
075: userDN = token.getPrincipal().toString();
076: user = connection.getUser(userDN);
077: initSessionID(token);
078: serviceManager = new ServiceManager(ssoToken);
079: } catch (SMSException smse) {
080: throw new TaskAdminException(
081: "TaskAdminISConnection.init(): "
082: + "Failed to get ServiceManager. ", smse);
083: } catch (SSOException se) {
084: // This means that SSOToken is invalid
085: throw new TaskAdminException(
086: "TaskAdminISConnection.init(): "
087: + "Failed to get SSOToken. ", se);
088: }
089: }
090:
091: public AMStoreConnection getConnection() {
092: return connection;
093: }
094:
095: public AMUser getUser() {
096: return user;
097: }
098:
099: public static SSOToken getSSOToken(HttpServletRequest request)
100: throws TaskAdminException {
101: SSOToken token = null;
102: try {
103: token = getSSOTokenManager().createSSOToken(request);
104: } catch (SSOException se) {
105: // This means that SSOToken is invalid
106: throw new TaskAdminException("TaskAdmin.getSSOToken(): "
107: + "Failed to get SSOToken. ", se);
108: }
109: return token;
110: }
111:
112: public SSOToken getSSOToken() {
113: return ssoToken;
114: }
115:
116: private static synchronized SSOTokenManager getSSOTokenManager()
117: throws TaskAdminException {
118: if (_tokenMgr == null) {
119: try {
120: _tokenMgr = SSOTokenManager.getInstance();
121: if (_tokenMgr == null) {
122: throw new TaskAdminException(
123: "TaskAdmin.getSSOTokenMgr(): "
124: + "Failed to get SSOTokenManager. ");
125: }
126: } catch (SSOException se) {
127: throw new TaskAdminException(
128: "TaskAdmin.getSSOTokenMgr(): "
129: + "Failed to get SSOTokenManager. ",
130: se);
131: }
132: }
133:
134: return _tokenMgr;
135: }
136:
137: protected void initSessionID(SSOToken token) {
138: this .sid = token.getTokenID().toString();
139: }
140:
141: public String getSessionID() {
142: return sid;
143: }
144:
145: /**
146: * Gets USER/DYNAMIC attribute.
147: * @param attributeName Name of the attribute to retrieve
148: * @return The attribute value in String format. If property is
149: * not not found, return null. If DSAME returns multi-value,
150: * warning is issued and only the first value is returned.
151: */
152: public String getAttribute(String attributeName) {
153: String val = null;
154: Set vals = getAttributeMultiVal(attributeName);
155:
156: if (vals != null && vals.size() > 0) {
157: Iterator iter = vals.iterator();
158: val = (String) iter.next();
159: }
160:
161: return val;
162: }
163:
164: /**
165: * Gets multi-valued USER/DYNAMIC attribute.
166: * @param attributeName Name of the attribute to retrieve
167: * @return Set of String values. If property is
168: * not not found, return null.
169: */
170: public Set getAttributeMultiVal(String attributeName) {
171: Set vals = null;
172:
173: try {
174: vals = user.getAttribute(attributeName);
175: } catch (Exception e) {
176: throw new ContextError(
177: "TaskAdminISConnection.getAttributeMultiVal(): "
178: + "attributeName=" + attributeName, e);
179: }
180: return vals;
181: }
182:
183: public String getGlobalAttribute(String serviceName,
184: String attributeName) {
185: Set vals = getGlobalAttributeMultiValue(serviceName,
186: attributeName);
187: if (vals == null || vals.size() < 1) {
188: return null;
189: }
190:
191: Iterator iter = vals.iterator();
192: String val = (String) iter.next();
193:
194: return val;
195: }
196:
197: public Set getGlobalAttributeMultiValue(String serviceName,
198: String attributeName) {
199: Map attrs = getGlobalAttributes(serviceName);
200: Set vals = (Set) attrs.get(attributeName);
201: return vals;
202: }
203:
204: public Map getGlobalAttributes(String serviceName) {
205: Map attrs = null;
206:
207: try {
208: ServiceSchemaManager schemaMgr = serviceManager
209: .getSchemaManager(serviceName, "1.0");
210: ServiceSchema schema = schemaMgr.getGlobalSchema();
211: attrs = schema.getReadOnlyAttributeDefaults();
212: } catch (Exception ex) {
213: throw new ContextError(
214: "TaskAdminISConnection.getGlobalAttributes(): "
215: + serviceName, ex);
216: }
217:
218: return attrs;
219: }
220:
221: }
|