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$
37   *
38   */
39  
40  public class DataURI
41          extends BaseURI
42  {
43      /**
44       * Empty C'tor. Uses Turbine.getDefaultServerData().
45       *
46       */
47      public DataURI()
48      {
49          super();
50      }
51  
52      /**
53       * Constructor with a RunData object
54       *
55       * @param runData A RunData object
56       */
57      public DataURI(RunData runData)
58      {
59          super(runData);
60      }
61  
62      /**
63       * Constructor, set explicit redirection
64       *
65       * @param runData A RunData object
66       * @param redirect True if redirection allowed.
67       */
68      public DataURI(RunData runData, boolean redirect)
69      {
70          super(runData, redirect);
71      }
72  
73      /**
74       * Constructor with a ServerData object
75       *
76       * @param serverData A ServerData object
77       */
78      public DataURI(ServerData serverData)
79      {
80          super(serverData);
81      }
82  
83      /**
84       * Constructor, set explicit redirection
85       *
86       * @param serverData A ServerData object
87       * @param redirect True if redirection allowed.
88       */
89      public DataURI(ServerData serverData, boolean redirect)
90      {
91          super(serverData, redirect);
92      }
93  
94  
95      /**
96       * Content Tool wants to be able to turn the encoding
97       * of the servlet container off. After calling this method,
98       * the encoding will not happen any longer.
99       */
100     public void clearResponse()
101     {
102         setResponse(null);
103     }
104 
105     /**
106      * Builds the URL with all of the data URL-encoded as well as
107      * encoded using HttpServletResponse.encodeUrl(). The resulting
108      * URL is absolute; it starts with http/https...
109      *
110      * <pre>
111      * TurbineURI tui = new TurbineURI (data, "UserScreen");
112      * tui.addPathInfo("user","jon");
113      * tui.getAbsoluteLink();
114      * </pre>
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      * </p>
121      *
122      * @return A String with the built URL.
123      */
124     public String getAbsoluteLink()
125     {
126         StringBuilder output = new StringBuilder();
127 
128         getSchemeAndPort(output);
129         getContextAndScript(output);
130 
131         if (hasReference())
132         {
133             output.append('#');
134             output.append(getReference());
135         }
136 
137         //
138         // Encode Response does all the fixup for the Servlet Container
139         //
140         return encodeResponse(output.toString());
141     }
142 
143     /**
144      * Builds the URL with all of the data URL-encoded as well as
145      * encoded using HttpServletResponse.encodeUrl(). The resulting
146      * URL is relative to the webserver root.
147      *
148      * <pre>
149      * TurbineURI tui = new TurbineURI (data, "UserScreen");
150      * tui.addPathInfo("user","jon");
151      * tui.getRelativeLink();
152      * </pre>
153      *
154      *  The above call to getRelativeLink() would return the String:
155      *
156      * <p>
157      * /servlets/Turbine/screen/UserScreen/user/jon
158      * </p>
159      *
160      * @return A String with the built URL.
161      */
162     public String getRelativeLink()
163     {
164         StringBuilder output = new StringBuilder();
165 
166         getContextAndScript(output);
167 
168         if (hasReference())
169         {
170             output.append('#');
171             output.append(getReference());
172         }
173 
174         //
175         // Encode Response does all the fixup for the Servlet Container
176         //
177         return encodeResponse(output.toString());
178     }
179 
180     /**
181      * toString() simply calls getAbsoluteLink. You should not use this in your
182      * code unless you have to. Use getAbsoluteLink.
183      *
184      * @return This URI as a String
185      *
186      */
187     @Override
188     public String toString()
189     {
190         return getAbsoluteLink();
191     }
192 }