001 package 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
025 import org.apache.commons.configuration.Configuration;
026 import org.apache.turbine.Turbine;
027 import org.apache.turbine.pipeline.PipelineData;
028 import org.apache.turbine.services.pull.ApplicationTool;
029 import org.apache.turbine.util.RunData;
030 import 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: ContentTool.java 1078552 2011-03-06 19:58:46Z tv $
048 */
049
050 public 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 initialise a ContentTool object that was
101 * constructed with the default constructor (ApplicationTool
102 * method).
103 *
104 * @param data assumed to be a RunData object
105 */
106 public void init(Object data)
107 {
108 // we just blithely cast to RunData as if another object
109 // or null is passed in we'll throw an appropriate runtime
110 // exception.
111 if (data instanceof PipelineData)
112 {
113 PipelineData pipelineData = (PipelineData) data;
114 RunData runData = (RunData)pipelineData;
115 dataURI = new DataURI(runData);
116 }
117 else
118 {
119 dataURI = new DataURI((RunData) data);
120
121 }
122
123 Configuration conf =
124 Turbine.getConfiguration().subset(CONTENT_TOOL_PREFIX);
125
126 if (conf != null)
127 {
128 wantRelative = conf.getBoolean(CONTENT_TOOL_RELATIVE_KEY,
129 CONTENT_TOOL_RELATIVE_DEFAULT);
130
131 wantEncoding = conf.getBoolean(CONTENT_TOOL_ENCODING_KEY,
132 CONTENT_TOOL_ENCODING_DEFAULT);
133 }
134
135 if (!wantEncoding)
136 {
137 dataURI.clearResponse();
138 }
139 }
140
141 /**
142 * Refresh method - does nothing
143 */
144 public void refresh()
145 {
146 // empty
147 }
148
149 /**
150 * Returns the Turbine URI of a given Path
151 *
152 * @param path The path to translate
153 *
154 * @return Turbine translated absolute path
155 */
156 public String getURI(String path)
157 {
158 dataURI.setScriptName(path);
159
160 return wantRelative ?
161 dataURI.getRelativeLink() : dataURI.getAbsoluteLink();
162 }
163
164 /**
165 * Returns the Turbine URI of a given Path. The
166 * result is always an absolute path starting with
167 * the server scheme (http/https).
168 *
169 * @param path The path to translate
170 *
171 * @return Turbine translated absolute path
172 */
173 public String getAbsoluteURI(String path)
174 {
175 dataURI.setScriptName(path);
176
177 return dataURI.getAbsoluteLink();
178 }
179
180 /**
181 * Returns the Turbine URI of a given Path. The
182 * result is always relative to the context of
183 * the application.
184 *
185 * @param path The path to translate
186 *
187 * @return Turbine translated absolute path
188 */
189 public String getRelativeURI(String path)
190 {
191 dataURI.setScriptName(path);
192
193 return dataURI.getRelativeLink();
194 }
195
196 }