Commits

Anonymous committed 679e871

Fix nullpointer when scheme is null

Issue number: WW-1391
Obtained from: Philip Luppens
Submitted by: Philip Luppens
Reviewed by: Tom Schneider

git-svn-id: http://svn.opensymphony.com/svn/webwork/trunk@2827573baa09-0c28-0410-bef9-dab3c582ae83

Comments (0)

Files changed (2)

src/java/com/opensymphony/webwork/views/util/UrlHelper.java

         // only append scheme if it is different to the current scheme *OR*
         // if we explicity want it to be appended by having forceAddSchemeHostAndPort = true
         if (forceAddSchemeHostAndPort) {
-        	String reqScheme = request.getScheme();
-        	changedScheme = true;
-        	link.append(scheme != null ? scheme : reqScheme);
-        	link.append("://");
-        	link.append(request.getServerName());
-        	
-        	if ((scheme.equals("http") && (httpPort != DEFAULT_HTTP_PORT)) || (scheme.equals("https") && httpsPort != DEFAULT_HTTPS_PORT))
-            {
-                link.append(":");
-                link.append(scheme.equals("http") ? httpPort : httpsPort);
+            String reqScheme = request.getScheme();
+            changedScheme = true;
+            link.append(scheme != null ? scheme : reqScheme);
+            link.append("://");
+            link.append(request.getServerName());
+            if(scheme != null) {
+                if((scheme.equals("http") && (httpPort != DEFAULT_HTTP_PORT))
+                    || (scheme.equals("https") && httpsPort != DEFAULT_HTTPS_PORT)) {
+                    link.append(":");
+                    link.append(scheme.equals("http") ? httpPort : httpsPort);
+                }
             }
         }
         else if (  

src/test/com/opensymphony/webwork/views/util/UrlHelperTest.java

 		assertEquals(expectedUrl, result);
         mockHttpServletRequest.verify();
     }
-	
+
+    public void testForceAddNullSchemeHostAndPort() throws Exception {
+        String expectedUrl = "http://localhost/contextPath/path1/path2/myAction.action";
+
+        Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
+        mockHttpServletRequest.expectAndReturn("getScheme", "http");
+        mockHttpServletRequest.expectAndReturn("getServerName", "localhost");
+        mockHttpServletRequest.expectAndReturn("getContextPath",
+            "/contextPath");
+
+        Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
+        mockHttpServletResponse.expectAndReturn("encodeURL", expectedUrl,
+            expectedUrl);
+
+        String result = UrlHelper.buildUrl("/path1/path2/myAction.action",
+            (HttpServletRequest) mockHttpServletRequest.proxy(),
+            (HttpServletResponse) mockHttpServletResponse.proxy(), null,
+            null, true, true, true);
+        assertEquals(expectedUrl, result);
+        mockHttpServletRequest.verify();
+    }
+
 	public void testDoNotForceAddSchemeHostAndPort() throws Exception {
 		String expectedUrl = "/contextPath/path1/path2/myAction.action";