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