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 2019/08/19 20:17:17 UTC

[jspwiki] 18/24: small refactor of parseLong

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 22a7d523d6b2a7a301353ded2ba96d0d04ce3ffd
Author: juanpablo <ju...@apache.org>
AuthorDate: Sun Aug 18 20:54:06 2019 +0200

    small refactor of parseLong
---
 .../wiki/auth/user/AbstractUserDatabase.java       | 74 ++++++++--------------
 1 file changed, 27 insertions(+), 47 deletions(-)

diff --git a/jspwiki-main/src/main/java/org/apache/wiki/auth/user/AbstractUserDatabase.java b/jspwiki-main/src/main/java/org/apache/wiki/auth/user/AbstractUserDatabase.java
index 5c985d6..727d0cd 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/auth/user/AbstractUserDatabase.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/auth/user/AbstractUserDatabase.java
@@ -18,14 +18,7 @@
  */
 package org.apache.wiki.auth.user;
 
-import java.nio.charset.StandardCharsets;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
-import java.security.Principal;
-import java.util.ArrayList;
-import java.util.Properties;
-import java.util.UUID;
-
+import org.apache.commons.lang3.math.NumberUtils;
 import org.apache.log4j.Logger;
 import org.apache.wiki.WikiEngine;
 import org.apache.wiki.api.exceptions.NoRequiredPropertyException;
@@ -35,6 +28,14 @@ import org.apache.wiki.auth.WikiSecurityException;
 import org.apache.wiki.util.ByteUtils;
 import org.apache.wiki.util.CryptoUtil;
 
+import java.nio.charset.StandardCharsets;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.security.Principal;
+import java.util.ArrayList;
+import java.util.Properties;
+import java.util.UUID;
+
 /**
  * Abstract UserDatabase class that provides convenience methods for finding
  * profiles, building Principal collections and hashing passwords.
@@ -260,8 +261,7 @@ public abstract class AbstractUserDatabase implements UserDatabase
      * @param db The database for which the UID should be generated.
      * @return A random, unique UID.
      */
-    protected static String generateUid( UserDatabase db )
-    {
+    protected static String generateUid( UserDatabase db ) {
         // Keep generating UUIDs until we find one that doesn't collide
         String uid = null;
         boolean collision;
@@ -290,19 +290,13 @@ public abstract class AbstractUserDatabase implements UserDatabase
      * @param text the text to hash
      * @return the result hash
      */
-    protected String getHash( String text )
-    {
-        String hash = null;
-        try
-        {
-            hash = CryptoUtil.getSaltedPassword( text.getBytes(StandardCharsets.UTF_8) );
-        }
-        catch( NoSuchAlgorithmException e )
-        {
+    protected String getHash( final String text ) {
+        try {
+            return CryptoUtil.getSaltedPassword( text.getBytes(StandardCharsets.UTF_8 ) );
+        } catch( final NoSuchAlgorithmException e ) {
             log.error( "Error creating salted SHA password hash:" + e.getMessage() );
-            hash = text;
+            return text;
         }
-        return hash;
     }
 
     /**
@@ -312,22 +306,16 @@ public abstract class AbstractUserDatabase implements UserDatabase
      * @return the result hash
      * @deprecated this method is retained for backwards compatibility purposes; use {@link #getHash(String)} instead
      */
-    protected String getOldHash( String text )
-    {
-        String hash = null;
-        try
-        {
-            MessageDigest md = MessageDigest.getInstance( "SHA" );
-            md.update( text.getBytes(StandardCharsets.UTF_8) );
+    protected String getOldHash( final String text ) {
+        try {
+            final MessageDigest md = MessageDigest.getInstance( "SHA" );
+            md.update( text.getBytes( StandardCharsets.UTF_8 ) );
             byte[] digestedBytes = md.digest();
-            hash = ByteUtils.bytes2hex( digestedBytes );
-        }
-        catch( NoSuchAlgorithmException e )
-        {
+            return ByteUtils.bytes2hex( digestedBytes );
+        } catch( final NoSuchAlgorithmException e ) {
             log.error( "Error creating SHA password hash:" + e.getMessage() );
-            hash = text;
+            return text;
         }
-        return hash;
     }
 
     /**
@@ -335,19 +323,11 @@ public abstract class AbstractUserDatabase implements UserDatabase
      * @param value the string to parse
      * @return the value parsed
      */
-    protected long parseLong( String value )
-    {
-        if ( value == null || value.length() == 0 )
-        {
-            return 0;
-        }
-        try
-        {
-            return Long.parseLong( value );
-        }
-        catch ( NumberFormatException e )
-        {
-            return 0;
+    protected long parseLong( final String value ) {
+        if( NumberUtils.isParsable( value ) ) {
+            return Long.valueOf( value );
+        } else {
+            return 0L;
         }
     }