1 package org.apache.turbine.services.pull.tools;
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
25 import org.apache.commons.configuration.Configuration;
26 import org.apache.turbine.Turbine;
27 import org.apache.turbine.pipeline.PipelineData;
28 import org.apache.turbine.services.pull.ApplicationTool;
29 import org.apache.turbine.util.RunData;
30 import org.apache.turbine.util.uri.DataURI;
31
32 /**
33 * Terribly simple tool to translate URIs into Turbine Links.
34 * Equivalent to URIUtils.getAbsoluteLink() in a pull tool.
35 *
36 * <p>
37 * If you're missing any routines from the 'old' $content tool concerning
38 * path_info or query data, you did use the wrong tool then. You should've used
39 * the TemplateLink tool which should be available as "$link" in your context.
40 * <p>
41 *
42 * This is an application pull tool for the template system. You should <b>not</b>
43 * use it in a normal application!
44 *
45 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
46 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
47 * @version $Id: ContentTool.java 1706239 2015-10-01 13:18:35Z tv $
48 */
49
50 public class ContentTool
51 implements ApplicationTool
52 {
53 /** Prefix for Parameters for this tool */
54 public static final String CONTENT_TOOL_PREFIX = "tool.content";
55
56 /**
57 * Should this tool add Container Encoding to the URIs returned?
58 * True might cause trouble e.g. if you run with Apache HTTP Daemon / Tomcat Combo.
59 *
60 * Default is false (like Turbine 2.2)
61 */
62 public static final String CONTENT_TOOL_ENCODING_KEY = "want.encoding";
63
64 /** Default Value for CONTENT_TOOL_ENCODING_KEY */
65 public static final boolean CONTENT_TOOL_ENCODING_DEFAULT = false;
66
67 /** Should this tool return relative URIs or absolute? Default: Absolute. */
68 public static final String CONTENT_TOOL_RELATIVE_KEY = "want.relative";
69
70 /** Default Value for CONTENT_TOOL_RELATIVE_KEY */
71 public static final boolean CONTENT_TOOL_RELATIVE_DEFAULT = false;
72
73 /** Do we want the container to encode the response? */
74 boolean wantEncoding = false;
75
76 /** Do we want a relative link? */
77 boolean wantRelative = false;
78
79 /** Caches a DataURI object which provides the translation routines */
80 private DataURI dataURI = null;
81
82 /**
83 * C'tor
84 */
85 public ContentTool()
86 {
87 // empty
88 }
89
90 /*
91 * ========================================================================
92 *
93 * Application Tool Interface
94 *
95 * ========================================================================
96 *
97 */
98
99 /**
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 }