/**
 * Axelor Business Solutions
 *
 * Copyright (C) 2005-2017 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.app.internal;

import java.io.IOException;
import java.util.Locale;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import com.axelor.app.AppSettings;
import com.axelor.auth.AuthUtils;
import com.axelor.auth.db.User;
import com.google.inject.Singleton;

@Singleton
public class AppFilter implements Filter {

	private static final ThreadLocal<String> BASE_URL = new ThreadLocal<>();
	private static final ThreadLocal<Locale> LANGUAGE = new ThreadLocal<>();

	private static Locale APP_LOCALE;

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		try {
			final String appLocale = AppSettings.get().get("application.locale", null);
			APP_LOCALE =  appLocale == null ? null : new Locale(appLocale);
		} catch (Exception e) {
		}
	}

	public static String getBaseURL() {
		return BASE_URL.get();
	}

	public static Locale getLocale() {
		User user = AuthUtils.getUser();
		if (user != null && user.getLanguage() != null) {
			return new Locale(user.getLanguage());
		}
		if (user != null && APP_LOCALE != null) {
			return APP_LOCALE;
		}
		if (LANGUAGE.get() == null) {
			return Locale.getDefault();
		}
		return LANGUAGE.get();
	}

	private String getBaseUrl(ServletRequest req) {
		if (req.getServerPort() == 80 ||
			req.getServerPort() == 443) {
			return req.getScheme() + "://" + req.getServerName() + req.getServletContext().getContextPath();
		}
		return req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + req.getServletContext().getContextPath();
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		BASE_URL.set(getBaseUrl(request));
		LANGUAGE.set(request.getLocale());
		try {
			chain.doFilter(request, response);
		} finally {
			LANGUAGE.remove();
			BASE_URL.remove();
		}
	}

	@Override
	public void destroy() {
		LANGUAGE.remove();
		BASE_URL.remove();
	}
}

@@ -997,16 +997,62 @@
 esponse;
+%0Aimport javax.servlet.http.HttpServletRequest;
 %0A%0Aimport
@@ -1142,24 +1142,62 @@
 th.db.User;%0A
+import com.axelor.common.StringUtils;%0A
 import com.g
@@ -2131,16 +2131,17 @@
 t();%0A%09%7D%0A
+%09
 %0A%09privat
@@ -2152,23 +2152,22 @@
 ring get
-BaseUrl
+Header
 (Servlet
@@ -2181,309 +2181,705 @@
  req
-) %7B%0A%09%09if (req.getServerPort() == 80 %7C%7C%0A%09%09%09req.getServerPort() == 443) %7B%0A%09%09%09return req.getScheme() + %22://%22 + req.getServerName() + req.getServletContext().getContextPath();%0A%09%09%7D%0A%09%09return req.getScheme() + %22://%22 + req.getServerName() + %22:%22 + req.getServerPort() + req.getServletContext().getContextPath()
+, String name, String defaultValue) %7B%0A%09%09final String value = ((HttpServletRequest) req).getHeader(name);%0A%09%09return StringUtils.isBlank(value) ? defaultValue : value;%0A%09%7D%0A%0A%09private String getBaseUrl(ServletRequest req) %7B%0A%09%09final String proto = getHeader(req, %22X-Forwarded-Proto%22, req.getScheme());%0A%09%09final String port = getHeader(req, %22X-Forwarded-Port%22, %22%22 + req.getServerPort());%0A%09%09final String host = getHeader(req, %22X-Forwarded-Host%22, req.getServerName());%0A%09%09final String context = getHeader(req, %22X-Forwarded-Context%22, req.getServletContext().getContextPath());%0A%09%09return port.equals(%2280%22) %7C%7C port.equals(%22443%22)%0A%09%09%09? proto + %22://%22 + host + context%0A%09%09%09: proto + %22://%22 + host + %22:%22 + port + context
 ;%0A%09%7D

Consider X-Forwarded-* headers when preparing base url