View Javadoc

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 org.apache.turbine.util.RunData;
20  import org.apache.turbine.util.ServerData;
21  
22  /***
23   * This class can convert a simple link into a turbine relative
24   * URL. It should be used to convert references for images, style
25   * sheets and similar references.
26   *
27   * The resulting links have no query data or path info. If you need
28   * this, use TurbineURI or TemplateURI.
29   *
30   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
31   * @version $Id: DataURI.java 264148 2005-08-29 14:21:04Z henning $
32   *
33   */
34  
35  public class DataURI
36          extends BaseURI
37          implements URIConstants
38  {
39      /***
40       * Empty C'tor. Uses Turbine.getDefaultServerData().
41       *
42       */
43      public DataURI()
44      {
45          super();
46      }
47  
48      /***
49       * Constructor with a RunData object
50       *
51       * @param runData A RunData object
52       */
53      public DataURI(RunData runData)
54      {
55          super(runData);
56      }
57  
58      /***
59       * Constructor, set explicit redirection
60       *
61       * @param runData A RunData object
62       * @param redirect True if redirection allowed.
63       */
64      public DataURI(RunData runData, boolean redirect)
65      {
66          super(runData, redirect);
67      }
68  
69      /***
70       * Constructor with a ServerData object
71       *
72       * @param serverData A ServerData object
73       */
74      public DataURI(ServerData serverData)
75      {
76          super(serverData);
77      }
78  
79      /***
80       * Constructor, set explicit redirection
81       *
82       * @param serverData A ServerData object
83       * @param redirect True if redirection allowed.
84       */
85      public DataURI(ServerData serverData, boolean redirect)
86      {
87          super(serverData, redirect);
88      }
89  
90  
91      /***
92       * Content Tool wants to be able to turn the encoding
93       * of the servlet container off. After calling this method,
94       * the encoding will not happen any longer.
95       */
96      public void clearResponse()
97      {
98          setResponse(null);
99      }
100 
101     /***
102      * Builds the URL with all of the data URL-encoded as well as
103      * encoded using HttpServletResponse.encodeUrl(). The resulting
104      * URL is absolute; it starts with http/https...
105      *
106      * <p>
107      * <code><pre>
108      * TurbineURI tui = new TurbineURI (data, "UserScreen");
109      * tui.addPathInfo("user","jon");
110      * tui.getAbsoluteLink();
111      * </pre></code>
112      *
113      *  The above call to absoluteLink() would return the String:
114      *
115      * <p>
116      * http://www.server.com/servlets/Turbine/screen/UserScreen/user/jon
117      *
118      * @return A String with the built URL.
119      */
120     public String getAbsoluteLink()
121     {
122         StringBuffer output = new StringBuffer();
123 
124         getSchemeAndPort(output);
125         getContextAndScript(output);
126 
127         if (hasReference())
128         {
129             output.append('#');
130             output.append(getReference());
131         }
132 
133         //
134         // Encode Response does all the fixup for the Servlet Container
135         //
136         return encodeResponse(output.toString());
137     }
138 
139     /***
140      * Builds the URL with all of the data URL-encoded as well as
141      * encoded using HttpServletResponse.encodeUrl(). The resulting
142      * URL is relative to the webserver root.
143      *
144      * <p>
145      * <code><pre>
146      * TurbineURI tui = new TurbineURI (data, "UserScreen");
147      * tui.addPathInfo("user","jon");
148      * tui.getRelativeLink();
149      * </pre></code>
150      *
151      *  The above call to absoluteLink() would return the String:
152      *
153      * <p>
154      * /servlets/Turbine/screen/UserScreen/user/jon
155      *
156      * @return A String with the built URL.
157      */
158     public String getRelativeLink()
159     {
160         StringBuffer output = new StringBuffer();
161 
162         getContextAndScript(output);
163 
164         if (hasReference())
165         {
166             output.append('#');
167             output.append(getReference());
168         }
169 
170         //
171         // Encode Response does all the fixup for the Servlet Container
172         //
173         return encodeResponse(output.toString());
174     }
175 
176     /***
177      * toString() simply calls getAbsoluteLink. You should not use this in your
178      * code unless you have to. Use getAbsoluteLink.
179      *
180      * @return This URI as a String
181      *
182      */
183     public String toString()
184     {
185         return getAbsoluteLink();
186     }
187 }