summaryrefslogtreecommitdiffstats
path: root/base/silent/src
diff options
context:
space:
mode:
Diffstat (limited to 'base/silent/src')
-rw-r--r--base/silent/src/com/netscape/pkisilent/ConfigureCA.java89
-rw-r--r--base/silent/src/com/netscape/pkisilent/ConfigureDRM.java54
-rw-r--r--base/silent/src/com/netscape/pkisilent/ConfigureOCSP.java40
-rw-r--r--base/silent/src/com/netscape/pkisilent/ConfigureTKS.java41
-rw-r--r--base/silent/src/com/netscape/pkisilent/common/Con2Agent.java122
-rw-r--r--base/silent/src/com/netscape/pkisilent/common/DirEnroll.java60
-rw-r--r--base/silent/src/com/netscape/pkisilent/common/ServerInfo.java16
-rw-r--r--base/silent/src/com/netscape/pkisilent/common/TestClient.java23
-rw-r--r--base/silent/src/com/netscape/pkisilent/common/UserEnroll.java59
-rw-r--r--base/silent/src/com/netscape/pkisilent/common/Utilities.java105
-rw-r--r--base/silent/src/com/netscape/pkisilent/http/HTTPClient.java152
11 files changed, 534 insertions, 227 deletions
diff --git a/base/silent/src/com/netscape/pkisilent/ConfigureCA.java b/base/silent/src/com/netscape/pkisilent/ConfigureCA.java
index f8c90df38..7f6834771 100644
--- a/base/silent/src/com/netscape/pkisilent/ConfigureCA.java
+++ b/base/silent/src/com/netscape/pkisilent/ConfigureCA.java
@@ -23,6 +23,7 @@ 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.io.PrintStream;
import java.net.URLEncoder;
@@ -662,10 +663,12 @@ public class ConfigureCA {
return false;
}
} else {
+ FileInputStream fis = null;
+ BufferedReader in = null;
try {
ca_cert_cert = "";
- FileInputStream fis = new FileInputStream(ext_ca_cert_file);
- BufferedReader in = new BufferedReader(new InputStreamReader(fis));
+ fis = new FileInputStream(ext_ca_cert_file);
+ in = new BufferedReader(new InputStreamReader(fis));
String line;
while ((line = in.readLine()) != null) {
ca_cert_cert += line;
@@ -678,13 +681,27 @@ public class ConfigureCA {
while ((line = in.readLine()) != null) {
signing_cc += line;
}
- in.close();
return true;
} catch (Exception e) {
System.out.println(
"CertSubjectPanel: Unable to read in external approved CA cert or certificate chain.");
System.out.println(e.toString());
return false;
+ } finally {
+ if (fis != null) {
+ try {
+ fis.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (in != null) {
+ try {
+ in.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
}
}
@@ -883,44 +900,52 @@ public class ConfigureCA {
hr = hc.sslConnect(cs_hostname, cs_port, pkcs12_uri, query_string);
// dump hr.getResponseData() to file
-
+ FileOutputStream fos = null;
try {
- FileOutputStream fos = new FileOutputStream(backup_fname);
-
+ fos = new FileOutputStream(backup_fname);
fos.write(hr.getResponseData());
- fos.close();
-
- // set file to permissions 600
- String rtParams[] = { "chmod", "600", backup_fname };
- Process proc = Runtime.getRuntime().exec(rtParams);
-
- BufferedReader br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
+ } finally {
+ if (fos != null) {
+ fos.close();
+ }
+ }
+ // set file to permissions 600
+ String rtParams[] = { "chmod", "600", backup_fname };
+ Process proc = Runtime.getRuntime().exec(rtParams);
+ BufferedReader br = null;
+ try {
+ br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String line = null;
while ((line = br.readLine()) != null)
System.out.println("Error: " + line);
- proc.waitFor();
+ } finally {
+ if (br != null) {
+ br.close();
+ }
+ }
+ proc.waitFor();
- // verify p12 file
- // Decode the P12 file
- FileInputStream fis = new FileInputStream(backup_fname);
+ // verify p12 file
+ // Decode the P12 file
+ FileInputStream fis = null;
+ PFX pfx = null;
+ try {
+ fis = new FileInputStream(backup_fname);
PFX.Template pfxt = new PFX.Template();
- PFX pfx = (PFX) pfxt.decode(new BufferedInputStream(fis, 2048));
-
- System.out.println("Decoded PFX");
-
- // now peruse it for interesting info
- System.out.println("Version: " + pfx.getVersion());
- AuthenticatedSafes authSafes = pfx.getAuthSafes();
- SEQUENCE asSeq = authSafes.getSequence();
+ pfx = (PFX) pfxt.decode(new BufferedInputStream(fis, 2048));
+ } finally {
+ if (fis != null)
+ fis.close();
+ }
+ System.out.println("Decoded PFX");
- System.out.println(
- "AuthSafes has " + asSeq.size() + " SafeContents");
+ // now peruse it for interesting info
+ System.out.println("Version: " + pfx.getVersion());
+ AuthenticatedSafes authSafes = pfx.getAuthSafes();
+ SEQUENCE asSeq = authSafes.getSequence();
- fis.close();
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
+ System.out.println(
+ "AuthSafes has " + asSeq.size() + " SafeContents");
}
return true;
diff --git a/base/silent/src/com/netscape/pkisilent/ConfigureDRM.java b/base/silent/src/com/netscape/pkisilent/ConfigureDRM.java
index ca3581869..d4bbe599c 100644
--- a/base/silent/src/com/netscape/pkisilent/ConfigureDRM.java
+++ b/base/silent/src/com/netscape/pkisilent/ConfigureDRM.java
@@ -23,6 +23,7 @@ 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;
@@ -661,27 +662,46 @@ public class ConfigureDRM {
hr = hc.sslConnect(cs_hostname, cs_port, pkcs12_uri, query_string);
// dump hr.getResponseData() to file
- try {
- FileOutputStream fos = new FileOutputStream(backup_fname);
- fos.write(hr.getResponseData());
- fos.close();
+ 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);
-
- BufferedReader 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
- FileInputStream 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
@@ -691,8 +711,6 @@ public class ConfigureDRM {
System.out.println("AuthSafes has " +
asSeq.size() + " SafeContents");
- fis.close();
-
if (clone) {
query_string = "p=12" + "&op=next" + "&xml=true";
hr = hc.sslConnect(cs_hostname, cs_port, wizard_uri, query_string);
@@ -705,6 +723,14 @@ public class ConfigureDRM {
} catch (Exception e) {
System.out.println("ERROR: Exception=" + e.getMessage());
return false;
+ } finally {
+ if (bais != null) {
+ try {
+ bais.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
return true;
diff --git a/base/silent/src/com/netscape/pkisilent/ConfigureOCSP.java b/base/silent/src/com/netscape/pkisilent/ConfigureOCSP.java
index 0ca0a082a..51ba65016 100644
--- a/base/silent/src/com/netscape/pkisilent/ConfigureOCSP.java
+++ b/base/silent/src/com/netscape/pkisilent/ConfigureOCSP.java
@@ -553,28 +553,44 @@ public class ConfigureOCSP {
hr = hc.sslConnect(cs_hostname, cs_port, pkcs12_uri, query_string);
// dump hr.getResponseData() to file
-
try {
- FileOutputStream 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);
-
- BufferedReader 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
- FileInputStream 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
diff --git a/base/silent/src/com/netscape/pkisilent/ConfigureTKS.java b/base/silent/src/com/netscape/pkisilent/ConfigureTKS.java
index aa8dccee8..dc8ce665d 100644
--- a/base/silent/src/com/netscape/pkisilent/ConfigureTKS.java
+++ b/base/silent/src/com/netscape/pkisilent/ConfigureTKS.java
@@ -524,28 +524,44 @@ public class ConfigureTKS {
HTTPResponse hr = hc.sslConnect(cs_hostname, cs_port, pkcs12_uri, query_string);
// dump hr.getResponseData() to file
-
try {
- FileOutputStream 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);
-
- BufferedReader 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
- FileInputStream 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
@@ -555,7 +571,6 @@ public class ConfigureTKS {
System.out.println("AuthSafes has " +
asSeq.size() + " SafeContents");
- fis.close();
} catch (Exception e) {
System.out.println("ERROR: Exception=" + e.getMessage());
return false;
diff --git a/base/silent/src/com/netscape/pkisilent/common/Con2Agent.java b/base/silent/src/com/netscape/pkisilent/common/Con2Agent.java
index 6d02fcd33..16fc7c77a 100644
--- a/base/silent/src/com/netscape/pkisilent/common/Con2Agent.java
+++ b/base/silent/src/com/netscape/pkisilent/common/Con2Agent.java
@@ -20,6 +20,7 @@ package com.netscape.pkisilent.common;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
+import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
@@ -157,13 +158,18 @@ public class Con2Agent implements SSLClientCertificateSelectionCallback,
// Submit requests
public boolean Send() {
+ SSLSocket socket = null;
+ OutputStream rawos = null;
+ BufferedOutputStream os = null;
+ PrintStream ps = null;
+ BufferedReader stdin1 = null;
try {
if (!loginCertDB()) {
return false;
}
- SSLSocket socket = new SSLSocket(host, port, null, 0, this, null);
+ socket = new SSLSocket(host, port, null, 0, this, null);
System.out.println("Con2Agent.java: host = " + host);
System.out.println("Con2Agent.java: port = " + port);
@@ -172,9 +178,9 @@ public class Con2Agent implements SSLClientCertificateSelectionCallback,
socket.setClientCertNickname(certnickname);
System.out.println("Connected to the socket");
- OutputStream rawos = socket.getOutputStream();
- BufferedOutputStream os = new BufferedOutputStream(rawos);
- PrintStream ps = new PrintStream(os);
+ rawos = socket.getOutputStream();
+ os = new BufferedOutputStream(rawos);
+ ps = new PrintStream(os);
System.out.println(ACTIONURL);
System.out.println("Query :" + query);
@@ -187,7 +193,7 @@ public class Con2Agent implements SSLClientCertificateSelectionCallback,
ps.println("\r");
ps.flush();
os.flush();
- BufferedReader stdin1 = new BufferedReader(
+ stdin1 = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String line;
@@ -195,32 +201,50 @@ public class Con2Agent implements SSLClientCertificateSelectionCallback,
stdout.append(line + "\n");
System.out.println(line);
}
-
+ ps.println("Connection: close");
+ } catch (Exception e) {
+ System.out.println("some exception: in Send routine" + e);
+ return false;
+ } finally {
// Send Connection: close to let the server close the connection.
// Else the socket on the server side continues to remain in TIME_WAIT state
-
- ps.println("Connection: close");
- ps.flush();
- os.flush();
- os.close();
- rawos.close();
- ps.close();
- stdin1.close();
- socket.close();
-
+ if (ps != null)
+ ps.close();
+ if (stdin1 != null) {
+ try {
+ stdin1.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (os != null) {
+ try {
+ os.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (rawos != null) {
+ try {
+ rawos.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
if (socket.isClosed()) {
System.out.println("Con2Agent.java : Socket is Closed");
} else {
System.out.println("Con2Agent.java : Socket not Closed");
}
-
- } catch (Exception e) {
- System.out.println("some exception: in Send routine" + e);
- return false;
}
-
return true;
-
}
private boolean loginCertDB() {
@@ -271,6 +295,11 @@ public class Con2Agent implements SSLClientCertificateSelectionCallback,
}
public boolean Send_withGET() {
+ SSLSocket socket = null;
+ OutputStream rawos = null;
+ BufferedOutputStream os = null;
+ PrintStream ps = null;
+ BufferedReader stdin2 = null;
try {
@@ -278,14 +307,14 @@ public class Con2Agent implements SSLClientCertificateSelectionCallback,
return false;
}
- SSLSocket socket = new SSLSocket(host, port, null, 0, this, null);
+ socket = new SSLSocket(host, port, null, 0, this, null);
socket.setClientCertNickname(certnickname);
System.out.println("Connected to the socket");
- OutputStream rawos = socket.getOutputStream();
- BufferedOutputStream os = new BufferedOutputStream(rawos);
- PrintStream ps = new PrintStream(os);
+ rawos = socket.getOutputStream();
+ os = new BufferedOutputStream(rawos);
+ ps = new PrintStream(os);
System.out.println("Query in con2agent :" + query);
System.out.println("ACTIONURL in con2agent : " + ACTIONURL);
@@ -295,24 +324,51 @@ public class Con2Agent implements SSLClientCertificateSelectionCallback,
ps.println("\r");
ps.flush();
os.flush();
- BufferedReader stdin2 = new BufferedReader(
+ stdin2 = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String line;
while ((line = stdin2.readLine()) != null) {
stdout.append(line + "\n");
}
- stdin2.close();
-
- socket.close();
-
} catch (Exception e) {
System.err.println("some exception: in Send routine" + e);
return false;
- }
+ } finally {
+
+ if (ps != null)
+ ps.close();
+ if (stdin2 != null) {
+ try {
+ stdin2.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (os != null) {
+ try {
+ os.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (rawos != null) {
+ try {
+ rawos.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
return true;
-
}
} // end of class
diff --git a/base/silent/src/com/netscape/pkisilent/common/DirEnroll.java b/base/silent/src/com/netscape/pkisilent/common/DirEnroll.java
index 809a65c5b..6ab1fb190 100644
--- a/base/silent/src/com/netscape/pkisilent/common/DirEnroll.java
+++ b/base/silent/src/com/netscape/pkisilent/common/DirEnroll.java
@@ -20,6 +20,7 @@ package com.netscape.pkisilent.common;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
+import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
@@ -260,7 +261,11 @@ public class DirEnroll extends TestClient {
private boolean Send() {
boolean st = false;
-
+ SSLSocket socket = null;
+ OutputStream rawos = null;
+ BufferedOutputStream os = null;
+ PrintStream ps = null;
+ BufferedReader stdin = null;
try {
if (debug) {
System.out.println("Step 3 : Socket initialize");
@@ -273,13 +278,13 @@ public class DirEnroll extends TestClient {
GregorianCalendar begin = new GregorianCalendar();
// SSLSocket socket = new SSLSocket(host,port);
- SSLSocket socket = new SSLSocket(host, port, null, 0, this, null);
+ socket = new SSLSocket(host, port, null, 0, this, null);
socket.setUseClientMode(true);
- OutputStream rawos = socket.getOutputStream();
- BufferedOutputStream os = new BufferedOutputStream(rawos);
- PrintStream ps = new PrintStream(os);
+ rawos = socket.getOutputStream();
+ os = new BufferedOutputStream(rawos);
+ ps = new PrintStream(os);
ps.println("POST /enrollment HTTP/1.0");
ps.println("Connection: Keep-Alive");
@@ -290,7 +295,7 @@ public class DirEnroll extends TestClient {
ps.println("\r");
ps.flush();
os.flush();
- BufferedReader stdin = new BufferedReader(
+ stdin = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
if (debug) {
@@ -327,16 +332,6 @@ public class DirEnroll extends TestClient {
}
}
- stdin.close();
- socket.close();
- os.close();
- rawos.close();
- ps.close();
- os = null;
- rawos = null;
- stdin = null;
- ps = null;
- line = null;
GregorianCalendar end = new GregorianCalendar();
long diff = calculateElapsedTime(begin, end);
@@ -346,10 +341,39 @@ public class DirEnroll extends TestClient {
} catch (Exception e) {
System.err.println("some exception: in Send routine" + e);
return false;
+ } finally {
+ if (ps != null)
+ ps.close();
+ if (stdin != null) {
+ try {
+ stdin.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (os != null) {
+ try {
+ os.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (rawos != null) {
+ try {
+ rawos.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
-
return st;
-
}
private void buildquery() throws UnsupportedEncodingException {
diff --git a/base/silent/src/com/netscape/pkisilent/common/ServerInfo.java b/base/silent/src/com/netscape/pkisilent/common/ServerInfo.java
index 637f0c1ee..ff99a47f4 100644
--- a/base/silent/src/com/netscape/pkisilent/common/ServerInfo.java
+++ b/base/silent/src/com/netscape/pkisilent/common/ServerInfo.java
@@ -21,6 +21,7 @@ package com.netscape.pkisilent.common;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
+import java.io.IOException;
import java.net.InetAddress;
import java.util.StringTokenizer;
@@ -260,9 +261,9 @@ public class ServerInfo {
String ldapHostStr = "ldapHost:";
String ldapPortStr = "ldapPort:";
String adminPortStr = "port:";
-
+ FileInputStream fis = null;
try {
- FileInputStream fis = new FileInputStream(AdminConfigFile);
+ fis = new FileInputStream(AdminConfigFile);
int size = fis.available();
byte b[] = new byte[size];
@@ -294,12 +295,17 @@ public class ServerInfo {
}
}
-
- fis.close();
} catch (Exception e) {
System.out.println("exception " + e.getMessage());
+ } finally {
+ if (fis != null) {
+ try {
+ fis.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
-
}
private void readCMSConfig() {
diff --git a/base/silent/src/com/netscape/pkisilent/common/TestClient.java b/base/silent/src/com/netscape/pkisilent/common/TestClient.java
index 22275ede5..6fb5bd120 100644
--- a/base/silent/src/com/netscape/pkisilent/common/TestClient.java
+++ b/base/silent/src/com/netscape/pkisilent/common/TestClient.java
@@ -20,6 +20,7 @@ package com.netscape.pkisilent.common;
import java.io.BufferedReader;
import java.io.FileInputStream;
+import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.util.Properties;
@@ -357,15 +358,15 @@ public class TestClient implements SSLCertificateApprovalCallback {
*/
public void getProperties(String fileName) throws Exception {
+ FileInputStream fis = null;
try {
- FileInputStream fis = new FileInputStream(fileName);
-
+ fis = new FileInputStream(fileName);
props = new Properties();
props.load(fis);
- } catch (Exception e) {
- System.out.println("exception " + e.getMessage());
+ } finally {
+ if (fis != null)
+ fis.close();
}
-
}
public String ReadEnv(String str) {
@@ -488,8 +489,9 @@ public class TestClient implements SSLCertificateApprovalCallback {
**/
public String readRequest(String filename) {
+ FileInputStream f1 = null;
try {
- FileInputStream f1 = new FileInputStream(filename);
+ f1 = new FileInputStream(filename);
int size = f1.available();
byte b[] = new byte[size];
@@ -497,13 +499,20 @@ public class TestClient implements SSLCertificateApprovalCallback {
return null;
}
- f1.close();
String s = new String(b);
return s;
} catch (Exception e) {
System.out.println("exception " + e.getMessage());
return null;
+ } finally {
+ if (f1 != null) {
+ try {
+ f1.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
}
diff --git a/base/silent/src/com/netscape/pkisilent/common/UserEnroll.java b/base/silent/src/com/netscape/pkisilent/common/UserEnroll.java
index 0e31eeb57..2f6a03924 100644
--- a/base/silent/src/com/netscape/pkisilent/common/UserEnroll.java
+++ b/base/silent/src/com/netscape/pkisilent/common/UserEnroll.java
@@ -20,6 +20,7 @@ package com.netscape.pkisilent.common;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
+import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
@@ -218,7 +219,11 @@ public class UserEnroll extends TestClient {
private boolean Send() {
boolean st = false;
-
+ SSLSocket socket = null;
+ OutputStream rawos = null;
+ BufferedOutputStream os = null;
+ PrintStream ps = null;
+ BufferedReader stdin = null;
try {
if (debug) {
@@ -232,12 +237,12 @@ public class UserEnroll extends TestClient {
GregorianCalendar begin = new GregorianCalendar();
// SSLSocket socket = new SSLSocket(host,port);
- SSLSocket socket = new SSLSocket(host, port, null, 0, this, null);
+ socket = new SSLSocket(host, port, null, 0, this, null);
socket.setUseClientMode(true);
- OutputStream rawos = socket.getOutputStream();
- BufferedOutputStream os = new BufferedOutputStream(rawos);
- PrintStream ps = new PrintStream(os);
+ rawos = socket.getOutputStream();
+ os = new BufferedOutputStream(rawos);
+ ps = new PrintStream(os);
ps.println("POST /enrollment HTTP/1.0");
ps.println("Connection: Keep-Alive");
@@ -248,7 +253,7 @@ public class UserEnroll extends TestClient {
ps.println("\r");
ps.flush();
os.flush();
- BufferedReader stdin = new BufferedReader(
+ stdin = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
if (debug) {
@@ -274,16 +279,6 @@ public class UserEnroll extends TestClient {
}
}
- stdin.close();
- socket.close();
- os.close();
- rawos.close();
- ps.close();
- os = null;
- rawos = null;
- stdin = null;
- ps = null;
- line = null;
GregorianCalendar end = new GregorianCalendar();
long diff = calculateElapsedTime(begin, end);
@@ -292,6 +287,38 @@ public class UserEnroll extends TestClient {
} catch (Exception e) {
System.err.println("some exception: in Send routine" + e);
return false;
+ } finally {
+ if (ps != null) {
+ ps.close();
+ }
+ if (stdin != null) {
+ try {
+ stdin.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (os != null) {
+ try {
+ os.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (rawos != null) {
+ try {
+ rawos.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
return st;
diff --git a/base/silent/src/com/netscape/pkisilent/common/Utilities.java b/base/silent/src/com/netscape/pkisilent/common/Utilities.java
index 23fd2c54e..79712eb11 100644
--- a/base/silent/src/com/netscape/pkisilent/common/Utilities.java
+++ b/base/silent/src/com/netscape/pkisilent/common/Utilities.java
@@ -20,6 +20,7 @@ package com.netscape.pkisilent.common;
import java.io.BufferedReader;
import java.io.FileInputStream;
+import java.io.IOException;
import java.io.InputStreamReader;
import netscape.security.x509.CertificateSerialNumber;
@@ -162,10 +163,11 @@ public class Utilities {
*/
public String getcertfromfile(String filename) {
StringBuffer tempBuffer = new StringBuffer();
-
+ BufferedReader in = null;
+ FileInputStream fis = null;
try {
- FileInputStream fis = new FileInputStream(filename);
- BufferedReader in = new BufferedReader(new InputStreamReader(fis));
+ fis = new FileInputStream(filename);
+ in = new BufferedReader(new InputStreamReader(fis));
String temp;
while ((temp = in.readLine()) != null) {
@@ -180,17 +182,33 @@ public class Utilities {
return tempBuffer.toString();
} catch (Exception e) {
System.out.println("ERROR: getcertfromfile" + e.toString());
- return null;
+ e.printStackTrace();
+ } finally {
+ if (in != null) {
+ try {
+ in.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (fis != null) {
+ try {
+ fis.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
-
+ return null;
}
public String getcertfromfile_withheaders(String filename) {
StringBuffer tempBuffer = new StringBuffer();
-
+ BufferedReader in = null;
+ FileInputStream fis = null;
try {
- FileInputStream fis = new FileInputStream(filename);
- BufferedReader in = new BufferedReader(new InputStreamReader(fis));
+ fis = new FileInputStream(filename);
+ in = new BufferedReader(new InputStreamReader(fis));
String temp;
while ((temp = in.readLine()) != null) {
@@ -200,8 +218,24 @@ public class Utilities {
} catch (Exception e) {
System.out.println(
"ERROR: getcertfromfile_withheaders" + e.toString());
- return null;
+ e.printStackTrace();
+ } finally {
+ if (in != null) {
+ try {
+ in.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (fis != null) {
+ try {
+ fis.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
+ return null;
}
/*
@@ -212,10 +246,11 @@ public class Utilities {
*/
public String getcrlfromfile(String filename) {
StringBuffer tempBuffer = new StringBuffer();
-
+ BufferedReader in = null;
+ FileInputStream fis = null;
try {
- FileInputStream fis = new FileInputStream(filename);
- BufferedReader in = new BufferedReader(new InputStreamReader(fis));
+ fis = new FileInputStream(filename);
+ in = new BufferedReader(new InputStreamReader(fis));
String temp;
while ((temp = in.readLine()) != null) {
@@ -225,9 +260,26 @@ public class Utilities {
return tempBuffer.toString();
} catch (Exception e) {
System.out.println("ERROR: getcrlfromfile" + e.toString());
- return null;
+ e.printStackTrace();
+ } finally {
+ if (in != null) {
+ try {
+ in.close();
+ } catch (IOException e) {
+ System.out.println("ERROR: Unable to close the input reader");
+ e.printStackTrace();
+ }
+ }
+ if (fis != null) {
+ try {
+ fis.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
+ return null;
}
/*
@@ -238,10 +290,11 @@ public class Utilities {
*/
public String getcafromfile(String filename) {
StringBuffer tempBuffer = new StringBuffer();
-
+ BufferedReader in = null;
+ FileInputStream fis = null;
try {
- FileInputStream fis = new FileInputStream(filename);
- BufferedReader in = new BufferedReader(new InputStreamReader(fis));
+ fis = new FileInputStream(filename);
+ in = new BufferedReader(new InputStreamReader(fis));
String temp;
while ((temp = in.readLine()) != null) {
@@ -251,9 +304,25 @@ public class Utilities {
return tempBuffer.toString();
} catch (Exception e) {
System.out.println("ERROR: getcafromfile" + e.toString());
- return null;
+ e.printStackTrace();
+ } finally {
+ if (in != null) {
+ try {
+ in.close();
+ } catch (IOException e) {
+ System.out.println("ERROR: Unable to close the input reader");
+ e.printStackTrace();
+ }
+ }
+ if (fis != null) {
+ try {
+ fis.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
-
+ return null;
}
/*
diff --git a/base/silent/src/com/netscape/pkisilent/http/HTTPClient.java b/base/silent/src/com/netscape/pkisilent/http/HTTPClient.java
index 01ee0f251..71e5e8810 100644
--- a/base/silent/src/com/netscape/pkisilent/http/HTTPClient.java
+++ b/base/silent/src/com/netscape/pkisilent/http/HTTPClient.java
@@ -192,7 +192,11 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
boolean st = true;
HTTPResponse hr = null;
-
+ PrintStream ps = null;
+ SSLSocket socket = null;
+ Socket js = null;
+ OutputStream rawos = null;
+ BufferedOutputStream os = null;
try {
System.out.println("#############################################");
@@ -210,8 +214,8 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
// Client Cert for Auth is set here
certSelectionCallback.setClientCert(client_cert);
- Socket js = new Socket(InetAddress.getByName(hostname), port);
- SSLSocket socket = new SSLSocket(js, hostname, approvalCallback,
+ js = new Socket(InetAddress.getByName(hostname), port);
+ socket = new SSLSocket(js, hostname, approvalCallback,
certSelectionCallback);
disableSSL2(socket);
setCipherPref(socket);
@@ -225,9 +229,9 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
"/" + url +
"?" + query);
- OutputStream rawos = socket.getOutputStream();
- BufferedOutputStream os = new BufferedOutputStream(rawos);
- PrintStream ps = new PrintStream(os);
+ rawos = socket.getOutputStream();
+ os = new BufferedOutputStream(rawos);
+ ps = new PrintStream(os);
ps.println("POST " + url + " HTTP/1.0");
ps.println("Connection: Keep-Alive");
@@ -237,32 +241,44 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
ps.print(query);
ps.flush();
os.flush();
-
- try {
- hr = readResponse(socket.getInputStream());
- hr.parseContent();
-
- } catch (Exception e) {
- System.out.println("Exception");
- e.printStackTrace();
- st = false;
- }
-
- socket.close();
- os.close();
- rawos.close();
- ps.close();
-
- os = null;
- rawos = null;
- ps = null;
-
+ hr = readResponse(socket.getInputStream());
+ hr.parseContent();
}
catch (Exception e) {
System.err.println("Exception: Unable to Send Request:" + e);
e.printStackTrace();
st = false;
+ } finally {
+ if (ps != null) {
+ ps.close();
+ ps = null;
+ }
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (os != null)
+ try {
+ os.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ if (rawos != null)
+ try {
+ rawos.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ if (js != null)
+ try {
+ js.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
}
if (!st)
@@ -387,7 +403,6 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
OutputStream rawos = null;
BufferedOutputStream os = null;
PrintStream ps = null;
-
try {
System.out.println("#############################################");
@@ -948,7 +963,9 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
boolean st = true;
HTTPResponse hr = null;
-
+ DataOutputStream dos = null;
+ SSLSocket socket = null;
+ Socket js = null;
try {
System.out.println("#############################################");
@@ -963,8 +980,8 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
SSLClientCertificateSelectionCallback certSelectionCallback =
new TestClientCertificateSelectionCallback();
- Socket js = new Socket(InetAddress.getByName(hostname), port);
- SSLSocket socket = new SSLSocket(js, hostname, approvalCallback,
+ js = new Socket(InetAddress.getByName(hostname), port);
+ socket = new SSLSocket(js, hostname, approvalCallback,
certSelectionCallback);
setCipherPref(socket);
disableSSL2(socket);
@@ -972,26 +989,15 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
System.out.println("Connected.");
socket.setUseClientMode(true);
- DataOutputStream dos =
- new DataOutputStream(socket.getOutputStream());
+ dos = new DataOutputStream(socket.getOutputStream());
dos.writeBytes("POST /ocsp HTTP/1.0\r\n");
dos.writeBytes("Content-length: " + data.length + "\r\n");
dos.writeBytes("\r\n");
dos.write(data);
dos.writeBytes("\r\n");
dos.flush();
-
- try {
- hr = readResponse(socket.getInputStream());
- hr.parseContent();
- } catch (Exception e) {
- System.out.println("Exception");
- e.printStackTrace();
- st = false;
- }
-
- socket.close();
- dos.close();
+ hr = readResponse(socket.getInputStream());
+ hr.parseContent();
}
@@ -999,6 +1005,28 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
System.err.println("Exception: Unable to Send Request:" + e);
e.printStackTrace();
st = false;
+ } finally {
+ if (dos != null) {
+ try {
+ dos.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (js != null) {
+ try {
+ js.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
if (!st)
@@ -1015,7 +1043,8 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
boolean st = true;
HTTPResponse hr = null;
-
+ DataOutputStream dos = null;
+ Socket socket = null;
try {
System.out.println("#############################################");
@@ -1025,7 +1054,7 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
Integer x = new Integer(portnumber);
int port = x.intValue();
- Socket socket = new Socket(hostname, port);
+ socket = new Socket(hostname, port);
System.out.println("Posting Query = " +
"http://" + hostname +
@@ -1034,8 +1063,7 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
System.out.println("Connected.");
- DataOutputStream dos =
- new DataOutputStream(socket.getOutputStream());
+ dos = new DataOutputStream(socket.getOutputStream());
dos.writeBytes("POST " + url + " HTTP/1.0\r\n");
dos.writeBytes("Content-length: " + data.length + "\r\n");
dos.writeBytes("\r\n");
@@ -1043,17 +1071,8 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
dos.writeBytes("\r\n");
dos.flush();
- try {
- hr = readResponse(socket.getInputStream());
- hr.parseContent();
- } catch (Exception e) {
- System.out.println("Exception");
- e.printStackTrace();
- st = false;
- }
-
- socket.close();
- dos.close();
+ hr = readResponse(socket.getInputStream());
+ hr.parseContent();
}
@@ -1061,6 +1080,21 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
System.err.println("Exception: Unable to Send Request:" + e);
e.printStackTrace();
st = false;
+ } finally {
+ if (dos != null) {
+ try {
+ dos.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
if (!st)