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/"; } HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest(); String serverName = request.getServerName(); 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: String gwtModuleBase = request.getHeader(X_GWT_MODULE_HEADER); if (null != gwtModuleBase) { int contextEndIndex = gwtModuleBase.lastIndexOf("/", gwtModuleBase.length() - 2); if (contextEndIndex > -1) { return gwtModuleBase.substring(0, contextEndIndex) + "/d/"; } } else { serverName = request.getHeader(X_FORWARD_HOST_HEADER); } } return getBasePathForHostNamePort(request, serverName, request.getServerPort()); }  @Override String getDispatcherUrl(); @Override String getLocalDispatcherUrl(); @Api void setLocalDispatcherUrl(String localDispatcherUrl); @Override String localize(String externalUrl);  }
@Test public void testBuildUrl() throws Exception { AutomaticDispatcherUrlService adus = new AutomaticDispatcherUrlService(); MockHttpServletRequest mockRequest = new MockHttpServletRequest(); mockRequest.setScheme("http"); mockRequest.setServerName("geomajas.org"); mockRequest.setServerPort(80); mockRequest.setContextPath("/test"); ServletRequestAttributes attributes = new ServletRequestAttributes(mockRequest); RequestContextHolder.setRequestAttributes(attributes); Assert.assertEquals("http: mockRequest.setScheme("https"); mockRequest.setServerName("secure.geomajas.org"); mockRequest.setServerPort(8443); mockRequest.setContextPath(null); Assert.assertEquals("https: RequestContextHolder.setRequestAttributes(null); }
@Test public void testReverseProxyWithModuleBase() { AutomaticDispatcherUrlService adus = new AutomaticDispatcherUrlService(); MockHttpServletRequest mockRequest = new MockHttpServletRequest(); mockRequest.setScheme("http"); mockRequest.setServerName("myhost"); mockRequest.setServerPort(80); mockRequest.setContextPath("/test"); mockRequest.addHeader(X_FORWARD_HOST_HEADER, "geomajas.org"); mockRequest.addHeader(X_GWT_MODULE_HEADER, "http: ServletRequestAttributes attributes = new ServletRequestAttributes(mockRequest); RequestContextHolder.setRequestAttributes(attributes); Assert.assertEquals("http: RequestContextHolder.setRequestAttributes(null); }
@Test public void testReverseProxyWithoutModuleBase() { AutomaticDispatcherUrlService adus = new AutomaticDispatcherUrlService(); MockHttpServletRequest mockRequest = new MockHttpServletRequest(); mockRequest.setScheme("http"); mockRequest.setServerName("myhost"); mockRequest.setServerPort(80); mockRequest.setContextPath("/test"); mockRequest.addHeader(X_FORWARD_HOST_HEADER, "geomajas.org"); ServletRequestAttributes attributes = new ServletRequestAttributes(mockRequest); RequestContextHolder.setRequestAttributes(attributes); Assert.assertEquals("http: RequestContextHolder.setRequestAttributes(null); }