1/*2 * Copyright 2000-2001,2004 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.util.template;
1819import java.lang.reflect.Method;
20import org.apache.turbine.util.RunData;
21import org.apache.turbine.util.DynamicURI;
22import org.apache.turbine.services.pull.ApplicationTool;
23import org.apache.jetspeed.services.resources.JetspeedResources;
2425/***26 * A customized version of the DynamicURI for linking to non-servlet27 * webapp resources.28 *29 * @author <a href="mailto:raphael@apache.org">Raphaël Luta</a>30 * @author <a href="mailto:sgala@apache.org">Santiago Gala</a>31 * @version $Id: ContentTemplateLink.java,v 1.7 2004/02/23 03:20:45 jford Exp $32 */33publicclassContentTemplateLink34extends DynamicURI
35 implements ApplicationTool
36 {
3738/*** the servlet 2.2+ webapp context */39private String contextPath;
4041/*** the webapp relative URI to find */42private String pathToContent;
4344/*** decide whether we should output full external URIs or simple45 absolute URIs */46privateboolean useExternalForm = false;
4748/*** Empty Constructor for introspection */49publicContentTemplateLink ()
50 {
51 }
5253/*** Constructor */54publicContentTemplateLink (RunData data)
55 {
56super(data);
57 initForceSecure();
58 initContextPath(data);
59 }
6061/***62 * This will initialise a ContentTemplateLink object that was63 * constructed with the default constructor (ApplicationTool64 * method).65 *66 * @param data assumed to be a RunData object67 */68publicvoid init(Object data)
69 {
70super.init((RunData)data);
71 initForceSecure();
72 initContextPath(data);
73 }
7475/*** Inits the contextPath for this object 76 *77 * @param data the RunData to use 78 */79protectedvoid initContextPath(Object data)
80 {
81try82 {
83 Class runDataClass = RunData.class;
84 Method meth = runDataClass.getDeclaredMethod("getContextPath", null);
85 contextPath = (String)meth.invoke(data, null);
86 }
87catch (Exception e)
88 {
89/*90 * Ignore a NoSuchMethodException because it means we are91 * using Servlet API 2.0. Make sure scriptName is not92 * null.93 */94 contextPath = "";
95 }
96 }
9798/***99 * Inits the force secure setting.100 */101protectedvoid initForceSecure()
102 {
103// check if we need to force to a secure (https) link104if (JetspeedResources.getBoolean("force.ssl", false))
105 {
106 setSecure();
107 }
108 }
109110/***111 * Refresh method - does nothing112 */113publicvoid refresh()
114 {
115// empty116 }
117118/***119 * Specify the link should be expressed in external form (ie 120 * with protocol, server name and server port)121 * @return a self reference for easy link construction in templates122 */123publicContentTemplateLink getExternal() {
124this.useExternalForm = true;
125returnthis;
126 }
127128/***129 * Specify the link should be expressed in absolute form (ie 130 * only a URI and not a full URL)131 * @return a self reference for easy link construction in templates132 */133publicContentTemplateLink getAbsolute() {
134this.useExternalForm = false;
135returnthis;
136 }
137138/***139 * Specify the webapp resource to link to.140 *141 * @param pathToContent the path to resource, assumed to be relative to the142 * web application context143 * @return a self reference for easy link construction in templates144 */145publicContentTemplateLink setURI(String pathToContent)
146 {
147this.pathToContent = pathToContent;
148returnthis;
149 }
150151/***152 * Returns the URI. After rendering the URI, it clears the 153 * pathInfo and QueryString portions of the DynamicURI.154 *155 * @return A String with the URI in either external or absolute form156 */157public String toString()
158 {
159160 StringBuffer sb = new StringBuffer();
161162// we want external URL form so include protocol and server name163if (useExternalForm)
164 {
165 String scheme = getServerScheme();
166 sb.append ( getServerScheme() ); //http167 sb.append ("://");
168 sb.append (getServerName()); //www.foo.com169int port = getServerPort();
170if( ( "http".equals( scheme ) && port != 80 ) ||
171 ( "https".equals( scheme ) && port != 443 ) ) { //only for non-default ports, to preserve session tracking.172 sb.append (":");
173 sb.append ( port ); //port webserver running on (8080 for TDK)174 }
175 }
176//the context for tomcat adds a / so no need to add another177 sb.append (contextPath); //the tomcat context178 sb.append ("/");
179if (pathToContent!=null) sb.append (pathToContent);
180181// This was added to allow multilple $link variables in one182// template.183 removePathInfo();
184 removeQueryData();
185this.pathToContent=null;
186187return (sb.toString());
188 }
189190 }