001: /**
002: * $Id: NetFileContextImpl.java,v 1.21 2005/09/21 11:03:59 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 iPS Hana
017: *
018: * This class also implements the SSOTokenListener interface. Thus,
019: * any session events like destroy, idle time out & timeout will
020: * result in the NetFile session being destroyed.
021: *
022: * @author Suresh Yellamaraju
023: */package com.sun.portal.netfile.servlet.java1;
024:
025: import com.iplanet.sso.SSOToken;
026: import com.sun.portal.log.common.PortalLogger;
027: import com.iplanet.sso.SSOTokenManager;
028: import com.iplanet.sso.SSOException;
029: import com.iplanet.am.sdk.AMStoreConnection;
030: import com.iplanet.am.sdk.AMUser;
031: import com.sun.identity.sm.ServiceManager;
032: import com.sun.identity.sm.ServiceSchema;
033: import com.sun.identity.sm.ServiceSchemaManager;
034: import com.sun.identity.sm.SMSException;
035: import com.iplanet.am.sdk.AMTemplate;
036: import com.iplanet.am.sdk.AMOrganization;
037: import com.iplanet.am.sdk.AMException;
038:
039: import com.iplanet.am.util.AMClientDetector;
040:
041: import com.iplanet.services.cdm.Client;
042: import com.iplanet.services.cdm.ClientException;
043:
044: import com.sun.identity.policy.PolicyEvaluator;
045:
046: import java.util.Map;
047: import java.util.HashMap;
048: import java.util.Hashtable;
049: import java.util.Set;
050: import java.util.Iterator;
051: import java.util.Locale;
052: import java.util.StringTokenizer;
053: import java.util.ArrayList;
054: import java.util.Collections;
055: import java.util.logging.*;
056:
057: import javax.servlet.http.*;
058:
059: public class NetFileContextImpl implements NetFileContext {
060:
061: private AMUser user;
062: private AMStoreConnection connection;
063: private ServiceManager serviceManager;
064: private Map userAttributes;
065: private Locale userLocale;
066: private String HTMLCharset;
067:
068: private static Logger logger = PortalLogger
069: .getLogger(NetFileContextImpl.class);
070: private boolean applyDefault = false;
071:
072: private static String CONFIG_NAME = "configName";
073: private static String SUN_ONE_MAIL = "sunOneMail";
074: private static String SMTP_SERVER = "smtpServer";
075: private static String DOMAIN = "domain";
076:
077: public NetFileContextImpl(SSOToken token) {
078:
079: try {
080: connection = new AMStoreConnection(token);
081: serviceManager = new ServiceManager(token);
082: applyDefault = false;
083:
084: getAMUserObject(token);
085:
086: // Load NetFile Attributes
087: userAttributes = getServiceAttributes(SRAP_NETFILE_SERVICE);
088:
089: } catch (SSOException ssoe) {
090: applyDefault = true;
091: return;
092: } catch (SMSException e) {
093: applyDefault = true;
094: return;
095: }
096: }
097:
098: private Map[] getPSPreferences() {
099:
100: Map[] nfAttrs;
101:
102: nfAttrs = new HashMap[2];
103:
104: if (userAttributes == null)
105: nfAttrs[0] = getServiceAttributes(SRAP_NETFILE_SERVICE);
106: else
107: nfAttrs[0] = userAttributes;
108:
109: nfAttrs[1] = getGlobalAttributes(SRAP_NETFILE_SERVICE);
110:
111: return nfAttrs;
112: }
113:
114: private void getAMUserObject(SSOToken token) throws SSOException {
115: user = connection.getUser(token.getPrincipal().getName());
116: }
117:
118: /*
119: * Obtains the service attributes given a service name.
120: *
121: * If service name is null or zero length the NetFile
122: * Service attributes are fetched. In the event of an
123: * SSOException or AMException null is returned.
124: *
125: * @param szServiceName java.lang.String - name of the service
126: *
127: * @return java.util.Map - Service attributes or null if an
128: * exception has occured
129: */
130: private Map getServiceAttributes(String szServiceName) {
131:
132: Map serviceAttrs;
133:
134: try {
135:
136: if ((szServiceName == null)
137: || (szServiceName.length() == 0))
138: serviceAttrs = user
139: .getServiceAttributes(SRAP_NETFILE_SERVICE);
140: else
141: serviceAttrs = user.getServiceAttributes(szServiceName);
142:
143: return serviceAttrs;
144: } catch (SSOException ssoe) {
145: ssoe.printStackTrace();
146: } catch (AMException ame) {
147: ame.printStackTrace();
148: }
149:
150: return null;
151: }
152:
153: /*
154: * Obtains the global service attributes given a service name.
155: *
156: * If service name is null or zero length, then null is returned.
157: * In the event of an AMException, null is returned.
158: *
159: * @param szServiceName java.lang.String - name of the service
160: *
161: * @return java.util.Map - Global service attributes or null if an
162: * exception has occured
163: */
164: private Map getGlobalAttributes(String szServiceName) {
165:
166: Map attrs = null;
167:
168: if ((szServiceName == null) || (szServiceName.length() == 0))
169: return null;
170:
171: try {
172: ServiceSchemaManager schemaMgr = serviceManager
173: .getSchemaManager(szServiceName, IS_SVC_VERSION);
174: ServiceSchema schema = schemaMgr.getGlobalSchema();
175: attrs = schema.getAttributeDefaults();
176: schema = null;
177: } catch (SMSException smse) {
178: smse.printStackTrace();
179: } catch (SSOException ssoe) {
180: ssoe.printStackTrace();
181: }
182: return attrs;
183: }
184:
185: /*
186: * Obtains the organizational attribute's value,
187: * given the service name and attribute name.
188: *
189: * This is done by verifying if the service has a template.
190: * If the template exists, then the attribute value is obtained
191: * using the service template. If the template does not exist,
192: * then attribute is obtained through the schema.
193: *
194: * @param szServiceName java.lang.String - name of the service
195: * @param szAttributeName java.lang.String - attribute name
196: * @param szDefaultVal java.lang.String - default value of attribute
197: *
198: * @return String - org attribute value. If the attribute has more
199: * than one value, then first value is returned.
200: */
201: public String getOrganizationAttribute(String szServiceName,
202: String szAttributeName, String szDefaultVal) {
203:
204: String orgDN = null;
205: String val = szDefaultVal;
206: String value = szDefaultVal;
207: try {
208: orgDN = user.getOrganizationDN();
209: if (orgDN == null)
210: return null;
211:
212: AMOrganization org = connection.getOrganization(orgDN);
213:
214: if (org.orgTemplateExists(szServiceName)) {
215:
216: AMTemplate temp = org.getTemplate(szServiceName,
217: AMTemplate.ORGANIZATION_TEMPLATE);
218: //val = temp.getStringAttribute(szAttributeName);
219: Set vals = temp.getAttribute(szAttributeName);
220:
221: if (vals != null && vals.size() > 0) {
222: Iterator iter = vals.iterator();
223: value = (String) iter.next();
224: }
225: /* Commenting it out as it was printing out the password */
226: //debug.message(szAttributeName + " Org Attribute obtained through template is " + value);
227: } else {
228:
229: ServiceSchemaManager schemaMgr = serviceManager
230: .getSchemaManager(szServiceName, IS_SVC_VERSION);
231: ServiceSchema schema = schemaMgr
232: .getOrganizationSchema();
233:
234: Map mapVals = schema.getAttributeDefaults();
235:
236: NetFileAttributeExtractor nfAttrExtr = new NetFileAttributeExtractor(
237: mapVals);
238:
239: String data = nfAttrExtr.getString(szAttributeName,
240: szDefaultVal);
241:
242: if ((data == null) || (data.equals(""))) {
243: if (applyDefault)
244: return szDefaultVal;
245: } else {
246: // logger.info(szAttributeName + " Org Attribute obtained thru schema is " + data);
247: Object[] params0 = {
248: " Org Attribute obtained thru schema is ",
249: data };
250: logger
251: .log(Level.INFO, "PSSRNF_CSPNSJ1093",
252: params0);
253: return data;
254: }
255: }
256: } catch (AMException ame) {
257: // logger.severe("AMException in getting Org Attribute " + ame);
258: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1094", ame);
259: ame.printStackTrace();
260: } catch (SSOException ssoe) {
261: // logger.severe("SSOException in getting Org Attribute " + ssoe);
262: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1095", ssoe);
263: ssoe.printStackTrace();
264: } catch (Exception e) {
265: // logger.severe("Exception in getting Org Attribute " + e);
266: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1096", e);
267: e.printStackTrace();
268: }
269: val = value;
270: return val;
271: }
272:
273: public java.util.List getOrganizationAttributeValues(
274: String serviceName, String attributeName) {
275:
276: String orgDN = null;
277: ArrayList data = null;
278:
279: try {
280: orgDN = user.getOrganizationDN();
281:
282: if (orgDN == null)
283: return null;
284:
285: AMOrganization org = connection.getOrganization(orgDN);
286:
287: if (org.orgTemplateExists(serviceName)) {
288: AMTemplate template = org.getTemplate(serviceName,
289: AMTemplate.ORGANIZATION_TEMPLATE);
290: Set values = template.getAttribute(attributeName);
291: if ((values != null) && (values.size() > 0)) {
292: data = new ArrayList();
293: Iterator iter = values.iterator();
294: while (iter.hasNext()) {
295: data.add(iter.next());
296: }
297: }
298: } else {
299: ServiceSchemaManager schemaMgr = serviceManager
300: .getSchemaManager(serviceName, IS_SVC_VERSION);
301: ServiceSchema schema = schemaMgr
302: .getOrganizationSchema();
303:
304: Map mapVals = schema.getAttributeDefaults();
305:
306: Set keys = mapVals.keySet();
307:
308: for (Iterator i = keys.iterator(); i.hasNext();) {
309: String attrib = (String) i.next();
310: if (attrib.equalsIgnoreCase(attributeName)) {
311:
312: Set values = (Set) mapVals.get(attrib);
313: if (values != null) {
314: data = new ArrayList();
315: Object[] obj = values.toArray();
316:
317: for (int index = 0; index < obj.length; ++index) {
318: data.add(obj[index]);
319: }
320: break;
321: }
322: }
323: }
324: }
325: } catch (AMException ame) {
326: ame.printStackTrace();
327: } catch (SSOException ssoe) {
328: ssoe.printStackTrace();
329: } catch (Exception e) {
330: e.printStackTrace();
331: }
332: return data;
333: }
334:
335: /*
336: * Public API
337: */
338: public Map[] getPreferences() {
339:
340: try {
341: return getPSPreferences();
342: } catch (Exception e) {
343: // logger.log(Level.SEVERE, "Error in getting preferences", e);
344: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1097");
345: }
346: return null;
347: }
348:
349: public boolean savePreferences(Map prefs) throws Exception {
350: try {
351: user.setAttributes(prefs);
352: } catch (AMException pe) {
353: // logger.log(Level.SEVERE, "Preferences not saved", pe);
354: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1098");
355: return false;
356: } catch (SSOException pe) {
357: // logger.log(Level.SEVERE, "Preferences not saved", pe);
358: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1099");
359: return false;
360: } catch (Exception e) {
361: // logger.log(Level.SEVERE, "Preferences not saved", e);
362: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1100");
363: return false;
364: }
365:
366: try {
367: user.store();
368: return true;
369: } catch (Exception e) {
370: // logger.log(Level.SEVERE, "Preferences not saved", e);
371: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1101");
372: }
373: return false;
374: }
375:
376: /*
377: * Detemines if user has the policy assigned.
378: *
379: * return boolean - true, if policy is assigned, false otherwise.
380: */
381: public boolean isPolicyAssigned(SSOToken ssoToken)
382: throws AMException, SSOException {
383: boolean allowed = true;
384: // try {
385: // PolicyEvaluator policyEval = new PolicyEvaluator(
386: // SRAP_NETFILE_SERVICE);
387: // allowed = policyEval.isAllowed(
388: // ssoToken,
389: // "",
390: // "sunPortalNetFileExecute",
391: // Collections.EMPTY_MAP);
392: // if (!allowed) {
393: // writeDebug(
394: // "NetFileContext.isExecutable: Not allowed to execute NetFile.",
395: // null);
396: // }
397: // } catch (Exception ex) {
398: // writeErrorDebug("Exception in evaluating policy ", ex);
399: // return allowed;
400: // }
401: return allowed;
402: }
403:
404: /*
405: * Detemines if user has the service assigned.
406: *
407: * return boolean - true, if service is assigned, false otherwise.
408: */
409: public boolean isServiceAssigned(String szServiceName)
410: throws AMException, SSOException {
411:
412: Set vals = null;
413:
414: vals = user.getAssignedServices();
415:
416: if ((vals == null) || (vals.isEmpty())) {
417: writeDebug(
418: "NetFile service has not been assigned for the user",
419: null);
420: return false;
421: }
422:
423: Iterator iter = vals.iterator();
424:
425: while (iter.hasNext()) {
426: String szAServiceName = (String) iter.next();
427: if (szAServiceName.equals(szServiceName))
428: return true;
429: }
430:
431: return false;
432: }
433:
434: /*
435: * Detemines if user has the service and the policy assigned.
436: *
437: * return boolean - true if both policy and service are assigned and
438: * false for anything else.
439: */
440: public boolean isExecutable(SSOToken token) {
441: try {
442: return (isPolicyAssigned(token) && isServiceAssigned(SRAP_NETFILE_SERVICE));
443: } catch (Exception e) {
444: e.printStackTrace();
445: }
446: return false;
447: }
448:
449: public boolean isSessionValid(SSOToken token) {
450: try {
451: if (token == null)
452: return false;
453: SSOTokenManager manager = SSOTokenManager.getInstance();
454: if (!manager.isValidToken(token)) {
455: return false;
456: }
457: } catch (Exception e) {
458: return false;
459: }
460: return true;
461: }
462:
463: public Locale getUserLocale() {
464: Locale locale = null;
465: try {
466: Map data = getServiceAttributes(DSAME_USER_SERVICE);
467:
468: NetFileAttributeExtractor nfAttr = new NetFileAttributeExtractor(
469: data);
470:
471: String locstr = nfAttr
472: .getString("preferredlocale", "en_US");
473: StringTokenizer locst = new StringTokenizer(locstr, "_");
474:
475: if (locst.countTokens() > 0) {
476:
477: String lang = (locst.hasMoreTokens() ? locst
478: .nextToken() : "");
479: String country = (locst.hasMoreTokens() ? locst
480: .nextToken() : "");
481: String variant = (locst.hasMoreTokens() ? locst
482: .nextToken() : "");
483:
484: while (locst.hasMoreTokens()) {
485: variant += "_" + locst.nextToken();
486: }
487:
488: locale = new java.util.Locale(lang, country, variant);
489: } else {
490: locale = new java.util.Locale("en", "US");
491: }
492: userLocale = locale;
493: // logger.info("Locale is "+userLocale.toString());
494: Object[] params9 = { userLocale.toString() };
495: logger.log(Level.INFO, "PSSRNF_CSPNSJ1102", params9);
496: // logger.info(userLocale.toString());
497: logger.info("PSSRNF_CSPNSJ1103");
498: } catch (Exception e) {
499: e.printStackTrace();
500: // logger.log(Level.SEVERE, "UserLocale Error is ",e);
501: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1104");
502: return null;
503: }
504: return locale;
505: }
506:
507: public void determineHTMLCharset(HttpServletRequest req) {
508:
509: AMClientDetector amCD = new AMClientDetector();
510: String clientType = amCD.getClientType(req);
511:
512: try {
513: Client clientObj = Client.getInstance(clientType);
514: HTMLCharset = clientObj.getCharset(getUserLocale());
515: } catch (ClientException ce) {
516: ce.printStackTrace();
517: }
518: // logger.info("HTML Charset is " + HTMLCharset);
519: Object[] params12 = { HTMLCharset };
520: logger.log(Level.INFO, "PSSRNF_CSPNSJ1105", params12);
521: amCD = null;
522: clientType = null;
523: }
524:
525: /*
526: * Returns the HTML character set.
527: *
528: * If called after invoking determineHTMLCharset() will result
529: * in HTML character set based on user's locale. If invoked
530: * without invoking determineHTMLCharset(), then will return the
531: * default value of this variable.
532: */
533: public String getHTMLCharset() {
534: return HTMLCharset;
535: }
536:
537: /*
538: * This fetches the mail attributes and appends them into a StringBuffer.
539: * Finally, returns a String. This String is then sent to the applet
540: *
541: * This implementation could be slow. However, is more flexible to change.
542: */
543: public String getMailPreferences() {
544:
545: String fromAddr = "";
546: String replyToAddr = "";
547: String smtpSrvr = "";
548: String defaultMailDom = "";
549:
550: StringBuffer sb = new StringBuffer();
551:
552: Map mailData = getServiceAttributes(IPS_SSO_ADAPTER_SERVICE);
553:
554: if ((mailData == null) || (mailData.isEmpty())) {
555: writeDebug(
556: "Mail attribute could not be obtained. "
557: + "NetMail service might not have been assigned to the user",
558: null);
559: return "";
560: }
561: java.util.HashSet adapConfig = (java.util.HashSet) mailData
562: .get(IPS_SSO_ADAPTER_CONFIG);
563: Iterator it = adapConfig.iterator();
564: String prStr = null, result = null;
565:
566: while (it.hasNext()) {
567: prStr = (String) it.next();
568: result = getAttributeValue(prStr, CONFIG_NAME);
569: if (result.equals(SUN_ONE_MAIL)) {
570: java.util.StringTokenizer st = new java.util.StringTokenizer(
571: prStr, ":/");
572: fromAddr = getNextToken(st);
573: fromAddr = getNextToken(st);
574: smtpSrvr = getAttributeValue(prStr, SMTP_SERVER);
575: defaultMailDom = getAttributeValue(prStr, DOMAIN);
576: break;
577: }
578: }
579:
580: //Write the from, replyto and default mail domain to the StringBuffer
581: if (fromAddr == null) {
582: sb.append("sunPortalUserFromAddress").append("=")
583: .append("").append("\n");
584: } else {
585: sb.append("sunPortalUserFromAddress").append("=").append(
586: fromAddr).append("\n");
587: }
588:
589: if (replyToAddr == null) {
590: if (fromAddr == null)
591: sb.append("sunPortalUserReplyToAddress").append("=")
592: .append("").append("\n");
593: else
594: sb.append("sunPortalUserReplyToAddress").append("=")
595: .append(fromAddr).append("\n");
596: } else {
597: if (replyToAddr.length() == 0) {
598: if (fromAddr != null)
599: sb.append("sunPortalUserReplyToAddress")
600: .append("=").append(fromAddr).append("\n");
601: } else {
602: sb.append("sunPortalUserReplyToAddress").append("=")
603: .append(replyToAddr).append("\n");
604: }
605: }
606:
607: sb.append("mailserver").append("=").append(defaultMailDom)
608: .append("\n");
609:
610: return sb.toString();
611: }
612:
613: private String getAttributeValue(String sourceStr, String attribute) {
614: java.util.StringTokenizer st = new java.util.StringTokenizer(
615: sourceStr, "?=&");
616: String token = null;
617: String value = "";
618: while (st.hasMoreTokens()) {
619: token = st.nextToken();
620: if (token.equalsIgnoreCase(attribute)) {
621: value = st.nextToken();
622: break;
623: }
624: }
625: return value;
626: }
627:
628: private String getNextToken(java.util.StringTokenizer st) {
629: if (st.hasMoreTokens()) {
630: return st.nextToken();
631: }
632: return "";
633: }
634:
635: /*
636: * Same as the previous method.
637: * Returns a Hashtable instead of a String
638: */
639: public Hashtable getMailAttributesAsCollection() {
640:
641: Hashtable htMailData = new Hashtable();
642: String fromAddr = "";
643: String replyToAddr = "";
644: //String imapSrvr = "";
645: String smtpSrvr = "";
646: //String imapUserId = "";
647: String defaultMailDom = "";
648:
649: Map mailData = getServiceAttributes(IPS_SSO_ADAPTER_SERVICE);
650:
651: if ((mailData == null) || (mailData.isEmpty())) {
652: writeDebug("Could not obtain mail attributes", null);
653: return null;
654: }
655:
656: java.util.HashSet adapConfig = (java.util.HashSet) mailData
657: .get(IPS_SSO_ADAPTER_CONFIG);
658: Iterator it = adapConfig.iterator();
659: String prStr = null, result = null;
660:
661: while (it.hasNext()) {
662: prStr = (String) it.next();
663: result = getAttributeValue(prStr, CONFIG_NAME);
664: if (result.equals(SUN_ONE_MAIL)) {
665: java.util.StringTokenizer st = new java.util.StringTokenizer(
666: prStr, ":/");
667: fromAddr = getNextToken(st);
668: fromAddr = getNextToken(st);
669: smtpSrvr = getAttributeValue(prStr, SMTP_SERVER);
670: defaultMailDom = getAttributeValue(prStr, DOMAIN);
671: break;
672: }
673: }
674: htMailData.put("sunPortalUserReplyToAddress", fromAddr);
675: htMailData.put("sunPortalUserReplyToAddress", fromAddr);
676: htMailData.put("sunPortalUserFromAddress", fromAddr);
677: htMailData.put("mailserver", smtpSrvr);
678: return htMailData;
679: }
680:
681: protected void writeDebug(String szMsg, Exception e) {
682: if (e != null) {
683: // logger.log(Level.INFO, szMsg, e);
684: logger.log(Level.INFO, "PSSRNF_CSPNSJ1106");
685: } else {
686: // logger.info(szMsg);
687: logger.info("PSSRNF_CSPNSJ1107");
688: }
689: }
690:
691: protected void writeErrorDebug(String szError, Exception e) {
692: if (e != null)
693: // logger.log(Level.SEVERE, szError, e);
694: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1108");
695: else
696: // logger.severe(szError);
697: logger.severe("PSSRNF_CSPNSJ1109");
698: }
699:
700: public Map getUserPreferences() throws Exception {
701: throw new java.lang.UnsupportedOperationException(
702: "Method not implemented");
703: }
704:
705: }
|