1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.util.template;
18
19 import java.lang.reflect.Method;
20 import org.apache.turbine.util.RunData;
21 import org.apache.turbine.util.DynamicURI;
22 import org.apache.turbine.services.pull.ApplicationTool;
23 import org.apache.jetspeed.services.resources.JetspeedResources;
24
25 /***
26 * A customized version of the DynamicURI for linking to non-servlet
27 * 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 */
33 public class ContentTemplateLink
34 extends DynamicURI
35 implements ApplicationTool
36 {
37
38 /*** the servlet 2.2+ webapp context */
39 private String contextPath;
40
41 /*** the webapp relative URI to find */
42 private String pathToContent;
43
44 /*** decide whether we should output full external URIs or simple
45 absolute URIs */
46 private boolean useExternalForm = false;
47
48 /*** Empty Constructor for introspection */
49 public ContentTemplateLink ()
50 {
51 }
52
53 /*** Constructor */
54 public ContentTemplateLink (RunData data)
55 {
56 super(data);
57 initForceSecure();
58 initContextPath(data);
59 }
60
61 /***
62 * This will initialise a ContentTemplateLink object that was
63 * constructed with the default constructor (ApplicationTool
64 * method).
65 *
66 * @param data assumed to be a RunData object
67 */
68 public void init(Object data)
69 {
70 super.init((RunData)data);
71 initForceSecure();
72 initContextPath(data);
73 }
74
75 /*** Inits the contextPath for this object
76 *
77 * @param data the RunData to use
78 */
79 protected void initContextPath(Object data)
80 {
81 try
82 {
83 Class runDataClass = RunData.class;
84 Method meth = runDataClass.getDeclaredMethod("getContextPath", null);
85 contextPath = (String)meth.invoke(data, null);
86 }
87 catch (Exception e)
88 {
89
90
91
92
93
94 contextPath = "";
95 }
96 }
97
98 /***
99 * Inits the force secure setting.
100 */
101 protected void initForceSecure()
102 {
103
104 if (JetspeedResources.getBoolean("force.ssl", false))
105 {
106 setSecure();
107 }
108 }
109
110 /***
111 * Refresh method - does nothing
112 */
113 public void refresh()
114 {
115
116 }
117
118 /***
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 templates
122 */
123 public ContentTemplateLink getExternal() {
124 this.useExternalForm = true;
125 return this;
126 }
127
128 /***
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 templates
132 */
133 public ContentTemplateLink getAbsolute() {
134 this.useExternalForm = false;
135 return this;
136 }
137
138 /***
139 * Specify the webapp resource to link to.
140 *
141 * @param pathToContent the path to resource, assumed to be relative to the
142 * web application context
143 * @return a self reference for easy link construction in templates
144 */
145 public ContentTemplateLink setURI(String pathToContent)
146 {
147 this.pathToContent = pathToContent;
148 return this;
149 }
150
151 /***
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 form
156 */
157 public String toString()
158 {
159
160 StringBuffer sb = new StringBuffer();
161
162
163 if (useExternalForm)
164 {
165 String scheme = getServerScheme();
166 sb.append ( getServerScheme() );
167 sb.append ("://");
168 sb.append (getServerName());
169 int port = getServerPort();
170 if( ( "http".equals( scheme ) && port != 80 ) ||
171 ( "https".equals( scheme ) && port != 443 ) ) {
172 sb.append (":");
173 sb.append ( port );
174 }
175 }
176
177 sb.append (contextPath);
178 sb.append ("/");
179 if (pathToContent!=null) sb.append (pathToContent);
180
181
182
183 removePathInfo();
184 removeQueryData();
185 this.pathToContent=null;
186
187 return (sb.toString());
188 }
189
190 }