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