001// Copyright 2004, 2005 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry.link;
016
017import org.apache.tapestry.IRequestCycle;
018import org.apache.tapestry.components.ILinkComponent;
019import org.apache.tapestry.engine.ILink;
020
021/**
022 * Renders a link using an absolute URL, not simply a URI (as with
023 * {@link org.apache.tapestry.link.DefaultLinkRenderer}. In addition, the scheme, server and port
024 * may be changed (this may be appropriate when switching between secure and insecure portions of an
025 * application).
026 * 
027 * @author Howard Lewis Ship
028 * @since 3.0
029 */
030
031public class AbsoluteLinkRenderer extends DefaultLinkRenderer
032{
033    private String _scheme;
034
035    private String _serverName;
036
037    private int _port;
038
039    public int getPort()
040    {
041        return _port;
042    }
043
044    public String getScheme()
045    {
046        return _scheme;
047    }
048
049    public String getServerName()
050    {
051        return _serverName;
052    }
053
054    /**
055     * Used to override the port in the final URL, if specified. If not specified, the port provided
056     * by the {@link javax.servlet.ServletRequest#getServerPort() request} is used (typically, the
057     * value 80).
058     */
059
060    public void setPort(int port)
061    {
062        _port = port;
063    }
064
065    /**
066     * Used to override the scheme in the final URL, if specified. If not specified, the scheme
067     * provided by the {@link javax.servlet.ServletRequest#getScheme() request} is used (typically,
068     * <code>http</code>).
069     */
070
071    public void setScheme(String scheme)
072    {
073        _scheme = scheme;
074    }
075
076    /**
077     * Used to override the server name in the final URL, if specified. If not specified, the port
078     * provided by the {@link javax.servlet.ServletRequest#getServerName() request} is used.
079     */
080
081    public void setServerName(String serverName)
082    {
083        _serverName = serverName;
084    }
085
086    protected String constructURL(ILinkComponent component, IRequestCycle cycle)
087    {
088        ILink link = component.getLink(cycle);
089
090        return link.getAbsoluteURL(_scheme, _serverName, _port, component.getAnchor(), true);
091    }
092
093}