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    
015    package org.apache.tapestry.link;
016    
017    import org.apache.tapestry.IRequestCycle;
018    import org.apache.tapestry.engine.ILink;
019    
020    /**
021     *  Renders a link using an absolute URL, not simply a URI
022     *  (as with {@link org.apache.tapestry.link.DefaultLinkRenderer}.  In addition,
023     *  the scheme, server and port may be changed (this may be appropriate when
024     *  switching between secure and insecure portions of an application).
025     *
026     *  @author Howard Lewis Ship
027     *  @since 3.0
028     * 
029     **/
030    
031    public class AbsoluteLinkRenderer extends DefaultLinkRenderer
032    {
033        private String _scheme;
034        private String _serverName;
035        private int _port;
036    
037        public int getPort()
038        {
039            return _port;
040        }
041    
042        public String getScheme()
043        {
044            return _scheme;
045        }
046    
047        public String getServerName()
048        {
049            return _serverName;
050        }
051    
052        /**
053         *  Used to override the port in the final URL, if specified.  If not specified,
054         *  the port provided by the {@link javax.servlet.ServletRequest#getServerPort() request}
055         *  is used (typically, the value 80).
056         *
057         **/
058    
059        public void setPort(int port)
060        {
061            _port = port;
062        }
063        
064        /**
065         *  Used to override the scheme in the final URL, if specified.  If not specified,
066         *  the scheme provided by the {@link javax.servlet.ServletRequest#getScheme() request}
067         *  is used (typically, <code>http</code>).
068         *
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,
078         *  the port provided by the {@link javax.servlet.ServletRequest#getServerName() request}
079         *  is used.
080         *
081         **/
082    
083        public void setServerName(String serverName)
084        {
085            _serverName = serverName;
086        }
087    
088        protected String constructURL(ILink link, String anchor, IRequestCycle cycle)
089        {
090            return link.getAbsoluteURL(_scheme, _serverName, _port, anchor, true);
091        }
092    
093    }