001: /**
002: * $Id: UtilContext.java,v 1.35 2006/08/05 13:42:46 ss150821 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: */package com.sun.portal.wireless.taglibs.util;
014:
015: import java.util.*;
016: import java.util.logging.Level;
017: import java.util.logging.LogRecord;
018: import java.util.logging.Logger;
019: import java.text.*;
020: import java.net.*;
021: import java.io.*;
022:
023: import javax.servlet.*;
024: import javax.servlet.http.*;
025: import javax.servlet.jsp.*;
026:
027: import com.sun.portal.wireless.util.*;
028: import com.sun.mobile.util.*;
029: import com.sun.ssoadapter.*;
030:
031: import com.iplanet.sso.*;
032: import com.iplanet.services.cdm.*;
033: import com.iplanet.am.util.*;
034:
035: import com.sun.ssoadapter.config.PSClientAwareContextFactory;
036: import com.sun.ssoadapter.config.ClientAwareUserContext;
037: import com.sun.ssoadapter.config.SAALException;
038:
039: import com.sun.portal.wireless.taglibs.base.*;
040: import com.sun.portal.wireless.taglibs.dispatcher.*;
041:
042: import com.sun.portal.desktop.encode.*;
043: import com.sun.portal.log.common.PortalLogger;
044: import com.sun.portal.desktop.context.DSAMEMultiPortalConstants;
045:
046: /**
047: * UtilContext - track state across requests.
048: *
049: * This class implements a simple context mechanism
050: * that may be used for tracking user state across
051: * multiple requests.
052: *
053: * @version 1.0
054: * @see ContextCache
055: */
056:
057: public class UtilContext extends Context {
058:
059: protected static final String CONTEXT_CLASS_NAME = "com.sun.portal.wireless.taglibs.util.UtilContext";
060:
061: protected static final String CONTEXT_CACHE_CLASS_NAME = "com.sun.portal.wireless.taglibs.util.UtilContextCache";
062:
063: protected static final String CONTEXT_TYPE = "Util";
064:
065: private String jspDocRoot;
066:
067: private static HashMap lookupCache = new HashMap(100);
068: private static int cacheDefeatCounter = 0;
069:
070: private Compressor compressor = null;
071:
072: private String filePath;
073: private String locale = null;
074: private String type = null;
075: private String charset;
076:
077: private boolean rfc2396 = true;
078: private boolean urlAmpEntity = true;
079: private String counterName = null;
080: private String escapeClassName = EncoderClassNames.ENCODER_XML;
081: private String parmDelimiter = null;
082:
083: private HashMap attributes = new HashMap();
084:
085: private static Logger logger = PortalLogger
086: .getLogger(UtilContext.class);
087:
088: /**
089: * Initialize a context when there is no session.
090: *
091: * @param request
092: * @exception Exception
093: */
094: public void init(HttpServletRequest request) throws Exception {
095: //
096: // Find the JSP's in the file and doc namespaces...
097: //
098: jspDocRoot = MAConfigProperties.get("ps.jsp.doc.root", "/jsp");
099:
100: AMClientDetector mapCD = new AMClientDetector();
101: String clientType = mapCD.getClientType(request);
102: Client clientObj = Client.getInstance(clientType);
103:
104: filePath = clientObj.getProperty("filePath");
105: String escapeClassN = clientObj.getProperty("encoderClassName");
106:
107: if (escapeClassN != null) {
108: escapeClassName = escapeClassN;
109: }
110: // at minimum, we must ALWAYS at least
111: // do that XML escaping.
112:
113: // Use system-wide default locale...
114: locale = SystemProperties.get("com.iplanet.am.locale", "en_US");
115:
116: // First try getting charset from client data...
117: if (charset == null) {
118: charset = clientObj.getCharset(com.iplanet.am.util.Locale
119: .getLocale(locale));
120: }
121:
122: // Then try getting it from Accept-Charset..
123: if (charset == null) {
124: String acceptCharSet = request.getHeader("Accept-Charset");
125: if (acceptCharSet != null) {
126: StringTokenizer st = new StringTokenizer(acceptCharSet,
127: ",");
128: try {
129: // Just take the 1st one???
130: if (st.hasMoreTokens()) {
131: charset = st.nextToken();
132: }
133: } catch (Exception e) {
134: }
135: }
136: }
137:
138: // Then give up...
139: if (charset == null) {
140: charset = "ISO-8859-1";
141: }
142:
143: charSetString = charset;
144:
145: }
146:
147: /**
148: * Initialize a context with the specified session.
149: *
150: * @param session Context belongs to this session.
151: * @exception Exception
152: */
153: public void init(HttpServletRequest request,
154: com.iplanet.sso.SSOToken session, SSOAdapter ssoAdapter)
155: throws Exception {
156: //
157: // Find the JSP's in the file and doc namespaces...
158: //
159: jspDocRoot = MAConfigProperties.get("ps.jsp.doc.root", "/jsp");
160:
161: try {
162: compressor = Compressor.getCompressor(session);
163: } catch (Exception e) {
164:
165: if (logger.isLoggable(Level.SEVERE)) {
166: LogRecord rec = new LogRecord(Level.SEVERE,
167: "PSMA_CSPWTU0002");
168: rec.setThrown(e);
169: String[] param = { "getCompressor" };
170: rec.setParameters(param);
171: rec.setLoggerName(logger.getName());
172: logger.log(rec);
173: }
174:
175: throw e;
176: }
177:
178: try {
179: super .init(request, session, ssoAdapter);
180: } catch (Exception e) {
181: if (logger.isLoggable(Level.SEVERE)) {
182: LogRecord rec = new LogRecord(Level.SEVERE,
183: "PSMA_CSPWTU0002");
184: rec.setThrown(e);
185: String[] param = { "init" };
186: rec.setParameters(param);
187: rec.setLoggerName(logger.getName());
188: logger.log(rec);
189: }
190: throw e;
191: }
192:
193: //
194: // Grab relevant profile bits...
195: //
196: try {
197: profileDerivedSetup(request);
198: } catch (Exception e) {
199: if (logger.isLoggable(Level.SEVERE)) {
200: LogRecord rec = new LogRecord(Level.SEVERE,
201: "PSMA_CSPWTU0002");
202: rec.setThrown(e);
203: String[] param = { "profileDerivedSetup" };
204: rec.setParameters(param);
205: rec.setLoggerName(logger.getName());
206: logger.log(rec);
207: }
208: throw e;
209: }
210: }
211:
212: /**
213: * Does profile derived setup...
214: */
215: private void profileDerivedSetup(HttpServletRequest request)
216: throws Exception {
217: filePath = null;
218: Client clientObj = null;
219:
220: try {
221: clientObj = Client.getInstance(getClientType());
222: filePath = clientObj.getProperty("filePath");
223: } catch (Exception ee) {
224: if (logger.isLoggable(Level.SEVERE)) {
225: LogRecord rec = new LogRecord(Level.SEVERE,
226: "PSMA_CSPWTU0002");
227: rec.setThrown(ee);
228: String[] param = { "getProperty (filepath)" };
229: rec.setParameters(param);
230: rec.setLoggerName(logger.getName());
231: logger.log(rec);
232: }
233: }
234:
235: String escapeClassN = null;
236: try {
237: escapeClassN = clientObj.getProperty("encoderClassName");
238: if (escapeClassN != null) {
239: escapeClassName = escapeClassN;
240: }
241: } catch (Exception e) {
242: if (logger.isLoggable(Level.SEVERE)) {
243: LogRecord rec = new LogRecord(Level.SEVERE,
244: "PSMA_CSPWTU0002");
245: rec.setThrown(e);
246: String[] param = { "getProperty (encoderClassName)" };
247: rec.setParameters(param);
248: rec.setLoggerName(logger.getName());
249: logger.log(rec);
250: }
251: }
252:
253: locale = getUserLocaleString();
254:
255: try {
256: type = (DSAMEMultiPortalConstants.getInstance()).MP_ATTR_DESKTOP_TYPE;
257: } catch (Exception e) {
258: if (logger.isLoggable(Level.SEVERE)) {
259: LogRecord rec = new LogRecord(Level.SEVERE,
260: "PSMA_CSPWTU0003");
261: rec.setThrown(e);
262: rec.setLoggerName(logger.getName());
263: logger.log(rec);
264: }
265: }
266:
267: charset = super .getCharset();
268: }
269:
270: /**
271: * Returns a UtilContext for this session.
272: *
273: * Note: UtilContext.getContext() will always
274: * try to return a UtilContext, even in the absence
275: * of a valid session. This is necessary to enable
276: * minimal taglib functionality in the case
277: * of a session timeout.
278: *
279: * @param request
280: * @param pageContext
281: */
282: public static UtilContext getContextOriginal(PageContext pageContext)
283: throws Exception {
284: UtilContext context = (UtilContext) pageContext
285: .getAttribute(UtilContext.CONTEXT_CLASS_NAME);
286: if (context == null) {
287: logger.log(Level.INFO, "PSMA_CSPWTU0005");
288: context = new UtilContext();
289: context.init((HttpServletRequest) pageContext.getRequest());
290: pageContext.setAttribute(UtilContext.CONTEXT_CLASS_NAME,
291: context);
292: }
293: return context;
294: }
295:
296: public static UtilContext getContext(PageContext pageContext)
297: throws Exception {
298: return (UtilContext) Context.getContext(pageContext,
299: CONTEXT_CLASS_NAME, CONTEXT_CACHE_CLASS_NAME,
300: CONTEXT_TYPE);
301: }
302:
303: /**
304: * Get a context attribute
305: *
306: * @param name attribute name
307: */
308: public Object getAttribute(String name) {
309: return attributes.get(name);
310: }
311:
312: /**
313: * Set a context attribute
314: *
315: * @param value attribute value
316: * @param name attribute name
317: */
318: public void setAttribute(String name, Object value) {
319: attributes.put(name, value);
320: }
321:
322: /**
323: * Remove a context attribute
324: *
325: * @param name attribute name
326: */
327: public void removeAttribute(String name) {
328: attributes.remove(name);
329: }
330:
331: /**
332: * Generates client aware URL...
333: *
334: * @param pageContext
335: * @param fileName
336: * @param compName
337: * @param parms
338: *
339: * @return a client aware URL
340: */
341: public String getURL(PageContext pageContext, String fileName,
342: String compName, String parms) {
343: String finalUrl;
344: String subCompName = null;
345:
346: try {
347: Context context = (Context) pageContext
348: .getAttribute(compName + "context");
349: SSOAdapter ssoAdapter = context.getSSOAdapter();
350: subCompName = ssoAdapter.getProperties().getProperty(
351: "subType");
352: } catch (Exception e) {
353: logger.log(Level.INFO, "PSMA_CSPWTU0004", e);
354: }
355:
356: String resourceName = ResourceLookup.getFirstExisting(
357: pageContext.getServletContext(), type, locale,
358: compName, subCompName, filePath, fileName, jspDocRoot,
359: true);
360: if (resourceName != null) {
361: finalUrl = resourceName;
362: } else {
363: finalUrl = jspDocRoot + "/" + fileName;
364: }
365:
366: //
367: // Add parameters, if any...
368: //
369: if (parms != null) {
370: finalUrl = finalUrl + "?" + parms;
371: }
372:
373: return finalUrl;
374: }
375:
376: /**
377: * Generates client aware URL...
378: *
379: * @param path
380: * @param parms
381: *
382: * @return a client aware URL
383: */
384: public String getURL(String path, String parms) {
385: String finalUrl = path;
386:
387: //
388: //Add parameters, if any...
389: //
390: if (parms != null) {
391: finalUrl = finalUrl + "?" + parms;
392: }
393:
394: return finalUrl;
395: }
396:
397: /**
398: * Return the compressor for this session...
399: *
400: * @return Compressor
401: */
402: public Compressor getCompressor() {
403: return compressor;
404: }
405:
406: private HashMap clientAttributeCache = new HashMap();
407:
408: /**
409: * Lookup client dependant profile attribute...
410: *
411: * @return
412: */
413: public String getClientAttribute(HttpServletRequest request,
414: String serviceName, String attributeName) {
415: String targetValue = null;
416: String cacheKey = serviceName + attributeName;
417:
418: targetValue = (String) clientAttributeCache.get(cacheKey);
419: if (targetValue != null) {
420: return targetValue;
421: }
422:
423: String attributeValue = null;
424: try {
425: PSClientAwareContextFactory contextFactory = PSClientAwareContextFactory
426: .getInstance();
427: HashMap serviceMap = new HashMap();
428: serviceMap.put("serviceName", serviceName);
429: ClientAwareUserContext userContext = contextFactory
430: .getClientAwareUserContext(new SSOAdapterSession(
431: request));
432: targetValue = userContext.getStringAttribute(serviceMap,
433: attributeName);
434: } catch (Exception e) {
435: logger.log(Level.INFO, "", e);
436: targetValue = "";
437: return targetValue;
438: }
439:
440: clientAttributeCache.put(cacheKey, targetValue);
441: return targetValue;
442: }
443:
444: /**
445: * generate cache defeating counter...
446: *
447: * @return hex counter value.
448: */
449: public String getCounterString() {
450: String count = Integer
451: .toHexString(cacheDefeatCounter++ % 65536);
452: switch (count.length()) {
453: case 4:
454: return count;
455: case 3:
456: return "0" + count;
457: case 2:
458: return "00" + count;
459: case 1:
460: return "000" + count;
461: default:
462: return count;
463: }
464: }
465:
466: /**
467: * rfc2396 bean set method...
468: *
469: * @param b bean value.
470: */
471: public void setRfc2396(boolean b) {
472: rfc2396 = b;
473: }
474:
475: /**
476: * rfc2396 bean set method...
477: *
478: * @param b bean value.
479: */
480: public boolean getRfc2396() {
481: return rfc2396;
482: }
483:
484: /**
485: * urlAmpEntity bean set method...
486: *
487: * @param b bean value.
488: */
489: public void setUrlAmpEntity(boolean b) {
490: urlAmpEntity = b;
491: }
492:
493: /**
494: * urlAmpEntity bean set method...
495: *
496: * @param b bean value.
497: */
498: public boolean getUrlAmpEntity() {
499: return urlAmpEntity;
500: }
501:
502: /**
503: * counterName bean property set method...
504: *
505: * @param s bean value.
506: */
507: public void setCounterName(String s) {
508: if (s != null && s.length() == 0) {
509: counterName = null;
510: } else {
511: counterName = s;
512: }
513: }
514:
515: /**
516: * counterName bean property get method...
517: *
518: */
519: public String getCounterName() {
520: return counterName;
521: }
522:
523: /**
524: * parmDelimter bean property set method...
525: *
526: * @param s bean value.
527: */
528: public void setParmDelimiter(String s) {
529: if (s != null && s.length() == 0) {
530: parmDelimiter = null;
531: } else {
532: parmDelimiter = s;
533: }
534: }
535:
536: /**
537: * parmDelimiter bean property get method...
538: *
539: */
540: public String getParmDelimiter() {
541: return parmDelimiter;
542: }
543:
544: /**
545: * escapeStyle bean property set method...
546: *
547: */
548: public void setEscapeClassName(String escapeClassNameParam) {
549: escapeClassName = escapeClassNameParam;
550: return;
551: }
552:
553: /**
554: * escapeStyle bean property get method...
555: *
556: */
557: public String getEscapeClassName() {
558: return escapeClassName;
559: }
560:
561: }
|