001package org.apache.turbine.util.uri;
002
003
004/*
005 * Licensed to the Apache Software Foundation (ASF) under one
006 * or more contributor license agreements.  See the NOTICE file
007 * distributed with this work for additional information
008 * regarding copyright ownership.  The ASF licenses this file
009 * to you under the Apache License, Version 2.0 (the
010 * "License"); you may not use this file except in compliance
011 * with the License.  You may obtain a copy of the License at
012 *
013 *   http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing,
016 * software distributed under the License is distributed on an
017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018 * KIND, either express or implied.  See the License for the
019 * specific language governing permissions and limitations
020 * under the License.
021 */
022
023
024import org.apache.turbine.util.RunData;
025import org.apache.turbine.util.ServerData;
026
027/**
028 * This class can convert a simple link into a turbine relative
029 * URL. It should be used to convert references for images, style
030 * sheets and similar references.
031 *
032 * The resulting links have no query data or path info. If you need
033 * this, use TurbineURI or TemplateURI.
034 *
035 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
036 * @version $Id$
037 *
038 */
039
040public class DataURI
041        extends BaseURI
042{
043    /**
044     * Empty C'tor. Uses Turbine.getDefaultServerData().
045     *
046     */
047    public DataURI()
048    {
049        super();
050    }
051
052    /**
053     * Constructor with a RunData object
054     *
055     * @param runData A RunData object
056     */
057    public DataURI(RunData runData)
058    {
059        super(runData);
060    }
061
062    /**
063     * Constructor, set explicit redirection
064     *
065     * @param runData A RunData object
066     * @param redirect True if redirection allowed.
067     */
068    public DataURI(RunData runData, boolean redirect)
069    {
070        super(runData, redirect);
071    }
072
073    /**
074     * Constructor with a ServerData object
075     *
076     * @param serverData A ServerData object
077     */
078    public DataURI(ServerData serverData)
079    {
080        super(serverData);
081    }
082
083    /**
084     * Constructor, set explicit redirection
085     *
086     * @param serverData A ServerData object
087     * @param redirect True if redirection allowed.
088     */
089    public DataURI(ServerData serverData, boolean redirect)
090    {
091        super(serverData, redirect);
092    }
093
094
095    /**
096     * Content Tool wants to be able to turn the encoding
097     * of the servlet container off. After calling this method,
098     * the encoding will not happen any longer.
099     */
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}