Source code: com/RuntimeCollective/webapps/tag/PlainTextLinkTag.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/tag/PlainTextLinkTag.java,v 1.18 2003/09/30 15:13:18 joe Exp $
2 * $Revision: 1.18 $
3 * $Date: 2003/09/30 15:13:18 $
4 *
5 * ====================================================================
6 *
7 * Josephine : http://www.runtime-collective.com/josephine/index.html
8 *
9 * Copyright (C) 2003 Runtime Collective
10 *
11 * This product includes software developed by the
12 * Apache Software Foundation (http://www.apache.org/).
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30 package com.RuntimeCollective.webapps.tag;
31
32 import com.RuntimeCollective.webapps.RuntimeParameters;
33 import com.RuntimeCollective.webapps.TextUtils;
34 import com.RuntimeCollective.webapps.action.BetsieAction;
35
36 import java.util.Enumeration;
37 import java.net.URLEncoder;
38 import java.io.IOException;
39 import java.io.UnsupportedEncodingException;
40 import javax.servlet.jsp.tagext.Tag;
41 import javax.servlet.jsp.tagext.TagSupport;
42 import javax.servlet.jsp.JspWriter;
43 import javax.servlet.jsp.JspException;
44 import javax.servlet.jsp.JspTagException;
45 import javax.servlet.http.HttpServletRequest;
46
47 import org.apache.struts.taglib.html.BaseHandlerTag;
48 import org.apache.struts.util.RequestUtils;
49 import org.apache.struts.action.ActionMapping;
50
51
52 /** a custom jsp tag that creates a link to the Runtime Betsie server or another
53 * Betsie server if specified in web.xml or the tag. You can also define the
54 * hostname of the site using your link either directly to the tag or in the web.xml
55 *
56 * Betsie is an opensource accessability tool that uses perl to render a plaintext
57 * version of your site suitable for text to speach web browsing. You can learn more by
58 * visiting <a href="http://betsie.sourceforge.net/">http://betsie.sourceforge.net/</a>
59 *
60 * Takes the following attributes
61 * <ul>
62 * <li> <code> betsieDomain </code> - Where the Betsie server is running. If not set checks web.xml for a parameter, if thats not set uses the default Runtime Betsie server domain (http://betsie.runtime-collective.com/cgi-bin/parser.pl). betsieDomain should include the scheme (http://) [Optional]. </li>
63 * <li> <code> hostname </code> - This is the hostname of the server that needs to use Betsie. If not set will check the web.xml, if not set there will use HttpServletRequest getServerName() and getServerPort() to build the hostname. Hostnames in the tag or web.xml should not include the scheme (http://) [optional] </li>
64 * <li> <code> linkText </code> - The link text that the tag will display on the page. If not set uses default "Plain text" [Optional]. </li>
65 *
66 * @version $Id: PlainTextLinkTag.java,v 1.18 2003/09/30 15:13:18 joe Exp $
67 */
68 public class PlainTextLinkTag extends BaseHandlerTag {
69
70 /** the hostname of the server that needs to use Betsie. */
71 protected String hostname;
72 /** get the hostname from the tag. */
73 public String getHostname() { return this.hostname; }
74 /** set the hostname from the tag. */
75 public void setHostname(String hostname) { this.hostname = hostname; }
76
77 /** the betsieDomain, where the betsie server is running. */
78 protected String betsieDomain ;
79 /** get the betsieDomain from the tag. */
80 public String getBetsieDomain() { return this.betsieDomain; }
81 /** set the betsieDomain. */
82 public void setBetsieDomain(String betsieDomain) { this.betsieDomain = betsieDomain; }
83
84 /** the linkText from the tag. */
85 protected String linkText;
86 /** get the linkText from the tag. */
87 public String getLinkText() { return this.linkText; }
88 /** set the linkText. */
89 public void setLinkText(String linkText) { this.linkText = linkText; }
90
91 /** the styleClass from the tag. */
92 protected String styleClass;
93 /** get the styleClass from the tag. */
94 public String getStyleClass() { return this.styleClass; }
95 /** set the styleClass. */
96 public void setStyleClass(String styleClass) { this.styleClass = styleClass; }
97
98 public static final String AHREF = "<a href=\"";
99 public static final String SLASH = "/";
100 public static final String ENDQUOTE = "\"";
101 public static final String CLASSEQUAL = " class=\"";
102 public static final String ENDTAG = ">";
103 public static final String ENDA = "</a>";
104 public static final String BETSIE_DOMAIN_DEFAULT = "http://betsie.runtime-collective.com/cgi-bin/parser.pl";
105 public static final String BETSIE_QUERY_STRING_START = "getJsp=true&url=/";
106 public static final String PROCESS_BETSIE_ACTION_PATH = "/betsie/processPage";
107 public static final String BETSIE_DOMAIN_PARAM_KEY = "betsieDomain";
108 public static final String LINKTEXT_DEFAULT = "Plain text";
109 public static final String QUESTION = "?";
110 public static final String DOTDO = ".do";
111 public static final String SEMICOL = ":";
112 public static final String HTTP = "http://";
113
114 // == Tag methods ==================================
115
116 public int doStartTag() throws JspException {
117
118 HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
119 StringBuffer link = new StringBuffer();
120
121 // open the tag
122 link.append(AHREF);
123
124 // append the link
125 link.append(computeBetsieLink(request, betsieDomain, hostname));
126
127 // close the quote
128 link.append(ENDQUOTE);
129
130 // maybe append styleClass
131 if ((styleClass != null) && (!"".equals(styleClass))) {
132 link.append(CLASSEQUAL)
133 .append(styleClass)
134 .append(ENDQUOTE);
135 }
136
137 // close the tag
138 link.append(ENDTAG);
139
140 // append the link text
141 if (linkText != null ) {
142 link.append(linkText);
143 } else {
144 link.append(LINKTEXT_DEFAULT);
145 }
146
147 // end the tag
148 link.append(ENDA);
149
150 // put all that on the page
151 try {
152 //RuntimeParameters.logDebug(this, "Result : \n"+link.toString());
153 pageContext.getOut().println(link.toString());
154 } catch (IOException e) {
155 throw new JspTagException("I/O Exception " + e.getMessage() );
156 }
157
158 return SKIP_BODY;
159 }
160
161 public void release() {
162 linkText = "";
163 betsieDomain = "";
164 }
165
166
167
168
169 /** Compute the link to Betsie for the current page of a request. */
170 public static String computeBetsieLink(HttpServletRequest theRequest, String theBetsieDomain, String theHostname) {
171
172 StringBuffer link = new StringBuffer();
173
174 // FR: don't show the action if it's the Betsie action
175 // this prevents recursivety of betsie processing of links,
176 // which the betsie perl script has been changed to prevent,
177 // but it doesn't seem to work still
178 String actionPath = null;
179 ActionMapping mapping = (ActionMapping) theRequest.getAttribute("org.apache.struts.action.mapping.instance");
180 if (mapping != null) {
181 actionPath = mapping.getPath();
182 }
183 boolean alreadyBetsie = (actionPath != null) && (actionPath.indexOf(PROCESS_BETSIE_ACTION_PATH) >= 0);
184 //RuntimeParameters.logDebug("PlainTextLinkTag", "URI : "+theRequest.getRequestURI()+" aP : "+actionPath+" alreadyBetsie : "+alreadyBetsie);
185
186 if ((actionPath == null) || (actionPath.indexOf(PROCESS_BETSIE_ACTION_PATH) < 0)) {
187
188 // add the betsie domain
189 if (theBetsieDomain != null) {
190 link.append(theBetsieDomain);
191 } else if (RuntimeParameters.getParam(BETSIE_DOMAIN_PARAM_KEY) != null) {
192 link.append(RuntimeParameters.getParam(BETSIE_DOMAIN_PARAM_KEY));
193 } else {
194 link.append(BETSIE_DOMAIN_DEFAULT);
195 }
196
197 // and a slash
198 link.append(SLASH);
199
200 } else {
201 link.append(HTTP);
202 }
203
204 // and the host name
205 // if (!alreadyBetsie) {
206 if (theHostname != null) {
207 link.append(theHostname);
208 } else if (RuntimeParameters.getParam("hostname") != null) {
209 link.append(RuntimeParameters.getParam("hostname"));
210 } else {
211 link.append(theRequest.getServerName());
212 int port = theRequest.getServerPort();
213 if (port != 80) {
214 link.append(SEMICOL)
215 .append(port);
216 }
217 }
218 //}
219
220 // and the action or URI
221 if (alreadyBetsie) {
222
223 // when the real query has parameters, we can reconstruct it easily
224 // but when it doesn't, the actual QueryString is the QueryString
225 // of the betsie action, which we don't want. duh!
226 link.append(theRequest.getRequestURI());
227
228 String queryString = theRequest.getQueryString();
229 if (!queryString.startsWith(BETSIE_QUERY_STRING_START)) {
230 link.append("?")
231 .append(theRequest.getQueryString());
232 }
233
234 // FR: when recursive
235 // fetch the url from the query
236 // (can't do getParameter as that would be mixed up with extra params)
237 // String queryString = theRequest.getQueryString();
238 // RuntimeParameters.logDebug("PlainTextLinkTag", "QueryString : "+theRequest.getQueryString());
239 // int urlStart = theRequest.getQueryString().lastIndexOf("&url=/");
240 // if (urlStart >= 0) {
241 // String realUrl = theRequest.getQueryString().substring(urlStart+"&url".length());
242 // RuntimeParameters.logDebug("PlainTextLinkTag", "Real url : "+realUrl);
243 // // weird betsie stuff
244 // realUrl = TextUtils.replaceAll(realUrl, "/0005", "");
245 // link.append(realUrl.substring(1));
246 // } else {
247 // // this probably won't work though...
248 // link.append(theRequest.getRequestURI());
249 // }
250
251 } else if (actionPath != null) {
252 link.append(actionPath)
253 .append(DOTDO);
254 } else {
255 link.append(theRequest.getRequestURI());
256 }
257
258 // and parameters, if any
259 // FR: don't show queryString when recursive
260 if (!alreadyBetsie) {
261 String queryString = theRequest.getQueryString();
262 if (queryString != null) {
263 link.append(QUESTION)
264 .append(queryString);
265 }
266 }
267
268 return link.toString();
269 }
270 }
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285