mirror of
https://github.com/apache/struts.git
synced 2026-08-06 23:27:07 +00:00
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 git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@449915 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -124,8 +124,17 @@ public class UrlHelper {
|
||||
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 @@ public class UrlHelper {
|
||||
// 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();
|
||||
}
|
||||
|
||||
@@ -249,25 +249,27 @@ public class UrlHelperTest extends StrutsTestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
Reference in New Issue
Block a user