From d1cb85e4f39e2e2046d9001a42d4903fa954efa6 Mon Sep 17 00:00:00 2001 From: Luke Taylor Date: Fri, 1 May 2009 10:43:41 +0000 Subject: [PATCH] Refactoring of methods names in UrlUtils for consistency. --- .../security/web/FilterInvocation.java | 4 +- .../web/savedrequest/SavedRequest.java | 6 +-- .../security/web/util/UrlUtils.java | 44 +++++++++---------- .../web/savedrequest/SavedRequestTests.java | 16 +++++-- 4 files changed, 37 insertions(+), 33 deletions(-) diff --git a/web/src/main/java/org/springframework/security/web/FilterInvocation.java b/web/src/main/java/org/springframework/security/web/FilterInvocation.java index f639244a11..6d01c65e68 100644 --- a/web/src/main/java/org/springframework/security/web/FilterInvocation.java +++ b/web/src/main/java/org/springframework/security/web/FilterInvocation.java @@ -70,7 +70,7 @@ public class FilterInvocation { * @return the full URL of this request */ public String getFullRequestUrl() { - return UrlUtils.getFullRequestUrl(this); + return UrlUtils.buildFullRequestUrl(request); } public HttpServletRequest getHttpRequest() { @@ -87,7 +87,7 @@ public class FilterInvocation { * @return the URL, excluding any server name, context path or servlet path */ public String getRequestUrl() { - return UrlUtils.getRequestUrl(this); + return UrlUtils.buildRequestUrl(request); } public HttpServletRequest getRequest() { diff --git a/web/src/main/java/org/springframework/security/web/savedrequest/SavedRequest.java b/web/src/main/java/org/springframework/security/web/savedrequest/SavedRequest.java index 8a6b08d4c9..f9324ab145 100644 --- a/web/src/main/java/org/springframework/security/web/savedrequest/SavedRequest.java +++ b/web/src/main/java/org/springframework/security/web/savedrequest/SavedRequest.java @@ -52,6 +52,8 @@ public class SavedRequest implements java.io.Serializable { protected static final Log logger = LogFactory.getLog(SavedRequest.class); + public static final String SPRING_SECURITY_SAVED_REQUEST_KEY = "SPRING_SECURITY_SAVED_REQUEST_KEY"; + //~ Instance fields ================================================================================================ private ArrayList cookies = new ArrayList(); @@ -69,8 +71,6 @@ public class SavedRequest implements java.io.Serializable { private String servletPath; private int serverPort; - public static final String SPRING_SECURITY_SAVED_REQUEST_KEY = "SPRING_SECURITY_SAVED_REQUEST_KEY"; - //~ Constructors =================================================================================================== @SuppressWarnings("unchecked") @@ -228,7 +228,7 @@ public class SavedRequest implements java.io.Serializable { */ public String getFullRequestUrl() { return UrlUtils.buildFullRequestUrl(this.getScheme(), this.getServerName(), this.getServerPort(), this.getContextPath(), - this.getRequestURL(), this.getServletPath(), this.getRequestURI(), this.getPathInfo(), this.getQueryString()); + this.getServletPath(), this.getRequestURI(), this.getPathInfo(), this.getQueryString()); } public Iterator getHeaderNames() { diff --git a/web/src/main/java/org/springframework/security/web/util/UrlUtils.java b/web/src/main/java/org/springframework/security/web/util/UrlUtils.java index a9765eb0d3..d4f891dc6a 100644 --- a/web/src/main/java/org/springframework/security/web/util/UrlUtils.java +++ b/web/src/main/java/org/springframework/security/web/util/UrlUtils.java @@ -17,8 +17,6 @@ package org.springframework.security.web.util; import javax.servlet.http.HttpServletRequest; -import org.springframework.security.web.FilterInvocation; - /** * Provides static methods for composing URLs.

Placed into a separate class for visibility, so that changes to @@ -28,21 +26,23 @@ import org.springframework.security.web.FilterInvocation; * @version $Id$ */ public final class UrlUtils { - //~ Constructors =================================================================================================== + //~ Methods ======================================================================================================== - private UrlUtils() { + public static String buildFullRequestUrl(HttpServletRequest r) { + return buildFullRequestUrl(r.getScheme(), r.getServerName(), r.getServerPort(), r.getContextPath(), + r.getServletPath(), r.getRequestURI(), r.getPathInfo(), r.getQueryString()); } - //~ Methods ======================================================================================================== - /** - * Obtains the full URL the client used to make the request.

Note that the server port will not be shown - * if it is the default server port for HTTP or HTTPS (ie 80 and 443 respectively).

+ * Obtains the full URL the client used to make the request. + *

+ * Note that the server port will not be shown if it is the default server port for HTTP or HTTPS + * (80 and 443 respectively). * * @return the full URL */ public static String buildFullRequestUrl(String scheme, String serverName, int serverPort, String contextPath, - String requestUrl, String servletPath, String requestURI, String pathInfo, String queryString) { + String servletPath, String requestURI, String pathInfo, String queryString) { boolean includePort = true; @@ -55,7 +55,17 @@ public final class UrlUtils { } return scheme + "://" + serverName + ((includePort) ? (":" + serverPort) : "") + contextPath - + buildRequestUrl(servletPath, requestURI, contextPath, pathInfo, queryString); + + buildRequestUrl(servletPath, requestURI, contextPath, pathInfo, queryString); + } + + /** + * Obtains the web application-specific fragment of the request URL. + * + * @return the URL, excluding any server name, context path or servlet path + */ + public static String buildRequestUrl(HttpServletRequest r) { + return buildRequestUrl(r.getServletPath(), r.getRequestURI(), r.getContextPath(), r.getPathInfo(), + r.getQueryString()); } /** @@ -76,20 +86,6 @@ public final class UrlUtils { return uri + ((pathInfo == null) ? "" : pathInfo) + ((queryString == null) ? "" : ("?" + queryString)); } - public static String getFullRequestUrl(FilterInvocation fi) { - HttpServletRequest r = fi.getHttpRequest(); - - return buildFullRequestUrl(r.getScheme(), r.getServerName(), r.getServerPort(), r.getContextPath(), - r.getRequestURL().toString(), r.getServletPath(), r.getRequestURI(), r.getPathInfo(), r.getQueryString()); - } - - public static String getRequestUrl(FilterInvocation fi) { - HttpServletRequest r = fi.getHttpRequest(); - - return buildRequestUrl(r.getServletPath(), r.getRequestURI(), r.getContextPath(), r.getPathInfo(), - r.getQueryString()); - } - /** * Returns true if the supplied URL starts with a "/" or "http". */ diff --git a/web/src/test/java/org/springframework/security/web/savedrequest/SavedRequestTests.java b/web/src/test/java/org/springframework/security/web/savedrequest/SavedRequestTests.java index 9edb4f706a..60b1f5f241 100644 --- a/web/src/test/java/org/springframework/security/web/savedrequest/SavedRequestTests.java +++ b/web/src/test/java/org/springframework/security/web/savedrequest/SavedRequestTests.java @@ -1,20 +1,28 @@ package org.springframework.security.web.savedrequest; -import junit.framework.TestCase; +import static org.junit.Assert.*; + +import org.junit.Test; import org.springframework.security.MockPortResolver; import org.springframework.security.web.savedrequest.SavedRequest; import org.springframework.mock.web.MockHttpServletRequest; -public class SavedRequestTests extends TestCase { +/** + * + */ +public class SavedRequestTests { - public void testCaseInsensitveHeaders() throws Exception { + @Test + public void headersAreCaseInsensitive() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("USER-aGenT", "Mozilla"); SavedRequest saved = new SavedRequest(request, new MockPortResolver(8080, 8443)); assertEquals("Mozilla", saved.getHeaderValues("user-agent").next()); } - public void testCaseInsensitveParameters() throws Exception { + // TODO: Why are parameters case insensitive. I think this is a mistake + @Test + public void parametersAreCaseInsensitive() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("ThisIsATest", "Hi mom"); SavedRequest saved = new SavedRequest(request, new MockPortResolver(8080, 8443));