View Javadoc

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