001: /**
002: * $Id: PSClientAwareUserContext.java,v 1.8 2005/10/10 23:45:29 rakeshn Exp $
003: * Copyright 2002 Sun Microsystems, Inc. Allrights reserved. Use of
004: * this product is subjectto license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users Subject to Standard License
006: * Terms and Conditions.
007: *
008: * Sun, Sun Microsystems, the Sun logo, and Sun ONE are trademarks or
009: * registered trademarks of Sun Microsystems,Inc. in the United States
010: * and other countries.
011: */package com.sun.ssoadapter.config;
012:
013: import java.util.Map;
014: import java.util.HashMap;
015: import java.util.Iterator;
016: import java.util.Set;
017: import java.util.HashSet;
018: import java.util.Locale;
019: import java.util.StringTokenizer;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.io.FileNotFoundException;
024: import java.util.MissingResourceException;
025: import java.util.logging.Level;
026: import java.util.logging.Logger;
027:
028: //import com.sun.identity.sm.AttributeSchema;
029:
030: import com.sun.ssoadapter.SSOAdapterLogger;
031: import com.sun.ssoadapter.SSOAdapterSession;
032:
033: public class PSClientAwareUserContext implements
034: ClientAwareUserContext, DSAMEConstants {
035: private String this ClassName = null;
036:
037: private DSAMEConnection dsameConn = null;
038: private DSAMEUtils dsameUtils = null;
039:
040: private String sessClientType = null;
041: private String charset = null;
042:
043: private ClientAwareAppContext caAppContext = null;
044:
045: private static Logger logger = SSOAdapterLogger
046: .getLogger("com.sun.portal.ssoadapter.config");
047:
048: /**
049: *
050: * @param session
051: * @param appContext
052: * @throws java.lang.IllegalStateException
053: * @throws com.sun.ssoadapter.config.SAALException
054: */
055: public synchronized void init(SSOAdapterSession session,
056: ClientAwareAppContext appContext)
057: throws IllegalStateException, SAALException {
058: //
059: // order matters here!
060: //
061: this ClassName = getClass().getName(); // for debug
062:
063: // store ClientAware AppContext - needed for debugMessage()
064: caAppContext = appContext;
065:
066: if (logger.isLoggable(Level.FINEST)) {
067: logger.log(Level.FINEST, "PSSA_CSSC0001");
068: }
069:
070: /**
071: * Perf - Before calling init(), check if the session is valid
072: */
073: if (caAppContext.validateSession(session) != true) {
074: if (logger.isLoggable(Level.SEVERE)) {
075: logger.log(Level.SEVERE, "PSSA_CSSC0002");
076: }
077: throw new IllegalStateException("Session Invalid:");
078: }
079:
080: sessClientType = caAppContext.getClientType(session);
081:
082: //
083: // Initialize the Connection to DSAME - throws IllegalStateExcptn
084: //
085: dsameConn = new DSAMEConnection(session);
086:
087: //
088: // Get the instance of DSAMEUtils, should have been created by
089: // DSAMEServiceAppContext
090: //
091: dsameUtils = DSAMEUtils.getInstance();
092: if (dsameUtils == null) {
093: throw new SAALException(this ClassName
094: + ": DSAMEUtils not Initialized");
095: }
096:
097: // initLocale();
098: // initCharset();
099:
100: if (logger.isLoggable(Level.FINEST)) {
101: logger.log(Level.FINEST, "PSSA_CSSC0003");
102: }
103: }
104:
105: public void store() {
106: //
107: // DSAME writes all changes immediately to the directory server
108: // so we dont have to do anything for now.
109: //
110: }
111:
112: //
113: // service methods - to get classnames
114: //
115:
116: /**
117: * Defaults:
118: * clientType - session
119: * @param service
120: * @param attrName
121: * @throws java.lang.IllegalStateException
122: * @throws java.io.IOException
123: * @return
124: */
125: public String getStringAttribute(Map service, String attrName)
126: throws IllegalStateException, IOException {
127: return getStringAttribute(service, getClientType(), attrName);
128: }
129:
130: /**
131: *
132: * @param service
133: * @param client
134: * @param attrName
135: * @throws java.lang.IllegalStateException
136: * @throws java.io.IOException
137: * @return
138: */
139: public String getStringAttribute(Map service, String client,
140: String attrName) throws IllegalStateException, IOException {
141: String val = null;
142: Set vals = getAttribute(service, client, attrName);
143:
144: if (vals != null && vals.size() > 0) {
145: Iterator iter = vals.iterator();
146: val = (String) iter.next();
147: }
148:
149: if (logger.isLoggable(Level.FINEST)) {
150: String[] param = { attrName, val };
151: logger.log(Level.FINEST, "PSSA_CSSC0004", param);
152: }
153:
154: return (val);
155: }
156:
157: /**
158: * Defaults:
159: * clientType - session
160: * @param service
161: * @param attrName
162: * @throws java.lang.IllegalStateException
163: * @throws java.io.IOException
164: * @return
165: */
166: public Set getAttribute(Map service, String attrName)
167: throws IllegalStateException, IOException {
168: return getAttribute(service, getClientType(), attrName);
169:
170: }
171:
172: /**
173: *
174: * @param service
175: * @param client
176: * @param attrName
177: * @throws java.lang.IllegalStateException
178: * @throws java.io.IOException
179: * @return
180: */
181: public Set getAttribute(Map service, String client, String attrName)
182: throws IllegalStateException, IOException {
183: String serviceName = dsameUtils.getServiceName(service);
184: return getAttribute(serviceName, client, attrName, true, null,
185: 0, 0);
186: }
187:
188: /**
189: * All getAttribute() calls map into this one.
190: *
191: * @return Set of all client-aware attributes
192: * @param serviceName
193: * @param client
194: * @param attrName The name of the attribute
195: * @param removeClientInfo When the getAttribute() calls DSAMEUtils.
196: * getClientValues(), it removes the client-info from the vals. This
197: * flag prevents it from doing so. (Used in the call from setAttribute())
198: * @param awareMap To store the results from the parsing of attributes.
199: * Used by setAttribute() to reused the parsed objects.
200: * @param scope The scope of the attribute to look for (used by setAttr())
201: * @param type The attribute type (list/string..) (used by setAttr()).
202: * @throws java.lang.IllegalStateException
203: * @throws java.io.IOException
204: */
205: private Set getAttribute(String serviceName, String client,
206: String attrName, boolean removeClientInfo, Map awareMap,
207: int scope, int type) throws IllegalStateException,
208: IOException {
209: Set vals = null;
210: Set retVals = null;
211:
212: if (logger.isLoggable(Level.FINEST)) {
213: String[] param = { attrName, serviceName, client };
214: logger.log(Level.FINEST, "PSSA_CSSC0005", param);
215: }
216:
217: //
218: // Lookup attrName in our attribute name Hashmap and get the
219: // the attribute type - Global/Org/User-Dynamic
220: //
221:
222: scope = (scope != 0) ? scope : dsameUtils.getAttributeScope(
223: serviceName, attrName);
224:
225: switch (scope) {
226: case GLOBAL: //
227: vals = dsameConn.getGlobalAttribute(serviceName, attrName);
228: break;
229:
230: case ORGANIZATION: //
231: vals = dsameConn.getOrganizationAttribute(serviceName,
232: attrName);
233: break;
234:
235: case INSTANCE: //
236: break;
237:
238: case POLICY: //
239: case DYNAMIC:
240: case USER:
241: default: // assume user scope
242: //
243: // Policy/Dynamic/User/Unknown - USER scope
244: //
245: vals = dsameConn.getAttribute(attrName);
246: break;
247: }
248:
249: //
250: // Get the Type
251: //
252:
253: type = (type != 0) ? type : dsameUtils.getAttributeType(
254: serviceName, attrName);
255: switch (type) {
256: case SINGLE:
257: //
258: // return the List. The getStringAttribute() will pick up
259: // only the first value !
260: //
261: retVals = vals;
262: break;
263:
264: case LIST:
265: //
266: //
267: //
268: retVals = dsameUtils.getClientValues(vals, client,
269: awareMap, removeClientInfo);
270: break;
271:
272: default:
273: retVals = vals;
274: break;
275: }
276:
277: return retVals;
278: }
279:
280: /**
281: * Defaults:
282: * clientType - session
283: * forceClientAwareness - FALSE
284: * @param service
285: * @param attrName
286: * @param val
287: * @throws java.lang.IllegalStateException
288: * @throws java.util.MissingResourceException
289: * @throws java.io.IOException
290: */
291: public void setStringAttribute(Map service, String attrName,
292: String val) throws IllegalStateException,
293: MissingResourceException, IOException {
294: setStringAttribute(service, getClientType(), attrName, val,
295: false);
296: }
297:
298: /**
299: * Defaults:
300: * clientType - session
301: * @param service
302: * @param attrName
303: * @param val
304: * @param forceCA
305: * @throws java.lang.IllegalStateException
306: * @throws java.util.MissingResourceException
307: * @throws java.io.IOException
308: */
309: public void setStringAttribute(Map service, String attrName,
310: String val, boolean forceCA) throws IllegalStateException,
311: MissingResourceException, IOException {
312: setStringAttribute(service, getClientType(), attrName, val,
313: forceCA);
314: }
315:
316: /**
317: * Defaults:
318: * forceClientAwareness - TRUE (implicit)
319: * @param service
320: * @param client
321: * @param attrName
322: * @param val
323: * @throws java.lang.IllegalStateException
324: * @throws java.util.MissingResourceException
325: * @throws java.io.IOException
326: */
327: public void setStringAttribute(Map service, String client,
328: String attrName, String val) throws IllegalStateException,
329: MissingResourceException, IOException {
330: setStringAttribute(service, client, attrName, val, true);
331: }
332:
333: /**
334: * All setStringAttribute() calls map into this one. Look at
335: * PSClientAwareUserContext object to get the mapping.
336: *
337: * @param client
338: * @param service contains the serviceName & clientType the attribute
339: * value needs to be set for
340: * @param attrName The name of the attribute
341: * @param val The string value
342: * @param forceClientAwareness Determines if the attribute values
343: * need to be stored client-aware.
344: * @throws java.lang.IllegalStateException
345: * @throws java.util.MissingResourceException
346: * @throws java.io.IOException
347: */
348: private void setStringAttribute(Map service, String client,
349: String attrName, String val, boolean forceClientAwareness)
350: throws IllegalStateException, MissingResourceException,
351: IOException {
352: HashSet vals = new HashSet();
353: vals.add(val);
354:
355: setAttribute(service, client, attrName, vals,
356: forceClientAwareness);
357: }
358:
359: /**
360: * Defaults:
361: * clientType - from session
362: * forceClientAwareness - FALSE
363: * @param service
364: * @param attrName
365: * @param vals
366: * @throws java.lang.IllegalStateException
367: * @throws java.util.MissingResourceException
368: * @throws java.io.IOException
369: */
370: public void setAttribute(Map service, String attrName, Set vals)
371: throws IllegalStateException, MissingResourceException,
372: IOException {
373: setAttribute(service, getClientType(), attrName, vals, false);
374: }
375:
376: /**
377: * Defaults:
378: * clientType - session
379: * @param service
380: * @param attrName
381: * @param vals
382: * @param forceClientAwareness
383: * @throws java.lang.IllegalStateException
384: * @throws java.util.MissingResourceException
385: * @throws java.io.IOException
386: */
387: public void setAttribute(Map service, String attrName, Set vals,
388: boolean forceClientAwareness) throws IllegalStateException,
389: MissingResourceException, IOException {
390: setAttribute(service, getClientType(), attrName, vals,
391: forceClientAwareness);
392: }
393:
394: /**
395: * Defaults:
396: * forceClientAwareness - TRUE (implicit)
397: * @param service
398: * @param client
399: * @param attrName
400: * @param vals
401: * @throws java.lang.IllegalStateException
402: * @throws java.util.MissingResourceException
403: * @throws java.io.IOException
404: */
405: public void setAttribute(Map service, String client,
406: String attrName, Set vals) throws IllegalStateException,
407: MissingResourceException, IOException {
408: setAttribute(service, client, attrName, vals, true);
409: }
410:
411: /**
412: * All setAttribute() calls map into this one.
413: *
414: * @param service
415: * @param client The attribute value needs to be set for
416: * @param attrName The name of the attribute
417: * @param vals The values as a Set
418: * @param forceCA Dtermines if the attribute values
419: * need to be stored client-aware.
420: * @throws java.lang.IllegalStateException
421: * @throws java.util.MissingResourceException
422: * @throws java.io.IOException
423: */
424: private void setAttribute(Map service, String client,
425: String attrName, Set vals, boolean forceCA)
426: throws IllegalStateException, MissingResourceException,
427: IOException {
428: String serviceName = dsameUtils.getServiceName(service);
429:
430: if (serviceName == null) {
431: throw new MissingResourceException("Missing in Map",
432: "DSAMEServiceUserContext", SERVICENAME);
433: }
434:
435: //
436: // Lookup attrName in our attribute name Hashmap and get the
437: // the attribute scope & type - Global/Org/User-Dynamic & single/list
438: //
439:
440: int scope = dsameUtils.getAttributeScope(serviceName, attrName);
441: int type = dsameUtils.getAttributeType(serviceName, attrName);
442:
443: Set newVals = null;
444:
445: if ((type == MULTIPLE_CHOICE) || (type == SINGLE_CHOICE)) {
446: //
447: // for the time being - not client aware !!
448: //
449: newVals = vals;
450: } else if (type == SINGLE) {
451: //
452: // replace all the values with this (there should be only one)
453: //
454: //
455: String msg = null;
456:
457: if (vals != null && vals.size() > 1) {
458: msg = cannotSetMultiInSingle; // from DSAMEConstants
459: }
460:
461: if (forceCA == true) {
462: msg += cannotForceSingle; // from DSAMEConstants
463: }
464:
465: if (msg != null) {
466: throw new IOException(msg);
467: }
468:
469: newVals = vals;
470:
471: } else {
472: //
473: // Get all the values - The 4th param is false to prevent the
474: // getClientValues() (called by getAttribute()), removing the
475: // client-info from vals.
476: //
477: Map awareMap = new HashMap();
478: getAttribute(serviceName, client, attrName, false,
479: awareMap, scope, type);
480:
481: //
482: // Check if they are client-aware
483: //
484:
485: //
486: // Modify the values set according to the parsing in awareMap
487: //
488: newVals = dsameUtils.modifyValues(client, awareMap, vals,
489: forceCA);
490: }
491:
492: if (logger.isLoggable(Level.FINEST)) {
493: String[] param = { attrName, serviceName, client };
494: logger.log(Level.FINEST, "PSSA_CSSC0006", param);
495: }
496:
497: switch (scope) {
498: case GLOBAL: //
499: dsameConn
500: .setGlobalAttribute(serviceName, attrName, newVals);
501: break;
502:
503: case ORGANIZATION: //
504: dsameConn.setOrganizationAttribute(serviceName, attrName,
505: newVals);
506: break;
507:
508: case INSTANCE: //
509: break;
510:
511: case POLICY: //
512: case DYNAMIC:
513: case USER:
514: default: // assume user scope
515: //
516: // Policy/Dynamic/User/Unknown - USER scope
517: //
518: dsameConn.setAttribute(attrName, newVals);
519: break;
520: }
521: }
522:
523: /**
524: *
525: * @param service
526: * @param attrName
527: * @throws java.lang.IllegalStateException
528: * @throws java.util.MissingResourceException
529: * @throws java.io.IOException
530: */
531: public void removeAttribute(Map service, String attrName)
532: throws IllegalStateException, MissingResourceException,
533: IOException {
534: String serviceName = dsameUtils.getServiceName(service);
535: int scope = dsameUtils.getAttributeScope(serviceName, attrName);
536:
537: Set attrs = new HashSet();
538: attrs.add(attrName);
539:
540: switch (scope) {
541: case GLOBAL: //
542: throw new IOException("Cannot remove global attributes");
543:
544: case ORGANIZATION: //
545: dsameConn.removeOrganizationAttribute(serviceName, attrs);
546: break;
547:
548: case INSTANCE: //
549: break;
550:
551: case POLICY: //
552: case DYNAMIC:
553: case USER:
554: default: // assume user scope
555: //
556: // Policy/Dynamic/User/Unknown - USER scope
557: //
558: dsameConn.removeAttribute(attrs);
559: break;
560: }
561: }
562:
563: //
564: // client
565: //
566:
567: /**
568: *
569: * @return
570: */
571: public String getClientType() {
572: return sessClientType;
573: }
574:
575: /**
576: * This merge is required as SSOAdapterConfigurations is a dynamic as well as User attribute
577: * This is invoked for SSOAdapterTemplates as well, which is global , hence handle is differently
578: */
579:
580: Set getMergedDynamicConfigurations(Map service, String attrName)
581: throws IllegalStateException, IOException {
582: String serviceName = dsameUtils.getServiceName(service);
583: int scope = dsameUtils.getAttributeScope(serviceName, attrName);
584: Set vals = null;
585: if (scope == GLOBAL) {
586: //donot look up in Orgs for SSOAdapterTemplates
587: vals = dsameConn.getGlobalAttribute(serviceName, attrName);
588: } else {
589: //If it is aUserAttribute or dynamic attribute only for SSOAdapterConfigurations
590: vals = dsameConn.getMergedDynamicConfigurations(
591: serviceName, attrName);
592: }
593: return dsameUtils.getClientValues(vals, getClientType(), null,
594: true);
595:
596: }
597:
598: }
|