%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.turbine.util.uri.BaseURI |
|
|
1 | package org.apache.turbine.util.uri; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | ||
19 | import javax.servlet.http.HttpServletResponse; |
|
20 | ||
21 | import org.apache.commons.lang.StringUtils; |
|
22 | ||
23 | import org.apache.commons.logging.Log; |
|
24 | import org.apache.commons.logging.LogFactory; |
|
25 | ||
26 | import org.apache.turbine.Turbine; |
|
27 | import org.apache.turbine.TurbineConstants; |
|
28 | ||
29 | import org.apache.turbine.util.RunData; |
|
30 | import org.apache.turbine.util.ServerData; |
|
31 | ||
32 | /** |
|
33 | * This is the base class for all dynamic URIs in the Turbine System. |
|
34 | * |
|
35 | * All of the classes used for generating URIs are derived from this. |
|
36 | * |
|
37 | * @author <a href="mailto:jon@clearink.com">Jon S. Stevens</a> |
|
38 | * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a> |
|
39 | * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> |
|
40 | * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a> |
|
41 | * @version $Id: BaseURI.java 264148 2005-08-29 14:21:04Z henning $ |
|
42 | */ |
|
43 | ||
44 | 2 | public abstract class BaseURI |
45 | implements URI, |
|
46 | URIConstants |
|
47 | { |
|
48 | /** Logging */ |
|
49 | 6 | private static Log log = LogFactory.getLog(BaseURI.class); |
50 | ||
51 | /** ServerData Object for scheme, name, port etc. */ |
|
52 | 16 | private ServerData serverData = |
53 | new ServerData(null, HTTP_PORT, HTTP, class="keyword">null, class="keyword">null); |
|
54 | ||
55 | /** Whether we want to redirect or not. */ |
|
56 | 16 | private boolean redirect = false; |
57 | ||
58 | /** Servlet response interface. */ |
|
59 | 16 | private HttpServletResponse response = null; |
60 | ||
61 | /** Reference Anchor (#ref) */ |
|
62 | 16 | private String reference = null; |
63 | ||
64 | /* |
|
65 | * ======================================================================== |
|
66 | * |
|
67 | * Constructors |
|
68 | * |
|
69 | * ======================================================================== |
|
70 | * |
|
71 | */ |
|
72 | ||
73 | /** |
|
74 | * Empty C'tor. Uses Turbine.getDefaultServerData(). |
|
75 | * |
|
76 | */ |
|
77 | public BaseURI() |
|
78 | 0 | { |
79 | 0 | init(Turbine.getDefaultServerData()); |
80 | 0 | setResponse(null); |
81 | 0 | } |
82 | ||
83 | /** |
|
84 | * Constructor with a RunData object |
|
85 | * |
|
86 | * @param runData A RunData object |
|
87 | */ |
|
88 | public BaseURI(RunData runData) |
|
89 | 0 | { |
90 | 0 | init(runData.getServerData()); |
91 | 0 | setResponse(runData.getResponse()); |
92 | 0 | } |
93 | ||
94 | /** |
|
95 | * Constructor, set explicit redirection |
|
96 | * |
|
97 | * @param runData A RunData object |
|
98 | * @param redirect True if redirection allowed. |
|
99 | */ |
|
100 | public BaseURI(RunData runData, boolean redirect) |
|
101 | 0 | { |
102 | 0 | init(runData.getServerData()); |
103 | 0 | setResponse(runData.getResponse()); |
104 | 0 | setRedirect(redirect); |
105 | 0 | } |
106 | ||
107 | /** |
|
108 | * Constructor with a ServerData object |
|
109 | * |
|
110 | * @param serverData A ServerData object |
|
111 | */ |
|
112 | public BaseURI(ServerData serverData) |
|
113 | 16 | { |
114 | 16 | init(serverData); |
115 | 16 | setResponse(null); |
116 | 16 | } |
117 | ||
118 | /** |
|
119 | * Constructor, set explicit redirection |
|
120 | * |
|
121 | * @param serverData A ServerData object |
|
122 | * @param redirect True if redirection allowed. |
|
123 | */ |
|
124 | public BaseURI(ServerData serverData, boolean redirect) |
|
125 | 0 | { |
126 | 0 | init(serverData); |
127 | 0 | setResponse(null); |
128 | 0 | setRedirect(redirect); |
129 | 0 | } |
130 | ||
131 | /* |
|
132 | * ======================================================================== |
|
133 | * |
|
134 | * Init |
|
135 | * |
|
136 | * ======================================================================== |
|
137 | * |
|
138 | */ |
|
139 | ||
140 | /** |
|
141 | * Init with a ServerData object |
|
142 | * |
|
143 | * @param serverData A ServerData object |
|
144 | * |
|
145 | */ |
|
146 | private void init(ServerData serverData) |
|
147 | { |
|
148 | 16 | log.debug("init(" + serverData + ")"); |
149 | ||
150 | 16 | if(serverData != null) |
151 | { |
|
152 | // We must clone this, because if BaseURI is used in a pull tool, |
|
153 | // then the fields might be changed. If we don't clone, this might pull |
|
154 | // through to the ServerData object saved at firstRequest() in the |
|
155 | // Turbine object. |
|
156 | 16 | this.serverData = (ServerData) serverData.clone(); |
157 | } |
|
158 | else |
|
159 | { |
|
160 | 0 | log.error("Passed null ServerData object!"); |
161 | } |
|
162 | 16 | reference = null; |
163 | 16 | } |
164 | ||
165 | /* |
|
166 | * ======================================================================== |
|
167 | * |
|
168 | * Getter / Setter |
|
169 | * |
|
170 | * ======================================================================== |
|
171 | * |
|
172 | */ |
|
173 | ||
174 | /** |
|
175 | * Set the redirect Flag |
|
176 | * |
|
177 | * @param redirect The new value of the redirect flag. |
|
178 | */ |
|
179 | public void setRedirect(boolean redirect) |
|
180 | { |
|
181 | 0 | this.redirect = redirect; |
182 | 0 | } |
183 | ||
184 | /** |
|
185 | * Returns the current value of the Redirect flag |
|
186 | * |
|
187 | * @return True if Redirect is allowed |
|
188 | * |
|
189 | */ |
|
190 | public boolean isRedirect() |
|
191 | { |
|
192 | 0 | return redirect; |
193 | } |
|
194 | ||
195 | /** |
|
196 | * Gets the script name (/servlets/Turbine). |
|
197 | * |
|
198 | * @return A String with the script name. |
|
199 | */ |
|
200 | public String getScriptName() |
|
201 | { |
|
202 | 14 | return serverData.getScriptName(); |
203 | } |
|
204 | ||
205 | /** |
|
206 | * Sets the script name (/servlets/Turbine). |
|
207 | * |
|
208 | * @param scriptName A String with the script name. |
|
209 | */ |
|
210 | public void setScriptName(String scriptName) |
|
211 | { |
|
212 | 14 | serverData.setScriptName(scriptName); |
213 | 14 | } |
214 | ||
215 | /** |
|
216 | * Gets the context path. |
|
217 | * |
|
218 | * @return A String with the context path. |
|
219 | */ |
|
220 | public String getContextPath() |
|
221 | { |
|
222 | 14 | return serverData.getContextPath(); |
223 | } |
|
224 | ||
225 | /** |
|
226 | * Sets the context path. |
|
227 | * |
|
228 | * @param contextPath A String with the context path |
|
229 | */ |
|
230 | public void setContextPath(String contextPath) |
|
231 | { |
|
232 | 0 | serverData.setContextPath(contextPath); |
233 | 0 | } |
234 | ||
235 | /** |
|
236 | * Gets the server name. |
|
237 | * |
|
238 | * @return A String with the server name. |
|
239 | */ |
|
240 | public String getServerName() |
|
241 | { |
|
242 | 14 | return serverData.getServerName(); |
243 | } |
|
244 | ||
245 | /** |
|
246 | * Sets the server name. |
|
247 | * |
|
248 | * @param serverName A String with the server name. |
|
249 | */ |
|
250 | public void setServerName(String serverName) |
|
251 | { |
|
252 | 0 | serverData.setServerName(serverName); |
253 | 0 | } |
254 | ||
255 | /** |
|
256 | * Gets the server port. |
|
257 | * |
|
258 | * @return A String with the server port. |
|
259 | */ |
|
260 | public int getServerPort() |
|
261 | { |
|
262 | 14 | int serverPort = serverData.getServerPort(); |
263 | ||
264 | 14 | if (serverPort == 0) |
265 | { |
|
266 | 0 | if(getServerScheme().equals(HTTPS)) |
267 | { |
|
268 | 0 | serverPort = HTTPS_PORT; |
269 | } |
|
270 | else |
|
271 | { |
|
272 | 0 | serverPort = HTTP_PORT; |
273 | } |
|
274 | } |
|
275 | 14 | return serverPort; |
276 | } |
|
277 | ||
278 | /** |
|
279 | * Sets the server port. |
|
280 | * |
|
281 | * @param serverPort An int with the port. |
|
282 | */ |
|
283 | public void setServerPort(int serverPort) |
|
284 | { |
|
285 | 0 | serverData.setServerPort(serverPort); |
286 | 0 | } |
287 | ||
288 | /** |
|
289 | * Method to specify that a URI should use SSL. The default port |
|
290 | * is used. |
|
291 | */ |
|
292 | public void setSecure() |
|
293 | { |
|
294 | 0 | setSecure(HTTPS_PORT); |
295 | 0 | } |
296 | ||
297 | /** |
|
298 | * Method to specify that a URI should use SSL. |
|
299 | * Whether or not it does is determined from Turbine.properties. |
|
300 | * If use.ssl in the Turbine.properties is set to false, then |
|
301 | * http is used in any case. (Default of use.ssl is true). |
|
302 | * |
|
303 | * @param port An int with the port number. |
|
304 | */ |
|
305 | public void setSecure(int port) |
|
306 | { |
|
307 | 0 | boolean useSSL = |
308 | Turbine.getConfiguration() |
|
309 | .getBoolean(TurbineConstants.USE_SSL_KEY, |
|
310 | TurbineConstants.USE_SSL_DEFAULT); |
|
311 | ||
312 | 0 | setServerScheme(useSSL ? HTTPS : HTTP); |
313 | 0 | setServerPort(port); |
314 | 0 | } |
315 | ||
316 | /** |
|
317 | * Sets the scheme (HTTP or HTTPS). |
|
318 | * |
|
319 | * @param serverScheme A String with the scheme. |
|
320 | */ |
|
321 | public void setServerScheme(String serverScheme) |
|
322 | { |
|
323 | 0 | serverData.setServerScheme(StringUtils.isNotEmpty(serverScheme) |
324 | ? serverScheme : ""); |
|
325 | 0 | } |
326 | ||
327 | /** |
|
328 | * Returns the current Server Scheme |
|
329 | * |
|
330 | * @return The current Server scheme |
|
331 | * |
|
332 | */ |
|
333 | public String getServerScheme() |
|
334 | { |
|
335 | 42 | String serverScheme = serverData.getServerScheme(); |
336 | ||
337 | 42 | return StringUtils.isNotEmpty(serverScheme) ? serverScheme : HTTP; |
338 | } |
|
339 | ||
340 | /** |
|
341 | * Sets a reference anchor (#ref). |
|
342 | * |
|
343 | * @param reference A String containing the reference. |
|
344 | */ |
|
345 | public void setReference(String reference) |
|
346 | { |
|
347 | 0 | this.reference = reference; |
348 | 0 | } |
349 | ||
350 | /** |
|
351 | * Returns the current reference anchor. |
|
352 | * |
|
353 | * @return A String containing the reference. |
|
354 | */ |
|
355 | public String getReference() |
|
356 | { |
|
357 | 0 | return hasReference() ? reference : ""; |
358 | } |
|
359 | ||
360 | /** |
|
361 | * Does this URI contain an anchor? (#ref) |
|
362 | * |
|
363 | * @return True if this URI contains an anchor. |
|
364 | */ |
|
365 | public boolean hasReference() |
|
366 | { |
|
367 | 14 | return StringUtils.isNotEmpty(reference); |
368 | } |
|
369 | ||
370 | /* |
|
371 | * ======================================================================== |
|
372 | * |
|
373 | * Protected / Private Methods |
|
374 | * |
|
375 | * ======================================================================== |
|
376 | * |
|
377 | */ |
|
378 | ||
379 | /** |
|
380 | * Set a Response Object to use when creating the |
|
381 | * response string. |
|
382 | * |
|
383 | */ |
|
384 | protected void setResponse(HttpServletResponse response) |
|
385 | { |
|
386 | 16 | this.response = response; |
387 | 16 | } |
388 | ||
389 | /** |
|
390 | * Returns the Response Object from the Servlet Container. |
|
391 | * |
|
392 | * @return The Servlet Response object or null |
|
393 | * |
|
394 | */ |
|
395 | protected HttpServletResponse getResponse() |
|
396 | { |
|
397 | 14 | return response; |
398 | } |
|
399 | ||
400 | /** |
|
401 | * Append the Context Path and Script Name to the passed |
|
402 | * String Buffer. |
|
403 | * |
|
404 | * <p> |
|
405 | * This is a convenience method to be |
|
406 | * used in the Link output routines of derived classes to |
|
407 | * easily append the correct path. |
|
408 | * |
|
409 | * @param sb The StringBuffer to store context path and script name. |
|
410 | */ |
|
411 | protected void getContextAndScript(StringBuffer sb) |
|
412 | { |
|
413 | 14 | String context = getContextPath(); |
414 | ||
415 | 14 | if(StringUtils.isNotEmpty(context)) |
416 | { |
|
417 | 0 | if(context.charAt(0) != '/') |
418 | { |
|
419 | 0 | sb.append('/'); |
420 | } |
|
421 | 0 | sb.append (context); |
422 | } |
|
423 | ||
424 | // /servlet/turbine |
|
425 | 14 | String script = getScriptName(); |
426 | ||
427 | 14 | if(StringUtils.isNotEmpty(script)) |
428 | { |
|
429 | 14 | if(script.charAt(0) != '/') |
430 | { |
|
431 | 14 | sb.append('/'); |
432 | } |
|
433 | 14 | sb.append (script); |
434 | } |
|
435 | 14 | } |
436 | ||
437 | /** |
|
438 | * Appends Scheme, Server and optionally the port to the |
|
439 | * supplied String Buffer. |
|
440 | * |
|
441 | * <p> |
|
442 | * This is a convenience method to be |
|
443 | * used in the Link output routines of derived classes to |
|
444 | * easily append the correct server scheme. |
|
445 | * |
|
446 | * @param sb The StringBuffer to store the scheme and port information. |
|
447 | */ |
|
448 | protected void getSchemeAndPort(StringBuffer sb) |
|
449 | { |
|
450 | // http(s)://<servername> |
|
451 | 14 | sb.append(getServerScheme()); |
452 | 14 | sb.append(URIConstants.URI_SCHEME_SEPARATOR); |
453 | 14 | sb.append(getServerName()); |
454 | ||
455 | // (:<port>) |
|
456 | 14 | if ((getServerScheme().equals(HTTP) |
457 | && getServerPort() != HTTP_PORT) |
|
458 | || (getServerScheme().equals(HTTPS) |
|
459 | && getServerPort() != HTTPS_PORT)) |
|
460 | { |
|
461 | 0 | sb.append(':'); |
462 | 0 | sb.append(getServerPort()); |
463 | } |
|
464 | 14 | } |
465 | ||
466 | /** |
|
467 | * Encodes a Response Uri according to the Servlet Container. |
|
468 | * This might add a Java session identifier or do redirection. |
|
469 | * The resulting String can be used in a page or template. |
|
470 | * |
|
471 | * @param uri The Uri to encode |
|
472 | * |
|
473 | * @return An Uri encoded by the container. |
|
474 | */ |
|
475 | protected String encodeResponse(String uri) |
|
476 | { |
|
477 | 14 | String res = uri; |
478 | ||
479 | 14 | HttpServletResponse response = getResponse(); |
480 | ||
481 | 14 | if(response == null) |
482 | { |
|
483 | 14 | log.debug("No Response Object!"); |
484 | } |
|
485 | else |
|
486 | { |
|
487 | try |
|
488 | { |
|
489 | 0 | if(isRedirect()) |
490 | { |
|
491 | 0 | log.debug("Should Redirect"); |
492 | 0 | res = response.encodeRedirectURL(uri); |
493 | } |
|
494 | else |
|
495 | { |
|
496 | 0 | res = response.encodeURL(uri); |
497 | } |
|
498 | } |
|
499 | 0 | catch(Exception e) |
500 | { |
|
501 | 0 | log.error("response" + response + ", uri: " + uri); |
502 | 0 | log.error("While trying to encode the URI: ", e); |
503 | 0 | } |
504 | } |
|
505 | ||
506 | 14 | log.debug("encodeResponse(): " + res); |
507 | 14 | return res; |
508 | } |
|
509 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |