View Javadoc

1   package org.apache.turbine.util;
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.HttpServletRequest;
20  
21  /***
22   * This creates a Dynamic URI for use within the Turbine system
23   *
24   * <p>If you use this class to generate all of your href tags as well
25   * as all of your URI's, then you will not need to worry about having
26   * session data setup for you or using HttpServletRequest.encodeUrl()
27   * since this class does everything for you.
28   * This class generates relative URI's which can be used in environments with
29   * firewalls and gateways for outgoing connections.
30   *
31   * <code><pre>
32   * RelativeDynamicURI dui = new RelativeDynamicURI (data, "UserScreen" );
33   * dui.setName("Click Here").addPathInfo("user","jon");
34   * dui.getA();
35   * </pre></code>
36   *
37   * The above call to getA() would return the String:
38   *
39   * &lt;A HREF="/servlets/Turbine/screen=UserScreen&amp;amp;user=jon"&gt;ClickHere&lt;/A&gt;
40   *
41   * @author <a href="mailto:dfaller@raleigh.ibm.com">David S. Faller</a>
42   * @version $Id: RelativeDynamicURI.java 264148 2005-08-29 14:21:04Z henning $
43   * @deprecated Use {@link org.apache.turbine.util.uri.TurbineURI} with {@link org.apache.turbine.util.uri.TurbineURI#getRelativeLink} instead.
44   */
45  public class RelativeDynamicURI extends DynamicURI
46  {
47      /***
48       * Default constructor - one of the init methods must be called before use.
49       */
50      public RelativeDynamicURI()
51      {
52      }
53  
54      /***
55       * Constructor sets up some variables.
56       *
57       * @param data A Turbine RunData object.
58       */
59      public RelativeDynamicURI(RunData data)
60      {
61          super(data);
62      }
63  
64      /***
65       * Constructor sets up some variables.
66       *
67       * @param data A Turbine RunData object.
68       * @param screen A String with the name of a screen.
69       */
70      public RelativeDynamicURI(RunData data, String screen)
71      {
72          super(data, screen);
73      }
74  
75      /***
76       * Constructor sets up some variables.
77       *
78       * @param data A Turbine RunData object.
79       * @param screen A String with the name of a screen.
80       * @param action A String with the name of an action.
81       */
82      public RelativeDynamicURI(RunData data, String screen, String action)
83      {
84          super(data, screen, action);
85      }
86  
87      /***
88       * Constructor sets up some variables.
89       *
90       * @param data A Turbine RunData object.
91       * @param screen A String with the name of a screen.
92       * @param action A String with the name of an action.
93       * @param redirect True if it should redirect.
94       */
95      public RelativeDynamicURI(RunData data,
96                                String screen,
97                                String action,
98                                boolean redirect)
99      {
100         super(data, screen, action, redirect);
101     }
102 
103     /***
104      * Constructor sets up some variables.
105      *
106      * @param data A Turbine RunData object.
107      * @param screen A String with the name of a screen.
108      * @param redirect True if it should redirect.
109      */
110     public RelativeDynamicURI(RunData data,
111                               String screen,
112                               boolean redirect)
113     {
114         super(data, screen, redirect);
115     }
116 
117     /***
118      * Constructor sets up some variables.
119      *
120      * @param data A Turbine RunData object.
121      * @param redirect True if it should redirect.
122      */
123     public RelativeDynamicURI(RunData data, boolean redirect)
124     {
125         super(data, redirect);
126     }
127 
128     /***
129      * Main constructor for RelativeDynamicURI.  Uses ServerData.
130      *
131      * @param sd A ServerData.
132      */
133     public RelativeDynamicURI(ServerData sd)
134     {
135         super(sd);
136     }
137 
138     /***
139      * Main constructor for RelativeDynamicURI.  Uses ServerData.
140      *
141      * @param sd A ServerData.
142      * @param screen A String with the name of a screen.
143      */
144     public RelativeDynamicURI(ServerData sd, String screen)
145     {
146         super(sd, screen);
147     }
148 
149     /***
150      * Main constructor for RelativeDynamicURI.  Uses ServerData.
151      *
152      * @param sd A ServerData.
153      * @param screen A String with the name of a screen.
154      * @param action A String with the name of an action.
155      */
156     public RelativeDynamicURI(ServerData sd,
157                               String screen,
158                               String action)
159     {
160         super(sd, screen, action);
161     }
162 
163     /***
164      * Main constructor for RelativeDynamicURI.  Uses ServerData.
165      *
166      * @param sd A ServerData.
167      * @param screen A String with the name of a screen.
168      * @param action A String with the name of an action.
169      * @param redirect True if it should redirect.
170      */
171     public RelativeDynamicURI(ServerData sd,
172                               String screen,
173                               String action,
174                               boolean redirect)
175     {
176         super(sd, screen, action, redirect);
177     }
178 
179     /***
180      * Main constructor for RelativeDynamicURI.  Uses ServerData.
181      *
182      * @param sd A ServerData.
183      * @param screen A String with the name of a screen.
184      * @param redirect True if it should redirect.
185      */
186     public RelativeDynamicURI(ServerData sd,
187                               String screen,
188                               boolean redirect)
189     {
190         super(sd, screen, redirect);
191     }
192 
193     /***
194      * Main constructor for RelativeDynamicURI.  Uses ServerData.
195      *
196      * @param sd A ServerData.
197      * @param redirect True if it should redirect.
198      */
199     public RelativeDynamicURI(ServerData sd, boolean redirect)
200     {
201         super(sd, redirect);
202     }
203 
204     /***
205      * Builds the relative URL with all of the data URL-encoded as well as
206      * encoded using HttpServletResponse.encodeUrl().
207      *
208      * <p>
209      * <code><pre>
210      * RelativeDynamicURI dui = new RelativeDynamicURI (data, "UserScreen" );
211      * dui.addPathInfo("user","jon");
212      * dui.toString();
213      * </pre></code>
214      *
215      *  The above call to toString() would return the String:
216      *
217      * <p>
218      * /servlets/Turbine/screen/UserScreen/user/jon
219      *
220      * @return A String with the built relative URL.
221      */
222     public String toString()
223     {
224         assertInitialized();
225         StringBuffer output = new StringBuffer();
226         output.append(getContextPath());
227         output.append(getScriptName());
228         if (this.hasPathInfo)
229         {
230             output.append("/");
231             output.append(renderPathInfo(this.pathInfo));
232         }
233         if (this.hasQueryData)
234         {
235             output.append("?");
236             output.append(renderQueryString(this.queryData));
237         }
238         if (this.reference != null)
239         {
240             output.append("#");
241             output.append(this.getReference());
242         }
243 
244         // There seems to be a bug in Apache JServ 1.0 where the
245         // session id is not appended to the end of the url when a
246         // cookie has not been set.
247         if (this.res != null)
248         {
249             if (this.redirect)
250             {
251                 return res.encodeRedirectURL(output.toString());
252             }
253             else
254             {
255                 return res.encodeURL(output.toString());
256             }
257         }
258         else
259         {
260             return output.toString();
261         }
262     }
263 
264     /***
265      * Given a RunData object, get a relative URI for the request.  This is
266      * necessary sometimes when you want the relative URL and don't want
267      * RelativeDynamicURI to be too smart and remove actions, screens, etc.
268      * This also returns the Query Data where RelativeDynamicURI normally
269      * would not.
270      *
271      * @param data A Turbine RunData object.
272      * @return A String with the relative URL.
273      */
274     public static String toString(RunData data)
275     {
276         StringBuffer output = new StringBuffer();
277         HttpServletRequest request = data.getRequest();
278 
279         output.append(data.getServerData().getContextPath());
280         output.append(data.getServerData().getScriptName());
281 
282         if (request.getPathInfo() != null)
283         {
284             output.append(request.getPathInfo());
285         }
286 
287         if (request.getQueryString() != null)
288         {
289             output.append("?");
290             output.append(request.getQueryString());
291         }
292         return output.toString();
293     }
294 }