You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by or...@apache.org on 2019/11/18 19:01:48 UTC

[qpid-broker-j] branch master updated: QPID-8374: Code clean-up

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

orudyy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/qpid-broker-j.git


The following commit(s) were added to refs/heads/master by this push:
     new 29cb374  QPID-8374: Code clean-up
29cb374 is described below

commit 29cb3747828abe3baac9477ed367cfcde77abc74
Author: Stanislav Khomytskyi <Re...@protonmail.com>
AuthorDate: Wed Nov 13 17:39:28 2019 +0100

    QPID-8374: Code clean-up
    
    This closes #41
    
    The patch was supplied by Stanislav Khomytskyi <Re...@protonmail.com>
---
 .../server/security/TrustStoreMessageSource.java   |  7 +--
 .../Base64MD5PasswordFilePrincipalDatabase.java    | 10 ++---
 .../server/security/auth/database/HashedUser.java  | 12 ++---
 .../auth/manager/SimpleAuthenticationManager.java  |  4 +-
 .../SimpleLDAPAuthenticationManagerImpl.java       | 52 +++++++++-------------
 .../encryption/AESKeyFileEncrypterFactory.java     | 14 +++---
 .../server/security/group/FileGroupDatabase.java   | 11 +----
 .../qpid/server/security/group/GroupPrincipal.java | 10 +++--
 .../apache/qpid/disttest/ConfigFileHelperTest.java | 26 +++--------
 9 files changed, 57 insertions(+), 89 deletions(-)

diff --git a/broker-core/src/main/java/org/apache/qpid/server/security/TrustStoreMessageSource.java b/broker-core/src/main/java/org/apache/qpid/server/security/TrustStoreMessageSource.java
index 7f7ad04..732edf9 100644
--- a/broker-core/src/main/java/org/apache/qpid/server/security/TrustStoreMessageSource.java
+++ b/broker-core/src/main/java/org/apache/qpid/server/security/TrustStoreMessageSource.java
@@ -146,7 +146,7 @@ public class TrustStoreMessageSource extends AbstractSystemMessageSource
             }
         }
         InternalMessageHeader header = new InternalMessageHeader(Collections.<String,Object>emptyMap(),
-                                                                 null, 0l, null, null, UUID.randomUUID().toString(),
+                                                                 null, 0L, null, null, UUID.randomUUID().toString(),
                                                                  null, null, (byte)4, System.currentTimeMillis(),
                                                                  0L, null, null, System.currentTimeMillis());
         return InternalMessage.createListMessage(_virtualHost.getMessageStore(), header, messageList);
@@ -157,10 +157,7 @@ public class TrustStoreMessageSource extends AbstractSystemMessageSource
         try
         {
             Set<Certificate> certCache = new HashSet<>();
-            for(Certificate cert : _trustStore.getCertificates())
-            {
-                certCache.add(cert);
-            }
+            Collections.addAll(certCache, _trustStore.getCertificates());
             return certCache;
         }
         catch (GeneralSecurityException e)
diff --git a/broker-core/src/main/java/org/apache/qpid/server/security/auth/database/Base64MD5PasswordFilePrincipalDatabase.java b/broker-core/src/main/java/org/apache/qpid/server/security/auth/database/Base64MD5PasswordFilePrincipalDatabase.java
index 1965e33..aa8327f 100644
--- a/broker-core/src/main/java/org/apache/qpid/server/security/auth/database/Base64MD5PasswordFilePrincipalDatabase.java
+++ b/broker-core/src/main/java/org/apache/qpid/server/security/auth/database/Base64MD5PasswordFilePrincipalDatabase.java
@@ -84,21 +84,21 @@ public class Base64MD5PasswordFilePrincipalDatabase extends AbstractPasswordFile
             byteArray[index++] = (byte) c;
         }
         
-        byte[] MD5byteArray;
+        byte[] md5byteArray;
         try
         {
-            MD5byteArray = HashedUser.getMD5(byteArray);
+            md5byteArray = HashedUser.getMD5(byteArray);
         }
         catch (Exception e1)
         {
-            getLogger().warn("Unable to hash password for user '" + principal + "' for comparison");
+            getLogger().warn("Unable to hash password for user '{}' for comparison", principal);
             return false;
         }
         
