>From 99a1cc862f149fb94f7c6f7a1f7989d70ed38b4e Mon Sep 17 00:00:00 2001
From: Abhishek Koneru <akoneru@redhat.com>
Date: Thu, 21 Jun 2012 11:12:11 -0400
Subject: [PATCH] Fixes for review comments given for Patch 14

---
 .../src/com/netscape/cmstools/PKCS12Export.java    |   12 +++-
 .../src/com/netscape/pkisilent/ConfigureCA.java    |   72 ++++++++------------
 .../src/com/netscape/pkisilent/ConfigureDRM.java   |   67 +++++++++---------
 .../src/com/netscape/pkisilent/ConfigureOCSP.java  |   57 ++++++++--------
 .../src/com/netscape/pkisilent/ConfigureTKS.java   |   57 ++++++++--------
 .../com/netscape/pkisilent/common/Con2Agent.java   |    5 +-
 .../com/netscape/pkisilent/common/ServerInfo.java  |    2 -
 .../com/netscape/pkisilent/common/TestClient.java  |   16 +++--
 .../com/netscape/pkisilent/common/UserEnroll.java  |    3 -
 .../com/netscape/pkisilent/http/HTTPClient.java    |    4 +-
 10 files changed, 140 insertions(+), 155 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/PKCS12Export.java b/base/java-tools/src/com/netscape/cmstools/PKCS12Export.java
index 8e985bbe0f46b22ca02336dfc7fba771dacf1d20..df19bd9fea3aa76f6a3dcb2f34952303bb9727f8 100644
--- a/base/java-tools/src/com/netscape/cmstools/PKCS12Export.java
+++ b/base/java-tools/src/com/netscape/cmstools/PKCS12Export.java
@@ -233,16 +233,24 @@ public class PKCS12Export {
         }
 
         String pk12pwd = null;
