001: package org.apache.turbine.services.pull.tools;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.commons.configuration.Configuration;
023:
024: import org.apache.turbine.Turbine;
025: import org.apache.turbine.services.pull.ApplicationTool;
026: import org.apache.turbine.util.RunData;
027: import org.apache.turbine.util.uri.DataURI;
028:
029: /**
030: * Terribly simple tool to translate URIs into Turbine Links.
031: * Equivalent to URIUtils.getAbsoluteLink() in a pull tool.
032: *
033: * <p>
034: * If you're missing any routines from the 'old' $content tool concerning
035: * path_info or query data, you did use the wrong tool then. You should've used
036: * the TemplateLink tool which should be available as "$link" in your context.
037: * <p>
038: *
039: * This is an application pull tool for the template system. You should <b>not</b>
040: * use it in a normal application!
041: *
042: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
043: * @version $Id: ContentTool.java 534527 2007-05-02 16:10:59Z tv $
044: */
045:
046: public class ContentTool implements ApplicationTool {
047: /** Prefix for Parameters for this tool */
048: public static final String CONTENT_TOOL_PREFIX = "tool.content";
049:
050: /**
051: * Should this tool add Container Encoding to the URIs returned?
052: * True might cause trouble e.g. if you run with Apache HTTP Daemon / Tomcat Combo.
053: *
054: * Default is false (like Turbine 2.2)
055: */
056: public static final String CONTENT_TOOL_ENCODING_KEY = "want.encoding";
057:
058: /** Default Value for CONTENT_TOOL_ENCODING_KEY */
059: public static final boolean CONTENT_TOOL_ENCODING_DEFAULT = false;
060:
061: /** Should this tool return relative URIs or absolute? Default: Absolute. */
062: public static final String CONTENT_TOOL_RELATIVE_KEY = "want.relative";
063:
064: /** Default Value for CONTENT_TOOL_RELATIVE_KEY */
065: public static final boolean CONTENT_TOOL_RELATIVE_DEFAULT = false;
066:
067: /** Do we want the container to encode the response? */
068: boolean wantEncoding = false;
069:
070: /** Do we want a relative link? */
071: boolean wantRelative = false;
072:
073: /** Caches a DataURI object which provides the translation routines */
074: private DataURI dataURI = null;
075:
076: /**
077: * C'tor
078: */
079: public ContentTool() {
080: }
081:
082: /*
083: * ========================================================================
084: *
085: * Application Tool Interface
086: *
087: * ========================================================================
088: *
089: */
090:
091: /**
092: * This will initialise a ContentTool object that was
093: * constructed with the default constructor (ApplicationTool
094: * method).
095: *
096: * @param data assumed to be a RunData object
097: */
098: public void init(Object data) {
099: // we just blithely cast to RunData as if another object
100: // or null is passed in we'll throw an appropriate runtime
101: // exception.
102: dataURI = new DataURI((RunData) data);
103:
104: Configuration conf = Turbine.getConfiguration().subset(
105: CONTENT_TOOL_PREFIX);
106:
107: if (conf != null) {
108: wantRelative = conf.getBoolean(CONTENT_TOOL_RELATIVE_KEY,
109: CONTENT_TOOL_RELATIVE_DEFAULT);
110:
111: wantEncoding = conf.getBoolean(CONTENT_TOOL_ENCODING_KEY,
112: CONTENT_TOOL_ENCODING_DEFAULT);
113: }
114:
115: if (!wantEncoding) {
116: dataURI.clearResponse();
117: }
118: }
119:
120: /**
121: * Refresh method - does nothing
122: */
123: public void refresh() {
124: // empty
125: }
126:
127: /**
128: * Returns the Turbine URI of a given Path
129: *
130: * @param path The path to translate
131: *
132: * @return Turbine translated absolute path
133: */
134: public String getURI(String path) {
135: dataURI.setScriptName(path);
136:
137: return wantRelative ? dataURI.getRelativeLink() : dataURI
138: .getAbsoluteLink();
139: }
140:
141: /**
142: * Returns the Turbine URI of a given Path. The
143: * result is always an absolute path starting with
144: * the server scheme (http/https).
145: *
146: * @param path The path to translate
147: *
148: * @return Turbine translated absolute path
149: */
150: public String getAbsoluteURI(String path) {
151: dataURI.setScriptName(path);
152:
153: return dataURI.getAbsoluteLink();
154: }
155:
156: /**
157: * Returns the Turbine URI of a given Path. The
158: * result is always relative to the context of
159: * the application.
160: *
161: * @param path The path to translate
162: *
163: * @return Turbine translated absolute path
164: */
165: public String getRelativeURI(String path) {
166: dataURI.setScriptName(path);
167:
168: return dataURI.getRelativeLink();
169: }
170:
171: }
|