Added extractFullFilenameFromUrlPath to WebUtils

This commit is contained in:
Arjen Poutsma 2009-02-05 15:45:25 +00:00
parent b0180fdf32
commit 460977263d
2 changed files with 34 additions and 10 deletions

View File

@ -653,6 +653,20 @@ public abstract class WebUtils {
* @return the extracted URI filename (e.g. "index") * @return the extracted URI filename (e.g. "index")
*/ */
public static String extractFilenameFromUrlPath(String urlPath) { public static String extractFilenameFromUrlPath(String urlPath) {
String filename = extractFullFilenameFromUrlPath(urlPath);
int dotIndex = filename.lastIndexOf('.');
if (dotIndex != -1) {
filename = filename.substring(0, dotIndex);
}
return filename;
}
/**
* Extract the full URL filename (including file extension) from the given request URL path.
* Correctly resolves nested paths such as "/products/view.html" as well.
* @param urlPath the request URL path (e.g. "/products/index.html")
* @return the extracted URI filename (e.g. "index.html")
*/
public static String extractFullFilenameFromUrlPath(String urlPath) {
int end = urlPath.indexOf(';'); int end = urlPath.indexOf(';');
if (end == -1) { if (end == -1) {
end = urlPath.indexOf('?'); end = urlPath.indexOf('?');
@ -661,12 +675,7 @@ public abstract class WebUtils {
} }
} }
int begin = urlPath.lastIndexOf('/', end) + 1; int begin = urlPath.lastIndexOf('/', end) + 1;
String filename = urlPath.substring(begin, end); return urlPath.substring(begin, end);
int dotIndex = filename.lastIndexOf('.');
if (dotIndex != -1) {
filename = filename.substring(0, dotIndex);
}
return filename;
} }
} }

View File

@ -19,14 +19,18 @@ package org.springframework.web.util;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import junit.framework.TestCase; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;
/** /**
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Arjen Poutsma
*/ */
public class WebUtilsTests extends TestCase { public class WebUtilsTests {
public void testFindParameterValue() { @Test
public void findParameterValue() {
Map<String, Object> params = new HashMap<String, Object>(); Map<String, Object> params = new HashMap<String, Object>();
params.put("myKey1", "myValue1"); params.put("myKey1", "myValue1");
params.put("myKey2_myValue2", "xxx"); params.put("myKey2_myValue2", "xxx");
@ -40,7 +44,8 @@ public class WebUtilsTests extends TestCase {
assertEquals("myValue4", WebUtils.findParameterValue(params, "myKey4")); assertEquals("myValue4", WebUtils.findParameterValue(params, "myKey4"));
} }
public void testExtractFilenameFromUrlPath() { @Test
public void extractFilenameFromUrlPath() {
assertEquals("index", WebUtils.extractFilenameFromUrlPath("index.html")); assertEquals("index", WebUtils.extractFilenameFromUrlPath("index.html"));
assertEquals("index", WebUtils.extractFilenameFromUrlPath("/index.html")); assertEquals("index", WebUtils.extractFilenameFromUrlPath("/index.html"));
assertEquals("view", WebUtils.extractFilenameFromUrlPath("/products/view.html")); assertEquals("view", WebUtils.extractFilenameFromUrlPath("/products/view.html"));
@ -49,4 +54,14 @@ public class WebUtilsTests extends TestCase {
assertEquals("view", WebUtils.extractFilenameFromUrlPath("/products/view.html?param=/path/a.do")); assertEquals("view", WebUtils.extractFilenameFromUrlPath("/products/view.html?param=/path/a.do"));
} }
@Test
public void extractFullFilenameFromUrlPath() {
assertEquals("index.html", WebUtils.extractFullFilenameFromUrlPath("index.html"));
assertEquals("index.html", WebUtils.extractFullFilenameFromUrlPath("/index.html"));
assertEquals("view.html", WebUtils.extractFullFilenameFromUrlPath("/products/view.html"));
assertEquals("view.html", WebUtils.extractFullFilenameFromUrlPath("/products/view.html?param=a"));
assertEquals("view.html", WebUtils.extractFullFilenameFromUrlPath("/products/view.html?param=/path/a"));
assertEquals("view.html", WebUtils.extractFullFilenameFromUrlPath("/products/view.html?param=/path/a.do"));
}
} }