You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shindig.apache.org by jo...@apache.org on 2010/02/22 20:48:42 UTC
svn commit: r915037 - in /shindig/trunk/java/common/src:
main/java/org/apache/shindig/common/uri/UriBuilder.java
test/java/org/apache/shindig/common/uri/UriBuilderTest.java
Author: johnh
Date: Mon Feb 22 19:48:42 2010
New Revision: 915037
URL: http://svn.apache.org/viewvc?rev=915037&view=rev
Log:
Constructor to create UriBuilder from an HttpServletRequest.
Modified:
shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/UriBuilder.java
shindig/trunk/java/common/src/test/java/org/apache/shindig/common/uri/UriBuilderTest.java
Modified: shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/UriBuilder.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/UriBuilder.java?rev=915037&r1=915036&r2=915037&view=diff
==============================================================================
--- shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/UriBuilder.java (original)
+++ shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/UriBuilder.java Mon Feb 22 19:48:42 2010
@@ -29,6 +29,7 @@
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import javax.servlet.http.HttpServletRequest;
/**
* Constructs Uris from inputs.
@@ -55,6 +56,22 @@
query = new ParamString(uri.getQuery());
fragment = new ParamString(uri.getFragment());
}
+
+ /**
+ * Construct a new builder from a servlet request.
+ */
+ public UriBuilder(HttpServletRequest req) {
+ scheme = req.getScheme().toLowerCase();
+ int serverPort = req.getServerPort();
+ authority = req.getServerName() +
+ ((serverPort == 80 && "http".equals(scheme)) ||
+ (serverPort == 443 && "https".equals(scheme)) ||
+ (serverPort <= 0) ? "" :
+ ":" + serverPort);
+ path = req.getRequestURI();
+ query = new ParamString(req.getQueryString());
+ fragment = new ParamString();
+ }
/**
* Create an empty builder.
Modified: shindig/trunk/java/common/src/test/java/org/apache/shindig/common/uri/UriBuilderTest.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/uri/UriBuilderTest.java?rev=915037&r1=915036&r2=915037&view=diff
==============================================================================
--- shindig/trunk/java/common/src/test/java/org/apache/shindig/common/uri/UriBuilderTest.java (original)
+++ shindig/trunk/java/common/src/test/java/org/apache/shindig/common/uri/UriBuilderTest.java Mon Feb 22 19:48:42 2010
@@ -17,16 +17,24 @@
*/
package org.apache.shindig.common.uri;
+import static org.easymock.classextension.EasyMock.createMock;
+import static org.easymock.classextension.EasyMock.expect;
+import static org.easymock.classextension.EasyMock.replay;
+import static org.easymock.classextension.EasyMock.verify;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static junitx.framework.Assert.assertNotEquals;
+
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
+
import org.junit.Test;
-import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
-import static junitx.framework.Assert.assertNotEquals;
+import javax.servlet.http.HttpServletRequest;
/**
* Tests for UriBuilder
@@ -390,4 +398,74 @@
assertEquals(uri.hashCode(), uri2.hashCode());
}
+
+ @Test
+ public void constructFromServletRequestHttpStandardPortAndModify() {
+ HttpServletRequest req = createMock(HttpServletRequest.class);
+ expect(req.getScheme()).andReturn("http").once();
+ expect(req.getServerName()).andReturn("example.com");
+ expect(req.getServerPort()).andReturn(80).once();
+ expect(req.getRequestURI()).andReturn("/my/path");
+ expect(req.getQueryString()).andReturn("foo=bar&baz=bak");
+ replay(req);
+
+ UriBuilder builder = new UriBuilder(req);
+ verify(req);
+
+ assertEquals("http://example.com/my/path?foo=bar&baz=bak", builder.toString());
+ assertEquals("bar", builder.getQueryParameter("foo")); // sanity check on a single param
+ assertEquals(0, builder.getFragmentParameters().size()); // shouldn't NPE
+
+ // Simple modification
+ builder.setPath("/other/path");
+ assertEquals("http://example.com/other/path?foo=bar&baz=bak", builder.toString());
+ }
+
+ @Test
+ public void constructFromServletRequestHttpsStandardPort() {
+ HttpServletRequest req = createMock(HttpServletRequest.class);
+ expect(req.getScheme()).andReturn("https").once();
+ expect(req.getServerName()).andReturn("example.com");
+ expect(req.getServerPort()).andReturn(443).once();
+ expect(req.getRequestURI()).andReturn("/my/path");
+ expect(req.getQueryString()).andReturn("foo=bar&baz=bak");
+ replay(req);
+
+ UriBuilder builder = new UriBuilder(req);
+ verify(req);
+
+ assertEquals("https://example.com/my/path?foo=bar&baz=bak", builder.toString());
+ }
+
+ @Test
+ public void constructFromServletRequestNonStandardPort() {
+ HttpServletRequest req = createMock(HttpServletRequest.class);
+ expect(req.getScheme()).andReturn("HtTp").once(); // Shouldn't happen but try anyway.
+ expect(req.getServerName()).andReturn("example.com");
+ expect(req.getServerPort()).andReturn(5000).once();
+ expect(req.getRequestURI()).andReturn("/my/path");
+ expect(req.getQueryString()).andReturn("one=two&three=four");
+ replay(req);
+
+ UriBuilder builder = new UriBuilder(req);
+ verify(req);
+
+ assertEquals("http://example.com:5000/my/path?one=two&three=four", builder.toString());
+ }
+
+ @Test
+ public void constructFromServletRequestNonePort() {
+ HttpServletRequest req = createMock(HttpServletRequest.class);
+ expect(req.getScheme()).andReturn("http").once(); // Shouldn't happen but try anyway.
+ expect(req.getServerName()).andReturn("example.com");
+ expect(req.getServerPort()).andReturn(-1).once(); // No port specified (0 or -1)
+ expect(req.getRequestURI()).andReturn("/my/path");
+ expect(req.getQueryString()).andReturn("one=two&three=four");
+ replay(req);
+
+ UriBuilder builder = new UriBuilder(req);
+ verify(req);
+
+ assertEquals("http://example.com/my/path?one=two&three=four", builder.toString());
+ }
}