jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1520662 - /jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/proxy/Proxy.java
Date Fri, 06 Sep 2013 18:32:59 GMT
Author: sebb
Date: Fri Sep  6 18:32:59 2013
New Revision: 1520662

URL: http://svn.apache.org/r1520662
Log:
Sinplify KeyStore loading

Modified:
    jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/proxy/Proxy.java

Modified: jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/proxy/Proxy.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/proxy/Proxy.java?rev=1520662&r1=1520661&r2=1520662&view=diff
==============================================================================
--- jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/proxy/Proxy.java (original)
+++ jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/proxy/Proxy.java Fri Sep
 6 18:32:59 2013
@@ -334,33 +334,41 @@ public class Proxy extends Thread {
      * @throws IOException if the store cannot be opened or read or the alias is missing
      */
     private KeyManager[] getKeyManagers(String serverAlias) throws GeneralSecurityException,
IOException {
-        InputStream in = getCertificate();
-        if (in == null) {
-            throw new IOException("Cannot open keystore");
+        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KEYMANAGERFACTORY);
+        KeyStore ks = getKeyStore(JMeterUtils.getPropDefault("proxy.cert.keystorepass", DEFAULT_PASSWORD).toCharArray());
// $NON-NLS-1$
+        kmf.init(ks, JMeterUtils.getPropDefault("proxy.cert.keypassword", DEFAULT_PASSWORD).toCharArray());
// $NON-NLS-1$
+        final KeyManager[] keyManagers = kmf.getKeyManagers();
+        if (serverAlias == null) {
+            return keyManagers;
         } else {
-            try {
-                KeyStore ks = KeyStore.getInstance(KEYSTORE_TYPE);
-                ks.load(in, JMeterUtils.getPropDefault("proxy.cert.keystorepass", DEFAULT_PASSWORD).toCharArray());
// $NON-NLS-1$
-                KeyManagerFactory kmf = KeyManagerFactory.getInstance(KEYMANAGERFACTORY);
-                kmf.init(ks, JMeterUtils.getPropDefault("proxy.cert.keypassword", DEFAULT_PASSWORD).toCharArray());
// $NON-NLS-1$
-                final KeyManager[] keyManagers = kmf.getKeyManagers();
-                if (serverAlias == null) {
-                    return keyManagers;
-                } else {
-                    // Check if alias is suitable here, rather than waiting for connection
to fail
-                    if (!ks.containsAlias(serverAlias)) {
-                        throw new IOException("Keystore does not contain alias " + serverAlias);
-                    }
-                    final int keyManagerCount = keyManagers.length;
-                    final KeyManager[] wrappedKeyManagers = new KeyManager[keyManagerCount];
-                    for (int i =0; i < keyManagerCount; i++) {
-                        wrappedKeyManagers[i] = new ServerAliasKeyManager(keyManagers[i],
serverAlias);
-                    }
-                    return wrappedKeyManagers;
-                }
-            } finally {
-                IOUtils.closeQuietly(in);
+            // Check if alias is suitable here, rather than waiting for connection to fail
+            if (!ks.containsAlias(serverAlias)) {
+                throw new IOException("Keystore does not contain alias " + serverAlias);
+            }
+            final int keyManagerCount = keyManagers.length;
+            final KeyManager[] wrappedKeyManagers = new KeyManager[keyManagerCount];
+            for (int i =0; i < keyManagerCount; i++) {
+                wrappedKeyManagers[i] = new ServerAliasKeyManager(keyManagers[i], serverAlias);
             }
+            return wrappedKeyManagers;
+        }
+    }
+
+    private KeyStore getKeyStore(char[] password) throws GeneralSecurityException, IOException
{
+        File certFile = new File(CERT_DIRECTORY, CERT_FILE);
+        InputStream in = null;
+        final String certPath = certFile.getAbsolutePath();
+        try {
+            in = new BufferedInputStream(new FileInputStream(certFile));
+            log.info(port + "Opened Keystore file: " + certPath);
+            KeyStore ks = KeyStore.getInstance(KEYSTORE_TYPE);
+            ks.load(in, password);
+            return ks;
+        } catch (FileNotFoundException e) {
+            log.error(port + "Could not open Keystore file: " + certPath, e);
+            throw e;
+        } finally {
+            IOUtils.closeQuietly(in);
         }
     }
 
@@ -394,28 +402,6 @@ public class Proxy extends Thread {
         }
     }
 
-    /**
-     * Open the local certificate file.
-     *
-     * @return stream to key cert; null if there was a problem opening it
-     */
-    private InputStream getCertificate() {
-        File certFile = new File(CERT_DIRECTORY, CERT_FILE);
-        InputStream in = null;
-        final String certPath = certFile.getAbsolutePath();
-        if (certFile.exists() && certFile.canRead()) {
-            try {
-                in = new BufferedInputStream(new FileInputStream(certFile));
-                log.info(port + "Opened Keystore file: "+certPath);
-            } catch (FileNotFoundException e) {
-                log.error(port + "No server cert file found: "+certPath, e);
-            }
-        } else {
-            log.error(port + "No server cert file found: "+certPath);
-        }
-        return in;
-    }
-
     private SampleResult generateErrorResult(SampleResult result, HttpRequestHdr request,
Exception e) {
         return generateErrorResult(result, request, e, "");
     }



Mime
View raw message