directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cohei...@apache.org
Subject directory-kerby git commit: Fixing some errors thrown up by SpotBugs
Date Mon, 26 Feb 2018 13:12:29 GMT
Repository: directory-kerby
Updated Branches:
  refs/heads/trunk 2afba2e07 -> 439aeaed8


Fixing some errors thrown up by SpotBugs


Project: http://git-wip-us.apache.org/repos/asf/directory-kerby/repo
Commit: http://git-wip-us.apache.org/repos/asf/directory-kerby/commit/439aeaed
Tree: http://git-wip-us.apache.org/repos/asf/directory-kerby/tree/439aeaed
Diff: http://git-wip-us.apache.org/repos/asf/directory-kerby/diff/439aeaed

Branch: refs/heads/trunk
Commit: 439aeaed8a94814184fb8537587b8cdb8819b6e4
Parents: 2afba2e
Author: Colm O hEigeartaigh <coheigea@apache.org>
Authored: Mon Feb 26 13:12:16 2018 +0000
Committer: Colm O hEigeartaigh <coheigea@apache.org>
Committed: Mon Feb 26 13:12:16 2018 +0000

----------------------------------------------------------------------
 .../org/apache/kerby/has/client/HasClient.java  | 20 ++++++++------------
 .../has/client/HasClientPluginRegistry.java     |  3 +--
 .../apache/kerby/has/client/HasLoginModule.java |  5 +----
 .../apache/kerby/has/common/ssl/SSLFactory.java |  2 +-
 .../apache/kerby/has/common/util/HasUtil.java   |  7 ++++---
 .../org/apache/kerby/has/server/HasServer.java  |  2 +-
 .../kerby/has/server/web/rest/AsRequestApi.java |  2 +-
 .../kerby/has/server/web/rest/HadminApi.java    | 10 +++++-----
 .../admin/server/kadmin/AdminServerHandler.java |  5 ++---
 .../kerb/server/request/KdcRequest.java         |  8 +++-----
 .../kerby/kerberos/tool/cmd/InitKdcCmd.java     | 14 +++++---------
 11 files changed, 32 insertions(+), 46 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/439aeaed/has-project/has-client/src/main/java/org/apache/kerby/has/client/HasClient.java
