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.theme.jsf;
021
022import javax.faces.context.FacesContext;
023import javax.servlet.ServletRequest;
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.platform.ui.web.util.BaseURL;
027import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper;
028
029public final class URLUtils {
030
031    private static final Log log = LogFactory.getLog(URLUtils.class);
032
033    private URLUtils() {
034    }
035
036    public static String getServerURL() {
037        return getServerURL(null);
038    }
039
040    /**
041     * @return Server URL as : protocol://serverName:port/
042     */
043    public static String getServerURL(ServletRequest request) {
044        if (request == null) {
045            final FacesContext facesContext = FacesContext.getCurrentInstance();
046            request = (ServletRequest) facesContext.getExternalContext().getRequest();
047        }
048        return VirtualHostHelper.getServerURL(request);
049    }
050
051    /**
052     * @return WebApp name : ie : nuxeo
053     */
054    public static String getWebAppName() {
055        return BaseURL.getWebAppName();
056    }
057
058    /**
059     * @return base URL as protocol://serverName:port/webappName/
060     */
061    public static String getBaseURL() {
062        return getBaseURL(null);
063    }
064
065    public static String getBaseURL(ServletRequest request) {
066        String baseURL = null;
067        String serverUrl = getServerURL(request);
068        if (serverUrl != null) {
069            baseURL = serverUrl + getWebAppName() + '/';
070        }
071        if (baseURL == null) {
072            log.error("Could not retrieve base url correctly");
073        }
074        return baseURL;
075    }
076
077}