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 1078552 2011-03-06 19:58:46Z 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 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 }