KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > RelativeDynamicURI


1 package org.apache.turbine;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import javax.servlet.http.HttpServletRequest JavaDoc;
58 import org.apache.turbine.RunData;
59
60 /**
61  * This creates a Dynamic URI for use within the Turbine system
62  *
63  * <p>If you use this class to generate all of your href tags as well
64  * as all of your URI's, then you will not need to worry about having
65  * session data setup for you or using HttpServletRequest.encodeUrl()
66  * since this class does everything for you.
67  * This class generates relative URI's which can be used in environments with
68  * firewalls and gateways for outgoing connections.
69  *
70  * <code><pre>
71  * RelativeDynamicURI dui = new RelativeDynamicURI (data, "UserScreen" );
72  * dui.setName("Click Here").addPathInfo("user","jon");
73  * dui.getA();
74  * </pre></code>
75  *
76  * The above call to getA() would return the String:
77  *
78  * &lt;AHREF="/servlets/Turbine/screen=UserScreen&amp;amp;user=jon"&gt;ClickHere&lt;/A&gt;
79  *
80  * @author <a HREF="mailto:dfaller@raleigh.ibm.com">David S. Faller</a>
81  * @deprecated use org.apache.turbine.DynamicURI#setRelative(boolean)
82  */

83 public class RelativeDynamicURI
84     extends DynamicURI
85 {
86     /**
87      * Default constructor - one of the init methods must be called
88      * before use.
89      */

90     public RelativeDynamicURI()
91     {
92     }
93
94     /**
95      * Constructor sets up some variables.
96      *
97      * @param data A Turbine RunData object.
98      */

99     public RelativeDynamicURI( RunData data )
100     {
101         super(data);
102     }
103
104     /**
105      * Constructor sets up some variables.
106      *
107      * @param data A Turbine RunData object.
108      * @param redirect True if it should redirect.
109      */

110     public RelativeDynamicURI( RunData data,
111                                boolean redirect )
112     {
113         super(data, redirect);
114     }
115
116     /**
117      * Builds the relative URL with all of the data URL-encoded as well as
118      * encoded using HttpServletResponse.encodeUrl().
119      *
120      * <p>
121      * <code><pre>
122      * RelativeDynamicURI dui = new RelativeDynamicURI (data, "UserScreen" );
123      * dui.addPathInfo("user","jon");
124      * dui.toString();
125      * </pre></code>
126      *
127      * The above call to toString() would return the String:
128      *
129      * <p>
130      * /servlets/Turbine/screen/UserScreen/user/jon
131      *
132      * @return A String with the built relative URL.
133      */

134     public String JavaDoc toString()
135     {
136         StringBuffer JavaDoc output = new StringBuffer JavaDoc(128);
137         output.append ( getScriptName() );
138         if ( this.hasPathInfo() )
139         {
140             output.append ( "/" );
141             renderPathInfo(this.pathInfo, output);
142         }
143         if ( this.hasQueryData() )
144         {
145             output.append ( "?" );
146             renderQueryString(this.queryData, output);
147         }
148
149         // There seems to be a bug in Apache JServ 1.0 where the
150
// session id is not appended to the end of the url when a
151
// cookie has not been set.
152
if ( this.res != null )
153         {
154             if ( this.redirect )
155             {
156                 return res.encodeRedirectURL (output.toString());
157             }
158             else
159             {
160                 return res.encodeURL (output.toString());
161             }
162         }
163         else
164         {
165             return output.toString();
166         }
167     }
168
169     /**
170      * Given a RunData object, get a relative URI for the request. This is
171      * necessary sometimes when you want the relative URL and don't want
172      * RelativeDynamicURI to be too smart and remove actions, screens, etc.
173      * This also returns the Query Data where RelativeDynamicURI normally
174      * would not.
175      *
176      * @param data A Turbine RunData object.
177      * @return A String with the relative URL.
178      */

179     public static String JavaDoc toString(RunData data)
180     {
181         StringBuffer JavaDoc output = new StringBuffer JavaDoc(128);
182         HttpServletRequest JavaDoc request = data.getRequest();
183
184         output.append ( data.getScriptName() );
185
186         if ( request.getPathInfo() != null )
187         {
188             output.append( request.getPathInfo() );
189         }
190
191         if ( request.getQueryString() != null )
192         {
193             output.append ( "?" );
194             output.append ( request.getQueryString() );
195         }
196         return output.toString();
197     }
198 }
199
200
201
Popular Tags