001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.providers.userinfo;
007:
008: import java.net.URL;
009: import java.net.URLEncoder;
010:
011: import java.util.Hashtable;
012: import java.util.Enumeration;
013: import java.util.ResourceBundle;
014: import java.util.Locale;
015: import java.util.Set;
016: import java.util.List;
017: import java.util.logging.Logger;
018: import java.util.logging.Level;
019: import java.util.logging.LogRecord;
020:
021: import javax.servlet.http.HttpServletRequest;
022: import javax.servlet.http.HttpServletResponse;
023:
024: import com.iplanet.sso.SSOToken;
025: import com.iplanet.sso.SSOException;
026: import com.iplanet.sso.SSOTokenManager;
027: import com.iplanet.am.sdk.AMStoreConnection;
028: import com.iplanet.am.sdk.AMException;
029: import com.iplanet.am.sdk.AMUser;
030:
031: import com.sun.portal.providers.util.ProviderProperties;
032:
033: import com.sun.portal.providers.context.ProviderContext;
034: import com.sun.portal.providers.ProviderException;
035: import com.sun.portal.providers.ProfileProviderAdapter;
036: import com.sun.portal.providers.InvalidEditFormDataException;
037:
038: import com.sun.portal.desktop.util.I18n;
039: import com.sun.portal.desktop.util.IURLEncoder;
040: import com.sun.portal.desktop.util.Integers;
041: import com.sun.portal.desktop.DesktopException;
042: import com.sun.portal.desktop.context.ContextException;
043: import com.sun.portal.providers.userinfo.tag.ReadTag;
044: import com.sun.portal.providers.userinfo.tag.WriteTag;
045: import com.sun.portal.providers.userinfo.tag.TagException;
046: import com.sun.portal.providers.userinfo.tag.TagWrapperException;
047: import com.sun.portal.providers.userinfo.tag.UndefinedTagException;
048: import com.sun.portal.providers.userinfo.tag.TagModuleManager;
049: import com.sun.portal.log.common.PortalLogger;
050:
051: public class UserInfoProvider extends ProfileProviderAdapter implements
052: ProviderProperties {
053:
054: private static Logger logger = PortalLogger
055: .getLogger(UserInfoProvider.class);
056: private static final String contentTemplate = "content.template";
057: private static final String editTemplate = "edit.template";
058: private TagModuleManager moduleManager = null;
059: private ResourceBundle bundle = null;
060: private String editContainer = null;
061: private String container = null;
062:
063: /* TODO: when isAuthless()#4785678 is available client information can be cached for authenticaed users
064: */
065:
066: public void init(String n, HttpServletRequest req)
067: throws ProviderException {
068: super .init(n, req);
069:
070: try {
071: moduleManager = new TagModuleManager(getName(),
072: getProviderContext(), req);
073: } catch (TagException te) {
074: throw new ProviderException(
075: "UserInfoProvider.init(): couldn't get tag module manager",
076: te);
077: }
078:
079: if (moduleManager == null) {
080: throw new ProviderException(
081: "UserInfoProvider.init(): tag module manager failed it init");
082: }
083: }
084:
085: /**
086: * Determine if the provider is presentable
087: *
088: */
089: public boolean isPresentable(HttpServletRequest request) {
090: boolean isPresentable = false;
091: try {
092: if (getTemplatePath(contentTemplate) != null) {
093: isPresentable = true;
094: }
095: } catch (ProviderException pe) {
096: // Probable cause, channel/provider definition not found in dp
097: // log a warning and return false
098: ProviderContext pc = getProviderContext();
099: logger.log(Level.INFO, "PSCR_CSPPUI0002", pe);
100: }
101: return isPresentable;
102: }
103:
104: public StringBuffer getContent(HttpServletRequest req,
105: HttpServletResponse res) throws ProviderException {
106:
107: Hashtable tagTable = moduleManager.getTable();
108:
109: return getTemplate(contentTemplate, tagTable);
110: }
111:
112: /**
113: * Only html based clients are editable
114: *
115: * @return boolean
116: */
117: public boolean isEditable() throws ProviderException {
118: boolean isEditable = false;
119: if ((getTemplatePath(editTemplate) != null)
120: && getBooleanProperty("isEditable")) {
121: isEditable = true;
122: }
123: return isEditable;
124: }
125:
126: public StringBuffer getEdit(HttpServletRequest req,
127: HttpServletResponse res) throws ProviderException {
128:
129: // Used while constructing the next url
130: editContainer = req.getParameter("provider");
131: container = req.getParameter("containerName");
132:
133: Hashtable tagTable = moduleManager.getTable();
134:
135: SSOTokenManager tokenManager = null;
136: SSOToken token = null;
137: AMStoreConnection connection = null;
138: AMUser user = null;
139:
140: try {
141: tokenManager = SSOTokenManager.getInstance();
142:
143: if (tokenManager == null) {
144: throw new ProviderException(
145: "UserinfoProvider.getEdit(): "
146: + "Failed to getSSOTokenmanager.");
147: }
148:
149: token = tokenManager.createSSOToken(req);
150: connection = new AMStoreConnection(token);
151: user = connection.getUser(token.getPrincipal().getName());
152:
153: } catch (SSOException ssoe) {
154: throw new ProviderException("UserinfoProvider.getEdit():",
155: ssoe);
156: }
157:
158: //
159: // check and see if we know how to handle passwords for the
160: // user's auth type.
161: //
162: String authMethod = null;
163: try {
164: authMethod = token.getAuthType();
165: logger.log(Level.FINEST, "PSCR_CSPPUI0005", authMethod);
166: } catch (SSOException ssoe) {
167: logger.log(Level.INFO, "PSCR_CSPPUI0006", ssoe);
168: authMethod = "<unknown>";
169: }
170:
171: List authTypes = getListProperty("authTypes");
172: if (authTypes.contains(authMethod)) {
173: logger.log(Level.FINEST, "PSCR_CSPPUI0007");
174:
175: //
176: // we have a handler, include respective password template
177: //
178: tagTable.put("passwordHandler",
179: getTemplate("passwordHandler-" + authMethod
180: + ".template"));
181: } else {
182: //
183: // this is not an error, it just means there's no pw handler defined
184: // for their auth type. they can't change their login pw.
185: //
186: logger.log(Level.FINER, "PSCR_CSPPUI0008");
187:
188: tagTable.put("passwordHandler", "");
189: }
190:
191: // error tags (if any), needed for template based channel
192: String err = req.getParameter("error");
193:
194: if (err != null) {
195: tagTable.put(ERR_MESSAGE, err);
196: tagTable.put("inlineError",
197: getTemplate("inlineError.template"));
198: } else {
199: // no error, put in dummy tags so lookup doesn't fail
200: tagTable.put(ERR_MESSAGE, "");
201: tagTable.put(INLINE_ERROR, "");
202: }
203:
204: return getTemplate(editTemplate, tagTable);
205:
206: }
207:
208: /**
209: * create a dummy password, the same length as the string passed
210: * in but made of stars
211: */
212:
213: public URL processEdit(HttpServletRequest req,
214: HttpServletResponse res) throws ProviderException {
215: URL next = null;
216: boolean isPasswordSet = true;
217: String msg = "";
218:
219: editContainer = req.getParameter("provider");
220: container = req.getParameter("containerName");
221:
222: getProviderContext().allContentChanged();
223:
224: for (Enumeration e = req.getParameterNames(); e
225: .hasMoreElements();) {
226: String tag = (String) e.nextElement();
227: String val = req.getParameter(tag);
228:
229: if (val == null) {
230: val = "";
231: }
232: logger.log(Level.FINEST, "PSCR_CSPPUI0009", tag);
233: //
234: // get tag module for the key
235: //
236: WriteTag wt = null;
237: try {
238: wt = moduleManager.getWriteTag(tag);
239: } catch (UndefinedTagException ute) {
240: //
241: // this not not an error. it just means there was some parameter
242: // in the form we don't have a module to handle
243: //
244: logger.log(Level.FINER, "PSCR_CSPPUI0010", tag);
245:
246: continue;
247: } catch (TagException te) {
248: logger.log(Level.INFO, "PSCR_CSPPUI0011", te);
249: continue;
250: }
251:
252: try {
253: wt.set(tag, val);
254: } catch (PasswordTooShortException ptse) {
255: //
256: // error, re-throw so user sees
257: //
258: if (bundle == null) {
259: bundle = getResourceBundle();
260: }
261: String passwordTooShort = bundle
262: .getString("passwordTooShort");
263: Locale locale = null;
264:
265: locale = getProviderContext().getLocale();
266:
267: msg = I18n.format(passwordTooShort, Integers.get(ptse
268: .getRequiredLength()), locale);
269:
270: isPasswordSet = false;
271: } catch (OriginalPasswordDidntMatchException opdme) {
272: //
273: // error, re-throw so user sees
274: //
275: if (bundle == null) {
276: bundle = getResourceBundle();
277: }
278: msg = bundle.getString("originalPasswordDidntMatch");
279: isPasswordSet = false;
280: } catch (PasswordsDontMatchException pdnm) {
281: //
282: // error, re-throw so user sees
283: //
284: if (bundle == null) {
285: bundle = getResourceBundle();
286: }
287: msg = bundle.getString("passwordsDontMatch");
288: isPasswordSet = false;
289: } catch (PasswordException pe) {
290: //
291: // benign ... just means they didn't set the password
292: //
293: logger.log(Level.INFO, "PSCR_CSPPUI0011", pe);
294: continue;
295: } catch (TagException te) {
296: if (logger.isLoggable(Level.INFO)) {
297: LogRecord record = new LogRecord(Level.INFO,
298: "PSCR_CSPPUI0012");
299: record.setLoggerName(logger.getName());
300: record.setParameters(new Object[] { tag, val });
301: record.setThrown(te);
302: logger.log(record);
303: }
304: if (te instanceof TagWrapperException) {
305: TagWrapperException twe = (TagWrapperException) te;
306: Exception ex = twe.getWrappedException();
307: if (ex instanceof ContextException) {
308: if (bundle == null) {
309: bundle = getResourceBundle();
310: }
311: msg = bundle.getString("profileError");
312: throw new InvalidEditFormDataException(msg);
313: }
314: }
315: /*
316: The exception is not a ProfileException so just
317: do a generic message.
318: */
319: msg = "Could not set " + tag + " to " + val + ".";
320: throw new ProviderException(msg, te);
321: }
322: }
323:
324: if (!isPasswordSet) {
325: // Password not set, generate appropriate error message
326: StringBuffer nextURLString = new StringBuffer();
327: nextURLString.append(
328: getProviderContext().getDesktopURL(req)).append(
329: "?action=edit&provider=").append(
330: getProviderContext().encodeURLParameter(
331: editContainer)).append("&targetprovider=")
332: .append(
333: getProviderContext().encodeURLParameter(
334: getName())).append(
335: "&containerName=").append(
336: getProviderContext().encodeURLParameter(
337: container)).append("&error=")
338: .append(
339: getProviderContext()
340: .encodeURLParameter(msg));
341: try {
342: next = new URL(nextURLString.toString());
343: } catch (java.net.MalformedURLException e) {
344: }
345: }
346:
347: return next;
348: }
349: }
|