class AutomaticDispatcherUrlService implements DispatcherUrlService {

	@Override
	public String getDispatcherUrl() {
		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
		if (null == requestAttributes || !(requestAttributes instanceof ServletRequestAttributes)) {
			log.warn("Trying to automatically get the dispatcher URL, but not running inside a servlet request. " +
					"You are recommended to use StaticDispatcherUrlService");
			return "./d/"; // use relative URL as back-up, will fail in many cases
		}

		HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();

		String serverName = request.getServerName();

		// X-Forwarded-Host if behind a reverse proxy, fallback to general method.
		// Alternative we could use the gwt module url to guess the real URL.
		if (null != request.getHeader(X_FORWARD_HOST_HEADER)) {
			log.warn("AutomaticDispatcherService detected a X-Forwarded-Host header which means the server is " +
					"accessed using a reverse proxy server. This might cause problems in some cases. You are " +
					"recommended to configure your tomcat connector to be aware of the original url. " +
					"(see http://tomcat.apache.org/tomcat-6.0-doc/proxy-howto.html )");
			String gwtModuleBase = request.getHeader(X_GWT_MODULE_HEADER);
			if (null != gwtModuleBase) {
				// Get last slash in the gwtModuleBase, ignoring the trailing slash.
				int contextEndIndex = gwtModuleBase.lastIndexOf("/", gwtModuleBase.length() - 2);
				if (contextEndIndex > -1) {
					return gwtModuleBase.substring(0, contextEndIndex) + "/d/";
				}
			} else {
				// else get the information from the X-forwarded-host header and default to the standard behaviour 
				serverName = request.getHeader(X_FORWARD_HOST_HEADER);
			}
		}

		return getBasePathForHostNamePort(request, serverName, request.getServerPort());
	}

}