View Javadoc

1   package org.apache.turbine.util.uri;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.turbine.util.RunData;
23  import org.apache.turbine.util.ServerData;
24  
25  /***
26   * This class can convert a simple link into a turbine relative
27   * URL. It should be used to convert references for images, style
28   * sheets and similar references.
29   *
30   * The resulting links have no query data or path info. If you need
31   * this, use TurbineURI or TemplateURI.
32   *
33   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
34   * @version $Id: DataURI.java 644601 2008-04-04 03:58:58Z seade $
35   *
36   */
37  
38  public class DataURI
39          extends BaseURI
40          implements URIConstants
41  {
42      /***
43       * Empty C'tor. Uses Turbine.getDefaultServerData().
44       *
45       */
46      public DataURI()
47      {
48          super();
49      }
50  
51      /***
52       * Constructor with a RunData object
53       *
54       * @param runData A RunData object
55       */
56      public DataURI(RunData runData)
57      {
58          super(runData);
59      }
60  
61      /***
62       * Constructor, set explicit redirection
63       *
64       * @param runData A RunData object
65       * @param redirect True if redirection allowed.
66       */
67      public DataURI(RunData runData, boolean redirect)
68      {
69          super(runData, redirect);
70      }
71  
72      /***
73       * Constructor with a ServerData object
74       *
75       * @param serverData A ServerData object
76       */
77      public DataURI(ServerData serverData)
78      {
79          super(serverData);
80      }
81  
82      /***
83       * Constructor, set explicit redirection
84       *
85       * @param serverData A ServerData object
86       * @param redirect True if redirection allowed.
87       */
88      public DataURI(ServerData serverData, boolean redirect)
89      {
90          super(serverData, redirect);
91      }
92  
93  
94      /***
95       * Content Tool wants to be able to turn the encoding
96       * of the servlet container off. After calling this method,
97       * the encoding will not happen any longer.
98       */
99      public void clearResponse()
100     {
101         setResponse(null);
102     }
103 
104     /***
105      * Builds the URL with all of the data URL-encoded as well as
106      * encoded using HttpServletResponse.encodeUrl(). The resulting
107      * URL is absolute; it starts with http/https...
108      *
109      * <p>
110      * <code><pre>
111      * TurbineURI tui = new TurbineURI (data, "UserScreen");
112      * tui.addPathInfo("user","jon");
113      * tui.getAbsoluteLink();
114      * </pre></code>
115      *
116      *  The above call to getAbsoluteLink() would return the String:
117      *
118      * <p>
119      * http://www.server.com/servlets/Turbine/screen/UserScreen/user/jon
120      *
121      * @return A String with the built URL.
122      */
123     public String getAbsoluteLink()
124     {
125         StringBuffer output = new StringBuffer();
126 
127         getSchemeAndPort(output);
128         getContextAndScript(output);
129 
130         if (hasReference())
131         {
132             output.append('#');
133             output.append(getReference());
134         }
135 
136         //
137         // Encode Response does all the fixup for the Servlet Container
138         //
139         return encodeResponse(output.toString());
140     }
141 
142     /***
143      * Builds the URL with all of the data URL-encoded as well as
144      * encoded using HttpServletResponse.encodeUrl(). The resulting
145      * URL is relative to the webserver root.
146      *
147      * <p>
148      * <code><pre>
149      * TurbineURI tui = new TurbineURI (data, "UserScreen");
150      * tui.addPathInfo("user","jon");
151      * tui.getRelativeLink();
152      * </pre></code>
153      *
154      *  The above call to getRelativeLink() would return the String:
155      *
156      * <p>
157      * /servlets/Turbine/screen/UserScreen/user/jon
158      *
159      * @return A String with the built URL.
160      */
161     public String getRelativeLink()
162     {
163         StringBuffer output = new StringBuffer();
164 
165         getContextAndScript(output);
166 
167         if (hasReference())
168         {
169             output.append('#');
170             output.append(getReference());
171         }
172 
173         //
174         // Encode Response does all the fixup for the Servlet Container
175         //
176         return encodeResponse(output.toString());
177     }
178 
179     /***
180      * toString() simply calls getAbsoluteLink. You should not use this in your
181      * code unless you have to. Use getAbsoluteLink.
182      *
183      * @return This URI as a String
184      *
185      */
186     public String toString()
187     {
188         return getAbsoluteLink();
189     }
190 }