001: /**
002: * $Id: NetFileContextImpl.java,v 1.24 2005/09/21 11:05:16 dg154973 Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */
014:
015: /**
016: * This class implements the methods to access DSAME and PS
017: * for NetFile information.
018: *
019: * @author Suresh Yellamaraju
020: */package com.sun.portal.netfile.servlet.java2;
021:
022: import com.iplanet.sso.SSOToken;
023: import com.sun.portal.log.common.PortalLogger;
024: import com.iplanet.sso.SSOTokenManager;
025: import com.iplanet.sso.SSOException;
026: import com.iplanet.am.sdk.AMStoreConnection;
027: import com.iplanet.am.sdk.AMUser;
028: import com.sun.identity.sm.ServiceManager;
029: import com.sun.identity.sm.ServiceSchema;
030: import com.sun.identity.sm.ServiceSchemaManager;
031: import com.sun.identity.sm.SMSException;
032: import com.iplanet.am.sdk.AMTemplate;
033: import com.iplanet.am.sdk.AMOrganization;
034: import com.iplanet.am.sdk.AMException;
035:
036: import com.iplanet.am.util.AMClientDetector;
037:
038: import com.iplanet.services.cdm.Client;
039: import com.iplanet.services.cdm.ClientException;
040:
041: import com.sun.identity.policy.PolicyEvaluator;
042:
043: import java.util.Map;
044: import java.util.HashMap;
045: import java.util.Hashtable;
046: import java.util.List;
047: import java.util.ArrayList;
048: import java.util.Set;
049: import java.util.Iterator;
050: import java.util.Locale;
051: import java.util.StringTokenizer;
052: import java.util.Locale;
053: import java.util.Collections;
054: import java.util.logging.*;
055:
056: import javax.servlet.http.*;
057:
058: public class NetFileContextImpl implements NetFileContext {
059:
060: private AMUser user;
061: private AMStoreConnection connection;
062: private ServiceManager serviceManager;
063: private Map userAttributes;
064: private Locale userLocale;
065: private String HTMLCharset;
066:
067: private static Logger logger = PortalLogger
068: .getLogger(NetFileContextImpl.class);
069: private boolean applyDefault = false;
070:
071: private static String CONFIG_NAME = "configName";
072: private static String SUN_ONE_MAIL = "sunOneMail";
073: private static String SMTP_SERVER = "smtpServer";
074: private static String DOMAIN = "domain";
075:
076: public NetFileContextImpl(SSOToken token) {
077: try {
078: connection = new AMStoreConnection(token);
079: serviceManager = new ServiceManager(token);
080: applyDefault = false;
081:
082: getAMUserObject(token);
083:
084: // Load NetFile Attributes
085: userAttributes = getServiceAttributes(SRAP_NETFILE_SERVICE);
086:
087: } catch (SSOException ssoe) {
088: applyDefault = true;
089: return;
090: } catch (SMSException smse) {
091: applyDefault = true;
092: return;
093: }
094: }
095:
096: private Map[] getPSPreferences() {
097:
098: Map[] nfAttrs;
099:
100: nfAttrs = new HashMap[2];
101:
102: if (userAttributes == null)
103: nfAttrs[0] = getServiceAttributes(SRAP_NETFILE_SERVICE);
104: else
105: nfAttrs[0] = userAttributes;
106:
107: nfAttrs[1] = getGlobalAttributes(SRAP_NETFILE_SERVICE);
108:
109: return nfAttrs;
110: }
111:
112: private void getAMUserObject(SSOToken token) throws SSOException {
113: user = connection.getUser(token.getPrincipal().getName());
114: }
115:
116: /*
117: * Obtains the service attributes given a service name.
118: *
119: * If service name is null or zero length the NetFile
120: * Service attributes are fetched. In the event of an
121: * SSOException or AMException null is returned.
122: *
123: * @param szServiceName java.lang.String - name of the service
124: *
125: * @return java.util.Map - Service attributes or null if an
126: * exception has occured
127: */
128: private Map getServiceAttributes(String szServiceName) {
129:
130: Map serviceAttrs;
131:
132: try {
133:
134: if ((szServiceName == null)
135: || (szServiceName.length() == 0))
136: serviceAttrs = user
137: .getServiceAttributes(SRAP_NETFILE_SERVICE);
138: else
139: serviceAttrs = user.getServiceAttributes(szServiceName);
140:
141: return serviceAttrs;
142: } catch (SSOException ssoe) {
143: ssoe.printStackTrace();
144: } catch (AMException ame) {
145: ame.printStackTrace();
146: }
147:
148: return null;
149: }
150:
151: /*
152: * Obtains the global service attributes given a service name.
153: *
154: * If service name is null or zero length, then null is returned.
155: * In the event of an AMException, null is returned.
156: *
157: * @param szServiceName java.lang.String - name of the service
158: *
159: * @return java.util.Map - Global service attributes or null if an
160: * exception has occured
161: */
162: private Map getGlobalAttributes(String szServiceName) {
163:
164: Map attrs = null;
165:
166: if ((szServiceName == null) || (szServiceName.length() == 0))
167: return null;
168:
169: try {
170: ServiceSchemaManager schemaMgr = serviceManager
171: .getSchemaManager(szServiceName, IS_SVC_VERSION);
172: ServiceSchema schema = schemaMgr.getGlobalSchema();
173: attrs = schema.getAttributeDefaults();
174: schema = null;
175: } catch (SMSException smse) {
176: writeDebug("Exception in getting global attributes", smse);
177: } catch (SSOException ssoe) {
178: writeDebug("Exception in getting global attributes", ssoe);
179: }
180: return attrs;
181: }
182:
183: /*
184: * Obtains the organizational attribute's value,
185: * given the service name and attribute name.
186: *
187: * This is done by verifying if the service has a template.
188: * If the template exists, then the attribute value is obtained
189: * using the service template. If the template does not exist,
190: * then attribute is obtained through the schema.
191: *
192: * @param szServiceName java.lang.String - name of the service
193: * @param szAttributeName java.lang.String - attribute name
194: * @param szDefaultVal java.lang.String - default value of attribute
195: *
196: * @return String - org attribute value. If the attribute has more
197: * than one value, then first value is returned.
198: */
199: public String getOrganizationAttribute(String szServiceName,
200: String szAttributeName, String szDefaultVal) {
201:
202: String orgDN = null;
203: String val = szDefaultVal;
204: String value = szDefaultVal;
205:
206: AMOrganization org = null;
207: AMTemplate temp = null;
208: try {
209: orgDN = user.getOrganizationDN();
210: if (orgDN == null)
211: return null;
212:
213: org = connection.getOrganization(orgDN);
214:
215: if (org.orgTemplateExists(szServiceName)) {
216:
217: temp = org.getTemplate(szServiceName,
218: AMTemplate.ORGANIZATION_TEMPLATE);
219:
220: Set vals = temp.getAttribute(szAttributeName);
221:
222: if (vals != null && vals.size() > 0) {
223: Iterator iter = vals.iterator();
224: value = (String) iter.next();
225: }
226: //writeDebug(szAttributeName + " Org Attribute obtained through template is " + value);
227: } else {
228: ServiceSchemaManager schemaMgr = serviceManager
229: .getSchemaManager(szServiceName, IS_SVC_VERSION);
230: ServiceSchema schema = schemaMgr
231: .getOrganizationSchema();
232:
233: Map mapVals = schema.getAttributeDefaults();
234:
235: NetFileAttributeExtractor nfAttrExtr = new NetFileAttributeExtractor(
236: mapVals);
237:
238: String data = nfAttrExtr.getString(szAttributeName,
239: szDefaultVal);
240:
241: if ((data == null) || (data.trim().length() == 0)) {
242: if (applyDefault)
243: return szDefaultVal;
244: } else {
245: //writeDebug(szAttributeName + " Org Attribute obtained thru schema is " + data);
246: return data;
247: }
248: }
249: } catch (AMException ame) {
250: writeDebug("AMException in getting Org Attribute ", ame);
251: } catch (SSOException ssoe) {
252: writeDebug("SSOException in getting Org Attribute ", ssoe);
253: } catch (Exception e) {
254: writeDebug("Exception in getting Org Attribute ", e);
255: }
256: val = value;
257: return val;
258: }
259:
260: public List getOrganizationAttributeValues(String serviceName,
261: String attributeName) {
262:
263: String orgDN = null;
264: List data = null;
265:
266: try {
267: orgDN = user.getOrganizationDN();
268:
269: if (orgDN == null)
270: return null;
271:
272: AMOrganization org = connection.getOrganization(orgDN);
273:
274: if (org.orgTemplateExists(serviceName)) {
275: AMTemplate template = org.getTemplate(serviceName,
276: AMTemplate.ORGANIZATION_TEMPLATE);
277: Set values = template.getAttribute(attributeName);
278: if ((values != null) && (values.size() > 0)) {
279: data = new ArrayList();
280: Iterator iter = values.iterator();
281: while (iter.hasNext()) {
282: data.add(iter.next());
283: }
284: }
285: } else {
286: ServiceSchemaManager schemaMgr = serviceManager
287: .getSchemaManager(serviceName, IS_SVC_VERSION);
288: ServiceSchema schema = schemaMgr
289: .getOrganizationSchema();
290:
291: Map mapVals = schema.getAttributeDefaults();
292:
293: Set keys = mapVals.keySet();
294:
295: for (Iterator i = keys.iterator(); i.hasNext();) {
296: String attrib = (String) i.next();
297: if (attrib.equalsIgnoreCase(attributeName)) {
298:
299: Set values = (Set) mapVals.get(attrib);
300: if (values != null) {
301: data = new ArrayList();
302: Object[] obj = values.toArray();
303:
304: for (int index = 0; index < obj.length; ++index) {
305: data.add(obj[index]);
306: }
307: break;
308: }
309: }
310: }
311: }
312: } catch (AMException ame) {
313: writeDebug("AMException in getting Org Attribute ", ame);
314: } catch (SSOException ssoe) {
315: writeDebug("SSOException in getting Org Attribute ", ssoe);
316: } catch (Exception e) {
317: writeDebug("Exception in getting Org Attribute ", e);
318: }
319: return data;
320: }
321:
322: /*
323: * Public API
324: */
325: public Map[] getPreferences() {
326:
327: try {
328: return getPSPreferences();
329: } catch (Exception e) {
330: writeDebug("Error in getting preferences", e);
331: }
332: return null;
333: }
334:
335: public boolean savePreferences(Map prefs) throws Exception {
336: try {
337: user.setAttributes(prefs);
338: } catch (AMException pe) {
339: writeDebug("Preferences not saved", pe);
340: return false;
341: } catch (SSOException pe) {
342: writeDebug("Preferences not saved", pe);
343: return false;
344: } catch (Exception e) {
345: writeDebug("Preferences not saved", e);
346: return false;
347: }
348:
349: try {
350: user.store();
351: return true;
352: } catch (Exception e) {
353: writeDebug("Preferences not saved", e);
354: }
355: return false;
356: }
357:
358: /*
359: * Detemines if user has the policy assigned.
360: *
361: * return boolean - true, if policy is assigned, false otherwise.
362: */
363: public boolean isPolicyAssigned(SSOToken ssoToken)
364: throws AMException, SSOException {
365: return true;
366: /*boolean allowed = false;
367: try {
368: PolicyEvaluator policyEval = new PolicyEvaluator(
369: SRAP_NETFILE_SERVICE);
370: allowed = policyEval.isAllowed(
371: ssoToken,
372: "",
373: "sunPortalNetFileExecute",
374: Collections.EMPTY_MAP);
375: if (!allowed) {
376: writeDebug("NetFileContext.isExecutable: Not allowed to execute NetFile.");
377: }
378: } catch (Exception ex) {
379: writeErrorDebug("Exception in evaluating policy ", ex);
380: return allowed;
381: }
382: return allowed;*/
383: }
384:
385: /*
386: * Detemines if user has the service assigned.
387: *
388: * return boolean - true, if service is assigned, false otherwise.
389: */
390: public boolean isServiceAssigned(String szServiceName)
391: throws AMException, SSOException {
392:
393: Set vals = null;
394:
395: vals = user.getAssignedServices();
396:
397: if ((vals == null) || (vals.isEmpty())) {
398: writeDebug("NetFile service has not been assigned for the user");
399: return false;
400: }
401:
402: Iterator iter = vals.iterator();
403:
404: while (iter.hasNext()) {
405: String szAServiceName = (String) iter.next();
406: if (szAServiceName.equals(szServiceName))
407: return true;
408: }
409:
410: return false;
411: }
412:
413: /*
414: * Detemines if user has the service and the policy assigned.
415: *
416: * return boolean - true if both policy and service are assigned and
417: * false for anything else.
418: */
419: public boolean isExecutable(SSOToken token) {
420: try {
421: return (isPolicyAssigned(token) && isServiceAssigned(SRAP_NETFILE_SERVICE));
422: } catch (Exception e) {
423: writeDebug(
424: "Exception in determining if user can use NetFile",
425: e);
426: }
427: return false;
428: }
429:
430: public boolean isSessionValid(SSOToken token) {
431: try {
432: if (token == null)
433: return false;
434: SSOTokenManager manager = SSOTokenManager.getInstance();
435: if (!manager.isValidToken(token)) {
436: return false;
437: }
438: } catch (Exception e) {
439: writeDebug("Exception in determining if session is valid",
440: e);
441: return false;
442: }
443: return true;
444: }
445:
446: public Locale getUserLocale() {
447: Locale locale = null;
448: try {
449: Map data = getServiceAttributes(DSAME_USER_SERVICE);
450:
451: NetFileAttributeExtractor nfAttr = new NetFileAttributeExtractor(
452: data);
453:
454: String locstr = nfAttr.getString("preferredlocale", Locale
455: .getDefault().toString());
456: StringTokenizer locst = new StringTokenizer(locstr, "_");
457:
458: if (locst.countTokens() > 0) {
459:
460: String lang = (locst.hasMoreTokens() ? locst
461: .nextToken() : "");
462: String country = (locst.hasMoreTokens() ? locst
463: .nextToken() : "");
464: String variant = (locst.hasMoreTokens() ? locst
465: .nextToken() : "");
466:
467: while (locst.hasMoreTokens()) {
468: variant += "_" + locst.nextToken();
469: }
470:
471: locale = new java.util.Locale(lang, country, variant);
472: } else {
473: locale = Locale.getDefault();
474: }
475: userLocale = locale;
476:
477: writeDebug("Locale is " + userLocale.toString());
478: } catch (Exception e) {
479: writeDebug("UserLocale Error is ", e);
480: return null;
481: }
482: return locale;
483: }
484:
485: public void determineHTMLCharset(HttpServletRequest req) {
486:
487: AMClientDetector amCD = new AMClientDetector();
488: String clientType = amCD.getClientType(req);
489:
490: try {
491: Client clientObj = Client.getInstance(clientType);
492: HTMLCharset = clientObj.getCharset(getUserLocale());
493: } catch (ClientException ce) {
494: writeDebug("ClientException in determining charset ", ce);
495: }
496: writeDebug("HTML Charset is " + HTMLCharset);
497: amCD = null;
498: clientType = null;
499: }
500:
501: /*
502: * Returns the HTML character set.
503: *
504: * If called after invoking determineHTMLCharset() will result
505: * in HTML character set based on user's locale. If invoked
506: * without invoking determineHTMLCharset(), then will return the
507: * default value of this variable.
508: */
509: public String getHTMLCharset() {
510: return HTMLCharset;
511: }
512:
513: /*
514: * Loads the Mail attributes into the given Hashtable
515: */
516: public void loadMailAttributesAsCollection(Hashtable htMailData) {
517:
518: String fromAddr = "";
519: String replyToAddr = "";
520: //String imapSrvr = "";
521: String smtpSrvr = "";
522: //String imapUserId = "";
523: String defaultMailDom = "";
524:
525: Map mailData = getServiceAttributes(IPS_SSO_ADAPTER_SERVICE);
526:
527: if ((mailData == null) || (mailData.isEmpty())) {
528: writeDebug("Could not obtain mail attributes");
529: return;
530: }
531:
532: java.util.HashSet adapConfig = (java.util.HashSet) mailData
533: .get(IPS_SSO_ADAPTER_CONFIG);
534: Iterator it = adapConfig.iterator();
535: String prStr = null, result = null;
536:
537: while (it.hasNext()) {
538: prStr = (String) it.next();
539: result = getAttributeValue(prStr, CONFIG_NAME);
540: if (result.equals(SUN_ONE_MAIL)) {
541: java.util.StringTokenizer st = new java.util.StringTokenizer(
542: prStr, ":/");
543: fromAddr = getNextToken(st);
544: fromAddr = getNextToken(st);
545: smtpSrvr = getAttributeValue(prStr, SMTP_SERVER);
546: defaultMailDom = getAttributeValue(prStr, DOMAIN);
547: break;
548: }
549: }
550:
551: htMailData.put("sunPortalUserReplyToAddress", fromAddr);
552: htMailData.put("sunPortalUserReplyToAddress", fromAddr);
553: htMailData.put("sunPortalUserFromAddress", fromAddr);
554: htMailData.put("mailserver", smtpSrvr);
555: }
556:
557: private String getAttributeValue(String sourceStr, String attribute) {
558: java.util.StringTokenizer st = new java.util.StringTokenizer(
559: sourceStr, "?=&");
560: String token = null;
561: String value = "";
562: while (st.hasMoreTokens()) {
563: token = st.nextToken();
564: if (token.equalsIgnoreCase(attribute)) {
565: value = st.nextToken();
566: break;
567: }
568: }
569: return value;
570: }
571:
572: private String getNextToken(java.util.StringTokenizer st) {
573: if (st.hasMoreTokens()) {
574: return st.nextToken();
575: }
576: return "";
577: }
578:
579: public Map getUserPreferences() throws Exception {
580: throw new java.lang.UnsupportedOperationException(
581: "Method not implemented");
582: }
583:
584: protected void writeDebug(String szMsg) {
585: writeDebug(szMsg, null);
586: }
587:
588: protected void writeDebug(String szMsg, Exception e) {
589: if (e != null) {
590: // logger.log(Level.INFO, szMsg, e);
591: logger.log(Level.INFO, "PSSRNF_CSPNSJ2043");
592: } else {
593: // logger.info(szMsg);
594: logger.info("PSSRNF_CSPNSJ2044");
595: }
596: }
597:
598: protected void writeErrorDebug(String szError, Exception e) {
599: if (e != null)
600: // logger.log(Level.SEVERE, szError, e);
601: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ2045");
602: else
603: // logger.severe(szError);
604: logger.severe("PSSRNF_CSPNSJ2046");
605: }
606:
607: }
|