View Javadoc
1   package org.apache.turbine.services.urlmapper;
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.turbine.annotation.TurbineService;
23  import org.apache.turbine.services.pull.tools.TemplateLink;
24  
25  /**
26   * This is a pull to to be used in Templates to convert links in
27   * Templates into the correct references.
28   *
29   * The pull service might insert this tool into the Context.
30   * in templates.  Here's an example of its Velocity use:
31   *
32   * <p><code>
33   * $link.setPage("index.vm").addPathInfo("hello","world")
34   * This would return: http://foo.com/Turbine/template/index.vm/hello/world
35   * </code>
36   *
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="mbryson@mont.mindspring.com">Dave Bryson</a>
43   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
44   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
45   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
46   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
47   * @version $Id: TemplateLink.java 1854688 2019-03-03 10:36:42Z tv $
48   */
49  
50  public class MappedTemplateLink extends TemplateLink
51  {
52      /**
53       * The URL Mapper service.
54       */
55      @TurbineService
56      private URLMapperService urlMapperService;
57  
58  
59      /**
60       * Builds the URL with all of the data URL-encoded as well as
61       * encoded using HttpServletResponse.encodeUrl(). The resulting
62       * URL is absolute; it starts with http/https...
63       *
64       * <pre>
65       * TemplateURI tui = new TemplateURI (data, "UserScreen");
66       * tui.addPathInfo("user","jon");
67       * tui.getAbsoluteLink();
68       * </pre>
69       *
70       * The above call to absoluteLink() would return the String:
71       * <p>
72       * http://www.server.com/servlets/Turbine/screen/UserScreen/user/jon
73       * <p>
74       * After rendering the URI, it clears the
75       * pathInfo and QueryString portions of the TemplateURI. So you can
76       * use the $link reference multiple times on a page and start over
77       * with a fresh object every time.
78       *
79       * @return A String with the built URL.
80       */
81      @Override
82      public String getAbsoluteLink()
83      {
84          urlMapperService.mapToURL(templateURI);
85          return super.getAbsoluteLink();
86      }
87  
88  
89      /**
90       * Builds the URL with all of the data URL-encoded as well as
91       * encoded using HttpServletResponse.encodeUrl(). The resulting
92       * URL is relative to the webserver root.
93       *
94       * <pre>
95       * TemplateURI tui = new TemplateURI (data, "UserScreen");
96       * tui.addPathInfo("user","jon");
97       * tui.getRelativeLink();
98       * </pre>
99       *
100      * The above call to relativeLink() would return the String:
101      * <p>
102      * /servlets/Turbine/screen/UserScreen/user/jon
103      * <p>
104      * After rendering the URI, it clears the
105      * pathInfo and QueryString portions of the TemplateURI. So you can
106      * use the $link reference multiple times on a page and start over
107      * with a fresh object every time.
108      *
109      * @return A String with the built URL.
110      */
111     @Override
112     public String getRelativeLink()
113     {
114         urlMapperService.mapToURL(templateURI);
115         return super.getRelativeLink();
116     }
117 
118 }