001/*
002 * (C) Copyright 2006-2008 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$
020 */
021
022package org.nuxeo.ecm.webengine.jaxrs.coreiodelegate;
023
024import javax.servlet.ServletRequest;
025import javax.servlet.http.HttpServletRequest;
026
027import org.apache.commons.lang.StringUtils;
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * @deprecated Duplicate of org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper
034 * @since 7.2
035 */
036@Deprecated
037public class VirtualHostHelper {
038
039    private static final int HTTP_PORT_NUMBER = 80;
040
041    private static final int HTTPS_PORT_NUMBER = 443;
042
043    private static final Log log = LogFactory.getLog(VirtualHostHelper.class);
044
045    private static final String X_FORWARDED_HOST = "x-forwarded-host";
046
047    private static final String X_FORWARDED_PROTO = "x-forwarded-proto";
048
049    private static final String X_FORWARDED_PORT = "x-forwarded-port";
050
051    private static final String VH_HEADER = "nuxeo-virtual-host";
052
053    private static final String VH_PARAM = "nuxeo.virtual.host";
054
055    // Utility class.
056    private VirtualHostHelper() {
057    }
058
059    private static HttpServletRequest getHttpServletRequest(ServletRequest request) {
060        if (request instanceof HttpServletRequest) {
061            return (HttpServletRequest) request;
062        }
063        return null;
064    }
065
066    /**
067     * @return WebApp name : ie : nuxeo
068     */
069    public static String getWebAppName(ServletRequest request) {
070        return getContextPath(request).replace("/", "");
071    }
072
073    /**
074     * @return Server URL as : protocol://serverName:port/
075     */
076    public static String getServerURL(ServletRequest request) {
077        return getServerURL(request, false);
078    }
079
080    private static String getServerUrl(String scheme, String serverName, int serverPort) {
081        StringBuilder sbaseURL = new StringBuilder();
082        sbaseURL.append(scheme);
083        sbaseURL.append("://");
084        sbaseURL.append(serverName);
085        if (serverPort != 0) {
086            if ("http".equals(scheme) && serverPort != HTTP_PORT_NUMBER || "https".equals(scheme)
087                    && serverPort != HTTPS_PORT_NUMBER) {
088                sbaseURL.append(':');
089                sbaseURL.append(serverPort);
090            }
091        }
092        sbaseURL.append('/');
093        return sbaseURL.toString();
094    }
095
096    /**
097     * @return Server URL as : protocol://serverName:port/
098     */
099    public static String getServerURL(ServletRequest request, boolean local) {
100        String baseURL = null;
101        HttpServletRequest httpRequest = getHttpServletRequest(request);
102        if (httpRequest != null) {
103            // Detect Nuxeo specific header for VH
104            String nuxeoVH = httpRequest.getHeader(VH_HEADER);
105            if (nuxeoVH == null) {
106                nuxeoVH = Framework.getProperty(VH_PARAM);
107            }
108            if (!local && nuxeoVH != null && nuxeoVH.contains("http")) {
109                baseURL = nuxeoVH;
110            } else {
111                // default values
112                String serverName = httpRequest.getServerName();
113                int serverPort = httpRequest.getServerPort();
114                String scheme = httpRequest.getScheme();
115
116                if (!local) {
117                    String forwardedPort = httpRequest.getHeader(X_FORWARDED_PORT);
118
119                    if (forwardedPort != null) {
120                        try {
121                            serverPort = Integer.parseInt(forwardedPort);
122                        } catch (NumberFormatException e) {
123                            log.error("Unable to get forwarded port from header", e);
124                        }
125                    }
126
127                    String forwardedProto = httpRequest.getHeader(X_FORWARDED_PROTO);
128                    if (forwardedProto != null) {
129                        scheme = forwardedProto;
130                    }
131
132                    // Detect virtual hosting based in standard header
133                    String forwardedHost = httpRequest.getHeader(X_FORWARDED_HOST);
134                    if (forwardedHost != null) {
135                        if (forwardedHost.contains(":")) {
136                            serverName = forwardedHost.split(":")[0];
137                            serverPort = Integer.valueOf(forwardedHost.split(":")[1]);
138                        } else {
139                            serverName = forwardedHost;
140                            serverPort = HTTP_PORT_NUMBER; // fallback
141                        }
142                    }
143                }
144
145                baseURL = getServerUrl(scheme, serverName, serverPort);
146            }
147        }
148        if (baseURL == null) {
149            log.error("Could not retrieve base url correctly");
150            log.debug("Could not retrieve base url correctly", new Throwable());
151        }
152        return baseURL;
153    }
154
155    /**
156     * @return base URL as protocol://serverName:port/webappName/
157     */
158    public static String getBaseURL(ServletRequest request) {
159        String baseURL = null;
160        String serverUrl = getServerURL(request, false);
161        if (serverUrl != null) {
162            String webAppName = getWebAppName(request);
163
164            baseURL = StringUtils.isNotBlank(webAppName) ? serverUrl + webAppName + '/' : serverUrl;
165
166        }
167        return baseURL;
168    }
169
170    /**
171     * Returns the context path of the application. Try to get it from the {@code ServletRequest} and then from the
172     * {@code org.nuxeo.ecm.contextPath} system property. Fallback on default context path {@code /nuxeo}.
173     */
174    public static String getContextPath(ServletRequest request) {
175        HttpServletRequest httpRequest = getHttpServletRequest(request);
176        String contextPath = null;
177        if (httpRequest != null) {
178            contextPath = httpRequest.getContextPath();
179        }
180        return contextPath != null ? contextPath : getContextPathProperty();
181    }
182
183    public static String getContextPathProperty() {
184        return Framework.getProperty("org.nuxeo.ecm.contextPath", "/nuxeo");
185    }
186
187}