001: /**
002: * $Id: MAJspABAppHelper.java,v 1.14 2005/09/21 10:48:18 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 iPlanet
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.wireless.providers.ab;
014:
015: import java.net.URL;
016: import java.net.URLEncoder;
017: import java.net.MalformedURLException;
018: import java.util.*;
019: import java.util.logging.Level;
020: import java.util.logging.Logger;
021: import com.sun.portal.log.common.PortalLogger;
022:
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025:
026: import com.iplanet.sso.*;
027: import com.iplanet.am.sdk.AMStoreConnection;
028: import com.iplanet.am.sdk.AMUser;
029:
030: import com.sun.ssoadapter.SSOAdapter;
031: import com.sun.ssoadapter.SSOAdapterSession;
032: import com.sun.ssoadapter.config.*;
033: import com.sun.portal.providers.ab.*;
034: import com.sun.portal.providers.context.ProviderContext;
035: import com.sun.portal.providers.context.ProviderContextException;
036: import com.sun.portal.providers.ProviderEditUtility;
037:
038: import com.sun.addressbook.*;
039:
040: import com.sun.portal.wireless.taglibs.dispatcher.*;
041: import com.sun.mobile.util.*;
042: import com.sun.portal.wireless.util.*;
043: import com.sun.portal.wireless.providers.util.ProviderUtility;
044:
045: /**
046: * This class implements the AddressBookApplicationHelper interface.
047: * <p>This AddressBookApplicationHelper implementation consumes the following
048: * Configuration properties:
049: * <ul>
050: * </ul>
051: *
052: * @version 1.0
053: *
054: */
055:
056: public class MAJspABAppHelper implements AddressBookApplicationHelper {
057:
058: public SSOAdapter ssoAdapter = null;
059: public SSOToken ssoToken = null;
060: public Properties adapterProperties = null;
061: private AddressBookProvider provider = null;
062: protected String appName = "";
063: private String editContainer = null;
064: private String container = null;
065: private ConfigurationFactory appConfigFactory = null;
066: private Configuration appConfig = null;
067:
068: private static Logger logger = PortalLogger
069: .getLogger(MAJspABAppHelper.class);
070:
071: private static final String sortOrderAscending = "asc";
072: private static final String sortOrderDescending = "dsc";
073: private static final String sortOrderNone = "none";
074:
075: /**
076: * Initialize MAMailAppHelper.
077: *
078: * @param ssoAdapter SSOAdapter object
079: */
080: public void init(AddressBookProvider provider, SSOAdapter ssoAdapter) {
081: this .provider = provider;
082: this .ssoAdapter = ssoAdapter;
083: try {
084: this .ssoToken = ssoAdapter.getSSOToken();
085: } catch (Exception e) {
086: }
087: this .adapterProperties = ssoAdapter.getProperties();
088: appConfigFactory = ConfigurationFactory.getInstance(
089: AppConfigConstants.AB_APP_SERVICE_NAME,
090: AppConfigConstants.AB_APP_ATTR_CONFIGURATIONS_NAME,
091: AppConfigConstants.AB_APP_ATTR_TEMPLATES_NAME);
092: }
093:
094: /**
095: * Construct state that is required to process prefs editing requests...
096: */
097: private void prefsReqInit(HttpServletRequest req) throws Exception {
098: if (editContainer == null) {
099: editContainer = req.getParameter("provider");
100: }
101:
102: if (container == null) {
103: container = req.getParameter("containerName");
104: }
105:
106: if (appConfig == null) {
107:
108: //
109: // Verify user has been assigned to the proper service...
110: //
111: AMStoreConnection amConn = new AMStoreConnection(ssoToken);
112: AMUser amUser = amConn.getUser(ssoToken.getPrincipal()
113: .toString());
114: Set assignedServices = amUser.getAssignedServices();
115: Iterator svcIter = assignedServices.iterator();
116: boolean found = false;
117: while (svcIter.hasNext()) {
118: String svcName = (String) svcIter.next();
119: if (svcName
120: .equalsIgnoreCase(AppConfigConstants.AB_APP_SERVICE_NAME)) {
121: found = true;
122: break;
123: }
124: }
125: if (!found) {
126: throw new Exception(
127: AppConfigConstants.AB_APP_SERVICE_NAME
128: + " service not assigned to user");
129: }
130:
131: //
132: // Load/init app prefs...
133: //
134: String configName = ssoAdapter.getName();
135: String nameSuffix = "_" + provider.getName();
136: int i = configName.indexOf(nameSuffix);
137: if (i < 0) {
138: i = configName.length();
139: configName = configName + nameSuffix;
140: }
141:
142: appConfig = appConfigFactory.readConfiguration(configName,
143: true, new SSOAdapterSession(req));
144:
145: if (appConfig == null) {
146: //
147: // If not defined, try getting Dynamic version using naming
148: // convention established by Comm Channels...
149: //
150: appConfig = appConfigFactory.readConfiguration(
151: configName.substring(0, i), true,
152: new SSOAdapterSession(req));
153: if (appConfig != null) {
154: appConfig.setConfigurationName(configName);
155: }
156:
157: if (appConfig == null) {
158: appConfig = new Configuration();
159: appConfig.setConfigurationName(configName);
160: appConfig
161: .setConfigurationDescription(AppConfigConstants.AB_APP_TEMPLATE_NAME);
162: }
163: }
164: }
165: }
166:
167: /**
168: * Returns a client URL, if supported by the service.
169: */
170: public String getStartURL(AddressBookProvider provider,
171: HttpServletRequest request) {
172: return getApplicationURL(provider, request);
173: }
174:
175: /**
176: */
177: private String getApplicationURL(AddressBookProvider provider,
178: HttpServletRequest request) {
179: String appURL = null;
180: SSOToken token;
181:
182: try {
183: SSOTokenManager tokenManager = SSOTokenManager
184: .getInstance();
185: token = tokenManager.createSSOToken(request);
186:
187: appURL = "/jsp/default/launchAB.jsp"
188: + "?ai="
189: + ContentUtils.rfc2396Escape("", ssoAdapter
190: .getName());
191:
192: Compressor compressor = Compressor.getCompressor(token);
193:
194: appURL = request.getContextPath()
195: + compressor.compressURL(appURL);
196:
197: appURL = token.encodeURL(appURL);
198:
199: } catch (Exception e) {
200: return null;
201: }
202:
203: return appURL;
204: }
205:
206: public StringBuffer getAppPrefsEdit(AddressBookProvider aprov,
207: HttpServletRequest req, HttpServletResponse res) {
208: Hashtable tagTable = new Hashtable();
209: ProviderContext pc = aprov.getProviderContext();
210:
211: try {
212:
213: prefsReqInit(req);
214:
215: //Put theme attrs
216: try {
217: ProviderUtility.setDefaultPresentation(provider
218: .getName(), provider.getProviderContext(),
219: tagTable);
220: } catch (ProviderContextException pce) {
221: if (logger.isLoggable(Level.INFO))
222: logger.log(Level.INFO, "", pce);
223: }
224:
225: String sortBy = appConfig.getProperty("sortBy");
226: if ((sortBy == null) || (sortBy.equals(""))) {
227: tagTable.put("isNoSorting", "selected");
228: tagTable.put("isCnSelected", "");
229: } else {
230: tagTable.put("isNoSorting", "");
231:
232: if (sortBy.equals(ABFilter.CN)) {
233: tagTable.put("isCnSelected", "selected");
234: } else {
235: // No Sorting is the default sorting specified
236: tagTable.put("isCnSelected", "");
237: tagTable.put("isNoSorting", "selected");
238: }
239: }
240:
241: String sortOrder = appConfig.getProperty("sortOrder");
242: if (sortOrderNone.equals(sortOrder)) {
243: tagTable.put("isUpSelected", "");
244: tagTable.put("isDownSelected", "");
245: tagTable.put("isNoSorting", "selected");
246: } else {
247: tagTable.put("isNoSorting", "");
248:
249: if (sortOrderDescending.equals(sortOrder)) {
250: tagTable.put("isDownSelected", "selected");
251: tagTable.put("isUpSelected", "");
252: } else {
253: tagTable.put("isUpSelected", "selected");
254: tagTable.put("isDownSelected", "");
255: }
256: }
257:
258: tagTable.put("isAppHandler", this .getName());
259:
260: return (aprov.getTemplate("ma-edit.template", tagTable));
261:
262: } catch (Exception e) {
263: if (logger.isLoggable(Level.INFO))
264: logger.log(Level.INFO, "", e);
265: return null;
266: }
267:
268: }
269:
270: public URL processAppPrefsEdit(AddressBookProvider aprov,
271: HttpServletRequest req, HttpServletResponse res) {
272: URL url = null;
273:
274: try {
275: String desktopURL = aprov.getProviderContext()
276: .getDesktopURL(req);
277: desktopURL += "?action=edit";
278: desktopURL += "&provider="
279: + URLEncoder.encode(editContainer);
280: desktopURL += "&targetprovider=" + aprov.getName();
281: desktopURL += "&containerName="
282: + URLEncoder.encode(container);
283: url = new URL(desktopURL);
284:
285: String val;
286:
287: String sortBy = req.getParameter("sortBy");
288: if (sortBy != null) {
289: if (sortBy.equals("Commonname")) {
290: val = ABFilter.CN;
291: } else {
292: val = "";
293: }
294: appConfig.setProperty("sortBy", val);
295: }
296:
297: String sortOrder = req.getParameter("sortOrder");
298: if (sortOrder != null) {
299: if (sortOrder.equals("Down")) {
300: val = sortOrderDescending;
301: } else if (sortOrder.equals("Up")) {
302: val = sortOrderAscending;
303: } else {
304: val = sortOrderNone;
305: }
306: appConfig.setProperty("sortOrder", val);
307: }
308:
309: if (sortOrder != null || sortBy != null) {
310: appConfigFactory.writeConfiguration(appConfig,
311: new SSOAdapterSession(req));
312: }
313:
314: } catch (Exception e) {
315: //TODO: ???
316: return null;
317: }
318:
319: return url;
320: }
321:
322: /**
323: * Returns a HTML link containing encoding specific for the app helper
324: */
325:
326: public String getAppHelperEditLink(HttpServletRequest req,
327: ProviderContext pc) {
328:
329: try {
330: prefsReqInit(req);
331: } catch (Exception e) {
332: return "";
333: }
334:
335: Hashtable tagTable = new Hashtable();
336: StringBuffer link = new StringBuffer();
337: try {
338: link.append(pc.getDesktopURL(req));
339: } catch (Exception e) {
340: return "";
341: }
342: link.append("?action=edit&provider=");
343: link.append(ProviderEditUtility.getRequestParameter("provider",
344: req));
345: link.append("&targetprovider=" + provider.getName());
346: link.append("&containerName=");
347: link.append(ProviderEditUtility.getRequestParameter(
348: "containerName", req));
349: link.append("&appPref=" + this .getName());
350:
351: //Put theme attrs
352: try {
353: ProviderUtility.setDefaultPresentation(provider.getName(),
354: provider.getProviderContext(), tagTable);
355: } catch (ProviderContextException pce) {
356: if (logger.isLoggable(Level.INFO))
357: logger.log(Level.INFO, "", pce);
358: }
359:
360: tagTable.put("editLink", link.toString());
361:
362: String content = null;
363:
364: try {
365: String fontTag = pc.getStringProperty(provider.getName(),
366: "fontFace1", "Sans-serif");
367: tagTable.put("iwtDesktop-fontFace1", fontTag);
368: content = pc.getTemplate(provider.getName(),
369: "ma-edit-link.template", tagTable).toString();
370: if (logger.isLoggable(Level.FINEST)) {
371: String[] param = { "content", content.toString() };
372: logger.log(Level.FINEST, "PSMA_CSPWPA0001", param);
373: }
374:
375: } catch (Exception e) {
376: if (logger.isLoggable(Level.INFO))
377: logger.log(Level.INFO, "PSMA_CSPWPA0002", e);
378: content = "";
379: }
380:
381: if (logger.isLoggable(Level.FINEST))
382: logger.log(Level.FINEST, "PSMA_CSPWPC0003", content);
383:
384: return content;
385: }
386:
387: /**
388: * Sets the App Helpers name
389: */
390: public void setName(String name) {
391: this .appName = name;
392: }
393:
394: /**
395: * Gets the App Helpers name
396: */
397: public String getName() {
398: return this.appName;
399: }
400:
401: }
|