    001    package 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
    024    import org.apache.turbine.util.RunData;
    025    import 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: DataURI.java 938645 2010-04-27 20:57:51Z tv $
    037     *
    038     */
    039
    040    public class DataURI
    041            extends BaseURI
    042            implements URIConstants
    043    {
    044        /**
    045         * Empty C'tor. Uses Turbine.getDefaultServerData().
    046         *
    047         */
    048        public DataURI()
    049        {
    050            super();
    051        }
    052
    053        /**
    054         * Constructor with a RunData object
    055         *
    056         * @param runData A RunData object
    057         */
    058        public DataURI(RunData runData)
    059        {
    060            super(runData);
    061        }
    062
    063        /**
    064         * Constructor, set explicit redirection
    065         *
    066         * @param runData A RunData object
    067         * @param redirect True if redirection allowed.
    068         */
    069        public DataURI(RunData runData, boolean redirect)
    070        {
    071            super(runData, redirect);
    072        }
    073
    074        /**
    075         * Constructor with a ServerData object
    076         *
    077         * @param serverData A ServerData object
    078         */
    079        public DataURI(ServerData serverData)
    080        {
    081            super(serverData);
    082        }
    083
    084        /**
    085         * Constructor, set explicit redirection
    086         *
    087         * @param serverData A ServerData object
    088         * @param redirect True if redirection allowed.
    089         */
    090        public DataURI(ServerData serverData, boolean redirect)
    091        {
    092            super(serverData, redirect);
    093        }
    094
    095
    096        /**
    097         * Content Tool wants to be able to turn the encoding
    098         * of the servlet container off. After calling this method,
    099         * 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    }