-        char[] hashedPassword = new char[MD5byteArray.length];
+        char[] hashedPassword = new char[md5byteArray.length];
 
         index = 0;
-        for (byte c : MD5byteArray)
+        for (byte c : md5byteArray)
         {
             hashedPassword[index++] = (char) c;
         }
diff --git a/broker-core/src/main/java/org/apache/qpid/server/security/auth/database/HashedUser.java b/broker-core/src/main/java/org/apache/qpid/server/security/auth/database/HashedUser.java
index b1ccf29..88fd821 100644
--- a/broker-core/src/main/java/org/apache/qpid/server/security/auth/database/HashedUser.java
+++ b/broker-core/src/main/java/org/apache/qpid/server/security/auth/database/HashedUser.java
@@ -51,17 +51,17 @@ public class HashedUser implements PasswordPrincipal
 
         _name = data[0];
 
-        byte[] encoded_password;
+        byte[] encodedPassword;
         try
         {
-            encoded_password = data[1].getBytes(Base64MD5PasswordFilePrincipalDatabase.DEFAULT_ENCODING);
+            encodedPassword = data[1].getBytes(Base64MD5PasswordFilePrincipalDatabase.DEFAULT_ENCODING);
         }
         catch (UnsupportedEncodingException e)
         {
             throw new ServerScopedRuntimeException("MD5 encoding not supported, even though the Java standard requires it",e);
         }
 
-        _encodedPassword = encoded_password;
+        _encodedPassword = encodedPassword;
         byte[] decoded = Strings.decodeBase64(data[1]);
         _password = new char[decoded.length];
 
@@ -145,12 +145,12 @@ public class HashedUser implements PasswordPrincipal
                 byteArray[index++] = (byte) c;
             }
             
-            byte[] MD5byteArray = getMD5(byteArray);
+            byte[] md5ByteArray = getMD5(byteArray);
             
-            _password = new char[MD5byteArray.length];
+            _password = new char[md5ByteArray.length];
 
             index = 0;
-            for (byte c : MD5byteArray)
+            for (byte c : md5ByteArray)
             {
                 _password[index++] = (char) c;
             }
diff --git a/broker-core/src/main/java/org/apache/qpid/server/security/auth/manager/SimpleAuthenticationManager.java b/broker-core/src/main/java/org/apache/qpid/server/security/auth/manager/SimpleAuthenticationManager.java
index 28058c4..11b3a00 100644
--- a/broker-core/src/main/java/org/apache/qpid/server/security/auth/manager/SimpleAuthenticationManager.java
+++ b/broker-core/src/main/java/org/apache/qpid/server/security/auth/manager/SimpleAuthenticationManager.java
@@ -19,7 +19,7 @@
 
 package org.apache.qpid.server.security.auth.manager;
 
-import java.io.IOException;
+
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
@@ -177,7 +177,7 @@ public class SimpleAuthenticationManager extends AbstractAuthenticationManager<S
     }
 
     @Override
-    public void reload() throws IOException
+    public void reload()
     {
     }
 
diff --git a/broker-core/src/main/java/org/apache/qpid/server/security/auth/manager/SimpleLDAPAuthenticationManagerImpl.java b/broker-core/src/main/java/org/apache/qpid/server/security/auth/manager/SimpleLDAPAuthenticationManagerImpl.java
index 7a18c6c..46846a5 100644
--- a/broker-core/src/main/java/org/apache/qpid/server/security/auth/manager/SimpleLDAPAuthenticationManagerImpl.java
+++ b/broker-core/src/main/java/org/apache/qpid/server/security/auth/manager/SimpleLDAPAuthenticationManagerImpl.java
@@ -327,6 +327,18 @@ public class SimpleLDAPAuthenticationManagerImpl
         return getOrLoadAuthenticationResult(username, password);
     }
 
