View Javadoc

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