KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > uri > BaseURI


1 package org.apache.turbine.util.uri;
2
3 /*
4  * Copyright 2001-2004 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 JavaDoc;
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,v 1.5.2.2 2004/05/20 03:28:01 seade Exp $
42  */

43
44 public abstract class BaseURI
45         implements URI,
46                    URIConstants
47 {
48     /** Logging */
49     private static Log log = LogFactory.getLog(BaseURI.class);
50
51     /** ServerData Object for scheme, name, port etc. */
52     private ServerData serverData =
53             new ServerData(null, HTTP_PORT, HTTP, null, null);
54
55     /** Whether we want to redirect or not. */
56     private boolean redirect = false;
57
58     /** Servlet response interface. */
59     private HttpServletResponse JavaDoc response = null;
60
61     /** Reference Anchor (#ref) */
62     private String JavaDoc 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     {
79         init(Turbine.getDefaultServerData());
80         setResponse(null);
81     }
82
83     /**
84      * Constructor with a RunData object
85      *
86      * @param runData A RunData object
87      */

88     public BaseURI(RunData runData)
89     {
90         init(runData.getServerData());
91         setResponse(runData.getResponse());
92     }
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     {
102         init(runData.getServerData());
103         setResponse(runData.getResponse());
104         setRedirect(redirect);
105     }
106
107     /**
108      * Constructor with a ServerData object
109      *
110      * @param serverData A ServerData object
111      */

112     public BaseURI(ServerData serverData)
113     {
114         init(serverData);
115         setResponse(null);
116     }
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     {
126         init(serverData);
127         setResponse(null);
128         setRedirect(redirect);
129     }
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         log.debug("init(" + serverData + ")");
149
150         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
this.serverData = (ServerData) serverData.clone();
157         }
158         else
159         {
160             log.error("Passed null ServerData object!");
161         }
162         reference = null;
163     }
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         this.redirect = redirect;
182     }
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         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 JavaDoc getScriptName()
201     {
202         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 JavaDoc scriptName)
211     {
212         serverData.setScriptName(scriptName);
213     }
214
215     /**
216      * Gets the context path.
217      *
218      * @return A String with the context path.
219      */

220     public String JavaDoc getContextPath()
221     {
222         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 JavaDoc contextPath)
231     {
232         serverData.setContextPath(contextPath);
233     }
234
235     /**
236      * Gets the server name.
237      *
238      * @return A String with the server name.
239      */

240     public String JavaDoc getServerName()
241     {
242         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 JavaDoc serverName)
251     {
252         serverData.setServerName(serverName);
253     }
254
255     /**
256      * Gets the server port.
257      *
258      * @return A String with the server port.
259      */

260     public int getServerPort()
261     {
262         int serverPort = serverData.getServerPort();
263
264         if (serverPort == 0)
265         {
266             if(getServerScheme().equals(HTTPS))
267             {
268                 serverPort = HTTPS_PORT;
269             }
270             else
271             {
272                 serverPort = HTTP_PORT;
273             }
274         }
275         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         serverData.setServerPort(serverPort);
286     }
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         setSecure(HTTPS_PORT);
295     }
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         boolean useSSL =
308                 Turbine.getConfiguration()
309                 .getBoolean(TurbineConstants.USE_SSL_KEY,
310                         TurbineConstants.USE_SSL_DEFAULT);
311
312         setServerScheme(useSSL ? HTTPS : HTTP);
313         setServerPort(port);
314     }
315
316     /**
317      * Sets the scheme (HTTP or HTTPS).
318      *
319      * @param serverScheme A String with the scheme.
320      */

321     public void setServerScheme(String JavaDoc serverScheme)
322     {
323         serverData.setServerScheme(StringUtils.isNotEmpty(serverScheme)
324                 ? serverScheme : "");
325     }
326
327     /**
328      * Returns the current Server Scheme
329      *
330      * @return The current Server scheme
331      *
332      */

333     public String JavaDoc getServerScheme()
334     {
335         String JavaDoc serverScheme = serverData.getServerScheme();
336
337         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 JavaDoc reference)
346     {
347         this.reference = reference;
348     }
349
350     /**
351      * Returns the current reference anchor.
352      *
353      * @return A String containing the reference.
354      */

355     public String JavaDoc getReference()
356     {
357         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         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 JavaDoc response)
385     {
386         this.response = response;
387     }
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 JavaDoc getResponse()
396     {
397         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 JavaDoc sb)
412     {
413         String JavaDoc context = getContextPath();
414
415         if(StringUtils.isNotEmpty(context))
416         {
417             if(context.charAt(0) != '/')
418             {
419                 sb.append('/');
420             }
421             sb.append (context);
422         }
423
424         // /servlet/turbine
425
String JavaDoc script = getScriptName();
426
427         if(StringUtils.isNotEmpty(script))
428         {
429             if(script.charAt(0) != '/')
430             {
431                 sb.append('/');
432             }
433             sb.append (script);
434         }
435     }
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 JavaDoc sb)
449     {
450         // http(s)://<servername>
451
sb.append(getServerScheme());
452         sb.append(URIConstants.URI_SCHEME_SEPARATOR);
453         sb.append(getServerName());
454
455         // (:<port>)
456
if ((getServerScheme().equals(HTTP)
457                     && getServerPort() != HTTP_PORT)
458                 || (getServerScheme().equals(HTTPS)
459                         && getServerPort() != HTTPS_PORT))
460         {
461             sb.append(':');
462             sb.append(getServerPort());
463         }
464     }
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 JavaDoc encodeResponse(String JavaDoc uri)
476     {
477         String JavaDoc res = uri;
478
479         HttpServletResponse JavaDoc response = getResponse();
480
481         if(response == null)
482         {
483             log.debug("No Response Object!");
484         }
485         else
486         {
487             try
488             {
489                 if(isRedirect())
490                 {
491                     log.debug("Should Redirect");
492                     res = response.encodeRedirectURL(uri);
493                 }
494                 else
495                 {
496                     res = response.encodeURL(uri);
497                 }
498             }
499             catch(Exception JavaDoc e)
500             {
501                 log.error("response" + response + ", uri: " + uri);
502                 log.error("While trying to encode the URI: ", e);
503             }
504         }
505
506         log.debug("encodeResponse(): " + res);
507         return res;
508     }
509 }
510
Popular Tags