+    private AuthenticationResult getOrLoadAuthenticationResult(final String userId, final String password)
+    {
+        return _authenticationResultCacher.getOrLoad(new String[]{userId, password}, new Callable<AuthenticationResult>()
+        {
+            @Override
+            public AuthenticationResult call()
+            {
+                return doLDAPNameAuthentication(userId, password);
+            }
+        });
+    }
+
     private AuthenticationResult doLDAPNameAuthentication(String userId, String password)
     {
         Subject gssapiIdentity = null;
@@ -406,18 +418,6 @@ public class SimpleLDAPAuthenticationManagerImpl
         }
     }
 
-    private AuthenticationResult getOrLoadAuthenticationResult(final String userId, final String password)
-    {
-        return _authenticationResultCacher.getOrLoad(new String[]{userId, password}, new Callable<AuthenticationResult>()
-        {
-            @Override
-            public AuthenticationResult call()
-            {
-                return doLDAPNameAuthentication(userId, password);
-            }
-        });
-    }
-
     private boolean isGroupSearchRequired()
     {
         if (isSpecified(getGroupAttributeName()))
@@ -425,24 +425,19 @@ public class SimpleLDAPAuthenticationManagerImpl
             return true;
         }
 
-        if (isSpecified(getGroupSearchContext()) && isSpecified(getGroupSearchFilter()))
-        {
-            return true;
-        }
-
-        return false;
+        return (isSpecified(getGroupSearchContext()) && isSpecified(getGroupSearchFilter()));
     }
 
     private boolean isSpecified(String value)
     {
-        return value != null && !"".equals(value);
+        return (value != null && !value.isEmpty());
     }
 
     private Set<Principal> findGroups(DirContext context, String userDN, final Subject gssapiIdentity)
             throws NamingException
     {
         Set<Principal> groupPrincipals = new HashSet<>();
-        if (getGroupAttributeName() != null && !"".equals(getGroupAttributeName()))
+        if (getGroupAttributeName() != null && !getGroupAttributeName().isEmpty())
         {
             Attributes attributes = context.getAttributes(userDN, new String[]{getGroupAttributeName()});
             NamingEnumeration<? extends Attribute> namingEnum = attributes.getAll();
@@ -465,8 +460,8 @@ public class SimpleLDAPAuthenticationManagerImpl
             }
         }
 
