You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by ju...@apache.org on 2020/03/21 17:04:04 UTC

[jspwiki] 02/36: catch rare NPE + provide some tests

This is an automated email from the ASF dual-hosted git repository.

juanpablo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jspwiki.git

commit bdab6f2ce4ad1cf5d601563c1982f6ec69cb5b12
Author: juanpablo <ju...@apache.org>
AuthorDate: Tue Mar 17 22:00:36 2020 +0100

    catch rare NPE + provide some tests
---
 .../src/main/java/org/apache/wiki/util/HttpUtil.java  | 13 ++++++-------
 .../test/java/org/apache/wiki/util/HttpUtilTest.java  | 19 +++++++++++++++++++
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/jspwiki-util/src/main/java/org/apache/wiki/util/HttpUtil.java b/jspwiki-util/src/main/java/org/apache/wiki/util/HttpUtil.java
index 7ad7399..46ee931 100644
--- a/jspwiki-util/src/main/java/org/apache/wiki/util/HttpUtil.java
+++ b/jspwiki-util/src/main/java/org/apache/wiki/util/HttpUtil.java
@@ -70,12 +70,11 @@ public final class HttpUtil {
      */
     public static String retrieveCookieValue( final HttpServletRequest request, final String cookieName ) {
         final Cookie[] cookies = request.getCookies();
-
         if( cookies != null ) {
             for( final Cookie cookie : cookies ) {
                 if( cookie.getName().equals( cookieName ) ) {
                     String value = cookie.getValue();
-                    if( value.length() == 0 ) {
+                    if( value == null || value.length() == 0 ) {
                         return null;
                     }
                     if( value.charAt( 0 ) == '"' && value.charAt( value.length() - 1 ) == '"' ) {
@@ -180,7 +179,7 @@ public final class HttpUtil {
     }
 
 	static boolean notBeginningWithHttpOrHttps( final String uri ) {
-		return uri.length() > 0 && !( ( uri.startsWith("http://" ) || uri.startsWith( "https://" ) ) );
+		return uri.length() > 0 && !( uri.startsWith("http://" ) || uri.startsWith( "https://" ) );
 	}
 
     /**
@@ -203,12 +202,12 @@ public final class HttpUtil {
             // Ensure that the 'page=xyz' attribute is removed
             // FIXME: Is it really the mandate of this routine to do that?
             //
-            final int pos1 = res.indexOf("page=");
+            final int pos1 = res.indexOf( "page=" );
             if( pos1 >= 0 ) {
                 String tmpRes = res.substring( 0, pos1 );
-                final int pos2 = res.indexOf( "&",pos1 ) + 1;
-                if ( ( pos2 > 0 ) && ( pos2 < res.length() ) ) {
-                    tmpRes = tmpRes + res.substring(pos2);
+                final int pos2 = res.indexOf( '&', pos1 ) + 1;
+                if( ( pos2 > 0 ) && ( pos2 < res.length() ) ) {
+                    tmpRes = tmpRes + res.substring( pos2 );
                 }
                 res = tmpRes;
             }
diff --git a/jspwiki-util/src/test/java/org/apache/wiki/util/HttpUtilTest.java b/jspwiki-util/src/test/java/org/apache/wiki/util/HttpUtilTest.java
index 28d02d4..1afc63e 100644
--- a/jspwiki-util/src/test/java/org/apache/wiki/util/HttpUtilTest.java
+++ b/jspwiki-util/src/test/java/org/apache/wiki/util/HttpUtilTest.java
@@ -18,9 +18,12 @@
  */
 package org.apache.wiki.util;
 
+import net.sourceforge.stripes.mock.MockHttpServletRequest;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
+import javax.servlet.http.Cookie;
+
 
 public class HttpUtilTest {
 
@@ -47,4 +50,20 @@ public class HttpUtilTest {
         Assertions.assertTrue( HttpUtil.isIPV4Address( "123.123.123.123" ) );
     }
 
+    @Test
+    public void testRetrieveCookieValue() {
+        final Cookie[] cookies = new Cookie[] { new Cookie( "cookie1", "value1" ),
+                                                new Cookie( "cookie2", "\"value2\"" ),
+                                                new Cookie( "cookie3", "" ),
+                                                new Cookie( "cookie4", null ) };
+        final MockHttpServletRequest req = new MockHttpServletRequest( "/wiki", "/example" );
+        req.setCookies( cookies );
+
+        Assertions.assertEquals( "value1", HttpUtil.retrieveCookieValue( req, "cookie1" ) );
+        Assertions.assertEquals( "value2", HttpUtil.retrieveCookieValue( req, "cookie2" ) );
+        Assertions.assertNull( HttpUtil.retrieveCookieValue( req, "cookie3" ) );
+        Assertions.assertNull( HttpUtil.retrieveCookieValue( req, "cookie4" ) );
+        Assertions.assertNull( HttpUtil.retrieveCookieValue( req, "cookie5" ) );
+    }
+
 }