001package org.apache.turbine.services.urlmapper;
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
022import org.apache.turbine.annotation.TurbineService;
023import org.apache.turbine.services.pull.tools.TemplateLink;
024
025/**
026 * This is a pull to to be used in Templates to convert links in
027 * Templates into the correct references.
028 *
029 * The pull service might insert this tool into the Context.
030 * in templates.  Here's an example of its Velocity use:
031 *
032 * <p><code>
033 * $link.setPage("index.vm").addPathInfo("hello","world")
034 * This would return: http://foo.com/Turbine/template/index.vm/hello/world
035 * </code>
036 *
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="mbryson@mont.mindspring.com">Dave Bryson</a>
043 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
044 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
045 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
046 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
047 * @version $Id: TemplateLink.java 1854688 2019-03-03 10:36:42Z tv $
048 */
049
050public class MappedTemplateLink extends TemplateLink
051{
052    /**
053     * The URL Mapper service.
054     */
055    @TurbineService
056    private URLMapperService urlMapperService;
057
058
059    /**
060     * Builds the URL with all of the data URL-encoded as well as
061     * encoded using HttpServletResponse.encodeUrl(). The resulting
062     * URL is absolute; it starts with http/https...
063     *
064     * <pre>
065     * TemplateURI tui = new TemplateURI (data, "UserScreen");
066     * tui.addPathInfo("user","jon");
067     * tui.getAbsoluteLink();
068     * </pre>
069     *
070     * The above call to absoluteLink() would return the String:
071     * <p>
072     * http://www.server.com/servlets/Turbine/screen/UserScreen/user/jon
073     * <p>
074     * After rendering the URI, it clears the
075     * pathInfo and QueryString portions of the TemplateURI. So you can
076     * use the $link reference multiple times on a page and start over
077     * with a fresh object every time.
078     *
079     * @return A String with the built URL.
080     */
081    @Override
082    public String getAbsoluteLink()
083    {
084        urlMapperService.mapToURL(templateURI);
085        return super.getAbsoluteLink();
086    }
087
088
089    /**
090     * Builds the URL with all of the data URL-encoded as well as
091     * encoded using HttpServletResponse.encodeUrl(). The resulting
092     * URL is relative to the webserver root.
093     *
094     * <pre>
095     * TemplateURI tui = new TemplateURI (data, "UserScreen");
096     * tui.addPathInfo("user","jon");
097     * tui.getRelativeLink();
098     * </pre>
099     *
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}