SPR-6464 Add target request matching logic to DefaultFlashMapManager.

This commit is contained in:
Rossen Stoyanchev
2011-08-29 08:37:43 +00:00
parent 71984b8038
commit 6a06a17c47
12 changed files with 463 additions and 368 deletions

View File

@@ -21,6 +21,7 @@ import java.io.FileNotFoundException;
import java.util.Enumeration;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestWrapper;
@@ -723,4 +724,29 @@ public abstract class WebUtils {
return urlPath.substring(begin, end);
}
/**
* Extracts the path from the given URL by removing the query at the end
* and the scheme and authority in the front, if present.
* @param url a URL, never {@code null}
* @return the extracted URL path
*/
public static String extractUrlPath(String url) {
// Remove query/fragment
int end = url.indexOf('?');
if (end == -1) {
end = url.indexOf('#');
if (end == -1) {
end = url.length();
}
}
url = url.substring(0, end);
// Remove scheme + authority
int start = url.indexOf("://");
if (start != -1) {
start = url.indexOf('/', start + 3);
url = (start != -1 ) ? url.substring(start) : "";
}
return url;
}
}

View File

@@ -64,4 +64,15 @@ public class WebUtilsTests {
assertEquals("view.html", WebUtils.extractFullFilenameFromUrlPath("/products/view.html?param=/path/a.do"));
}
@Test
public void extractUriPath() {
assertEquals("", WebUtils.extractUrlPath("http://example.com"));
assertEquals("/", WebUtils.extractUrlPath("http://example.com/"));
assertEquals("/rfc/rfc3986.txt", WebUtils.extractUrlPath("http://www.ietf.org/rfc/rfc3986.txt"));
assertEquals("/over/there", WebUtils.extractUrlPath("http://example.com/over/there?name=ferret#nose"));
assertEquals("/over/there", WebUtils.extractUrlPath("http://example.com/over/there#nose"));
assertEquals("/over/there", WebUtils.extractUrlPath("/over/there?name=ferret#nose"));
assertEquals("/over/there", WebUtils.extractUrlPath("/over/there?url=http://example.com"));
}
}