----------------------------------------------------------------------
diff --git a/has-project/has-client/src/main/java/org/apache/kerby/has/client/HasClient.java
b/has-project/has-client/src/main/java/org/apache/kerby/has/client/HasClient.java
index 848b0c4..e94ae5d 100755
--- a/has-project/has-client/src/main/java/org/apache/kerby/has/client/HasClient.java
+++ b/has-project/has-client/src/main/java/org/apache/kerby/has/client/HasClient.java
@@ -56,7 +56,6 @@ import org.slf4j.LoggerFactory;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -127,14 +126,14 @@ public class HasClient {
         } else {
             config = new HasConfig();
             String[] urls = hadoopSecurityHas.split(";");
-            String host = "";
+            StringBuilder host = new StringBuilder();
             int port = 0;
             try {
                 for (String url : urls) {
                     URI uri = new URI(url.trim());
 
                     // parse host
-                    host = host + uri.getHost() + ",";
+                    host.append(uri.getHost()).append(",");
 
                     // parse port
                     if (port == 0) {
@@ -157,11 +156,10 @@ public class HasClient {
                         }
                     }
                 }
-                if (host == null || port == 0) {
+                if (host.length() == 0 || port == 0) {
                     throw new HasException("host is null.");
                 } else {
-                    host = host.substring(0, host.length() - 1);
-                    config.setString(HasConfigKey.HTTPS_HOST, host);
+                    config.setString(HasConfigKey.HTTPS_HOST,  host.subSequence(0, host.length()
- 1).toString());
                     config.setInt(HasConfigKey.HTTPS_PORT, port);
                     config.setString(HasConfigKey.AUTH_TYPE, type);
                 }
@@ -216,9 +214,6 @@ public class HasClient {
         } else {
             throw new HasException("Please set the plugin name in has client conf");
         }
-        if (clientPlugin == null) {
-            throw new HasException("Failed to create client plugin: " + pluginName);
-        }
         LOG.info("The plugin class is: " + clientPlugin);
 
         return clientPlugin;
@@ -559,9 +554,10 @@ public class HasClient {
             }
 
             CertificateFactory factory = CertificateFactory.getInstance("X.509");
-            FileInputStream in = new FileInputStream(caRootFile);
-            caRoot = (X509Certificate) factory.generateCertificate(in);
-        } catch (CertificateException | FileNotFoundException e) {
+            try (FileInputStream in = new FileInputStream(caRootFile)) {
+                caRoot = (X509Certificate) factory.generateCertificate(in);
+            }
+        } catch (CertificateException | IOException e) {
             throw new HasException("Failed to get certificate from ca root file", e);
         }
 

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/439aeaed/has-project/has-client/src/main/java/org/apache/kerby/has/client/HasClientPluginRegistry.java
----------------------------------------------------------------------
diff --git a/has-project/has-client/src/main/java/org/apache/kerby/has/client/HasClientPluginRegistry.java
b/has-project/has-client/src/main/java/org/apache/kerby/has/client/HasClientPluginRegistry.java
index 45cd193..2add250 100644
--- a/has-project/has-client/src/main/java/org/apache/kerby/has/client/HasClientPluginRegistry.java
+++ b/has-project/has-client/src/main/java/org/apache/kerby/has/client/HasClientPluginRegistry.java
@@ -53,8 +53,7 @@ public class HasClientPluginRegistry {
             throw new HasException("Unregistered plugin " + name);
         }
         try {
-            HasClientPlugin clientPlugin = (HasClientPlugin) allPlugins.get(name).newInstance();
-            return clientPlugin;
+            return (HasClientPlugin) allPlugins.get(name).newInstance();
         } catch (Exception e) {
             LOG.error("Create {} plugin failed", name, e);
             throw new HasException(e.getMessage());

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/439aeaed/has-project/has-client/src/main/java/org/apache/kerby/has/client/HasLoginModule.java
----------------------------------------------------------------------
diff --git a/has-project/has-client/src/main/java/org/apache/kerby/has/client/HasLoginModule.java
b/has-project/has-client/src/main/java/org/apache/kerby/has/client/HasLoginModule.java
index e222020..5b037ef 100644
--- a/has-project/has-client/src/main/java/org/apache/kerby/has/client/HasLoginModule.java
+++ b/has-project/has-client/src/main/java/org/apache/kerby/has/client/HasLoginModule.java
@@ -255,14 +255,11 @@ public class HasLoginModule implements LoginModule {
                     null);
 
                 // get the principal name from the ticket cache
-                if (cred != null && principal == null) {
+                if (principal == null) {
                     principal = cred.getClient();
                 }
                 if (debug) {
                     System.out.println("Principal is " + principal);
-                    if (cred == null) {
-                        System.out.println("null credentials from TGT Ticket");
-                    }
                 }
             }
         } catch (KrbException e) {

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/439aeaed/has-project/has-common/src/main/java/org/apache/kerby/has/common/ssl/SSLFactory.java
----------------------------------------------------------------------
diff --git a/has-project/has-common/src/main/java/org/apache/kerby/has/common/ssl/SSLFactory.java
b/has-project/has-common/src/main/java/org/apache/kerby/has/common/ssl/SSLFactory.java
index 874ff08..6dc08cc 100644
--- a/has-project/has-common/src/main/java/org/apache/kerby/has/common/ssl/SSLFactory.java
+++ b/has-project/has-common/src/main/java/org/apache/kerby/has/common/ssl/SSLFactory.java
@@ -117,7 +117,7 @@ public class SSLFactory implements ConnectionConfigurator {
     if (mode == Mode.CLIENT) {
       sslConfResource = conf.getString(SSLFactory.SSL_CLIENT_CONF_KEY);
     } else {
-      sslConfResource = conf.getString(SSLFactory.SSL_CLIENT_CONF_KEY);
+      sslConfResource = conf.getString(SSLFactory.SSL_SERVER_CONF_KEY);
     }
     try {
       sslConf.addIniConfig(new File(sslConfResource));

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/439aeaed/has-project/has-common/src/main/java/org/apache/kerby/has/common/util/HasUtil.java
----------------------------------------------------------------------
diff --git a/has-project/has-common/src/main/java/org/apache/kerby/has/common/util/HasUtil.java
b/has-project/has-common/src/main/java/org/apache/kerby/has/common/util/HasUtil.java
index 4fbdff8..19cbaea 100644
--- a/has-project/has-common/src/main/java/org/apache/kerby/has/common/util/HasUtil.java
+++ b/has-project/has-common/src/main/java/org/apache/kerby/has/common/util/HasUtil.java
@@ -74,9 +74,10 @@ public class HasUtil {
                 }
                 sb.append(tempString + "\n");
             }
-            PrintStream ps = new PrintStream(new FileOutputStream(hasConfFile));
-            ps.print(sb.toString());
-            bf.close();
+            try (PrintStream ps = new PrintStream(new FileOutputStream(hasConfFile))) {
+                ps.print(sb.toString());
+                bf.close();
+            }
         } catch (FileNotFoundException e) {
             throw new HasException("Can not load the has configuration file "
                     + hasConfFile.getAbsolutePath());

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/439aeaed/has-project/has-server/src/main/java/org/apache/kerby/has/server/HasServer.java
----------------------------------------------------------------------
diff --git a/has-project/has-server/src/main/java/org/apache/kerby/has/server/HasServer.java
b/has-project/has-server/src/main/java/org/apache/kerby/has/server/HasServer.java
index 8608bf0..4df8dfc 100644
--- a/has-project/has-server/src/main/java/org/apache/kerby/has/server/HasServer.java
+++ b/has-project/has-server/src/main/java/org/apache/kerby/has/server/HasServer.java
@@ -121,7 +121,7 @@ public class HasServer {
         try {
             krbConfig = ClientUtil.getConfig(confDir);
         } catch (KrbException e) {
-            new HasException("Errors occurred when getting the config from conf dir. "
+            throw new HasException("Errors occurred when getting the config from conf dir.
"
                 + e.getMessage());
         }
         if (krbConfig == null) {

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/439aeaed/has-project/has-server/src/main/java/org/apache/kerby/has/server/web/rest/AsRequestApi.java
----------------------------------------------------------------------
diff --git a/has-project/has-server/src/main/java/org/apache/kerby/has/server/web/rest/AsRequestApi.java
b/has-project/has-server/src/main/java/org/apache/kerby/has/server/web/rest/AsRequestApi.java
index 6415161..45e6ea6 100644
--- a/has-project/has-server/src/main/java/org/apache/kerby/has/server/web/rest/AsRequestApi.java
+++ b/has-project/has-server/src/main/java/org/apache/kerby/has/server/web/rest/AsRequestApi.java
@@ -87,7 +87,7 @@ public class AsRequestApi {
 
             if (hasServer.getKdcServer() == null) {
                 errMessage = "Please start the has KDC server.";
-            } else if (!tokenStr.isEmpty() && tokenStr != null) {
+            } else if (tokenStr != null && !tokenStr.isEmpty()) {
                 HasKdcHandler kdcHandler = new HasKdcHandler(hasServer);
 
                 TokenDecoder tokenDecoder = KrbRuntime.getTokenProvider("JWT").createTokenDecoder();

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/439aeaed/has-project/has-server/src/main/java/org/apache/kerby/has/server/web/rest/HadminApi.java
----------------------------------------------------------------------
diff --git a/has-project/has-server/src/main/java/org/apache/kerby/has/server/web/rest/HadminApi.java
b/has-project/has-server/src/main/java/org/apache/kerby/has/server/web/rest/HadminApi.java
index f81a266..7054a2a 100644
--- a/has-project/has-server/src/main/java/org/apache/kerby/has/server/web/rest/HadminApi.java
+++ b/has-project/has-server/src/main/java/org/apache/kerby/has/server/web/rest/HadminApi.java
@@ -95,7 +95,7 @@ public class HadminApi {
                 WebServer.LOG.info("Failed to create local hadmin." + e.getMessage());
             }
             JSONObject result = new JSONObject();
-            String msg = "";
+            StringBuilder msg = new StringBuilder();
             try {
                 StringBuilder data = new StringBuilder();
                 BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
@@ -109,16 +109,16 @@ public class HadminApi {
                     JSONObject host = (JSONObject) hostArray.get(i);
                     String[] roles = host.getString("hostRoles").split(",");
                     for (String role : roles) {
-                        msg += hasAdmin.addPrincByRole(host.getString("name"), role.toUpperCase());
+                        msg.append(hasAdmin.addPrincByRole(host.getString("name"), role.toUpperCase()));
                     }
                 }
                 result.put("result", "success");
-                result.put("msg", msg);
+                result.put("msg", msg.toString());
                 return Response.ok(result.toString()).build();
             } catch (Exception e) {
                 WebServer.LOG.error("Failed to create principals,because : " + e.getMessage());
-                msg = "Failed to create principals,because : " + e.getMessage();
-                return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
+                String error = "Failed to create principals,because : " + e.getMessage();
+                return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build();
             }
         }
         return Response.status(Response.Status.FORBIDDEN).entity("HTTPS required.\n").build();

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/439aeaed/kerby-kerb/kerb-admin-server/src/main/java/org/apache/kerby/kerberos/kerb/admin/server/kadmin/AdminServerHandler.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-admin-server/src/main/java/org/apache/kerby/kerberos/kerb/admin/server/kadmin/AdminServerHandler.java
b/kerby-kerb/kerb-admin-server/src/main/java/org/apache/kerby/kerberos/kerb/admin/server/kadmin/AdminServerHandler.java
index 43b042b..7875fa6 100644
--- a/kerby-kerb/kerb-admin-server/src/main/java/org/apache/kerby/kerberos/kerb/admin/server/kadmin/AdminServerHandler.java
+++ b/kerby-kerb/kerb-admin-server/src/main/java/org/apache/kerby/kerberos/kerb/admin/server/kadmin/AdminServerHandler.java
@@ -224,12 +224,11 @@ public class AdminServerHandler {
         AdminMessageCode value = new AdminMessageCode(xdrFieldInfos);
         adminMessage.setMessageBuffer(ByteBuffer.wrap(value.encode()));
 
-        ByteBuffer responseMessage = KadminCode.encodeMessage(adminMessage);
-        return responseMessage;
+        return KadminCode.encodeMessage(adminMessage);
     }
 
     private String listToString(List<String> list) {
-        if (list.size() <= 0) {
+        if (list.isEmpty()) {
             return null;
         }
         //Both speed and safety,so use StringBuilder

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/439aeaed/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/request/KdcRequest.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/request/KdcRequest.java
b/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/request/KdcRequest.java
index a2f009a..2e97451 100644
--- a/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/request/KdcRequest.java
+++ b/kerby-kerb/kerb-server/src/main/java/org/apache/kerby/kerberos/kerb/server/request/KdcRequest.java
@@ -220,17 +220,15 @@ public abstract class KdcRequest {
                 if (paEntry.getPaDataType() == PaDataType.FX_FAST) {
                     LOG.info("Found fast padata and starting to process it.");
 
-                    PaFxFastRequest paFxFastRequest = new PaFxFastRequest();
-                    KrbFastArmoredReq fastArmoredReq = null;
+                    PaFxFastRequest paFxFastRequest = null;
                     try {
-                        paFxFastRequest = KrbCodec.decode(paEntry.getPaDataValue(),
-                            PaFxFastRequest.class);
+                        paFxFastRequest = KrbCodec.decode(paEntry.getPaDataValue(), PaFxFastRequest.class);
                     } catch (KrbException e) {
                         String errMessage = "Decode PaFxFastRequest failed. " + e.getMessage();
                         LOG.error(errMessage);
                         throw new KrbException(errMessage);
                     }
-                    fastArmoredReq = paFxFastRequest.getFastArmoredReq();
+                    KrbFastArmoredReq fastArmoredReq = paFxFastRequest.getFastArmoredReq();
                     if (fastArmoredReq == null) {
                         return;
                     }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/439aeaed/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/cmd/InitKdcCmd.java
----------------------------------------------------------------------
diff --git a/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/cmd/InitKdcCmd.java
b/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/cmd/InitKdcCmd.java
index 63fe30a..7f13073 100644
--- a/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/cmd/InitKdcCmd.java
+++ b/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/cmd/InitKdcCmd.java
@@ -65,20 +65,16 @@ public class InitKdcCmd extends InitCmd {
             return;
         }
 
-        FileOutputStream fos = null;
-        try {
-            fos = new FileOutputStream(adminKeytab);
-        } catch (FileNotFoundException e) {
-            System.err.println("the admin keytab file not found. " + e.getMessage());
-        }
-        byte[] buffer = new byte[4 * 1024];
-        int read;
-        try {
+        try (FileOutputStream fos = new FileOutputStream(adminKeytab)) {
+            byte[] buffer = new byte[4 * 1024];
+            int read;
             while ((read = content.read(buffer)) > 0) {
                 fos.write(buffer, 0, read);
             }
             fos.close();
             content.close();
+        } catch (FileNotFoundException e) {
+            System.err.println("the admin keytab file not found. " + e.getMessage());
         } catch (IOException e) {
             System.err.println("Errors occurred when getting the admin.keytab. " + e.getMessage());
         }


Mime
View raw message