+        BufferedReader in = null;
         try {
-            BufferedReader in = new BufferedReader(new FileReader(pk12pwdfile));
+            in = new BufferedReader(new FileReader(pk12pwdfile));
             pk12pwd = in.readLine();
             if (pk12pwd == null) {
                 pk12pwd = "";
             }
-            in.close();
         } catch (Exception e) {
             debug("Failed to read the keydb password from the file. Exception: " + e.toString());
             System.exit(1);
+        } finally {
+            if (in != null) {
+                try {
+                    in.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
         }
 
         CryptoManager cm = null;
diff --git a/base/silent/src/com/netscape/pkisilent/ConfigureCA.java b/base/silent/src/com/netscape/pkisilent/ConfigureCA.java
index 0af43d03dc86a3e754b116407adb2ed8868f9b42..7f68347714ef464bb8c75e9b6672d9d53bdb4c50 100644
--- a/base/silent/src/com/netscape/pkisilent/ConfigureCA.java
+++ b/base/silent/src/com/netscape/pkisilent/ConfigureCA.java
@@ -882,9 +882,6 @@ public class ConfigureCA {
     }
 
     public boolean BackupPanel() {
-        FileOutputStream fos = null;
-        BufferedReader br = null;
-        FileInputStream fis = null;
         try {
             HTTPResponse hr = null;
 
@@ -903,27 +900,43 @@ public class ConfigureCA {
                 hr = hc.sslConnect(cs_hostname, cs_port, pkcs12_uri, query_string);
 
                 // dump hr.getResponseData() to file
-
-                fos = new FileOutputStream(backup_fname);
-
-                fos.write(hr.getResponseData());
-
+                FileOutputStream fos = null;
+                try {
+                    fos = new FileOutputStream(backup_fname);
+                    fos.write(hr.getResponseData());
+                } finally {
+                    if (fos != null) {
+                        fos.close();
+                    }
+                }
                 // set file to permissions 600
                 String rtParams[] = { "chmod", "600", backup_fname };
                 Process proc = Runtime.getRuntime().exec(rtParams);
-
-                br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
-                String line = null;
-                while ((line = br.readLine()) != null)
-                    System.out.println("Error: " + line);
+                BufferedReader br = null;
+                try {
+                    br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
+                    String line = null;
+                    while ((line = br.readLine()) != null)
+                        System.out.println("Error: " + line);
+                } finally {
+                    if (br != null) {
+                        br.close();
+                    }
+                }
                 proc.waitFor();
 
                 // verify p12 file
                 // Decode the P12 file
-                fis = new FileInputStream(backup_fname);
-                PFX.Template pfxt = new PFX.Template();
-                PFX pfx = (PFX) pfxt.decode(new BufferedInputStream(fis, 2048));
-
+                FileInputStream fis = null;
+                PFX pfx = null;
+                try {
+                    fis = new FileInputStream(backup_fname);
+                    PFX.Template pfxt = new PFX.Template();
+                    pfx = (PFX) pfxt.decode(new BufferedInputStream(fis, 2048));
+                } finally {
+                    if (fis != null)
+                        fis.close();
+                }
                 System.out.println("Decoded PFX");
 
                 // now peruse it for interesting info
@@ -933,9 +946,6 @@ public class ConfigureCA {
 
                 System.out.println(
                         "AuthSafes has " + asSeq.size() + " SafeContents");
-
-                fis.close();
-
             }
 
             return true;
@@ -943,28 +953,6 @@ public class ConfigureCA {
             System.out.println("Exception in BackupPanel(): " + e.toString());
             e.printStackTrace();
             return false;
-        } finally {
-            if (fis != null) {
-                try {
-                    fis.close();
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-            }
-            if (br != null) {
-                try {
-                    br.close();
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-            }
-            if (fos != null) {
-                try {
-                    fos.close();
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-            }
         }
     }
 
diff --git a/base/silent/src/com/netscape/pkisilent/ConfigureDRM.java b/base/silent/src/com/netscape/pkisilent/ConfigureDRM.java
index 7d5988baa76743478cf7e2b78a88497165431724..d4bbe599cba950c0af1eecee1dc0bb2498ea56af 100644
--- a/base/silent/src/com/netscape/pkisilent/ConfigureDRM.java
+++ b/base/silent/src/com/netscape/pkisilent/ConfigureDRM.java
@@ -662,29 +662,46 @@ public class ConfigureDRM {
         hr = hc.sslConnect(cs_hostname, cs_port, pkcs12_uri, query_string);
 
         // dump hr.getResponseData() to file
-        FileOutputStream fos = null;
-        BufferedReader br = null;
-        FileInputStream fis = null;
-        try {
-            fos = new FileOutputStream(backup_fname);
-            fos.write(hr.getResponseData());
 
+        try {
+            FileOutputStream fos = null;
+            try {
+                fos = new FileOutputStream(backup_fname);
+                fos.write(hr.getResponseData());
+            } finally {
+                if (fos != null) {
+                    fos.close();
+                }
+            }
             // set file to permissions 600
             String rtParams[] = { "chmod", "600", backup_fname };
             Process proc = Runtime.getRuntime().exec(rtParams);
-
-            br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
-            String line = null;
-            while ((line = br.readLine()) != null)
-                System.out.println("Error: " + line);
+            BufferedReader br = null;
+            try {
+                br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
+                String line = null;
+                while ((line = br.readLine()) != null)
+                    System.out.println("Error: " + line);
+            } finally {
+                if (br != null) {
+                    br.close();
+                }
+            }
             proc.waitFor();
 
             // verify p12 file
 
             // Decode the P12 file
-            fis = new FileInputStream(backup_fname);
-            PFX.Template pfxt = new PFX.Template();
-            PFX pfx = (PFX) pfxt.decode(new BufferedInputStream(fis, 2048));
+            FileInputStream fis = null;
+            PFX pfx = null;
+            try {
+                fis = new FileInputStream(backup_fname);
+                PFX.Template pfxt = new PFX.Template();
+                pfx = (PFX) pfxt.decode(new BufferedInputStream(fis, 2048));
+            } finally {
+                if (fis != null)
+                    fis.close();
+            }
             System.out.println("Decoded PFX");
 
             // now peruse it for interesting info
@@ -707,28 +724,6 @@ public class ConfigureDRM {
             System.out.println("ERROR: Exception=" + e.getMessage());
             return false;
         } finally {
-
-            if (fos != null) {
-                try {
-                    fos.close();
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-            }
-            if (br != null) {
-                try {
-                    br.close();
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-            }
-            if (fis != null) {
-                try {
-                    fis.close();
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-            }
             if (bais != null) {
                 try {
                     bais.close();
diff --git a/base/silent/src/com/netscape/pkisilent/ConfigureOCSP.java b/base/silent/src/com/netscape/pkisilent/ConfigureOCSP.java
index 8eb0e611666ba18e2344cda335508efbe72738fc..51ba650166af83aa1653714fe85e98075121e8ff 100644
--- a/base/silent/src/com/netscape/pkisilent/ConfigureOCSP.java
+++ b/base/silent/src/com/netscape/pkisilent/ConfigureOCSP.java
@@ -23,7 +23,6 @@ import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
-import java.io.IOException;
 import java.io.InputStreamReader;
 import java.net.URLEncoder;
 import java.util.ArrayList;
@@ -554,30 +553,44 @@ public class ConfigureOCSP {
         hr = hc.sslConnect(cs_hostname, cs_port, pkcs12_uri, query_string);
 
         // dump hr.getResponseData() to file
-        FileOutputStream fos = null;
-        BufferedReader br = null;
-        FileInputStream fis = null;
         try {
-            fos = new FileOutputStream(backup_fname);
-            fos.write(hr.getResponseData());
-            fos.close();
+            FileOutputStream fos = null;
+            try {
+                fos = new FileOutputStream(backup_fname);
+                fos.write(hr.getResponseData());
+            } finally {
+                if (fos != null)
+                    fos.close();
+            }
 
             // set file to permissions 600
             String rtParams[] = { "chmod", "600", backup_fname };
             Process proc = Runtime.getRuntime().exec(rtParams);
-
-            br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
-            String line = null;
-            while ((line = br.readLine()) != null)
-                System.out.println("Error: " + line);
+            BufferedReader br = null;
+            try {
+                br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
+                String line = null;
+                while ((line = br.readLine()) != null)
+                    System.out.println("Error: " + line);
+            } finally {
+                if (br != null)
+                    br.close();
+            }
             proc.waitFor();
 
             // verify p12 file
 
             // Decode the P12 file
-            fis = new FileInputStream(backup_fname);
-            PFX.Template pfxt = new PFX.Template();
-            PFX pfx = (PFX) pfxt.decode(new BufferedInputStream(fis, 2048));
+            FileInputStream fis = null;
+            PFX pfx = null;
+            try {
+                fis = new FileInputStream(backup_fname);
+                PFX.Template pfxt = new PFX.Template();
+                pfx = (PFX) pfxt.decode(new BufferedInputStream(fis, 2048));
+            } finally {
+                if (fis != null)
+                    fis.close();
+            }
             System.out.println("Decoded PFX");
 
             // now peruse it for interesting info
@@ -591,20 +604,6 @@ public class ConfigureOCSP {
         } catch (Exception e) {
             System.out.println("ERROR: Exception=" + e.getMessage());
             return false;
-        } finally {
-            try {
-                if (fos != null) {
-                    fos.close();
-                }
-                if (br != null) {
-                    br.close();
-                }
-                if (fis != null) {
-                    fis.close();
-                }
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
         }
 
         return true;
diff --git a/base/silent/src/com/netscape/pkisilent/ConfigureTKS.java b/base/silent/src/com/netscape/pkisilent/ConfigureTKS.java
index e4b8ab6c4a524c0e16ced027c054d32eec47a8cc..dc8ce665d2b45df9dd62e49b41113402a85c326c 100644
--- a/base/silent/src/com/netscape/pkisilent/ConfigureTKS.java
+++ b/base/silent/src/com/netscape/pkisilent/ConfigureTKS.java
@@ -23,7 +23,6 @@ import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
-import java.io.IOException;
 import java.io.InputStreamReader;
 import java.net.URLEncoder;
 import java.util.ArrayList;
@@ -525,30 +524,44 @@ public class ConfigureTKS {
         HTTPResponse hr = hc.sslConnect(cs_hostname, cs_port, pkcs12_uri, query_string);
 
         // dump hr.getResponseData() to file
-        FileOutputStream fos = null;
-        BufferedReader br = null;
-        FileInputStream fis = null;
         try {
-            fos = new FileOutputStream(backup_fname);
-            fos.write(hr.getResponseData());
-            fos.close();
+            FileOutputStream fos = null;
+            try {
+                fos = new FileOutputStream(backup_fname);
+                fos.write(hr.getResponseData());
+            } finally {
+                if (fos != null)
+                    fos.close();
+            }
 
             // set file to permissions 600
             String rtParams[] = { "chmod", "600", backup_fname };
             Process proc = Runtime.getRuntime().exec(rtParams);
-
-            br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
-            String line = null;
-            while ((line = br.readLine()) != null)
-                System.out.println("Error: " + line);
+            BufferedReader br = null;
+            try {
+                br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
+                String line = null;
+                while ((line = br.readLine()) != null)
+                    System.out.println("Error: " + line);
+            } finally {
+                if (br != null)
+                    br.close();
+            }
             proc.waitFor();
 
             // verify p12 file
 
             // Decode the P12 file
-            fis = new FileInputStream(backup_fname);
-            PFX.Template pfxt = new PFX.Template();
-            PFX pfx = (PFX) pfxt.decode(new BufferedInputStream(fis, 2048));
+            FileInputStream fis = null;
+            PFX pfx = null;
+            try {
+                fis = new FileInputStream(backup_fname);
+                PFX.Template pfxt = new PFX.Template();
+                pfx = (PFX) pfxt.decode(new BufferedInputStream(fis, 2048));
+            } finally {
+                if (fis != null)
+                    fis.close();
+            }
             System.out.println("Decoded PFX");
 
             // now peruse it for interesting info
@@ -561,20 +574,6 @@ public class ConfigureTKS {
         } catch (Exception e) {
             System.out.println("ERROR: Exception=" + e.getMessage());
             return false;
-        } finally {
-            try {
-                if (fos != null) {
-                    fos.close();
-                }
-                if (br != null) {
-                    br.close();
-                }
-                if (fis != null) {
-                    fis.close();
-                }
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
         }
 
         return true;
diff --git a/base/silent/src/com/netscape/pkisilent/common/Con2Agent.java b/base/silent/src/com/netscape/pkisilent/common/Con2Agent.java
index 4ea389647fab629b48b2d55e8c03507d5e2edb17..16fc7c77ab105ed6096fc886cace3f8661f461b5 100644
--- a/base/silent/src/com/netscape/pkisilent/common/Con2Agent.java
+++ b/base/silent/src/com/netscape/pkisilent/common/Con2Agent.java
@@ -191,6 +191,8 @@ public class Con2Agent implements SSLClientCertificateSelectionCallback,
             ps.println("");
             ps.println(query);
             ps.println("\r");
+            ps.flush();
+            os.flush();
             stdin1 = new BufferedReader(
                     new InputStreamReader(socket.getInputStream()));
             String line;
@@ -200,9 +202,6 @@ public class Con2Agent implements SSLClientCertificateSelectionCallback,
                 System.out.println(line);
             }
             ps.println("Connection: close");
-            ps.flush();
-            os.flush();
-
         } catch (Exception e) {
             System.out.println("some exception: in Send routine" + e);
             return false;
diff --git a/base/silent/src/com/netscape/pkisilent/common/ServerInfo.java b/base/silent/src/com/netscape/pkisilent/common/ServerInfo.java
index 1be4d0a301f5880095ee60268d4e6838050400c8..ff99a47f43e70103d53c43f376c958778fd49bc1 100644
--- a/base/silent/src/com/netscape/pkisilent/common/ServerInfo.java
+++ b/base/silent/src/com/netscape/pkisilent/common/ServerInfo.java
@@ -295,8 +295,6 @@ public class ServerInfo {
                 }
 
             }
-
-            fis.close();
         } catch (Exception e) {
             System.out.println("exception " + e.getMessage());
         } finally {
diff --git a/base/silent/src/com/netscape/pkisilent/common/TestClient.java b/base/silent/src/com/netscape/pkisilent/common/TestClient.java
index f3aaabf4199450fb3ff177ba4af432bf28b7a522..6fb5bd1207bec4cf8c71db6bf69299a2c3a44d64 100644
--- a/base/silent/src/com/netscape/pkisilent/common/TestClient.java
+++ b/base/silent/src/com/netscape/pkisilent/common/TestClient.java
@@ -358,13 +358,15 @@ public class TestClient implements SSLCertificateApprovalCallback {
      */
 
     public void getProperties(String fileName) throws Exception {
-        FileInputStream fis = new FileInputStream(fileName);
-
-        props = new Properties();
-        props.load(fis);
-        if (fis != null)
-            fis.close();
-
+        FileInputStream fis = null;
+        try {
+            fis = new FileInputStream(fileName);
+            props = new Properties();
+            props.load(fis);
+        } finally {
+            if (fis != null)
+                fis.close();
+        }
     }
 
     public String ReadEnv(String str) {
diff --git a/base/silent/src/com/netscape/pkisilent/common/UserEnroll.java b/base/silent/src/com/netscape/pkisilent/common/UserEnroll.java
index 6b400ef0f562c18b312483beef6c4778627337dc..2f6a039243b862b5c3918a1ba02437e735ea143c 100644
--- a/base/silent/src/com/netscape/pkisilent/common/UserEnroll.java
+++ b/base/silent/src/com/netscape/pkisilent/common/UserEnroll.java
@@ -302,7 +302,6 @@ public class UserEnroll extends TestClient {
                 try {
                     socket.close();
                 } catch (IOException e) {
-                    // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
             }
@@ -310,7 +309,6 @@ public class UserEnroll extends TestClient {
                 try {
                     os.close();
                 } catch (IOException e) {
-                    // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
             }
@@ -318,7 +316,6 @@ public class UserEnroll extends TestClient {
                 try {
                     rawos.close();
                 } catch (IOException e) {
-                    // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
             }
diff --git a/base/silent/src/com/netscape/pkisilent/http/HTTPClient.java b/base/silent/src/com/netscape/pkisilent/http/HTTPClient.java
index 8ba9e522bb53edb1c66f01ca33c465e8092bbfbf..71e5e8810b7575e6e2a7a1a6093d1cbb322fe675 100644
--- a/base/silent/src/com/netscape/pkisilent/http/HTTPClient.java
+++ b/base/silent/src/com/netscape/pkisilent/http/HTTPClient.java
@@ -239,6 +239,8 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
             ps.println("Content-length: " + query.length());
             ps.println("");
             ps.print(query);
+            ps.flush();
+            os.flush();
             hr = readResponse(socket.getInputStream());
             hr.parseContent();
         }
@@ -1010,7 +1012,6 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
-                dos = null;
             }
             if (socket != null) {
                 try {
@@ -1086,7 +1087,6 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
-                dos = null;
             }
             if (socket != null) {
                 try {
-- 
1.7.10.2

