You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by iv...@apache.org on 2011/11/11 22:46:06 UTC
svn commit: r1201063 - in /wicket/trunk/wicket-request/src:
main/java/org/apache/wicket/request/Url.java
test/java/org/apache/wicket/request/UrlTest.java
Author: ivaynberg
Date: Fri Nov 11 21:46:06 2011
New Revision: 1201063
URL: http://svn.apache.org/viewvc?rev=1201063&view=rev
Log:
WICKET-4220
Modified:
wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java
wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java
Modified: wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java?rev=1201063&r1=1201062&r2=1201063&view=diff
==============================================================================
--- wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java (original)
+++ wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/Url.java Fri Nov 11 21:46:06 2011
@@ -75,6 +75,21 @@ public final class Url implements Serial
private String host;
/**
+ * Modes with which urls can be stringized
+ *
+ * @author igor
+ */
+ public static enum StringMode {
+ /** local urls are rendered without the host name */
+ LOCAL,
+ /**
+ * full urls are written with hostname. if the hostname is not set or one of segments is
+ * {@literal ..} an {@link IllegalStateException} is thrown.
+ */
+ FULL;
+ }
+
+ /**
*
* @param qp
* @param charset
@@ -186,6 +201,11 @@ public final class Url implements Serial
result.host = hostAndPort.substring(0, portAt);
result.port = Integer.parseInt(hostAndPort.substring(portAt + 1));
}
+
+ if (relativeAt < 0)
+ {
+ relativeUrl = "/";
+ }
}
else
{
@@ -388,9 +408,9 @@ public final class Url implements Serial
}
/**
- * Returns whether the URL is absolute.
+ * Returns whether the Url is absolute. Absolute Urls start with a '{@literal /}'.
*
- * @return <code>true</code> if URL is absolute, <code>false</code> otherwise.
+ * @return <code>true</code> if Url is absolute, <code>false</code> otherwise.
*/
public boolean isAbsolute()
{
@@ -583,7 +603,7 @@ public final class Url implements Serial
}
/**
- * {@inheritDoc}
+ * Renders a url with {@link StringMode#LOCAL} using the url's charset
*/
@Override
public String toString()
@@ -596,7 +616,9 @@ public final class Url implements Serial
* representation
*
* @return absolute representation of the url
+ * @deprecated see {@link Url#toString(StringMode)}
*/
+ @Deprecated
public String toAbsoluteString()
{
return toAbsoluteString(getCharset());
@@ -609,11 +631,19 @@ public final class Url implements Serial
* @param charset
*
* @return see toStringRepresentation
+ * @deprecated see {@link Url#toString(StringMode, Charset)}
*/
+ @Deprecated
public String toAbsoluteString(final Charset charset)
{
StringBuilder result = new StringBuilder();
+ String protocol = this.protocol;
+ if (Strings.isEmpty(protocol))
+ {
+ protocol = "http";
+ }
+
// output scheme://host:port if specified
if (protocol != null && Strings.isEmpty(host) == false)
{
@@ -632,19 +662,91 @@ public final class Url implements Serial
return Strings.join("/", result.toString(), this.toString());
}
+
/**
+ * Stringizes this url
+ *
+ * @param mode
+ * {@link StringMode} that determins how to stringize the url
* @param charset
- * @return see toString()
+ * charset
+ * @return sringized version of this url
+ *
*/
- public String toString(final Charset charset)
+ public String toString(StringMode mode, Charset charset)
{
StringBuilder result = new StringBuilder();
- result.append(getPath(charset));
+ final String path = getPath(charset);
+
+ if (StringMode.FULL == mode)
+ {
+ if (Strings.isEmpty(host))
+ {
+ throw new IllegalStateException("Cannot render this url in " +
+ StringMode.FULL.name() + " mode because it does not have a host set.");
+ }
+
+ String protocol = this.protocol;
+ if (Strings.isEmpty(protocol))
+ {
+ protocol = "http";
+ }
+
+ // output scheme://host:port if specified
+ result.append(protocol);
+ result.append("://");
+ result.append(host);
+
+ if (port != null && port.equals(getDefaultPortForProtocol(protocol)) == false)
+ {
+ result.append(':');
+ result.append(port);
+ }
+
+ if (path.contains(".."))
+ {
+ throw new IllegalStateException("Cannot render this url in " +
+ StringMode.FULL.name() + " mode because it has a `..` segment: " + toString());
+ }
+
+ if (!path.startsWith("/"))
+ {
+ result.append("/");
+ }
+
+ }
+
+
+ result.append(path);
result.append(getQueryString(charset));
return result.toString();
}
/**
+ * Stringizes this url using the specifid {@link StringMode} and url's charset
+ *
+ * @param mode
+ * {@link StringMode} that determins how to stringize the url
+ * @return stringized url
+ */
+ public String toString(StringMode mode)
+ {
+ return toString(mode, getCharset());
+ }
+
+
+ /**
+ * Stringizes this url using {@link StringMode#LOCAL} and the specified charset
+ *
+ * @param charset
+ * @return stringized url
+ */
+ public String toString(final Charset charset)
+ {
+ return toString(StringMode.LOCAL, charset);
+ }
+
+ /**
*
* @return true if last segment contains a name and not something like "." or "..".
*/
Modified: wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java?rev=1201063&r1=1201062&r2=1201063&view=diff
==============================================================================
--- wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java (original)
+++ wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/UrlTest.java Fri Nov 11 21:46:06 2011
@@ -26,6 +26,7 @@ import java.util.Arrays;
import java.util.List;
import org.apache.wicket.request.Url.QueryParameter;
+import org.apache.wicket.request.Url.StringMode;
import org.junit.Assert;
import org.junit.Test;
@@ -212,6 +213,22 @@ public class UrlTest extends Assert
assertEquals(url.toString(), s);
}
+ @Test
+ public void render5()
+ {
+ Url url = Url.parse("https://www.domain.com/foo/bar?baz=ban");
+
+ // local string mode
+ assertEquals("/foo/bar?baz=ban", url.toString(StringMode.LOCAL));
+
+ // full string mode
+ assertEquals("https://www.domain.com/foo/bar?baz=ban", url.toString(StringMode.FULL));
+
+ // local is the default mode
+ assertEquals(url.toString(StringMode.LOCAL), url.toString());
+ }
+
+
/**
*
*/
@@ -256,6 +273,17 @@ public class UrlTest extends Assert
*
*/
@Test
+ public void absolute5()
+ {
+ Url url = Url.parse("http://www.domain.com");
+ assertTrue(url.isAbsolute());
+ }
+
+
+ /**
+ *
+ */
+ @Test
public void testConcat1()
{
Url url = Url.parse("abc/efg");
@@ -496,9 +524,9 @@ public class UrlTest extends Assert
public void testParseAbsoluteUrl()
{
Url url = Url.parse("ftp://myhost:8081");
- checkUrl(url, "ftp", "myhost", 8081);
- assertFalse(url.isAbsolute());
- assertEquals("ftp://myhost:8081", url.toAbsoluteString());
+ checkUrl(url, "ftp", "myhost", 8081, "", "");
+ assertTrue(url.isAbsolute());
+ assertEquals("ftp://myhost:8081/", url.toAbsoluteString());
url = Url.parse("gopher://myhost:8081/foo");
checkUrl(url, "gopher", "myhost", 8081, "", "foo");