Author: mrdon Date: Mon Sep 25 22:48:00 2006 New Revision: 449915 URL: http://svn.apache.org/viewvc?view=rev&rev=449915 Log: Fixed several url building issues dealing with relative paths - omitted forward slash and support for Servlet 2.4 forwarding behaviors Patch provided by Erik Pilz WW-1302 WW-1301
Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/util/UrlHelper.java struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/util/UrlHelperTest.java Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/util/UrlHelper.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/util/UrlHelper.java?view=diff&rev=449915&r1=449914&r2=449915 ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/util/UrlHelper.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/util/UrlHelper.java Mon Sep 25 22:48:00 2006 @@ -124,8 +124,17 @@ link.append(contextPath); } } else if (changedScheme) { - String uri = request.getRequestURI(); - link.append(uri.substring(0, uri.lastIndexOf('/'))); + + // (Applicable to Servlet 2.4 containers) + // If the request was forwarded, the attribute below will be set with the original URL + String uri = (String) request.getAttribute("javax.servlet.forward.request_uri"); + + // If the attribute wasn't found, default to the value in the request object + if (uri == null) { + uri = request.getRequestURI(); + } + + link.append(uri.substring(0, uri.lastIndexOf('/') + 1)); } // Add page @@ -134,6 +143,13 @@ // Go to "same page" String requestURI = (String) request.getAttribute("struts.request_uri"); + // (Applicable to Servlet 2.4 containers) + // If the request was forwarded, the attribute below will be set with the original URL + if (requestURI == null) { + requestURI = (String) request.getAttribute("javax.servlet.forward.request_uri"); + } + + // If neither request attributes were found, default to the value in the request object if (requestURI == null) { requestURI = request.getRequestURI(); } Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/util/UrlHelperTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/util/UrlHelperTest.java?view=diff&rev=449915&r1=449914&r2=449915 ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/util/UrlHelperTest.java (original) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/util/UrlHelperTest.java Mon Sep 25 22:48:00 2006 @@ -249,25 +249,27 @@ } /** - * A check to verify that the scheme, server, and port number are omitted when the scheme of the current request - * matches the scheme supplied when building the URL. + * The UrlHelper should build a URL that starts with "https" followed by the server name when the scheme of the + * current request is "http" and the port for the "https" scheme is 443. When the request has been forwarded + * in a Servlet 2.4 container, the UrlHelper should use the javax.servlet.forward.request_uri request attribute + * instead of a call to HttpServletRequest#getRequestURI(). */ - public void testBuildWithSameScheme() { - String expectedString = "/mywebapp/MyAction.action?foo=bar&hello=earth&hello=mars"; + public void testForwardedRequest() { + String expectedString = "https://www.example.com/mywebapp/product/widget/promo.html"; Mock mockHttpServletRequest = new Mock(HttpServletRequest.class); - mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com"); - mockHttpServletRequest.expectAndReturn("getScheme", "https"); - mockHttpServletRequest.expectAndReturn("getServerPort", 443); + mockHttpServletRequest.expectAndReturn("getServerName", "www.example.com"); + mockHttpServletRequest.expectAndReturn("getScheme", "http"); + mockHttpServletRequest.expectAndReturn("getServerPort", 80); mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp"); + mockHttpServletRequest.expectAndReturn("getAttribute", "javax.servlet.forward.request_uri", "/mywebapp/product/widget/"); + mockHttpServletRequest.expectAndReturn("getRequestURI", "/mywebapp/"); Mock mockHttpServletResponse = new Mock(HttpServletResponse.class); mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString); - String actionName = "/MyAction.action"; - TreeMap params = new TreeMap(); - params.put("hello", new String[]{"earth", "mars"}); - params.put("foo", "bar"); + String actionName = "promo.html"; + Map params = new TreeMap(); String urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params, "https", true, true); assertEquals(expectedString, urlString);