001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
018 */
019
020package org.nuxeo.ecm.platform.ui.web.util;
021
022import javax.faces.context.FacesContext;
023import javax.servlet.ServletRequest;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper;
028
029public final class BaseURL {
030
031    private static final Log log = LogFactory.getLog(BaseURL.class);
032
033    private BaseURL() {
034    }
035
036    static ServletRequest getRequest() {
037        final FacesContext facesContext = FacesContext.getCurrentInstance();
038        if (facesContext == null) {
039            return null;
040        }
041        return (ServletRequest) facesContext.getExternalContext().getRequest();
042    }
043
044    public static String getServerURL() {
045        return getServerURL(getRequest(), false);
046    }
047
048    /**
049     * @return Server URL as: protocol://serverName:port/
050     */
051    public static String getServerURL(ServletRequest request, boolean local) {
052        return VirtualHostHelper.getServerURL(request, local);
053    }
054
055    /**
056     * @return WebApp name, ie "nuxeo"
057     */
058    public static String getWebAppName() {
059        ServletRequest request = getRequest();
060        return VirtualHostHelper.getWebAppName(request);
061    }
062
063    /**
064     * @return base URL as protocol://serverName:port/webappName/
065     */
066    public static String getBaseURL() {
067        return getBaseURL(getRequest());
068    }
069
070    public static String getBaseURL(ServletRequest request) {
071        return VirtualHostHelper.getBaseURL(request);
072    }
073
074    public static String getLocalBaseURL(ServletRequest request) {
075        String localURL = null;
076        String serverUrl = getServerURL(request, true);
077        if (serverUrl != null) {
078            localURL = serverUrl + getWebAppName() + '/';
079        }
080        if (localURL == null) {
081            log.error("Could not retrieve local url correctly");
082        }
083        return localURL;
084    }
085
086    public static String getContextPath() {
087        return VirtualHostHelper.getContextPath(getRequest());
088    }
089
090}