You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by jd...@apache.org on 2007/12/02 11:22:40 UTC

svn commit: r600264 - in /wicket/trunk/jdk-1.4/wicket/src: main/java/org/apache/wicket/protocol/http/RequestUtils.java test/java/org/apache/wicket/protocol/http/RequestUtilsTest.java

Author: jdonnerstag
Date: Sun Dec  2 02:22:40 2007
New Revision: 600264

URL: http://svn.apache.org/viewvc?rev=600264&view=rev
Log:
fixed wicket-609: Wicket should provide an easy method to generate absolute urls to bookmarkable pages

Modified:
    wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/RequestUtils.java
    wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/protocol/http/RequestUtilsTest.java

Modified: wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/RequestUtils.java
URL: http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/RequestUtils.java?rev=600264&r1=600263&r2=600264&view=diff
==============================================================================
--- wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/RequestUtils.java (original)
+++ wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/RequestUtils.java Sun Dec  2 02:22:40 2007
@@ -22,6 +22,9 @@
 import java.util.Arrays;
 import java.util.List;
 
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.wicket.RequestCycle;
 import org.apache.wicket.WicketRuntimeException;
 import org.apache.wicket.util.string.Strings;
 import org.apache.wicket.util.value.ValueMap;
@@ -121,5 +124,67 @@
 		{
 			throw new WicketRuntimeException(e);
 		}
+	}
+
+	/**
+	 * Calculates absolute path to url relative to another absolute url.
+	 * 
+	 * @param relativePagePath
+	 *            path, relative to requestPath
+	 * @return absolute path for given url
+	 */
+	public final static String toAbsolutePath(final String relativePagePath)
+	{
+		HttpServletRequest req = ((WebRequest)RequestCycle.get().getRequest()).getHttpServletRequest();
+		return toAbsolutePath(req.getRequestURL().toString(), relativePagePath);
+	}
+
+	/**
+	 * Calculates absolute path to url relative to another absolute url.
+	 * 
+	 * @param requestPath
+	 *            absolute path.
+	 * @param relativePagePath
+	 *            path, relative to requestPath
+	 * @return absolute path for given url
+	 */
+	public final static String toAbsolutePath(final String requestPath,
+		final String relativePagePath)
+	{
+		final StringBuffer result;
+		if (requestPath.endsWith("/"))
+		{
+			result = new StringBuffer(requestPath);
+		}
+		else
+		{
+			// Remove everything after last slash (but not slash itself)
+			result = new StringBuffer(requestPath.substring(0, requestPath.lastIndexOf('/') + 1));
+		}
+
+		if (relativePagePath.startsWith("../"))
+		{
+			StringBuffer tempRelative = new StringBuffer(relativePagePath);
+
+			// Go up through hierarchy until we find most common directory for both pathes.
+			while (tempRelative.indexOf("../") == 0)
+			{
+				// Delete ../ from relative path
+				tempRelative.delete(0, 3);
+
+				// Delete last slash from result
+				result.setLength(result.length() - 1);
+
+				// Delete everyting up to last slash
+				result.delete(result.lastIndexOf("/") + 1, result.length());
+			}
+			result.append(tempRelative);
+		}
+		else
+		{
+			// Pages are in the same directory
+			result.append(relativePagePath);
+		}
+		return result.toString();
 	}
 }

Modified: wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/protocol/http/RequestUtilsTest.java
URL: http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/protocol/http/RequestUtilsTest.java?rev=600264&r1=600263&r2=600264&view=diff
==============================================================================
--- wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/protocol/http/RequestUtilsTest.java (original)
+++ wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/protocol/http/RequestUtilsTest.java Sun Dec  2 02:22:40 2007
@@ -18,8 +18,15 @@
 
 import junit.framework.TestCase;
 
+/**
+ * 
+ * @author Johan
+ */
 public class RequestUtilsTest extends TestCase
 {
+	/**
+	 * 
+	 */
 	public void testDoubleDotsMiddle()
 	{
 		assertEquals("/a/b", RequestUtils.removeDoubleDots("/a/b/../b"));
@@ -27,27 +34,54 @@
 		assertEquals("a/b/", RequestUtils.removeDoubleDots("a/b/../b/"));
 	}
 
+	/**
+	 * 
+	 */
 	public void testDoubleDotsEnd()
 	{
 		assertEquals("/a/b", RequestUtils.removeDoubleDots("/a/b/c/.."));
 		assertEquals("a/b", RequestUtils.removeDoubleDots("a/b/c/.."));
 	}
 
+	/**
+	 * 
+	 */
 	public void testDoubleDotsStart()
 	{
 		assertEquals("/../a/b", RequestUtils.removeDoubleDots("/../a/b"));
 		assertEquals("../a/b", RequestUtils.removeDoubleDots("../a/b"));
 	}
 
+	/**
+	 * 
+	 */
 	public void testEmptyDoubleDots()
 	{
 		assertEquals("", RequestUtils.removeDoubleDots(""));
 	}
 
+	/**
+	 * 
+	 */
 	public void testOneDoubleDots()
 	{
 		assertEquals("..", RequestUtils.removeDoubleDots(".."));
 		assertEquals("../", RequestUtils.removeDoubleDots("../"));
 		assertEquals("/..", RequestUtils.removeDoubleDots("/.."));
+	}
+
+	/**
+	 * 
+	 */
+	public void testToAbsolutePath()
+	{
+		assertEquals(RequestUtils.toAbsolutePath("http://aif.ru/test/test", "../blah/zzz"),
+			"http://aif.ru/blah/zzz");
+		assertEquals(RequestUtils.toAbsolutePath("http://aif.ru/test", "blah/zzz"),
+			"http://aif.ru/blah/zzz");
+		assertEquals(RequestUtils.toAbsolutePath("http://aif.ru/test/", "../blah/zzz"),
+			"http://aif.ru/blah/zzz");
+		assertEquals(RequestUtils.toAbsolutePath("http://aif.ru/blah/test", "zzz"),
+			"http://aif.ru/blah/zzz");
 	}
 }