View Javadoc

1   package org.apache.turbine.services.pull.tools;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.configuration.Configuration;
23  
24  import org.apache.turbine.Turbine;
25  import org.apache.turbine.services.pull.ApplicationTool;
26  import org.apache.turbine.util.RunData;
27  import org.apache.turbine.util.uri.DataURI;
28  
29  /***
30   * Terribly simple tool to translate URIs into Turbine Links.
31   * Equivalent to URIUtils.getAbsoluteLink() in a pull tool.
32   *
33   * <p>
34   * If you're missing any routines from the 'old' $content tool concerning
35   * path_info or query data, you did use the wrong tool then. You should've used
36   * the TemplateLink tool which should be available as "$link" in your context.
37   * <p>
38   *
39   * This is an application pull tool for the template system. You should <b>not</b>
40   * use it in a normal application!
41   *
42   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
43   * @version $Id: ContentTool.java 534527 2007-05-02 16:10:59Z tv $
44   */
45  
46  public class ContentTool
47      implements ApplicationTool
48  {
49      /*** Prefix for Parameters for this tool */
50      public static final String CONTENT_TOOL_PREFIX = "tool.content";
51  
52      /***
53       * Should this tool add Container Encoding to the URIs returned?
54       * True might cause trouble e.g. if you run with Apache HTTP Daemon / Tomcat Combo.
55       *
56       * Default is false (like Turbine 2.2)
57       */
58      public static final String CONTENT_TOOL_ENCODING_KEY = "want.encoding";
59  
60      /*** Default Value for CONTENT_TOOL_ENCODING_KEY */
61      public static final boolean CONTENT_TOOL_ENCODING_DEFAULT = false;
62  
63      /*** Should this tool return relative URIs or absolute? Default: Absolute. */
64      public static final String CONTENT_TOOL_RELATIVE_KEY = "want.relative";
65  
66      /*** Default Value for CONTENT_TOOL_RELATIVE_KEY */
67      public static final boolean CONTENT_TOOL_RELATIVE_DEFAULT = false;
68  
69      /*** Do we want the container to encode the response? */
70      boolean wantEncoding = false;
71  
72      /*** Do we want a relative link? */
73      boolean wantRelative = false;
74  
75      /*** Caches a DataURI object which provides the translation routines */
76      private DataURI dataURI = null;
77  
78      /***
79       * C'tor
80       */
81      public ContentTool()
82      {
83      }
84  
85      /*
86       * ========================================================================
87       *
88       * Application Tool Interface
89       *
90       * ========================================================================
91       *
92       */
93  
94      /***
95       * This will initialise a ContentTool object that was
96       * constructed with the default constructor (ApplicationTool
97       * method).
98       *
99       * @param data assumed to be a RunData object
100      */
101     public void init(Object data)
102     {
103         // we just blithely cast to RunData as if another object
104         // or null is passed in we'll throw an appropriate runtime
105         // exception.
106         dataURI = new DataURI((RunData) data);
107 
108         Configuration conf =
109                 Turbine.getConfiguration().subset(CONTENT_TOOL_PREFIX);
110 
111         if (conf != null)
112         {
113             wantRelative = conf.getBoolean(CONTENT_TOOL_RELATIVE_KEY,
114                     CONTENT_TOOL_RELATIVE_DEFAULT);
115 
116             wantEncoding = conf.getBoolean(CONTENT_TOOL_ENCODING_KEY,
117                     CONTENT_TOOL_ENCODING_DEFAULT);
118         }
119 
120         if (!wantEncoding)
121         {
122             dataURI.clearResponse();
123         }
124     }
125 
126     /***
127      * Refresh method - does nothing
128      */
129     public void refresh()
130     {
131         // empty
132     }
133 
134     /***
135      * Returns the Turbine URI of a given Path
136      *
137      * @param path The path to translate
138      *
139      * @return Turbine translated absolute path
140      */
141     public String getURI(String path)
142     {
143         dataURI.setScriptName(path);
144 
145         return wantRelative ?
146                 dataURI.getRelativeLink() : dataURI.getAbsoluteLink();
147     }
148 
149     /***
150      * Returns the Turbine URI of a given Path. The
151      * result is always an absolute path starting with
152      * the server scheme (http/https).
153      *
154      * @param path The path to translate
155      *
156      * @return Turbine translated absolute path
157      */
158     public String getAbsoluteURI(String path)
159     {
160         dataURI.setScriptName(path);
161 
162         return dataURI.getAbsoluteLink();
163     }
164 
165     /***
166      * Returns the Turbine URI of a given Path. The
167      * result is always relative to the context of
168      * the application.
169      *
170      * @param path The path to translate
171      *
172      * @return Turbine translated absolute path
173      */
174     public String getRelativeURI(String path)
175     {
176         dataURI.setScriptName(path);
177 
178         return dataURI.getRelativeLink();
179     }
180 
181 }