001package org.apache.turbine.services.pull.tools;
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
025import org.apache.commons.configuration2.Configuration;
026import org.apache.turbine.Turbine;
027import org.apache.turbine.pipeline.PipelineData;
028import org.apache.turbine.services.pull.ApplicationTool;
029import org.apache.turbine.util.RunData;
030import org.apache.turbine.util.uri.DataURI;
031
032/**
033 * Terribly simple tool to translate URIs into Turbine Links.
034 * Equivalent to URIUtils.getAbsoluteLink() in a pull tool.
035 *
036 * <p>
037 * If you're missing any routines from the 'old' $content tool concerning
038 * path_info or query data, you did use the wrong tool then. You should've used
039 * the TemplateLink tool which should be available as "$link" in your context.
040 * <p>
041 *
042 * This is an application pull tool for the template system. You should <b>not</b>
043 * use it in a normal application!
044 *
045 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
046 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
047 * @version $Id$
048 */
049
050public class ContentTool
051    implements ApplicationTool
052{
053    /** Prefix for Parameters for this tool */
054    public static final String CONTENT_TOOL_PREFIX = "tool.content";
055
056    /**
057     * Should this tool add Container Encoding to the URIs returned?
058     * True might cause trouble e.g. if you run with Apache HTTP Daemon / Tomcat Combo.
059     *
060     * Default is false (like Turbine 2.2)
061     */
062    public static final String CONTENT_TOOL_ENCODING_KEY = "want.encoding";
063
064    /** Default Value for CONTENT_TOOL_ENCODING_KEY */
065    public static final boolean CONTENT_TOOL_ENCODING_DEFAULT = false;
066
067    /** Should this tool return relative URIs or absolute? Default: Absolute. */
068    public static final String CONTENT_TOOL_RELATIVE_KEY = "want.relative";
069
070    /** Default Value for CONTENT_TOOL_RELATIVE_KEY */
071    public static final boolean CONTENT_TOOL_RELATIVE_DEFAULT = false;
072
073    /** Do we want the container to encode the response? */
074    boolean wantEncoding = false;
075
076    /** Do we want a relative link? */
077    boolean wantRelative = false;
078
079    /** Caches a DataURI object which provides the translation routines */
080    private DataURI dataURI = null;
081
082    /**
083     * C'tor
084     */
085    public ContentTool()
086    {
087        // empty
088    }
089
090    /*
091     * ========================================================================
092     *
093     * Application Tool Interface
094     *
095     * ========================================================================
096     *
097     */
098
099    /**
100     * This will initialize a ContentTool object that was
101     * constructed with the default constructor (ApplicationTool
102     * method).
103     *
104     * @param data assumed to be a PipelineData object
105     */
106    @Override
107    public void init(Object data)
108    {
109        // we just blithely cast to RunData as if another object
110        // or null is passed in we'll throw an appropriate runtime
111        // exception.
112        if (data instanceof PipelineData)
113        {
114            PipelineData pipelineData = (PipelineData) data;
115            RunData runData = (RunData)pipelineData;
116            dataURI = new DataURI(runData);
117        }
118        else
119        {
120            dataURI = new DataURI((RunData) data);
121
122        }
123
124        Configuration conf =
125                Turbine.getConfiguration().subset(CONTENT_TOOL_PREFIX);
126
127        if (conf != null)
128        {
129            wantRelative = conf.getBoolean(CONTENT_TOOL_RELATIVE_KEY,
130                    CONTENT_TOOL_RELATIVE_DEFAULT);
131
132            wantEncoding = conf.getBoolean(CONTENT_TOOL_ENCODING_KEY,
133                    CONTENT_TOOL_ENCODING_DEFAULT);
134        }
135
136        if (!wantEncoding)
137        {
138            dataURI.clearResponse();
139        }
140    }
141
142    /**
143     * Refresh method - does nothing
144     */
145    @Override
146    public void refresh()
147    {
148        // empty
149    }
150
151    /**
152     * Returns the Turbine URI of a given Path
153     *
154     * @param path The path to translate
155     *
156     * @return Turbine translated absolute path
157     */
158    public String getURI(String path)
159    {
160        dataURI.setScriptName(path);
161
162        return wantRelative ?
163                dataURI.getRelativeLink() : dataURI.getAbsoluteLink();
164    }
165
166    /**
167     * Returns the Turbine URI of a given Path. The
168     * result is always an absolute path starting with
169     * the server scheme (http/https).
170     *
171     * @param path The path to translate
172     *
173     * @return Turbine translated absolute path
174     */
175    public String getAbsoluteURI(String path)
176    {
177        dataURI.setScriptName(path);
178
179        return dataURI.getAbsoluteLink();
180    }
181
182    /**
183     * Returns the Turbine URI of a given Path. The
184     * result is always relative to the context of
185     * the application.
186     *
187     * @param path The path to translate
188     *
189     * @return Turbine translated absolute path
190     */
191    public String getRelativeURI(String path)
192    {
193        dataURI.setScriptName(path);
194
195        return dataURI.getRelativeLink();
196    }
197
198}