001/*
002 *  Copyright 2011 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016
017package org.ametys.plugins.site;
018
019import org.apache.cocoon.environment.Request;
020
021/**
022 * Represente a site access URL.
023 */
024public class SiteUrl
025{
026    private String _serverName;
027    private String _serverPort;
028    private String _serverPath;
029
030    /**
031     * Cosntructor.
032     * @param serverName the front-office server name.
033     * @param serverPort the front-office server port.
034     * @param serverPath the front-office server context path.
035     */
036    public SiteUrl(String serverName, String serverPort, String serverPath)
037    {
038        _serverName = serverName;
039        _serverPort = serverPort;
040        _serverPath = serverPath;
041    }
042    
043    /**
044     * Returns the front-office server name.
045     * @return the front-office server name.
046     */
047    public String getServerName()
048    {
049        return _serverName;
050    }
051    
052    /**
053     * Returns the front-office server port.
054     * @return the front-office server port.
055     */
056    public String getServerPort()
057    {
058        return _serverPort;
059    }
060    
061    /**
062     * Returns the front-office server context path.
063     * @return the front-office server context path.
064     */
065    public String getServerPath()
066    {
067        return _serverPath;
068    }
069    
070    
071    /**
072     * Get the site's base server path.
073     * @param request the request.
074     * @return the base site server path.
075     */
076    public String getBaseServerPath(Request request)
077    {
078        StringBuilder fullUri = new StringBuilder();
079        fullUri.append(request.getScheme()).append("://").append(getServerName());
080        
081        if (request.isSecure())
082        {
083            if (!"443".equals(getServerPort()))
084            {
085                fullUri.append(":").append(getServerPort());
086            }
087        }
088        else
089        {
090            if (!"80".equals(getServerPort()))
091            {
092                fullUri.append(":").append(getServerPort());
093            }
094        }
095        
096        return fullUri.toString();
097    }
098}