-        if (getGroupSearchContext() != null && !"".equals(getGroupSearchContext()) &&
-            getGroupSearchFilter() != null && !"".equals(getGroupSearchFilter()))
+        if (getGroupSearchContext() != null && !getGroupSearchContext().isEmpty() &&
+                getGroupSearchFilter() != null && !getGroupSearchFilter().isEmpty())
         {
             SearchControls searchControls = new SearchControls();
             searchControls.setReturningAttributes(new String[]{});
@@ -493,11 +488,8 @@ public class SimpleLDAPAuthenticationManagerImpl
     {
         StringBuilder encoded = new StringBuilder(value.length());
         char[] chars = value.toCharArray();
-        for (int i = 0; i < chars.length; i++)
-        {
-            char ch = chars[i];
-            switch (ch)
-            {
+        for (char ch : chars) {
+            switch (ch) {
                 case '\0':
                     encoded.append("\\00");
                     break;
@@ -687,12 +679,11 @@ public class SimpleLDAPAuthenticationManagerImpl
             {
                 SearchControls searchControls = new SearchControls();
                 searchControls.setReturningAttributes(new String[]{});
-                searchControls.setCountLimit(1l);
+                searchControls.setCountLimit(1L);
                 searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
-                NamingEnumeration<?> namingEnum = null;
 
                 LOGGER.debug("Searching for '{}'", id);
-                namingEnum = invokeContextOperationAs(gssapiIdentity,
+                NamingEnumeration<?> namingEnum = invokeContextOperationAs(gssapiIdentity,
                                                       (PrivilegedExceptionAction<NamingEnumeration<?>>) () -> ctx.search(
                                                               _searchContext,
                                                               _searchFilter,
@@ -800,7 +791,6 @@ public class SimpleLDAPAuthenticationManagerImpl
             if (ctx != null)
             {
                 ctx.close();
-                ctx = null;
             }
         }
         catch (Exception e)
diff --git a/broker-core/src/main/java/org/apache/qpid/server/security/encryption/AESKeyFileEncrypterFactory.java b/broker-core/src/main/java/org/apache/qpid/server/security/encryption/AESKeyFileEncrypterFactory.java
index 3be5bb4..c176575 100644
--- a/broker-core/src/main/java/org/apache/qpid/server/security/encryption/AESKeyFileEncrypterFactory.java
+++ b/broker-core/src/main/java/org/apache/qpid/server/security/encryption/AESKeyFileEncrypterFactory.java
@@ -79,6 +79,8 @@ public class AESKeyFileEncrypterFactory implements ConfigurationSecretEncrypterF
 
     private static final boolean IS_AVAILABLE;
 
+    private static final String ILLEGAL_ARG_EXCEPTION = "Unable to determine a mechanism to protect access to the key file on this filesystem";
+
     static
     {
         boolean isAvailable;
@@ -88,9 +90,9 @@ public class AESKeyFileEncrypterFactory implements ConfigurationSecretEncrypterF
             isAvailable = allowedKeyLength >=AES_KEY_SIZE_BITS;
             if(!isAvailable)
             {
-                LOGGER.warn("The " + TYPE + " configuration encryption encryption mechanism is not available. "
-                            + "Maximum available AES key length is " + allowedKeyLength + " but " + AES_KEY_SIZE_BITS + " is required."
-                            +"Ensure the full strength JCE policy has been installed into your JVM.");
+                LOGGER.warn("The {} configuration encryption encryption mechanism is not available. "
+                            + "Maximum available AES key length is {} but {} is required. "
+                            + "Ensure the full strength JCE policy has been installed into your JVM.", TYPE, allowedKeyLength, AES_KEY_SIZE_BITS);
             }
         }
         catch (NoSuchAlgorithmException e)
@@ -217,7 +219,7 @@ public class AESKeyFileEncrypterFactory implements ConfigurationSecretEncrypterF
         }
         else
         {
-            throw new IllegalArgumentException("Unable to determine a mechanism to protect access to the key file on this filesystem");
+            throw new IllegalArgumentException(ILLEGAL_ARG_EXCEPTION);
         }
     }
 
@@ -291,7 +293,7 @@ public class AESKeyFileEncrypterFactory implements ConfigurationSecretEncrypterF
         }
         else
         {
-            throw new IllegalArgumentException("Unable to determine a mechanism to protect access to the key file on this filesystem");
+            throw new IllegalArgumentException(ILLEGAL_ARG_EXCEPTION);
         }
     }
 
@@ -364,7 +366,7 @@ public class AESKeyFileEncrypterFactory implements ConfigurationSecretEncrypterF
         }
         else
         {
-            throw new IllegalArgumentException("Unable to determine a mechanism to protect access to the key file on this filesystem");
+            throw new IllegalArgumentException(ILLEGAL_ARG_EXCEPTION);
         }
     }
 
diff --git a/broker-core/src/main/java/org/apache/qpid/server/security/group/FileGroupDatabase.java b/broker-core/src/main/java/org/apache/qpid/server/security/group/FileGroupDatabase.java
index 725e5a6..90efc5f 100644
--- a/broker-core/src/main/java/org/apache/qpid/server/security/group/FileGroupDatabase.java
+++ b/broker-core/src/main/java/org/apache/qpid/server/security/group/FileGroupDatabase.java
@@ -197,18 +197,9 @@ public class FileGroupDatabase implements GroupDatabase
         _groupToUserMap.clear();
         _userToGroupMap.clear();
         Properties propertiesFile = new Properties();
-        FileInputStream fileInputStream = new FileInputStream(groupFile);
-        try
-        {
+        try (FileInputStream fileInputStream = new FileInputStream(groupFile)) {
             propertiesFile.load(fileInputStream);
         }
-        finally
-        {
-            if(fileInputStream != null)
-            {
-                fileInputStream.close();
-            }
-        }
 
         for (String propertyName : propertiesFile.stringPropertyNames())
         {
diff --git a/broker-core/src/main/java/org/apache/qpid/server/security/group/GroupPrincipal.java b/broker-core/src/main/java/org/apache/qpid/server/security/group/GroupPrincipal.java
index d760e7a..dde974e 100644
--- a/broker-core/src/main/java/org/apache/qpid/server/security/group/GroupPrincipal.java
+++ b/broker-core/src/main/java/org/apache/qpid/server/security/group/GroupPrincipal.java
@@ -22,6 +22,7 @@ package org.apache.qpid.server.security.group;
 
 import java.security.Principal;
 import java.util.Enumeration;
+import java.util.Objects;
 
 import org.apache.qpid.server.model.ConfiguredObject;
 import org.apache.qpid.server.security.QpidPrincipal;
@@ -39,6 +40,7 @@ public class GroupPrincipal implements QpidPrincipal
     /** Name of the group */
     private final String _groupName;
     private final ConfiguredObject<?> _origin;
+    private static final String msgException = "Not supported";
 
     public GroupPrincipal(final String groupName, final ConfiguredObject<?> origin)
     {
@@ -58,22 +60,22 @@ public class GroupPrincipal implements QpidPrincipal
 
     public boolean addMember(Principal user)
     {
-        throw new UnsupportedOperationException("Not supported");
+        throw new UnsupportedOperationException(msgException);
     }
 
     public boolean removeMember(Principal user)
     {
-        throw new UnsupportedOperationException("Not supported");
+        throw new UnsupportedOperationException(msgException);
     }
 
     public boolean isMember(Principal member)
     {
-        throw new UnsupportedOperationException("Not supported");
+        throw new UnsupportedOperationException(msgException);
     }
 
     public Enumeration<? extends Principal> members()
     {
-        throw new UnsupportedOperationException("Not supported");
+        throw new UnsupportedOperationException(msgException);
     }
 
     @Override
diff --git a/perftests/src/test/java/org/apache/qpid/disttest/ConfigFileHelperTest.java b/perftests/src/test/java/org/apache/qpid/disttest/ConfigFileHelperTest.java
index 9fe1069..2bcec3c 100644
--- a/perftests/src/test/java/org/apache/qpid/disttest/ConfigFileHelperTest.java
+++ b/perftests/src/test/java/org/apache/qpid/disttest/ConfigFileHelperTest.java
@@ -25,41 +25,29 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
-import org.junit.Assert;
 
 import org.apache.qpid.test.utils.TestFileUtils;
 
-import org.junit.Assert;
 import org.junit.Before;
-import org.junit.After;
 import org.junit.Test;
 
 import org.apache.qpid.test.utils.UnitTestBase;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertNotSame;
-import static org.junit.Assert.fail;
-import static org.junit.Assert.assertNotNull;
 
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
 
-public class ConfigFileHelperTest extends UnitTestBase
-{
+
+public class ConfigFileHelperTest extends UnitTestBase {
     private File _testDir;
     private ConfigFileHelper _configFileHelper = new ConfigFileHelper();
 
     @Before
-    public void setUp() throws Exception
-    {
+    public void setUp() throws Exception {
         _testDir = TestFileUtils.createTestDirectory();
     }
 
     @Test
-    public void testGetTestConfigFilesForDirectory() throws Exception
-    {
+    public void testGetTestConfigFilesForDirectory() throws Exception {
         String jsFile = createFile("file1.js");
         String jsonFile = createFile("file2.json");
         createFile("file.txt");
@@ -75,14 +63,12 @@ public class ConfigFileHelperTest extends UnitTestBase
         assertEquals(expectedFiles, actualFiles);
     }
 
-    private void createDir(String dirName)
-    {
+    private void createDir(String dirName) {
         File dir = new File(_testDir, dirName);
         dir.mkdir();
     }
 
-    private String createFile(String fileName) throws IOException
-    {
+    private String createFile(String fileName) throws IOException {
         File file = new File(_testDir, fileName);
         file.createNewFile();
         return file.getAbsolutePath();


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org