001: /**
002: * $Id: UrlMaker.java,v 1.16 2005/09/21 10:53:42 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: */package com.sun.portal.wireless.taglibs.util;
014:
015: import javax.servlet.jsp.*;
016: import javax.servlet.jsp.tagext.*;
017: import java.io.*;
018: import java.util.logging.Level;
019: import java.util.logging.LogRecord;
020: import java.util.logging.Logger;
021:
022: import javax.servlet.*;
023: import javax.servlet.http.*;
024:
025: import com.sun.portal.log.common.PortalLogger;
026: import com.sun.portal.wireless.taglibs.base.*;
027:
028: import com.iplanet.sso.*;
029: import com.iplanet.services.cdm.*;
030: import com.iplanet.am.util.*;
031:
032: /**
033: * Base implementation for tags that
034: * construct URL's using FileLookup.
035: *
036: *
037: * @version 1.0, 03/08/2001
038: */
039: public class UrlMaker extends BaseBodyTagSupport {
040:
041: protected String fileName = null;
042: protected String compName = null;
043: protected String staticParms = null;
044: protected String dynamicParms = null;
045: protected String path = null;
046: protected boolean compress = true;
047: protected boolean includeSid = true;
048: protected boolean deferLookup = false;
049: protected UtilContext context = null;
050:
051: private static Logger logger = PortalLogger
052: .getLogger(UrlMaker.class);
053:
054: /**
055: * Always set context, even if
056: * there is no iPS session...
057: *
058: */
059: protected void getContext() throws JspException {
060:
061: if (context != null)
062: return;
063:
064: try {
065: context = UtilContext.getContext(pageContext);
066: } catch (Exception e) {
067: context = null;
068: if (logger.isLoggable(Level.SEVERE)) {
069: LogRecord rec = new LogRecord(Level.SEVERE,
070: "PSMA_CSPWTU0002");
071: rec.setThrown(e);
072: String[] param = { "getContext" };
073: rec.setParameters(param);
074: rec.setLoggerName(logger.getName());
075: logger.log(rec);
076: }
077: throw new JspException(this .getClass().getName()
078: + ".getContext() failed.");
079: }
080: }
081:
082: /**
083: * Called at the beginning of a tag...
084: *
085: * @return what to do next
086: */
087: public int doStartTag() throws JspException {
088:
089: staticParms = null;
090: dynamicParms = null;
091:
092: getContext();
093:
094: return (EVAL_BODY_TAG);
095: }
096:
097: /**
098: * Computes appropriate URL...
099: *
100: * @return Rewritten URL...
101: */
102: protected String computeUrl(boolean staticParmsOnly)
103: throws JspException {
104: String finalUrl = fileName;
105: String parms;
106:
107: getContext();
108:
109: parms = staticParms;
110: if (!staticParmsOnly && dynamicParms != null) {
111: if (parms == null) {
112: parms = dynamicParms;
113: } else {
114: if (context.getUrlAmpEntity())
115: parms = parms + "&" + dynamicParms;
116: else
117: parms = parms + "&" + dynamicParms;
118: }
119: }
120:
121: try {
122: if (path != null) {
123: finalUrl = context.getURL(path, parms);
124: } else {
125: finalUrl = context.getURL(pageContext, fileName,
126: compName, parms);
127: }
128: } catch (Exception e) {
129: if (logger.isLoggable(Level.SEVERE)) {
130: LogRecord rec = new LogRecord(Level.SEVERE,
131: "PSMA_CSPWTU0002");
132: rec.setThrown(e);
133: String[] param = { "computeUrl" };
134: rec.setParameters(param);
135: rec.setLoggerName(logger.getName());
136: logger.log(rec);
137: }
138:
139: throw new JspException(this .getClass().getName()
140: + ".computeUrl() failed.");
141: }
142:
143: return (finalUrl);
144: }
145:
146: /**
147: * "file" attribute set method...
148: *
149: * @param s attribute value.
150: */
151: public void setFile(String s) {
152: fileName = Util.evalAttribute(pageContext, s);
153: }
154:
155: /**
156: * "deferLookup" attribute set method...
157: *
158: * @param s attribute value.
159: */
160: public void setDeferLookup(String s) {
161: deferLookup = Boolean.valueOf(s).booleanValue();
162: if (logger.isLoggable(Level.FINEST)) {
163: String[] param = { "deferLookup", "" + deferLookup };
164: logger.log(Level.FINEST, "PSMA_CSPWTU0001", param);
165: }
166: }
167:
168: /**
169: * "comp" attribute set method...
170: *
171: * @param s attribute value.
172: */
173: public void setComp(String s) {
174: compName = Util.evalAttribute(pageContext, s);
175: }
176:
177: /**
178: * "comp" attribute set method...
179: *
180: * @param s attribute value.
181: */
182: public void setPath(String s) {
183: path = Util.evalAttribute(pageContext, s);
184: }
185:
186: /**
187: * "compress" attribute set method...
188: *
189: * @param s attribute value.
190: */
191: public void setCompress(String s) {
192: // Take note that anything other than "true" will be treated as false
193: compress = Boolean.valueOf(s).booleanValue();
194: }
195:
196: /**
197: * "includeSid" attribute set method...
198: *
199: * @param s attribute value.
200: */
201: public void setIncludeSid(String s) {
202: // Take note that anything other than "true" will be treated as false
203: includeSid = Boolean.valueOf(s).booleanValue();
204: }
205:
206: public void release() {
207: super .release();
208: fileName = null;
209: compName = null;
210: path = null;
211: context = null;
212: }
213:
214: /**
215: * Used by the ParmTag class to add query string parameters to URLs
216: * being constructed by tag implementations which are subclasses of
217: * UrlMaker.
218: *
219: * Note: There is a hack to accomodate WML style dynamic URL generation.
220: * If a <util:parm> occurs whose content begins with the string
221: * "$(", it is assumed to be of the form "$(var)". Query string
222: * parameters of this form will be collected seperately, and will
223: * not be included in the URL passed to the Compressor.compressURL()
224: * method. Refer to the UrlMaker.computeUrl() method as well as
225: * the UrlTag, ForwardTag, and IncludeTag implementations for additional
226: * information.
227: */
228: public void addParm(String name, String value, boolean urlAmpEntity) {
229: String parms;
230: boolean isDynamic = value.regionMatches(0, "$(", 0, 2);
231:
232: if (isDynamic) {
233: parms = dynamicParms;
234: } else {
235: parms = staticParms;
236: }
237:
238: if (parms == null) {
239: parms = name + "=" + value;
240: } else {
241: if (urlAmpEntity
242: && !(path == null
243: && !isDynamic
244: && context.getCompressor()
245: .isCompressionEnabled() && compress))
246: parms = parms + "&" + name + "=" + value;
247: else
248: parms = parms + "&" + name + "=" + value;
249: }
250:
251: if (isDynamic) {
252: dynamicParms = parms;
253: } else {
254: staticParms = parms;
255: }
256: }
257:
258: }
|