Author: sebb Date: Fri May 27 13:14:32 2011 New Revision: 1128295 URL: http://svn.apache.org/viewvc?rev=1128295&view=rev Log: Refactor HC4 SSL factory setup
Added: jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/HC4TrustAllSSLSocketFactory.java (with props) Modified: jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/SlowHC4SSLSocketFactory.java Modified: jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java?rev=1128295&r1=1128294&r2=1128295&view=diff ============================================================================== --- jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java (original) +++ jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java Fri May 27 13:14:32 2011 @@ -29,7 +29,6 @@ import java.net.URL; import java.net.URLDecoder; import java.nio.charset.Charset; import java.security.GeneralSecurityException; -import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -66,9 +65,6 @@ import org.apache.http.client.protocol.R import org.apache.http.conn.params.ConnRoutePNames; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; -import org.apache.http.conn.ssl.AllowAllHostnameVerifier; -import org.apache.http.conn.ssl.SSLSocketFactory; -import org.apache.http.conn.ssl.TrustStrategy; import org.apache.http.entity.FileEntity; import org.apache.http.entity.StringEntity; import org.apache.http.entity.mime.FormBodyPart; @@ -94,6 +90,7 @@ import org.apache.jmeter.protocol.http.c import org.apache.jmeter.protocol.http.control.CookieManager; import org.apache.jmeter.protocol.http.control.HeaderManager; import org.apache.jmeter.protocol.http.util.EncoderCache; +import org.apache.jmeter.protocol.http.util.HC4TrustAllSSLSocketFactory; import org.apache.jmeter.protocol.http.util.HTTPArgument; import org.apache.jmeter.protocol.http.util.HTTPFileArg; import org.apache.jmeter.protocol.http.util.SlowHC4SSLSocketFactory; @@ -133,19 +130,11 @@ public class HTTPHC4Impl extends HTTPHCA } }; - // Trust all certificates - private static final TrustStrategy TRUSTALL = new TrustStrategy(){ - public boolean isTrusted(X509Certificate[] chain, String authType) { - return true; - } - }; - - // Allow all host names - private static final AllowAllHostnameVerifier ALLOW_ALL_HOSTNAMES = new AllowAllHostnameVerifier(); - - // Scheme used for slow sockets. Cannot be set as a default, because must be set on an HttpClient instance. + // Scheme used for slow HTTP sockets. Cannot be set as a default, because must be set on an HttpClient instance. private static final Scheme SLOW_HTTP; - private static final Scheme SLOW_HTTPS; + + // We always want to override the HTTPS scheme, because we want to trust all certificates and hosts + private static final Scheme HTTPS_SCHEME; /* * Create a set of default parameters from the ones initially created. @@ -166,24 +155,32 @@ public class HTTPHC4Impl extends HTTPHCA HttpClientDefaultParameters.load(file, DEFAULT_HTTP_PARAMS); } + // Set up HTTP scheme override if necessary if (CPS_HTTP > 0) { log.info("Setting up HTTP SlowProtocol, cps="+CPS_HTTP); SLOW_HTTP = new Scheme(PROTOCOL_HTTP, DEFAULT_HTTP_PORT, new SlowHC4SocketFactory(CPS_HTTP)); } else { SLOW_HTTP = null; } + + // We always want to override the HTTPS scheme + Scheme https = null; if (CPS_HTTPS > 0) { log.info("Setting up HTTPS SlowProtocol, cps="+CPS_HTTPS); - Scheme s = null; try { - s = new Scheme(PROTOCOL_HTTPS, DEFAULT_HTTPS_PORT, new SlowHC4SSLSocketFactory(CPS_HTTPS)); + https = new Scheme(PROTOCOL_HTTPS, DEFAULT_HTTPS_PORT, new SlowHC4SSLSocketFactory(CPS_HTTPS)); } catch (GeneralSecurityException e) { - log.warn("Failed to initialise SLOW_HTTPS scheme", e); + log.warn("Failed to initialise SLOW_HTTPS scheme, cps="+CPS_HTTPS, e); } - SLOW_HTTPS = s; } else { - SLOW_HTTPS = null; + log.info("Setting up HTTPS TrustAll scheme"); + try { + https = new Scheme(PROTOCOL_HTTPS, DEFAULT_HTTPS_PORT, new HC4TrustAllSSLSocketFactory()); + } catch (GeneralSecurityException e) { + log.warn("Failed to initialise HTTPS TrustAll scheme", e); + } } + HTTPS_SCHEME = https; if (localAddress != null){ DEFAULT_HTTP_PARAMS.setParameter(ConnRoutePNames.LOCAL_ADDRESS, localAddress); } @@ -448,7 +445,7 @@ public class HTTPHC4Impl extends HTTPHCA HttpClient httpClient = map.get(key); - if (httpClient == null){ + if (httpClient == null){ // One-time init for this client HttpParams clientParams = new DefaultedHttpParams(new BasicHttpParams(), DEFAULT_HTTP_PARAMS); @@ -456,22 +453,15 @@ public class HTTPHC4Impl extends HTTPHCA ((AbstractHttpClient) httpClient).addResponseInterceptor(new ResponseContentEncoding()); ((AbstractHttpClient) httpClient).addResponseInterceptor(METRICS_SAVER); // HACK + // Override the defualt schemes as necessary SchemeRegistry schemeRegistry = httpClient.getConnectionManager().getSchemeRegistry(); - // Allow all hostnames and all certificates - try { - SSLSocketFactory socketFactory = new SSLSocketFactory(TRUSTALL, ALLOW_ALL_HOSTNAMES); - Scheme sch = new Scheme(PROTOCOL_HTTPS, DEFAULT_HTTPS_PORT, socketFactory); - schemeRegistry.register(sch); - } catch (GeneralSecurityException e) { - log.warn("Failed to register trust-all socket factory", e); - } - if (SLOW_HTTP != null){ schemeRegistry.register(SLOW_HTTP); } - if (SLOW_HTTPS != null){ - schemeRegistry.register(SLOW_HTTPS); + + if (HTTPS_SCHEME != null){ + schemeRegistry.register(HTTPS_SCHEME); } // Set up proxy details Added: jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/HC4TrustAllSSLSocketFactory.java URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/HC4TrustAllSSLSocketFactory.java?rev=1128295&view=auto ============================================================================== --- jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/HC4TrustAllSSLSocketFactory.java (added) +++ jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/HC4TrustAllSSLSocketFactory.java Fri May 27 13:14:32 2011 @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.jmeter.protocol.http.util; + +import java.security.GeneralSecurityException; +import java.security.cert.X509Certificate; + +import org.apache.http.conn.ssl.SSLSocketFactory; +import org.apache.http.conn.ssl.TrustStrategy; + +/** + * Apache HttpClient protocol factory to generate SSL sockets + */ + +public class HC4TrustAllSSLSocketFactory extends SSLSocketFactory { + + private static final TrustStrategy TRUSTALL = new TrustStrategy(){ + public boolean isTrusted(X509Certificate[] chain, String authType) { + return true; + } + }; + + /** + * Create an SSL factory which trusts all certificates and hosts. + * {@link SSLSocketFactory#SSLSocketFactory(TrustStrategy, org.apache.http.conn.ssl.X509HostnameVerifier)} + * @throws GeneralSecurityException if there's a problem setting up the security + */ + public HC4TrustAllSSLSocketFactory() throws GeneralSecurityException { + super(TRUSTALL, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); + } +} Propchange: jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/HC4TrustAllSSLSocketFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/HC4TrustAllSSLSocketFactory.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Modified: jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/SlowHC4SSLSocketFactory.java URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/SlowHC4SSLSocketFactory.java?rev=1128295&r1=1128294&r2=1128295&view=diff ============================================================================== --- jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/SlowHC4SSLSocketFactory.java (original) +++ jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/util/SlowHC4SSLSocketFactory.java Fri May 27 13:14:32 2011 @@ -20,11 +20,7 @@ package org.apache.jmeter.protocol.http. import java.net.Socket; import java.security.GeneralSecurityException; -import java.security.cert.X509Certificate; -import org.apache.http.conn.ssl.AllowAllHostnameVerifier; -import org.apache.http.conn.ssl.SSLSocketFactory; -import org.apache.http.conn.ssl.TrustStrategy; import org.apache.http.params.HttpParams; import org.apache.jmeter.util.SlowSocket; @@ -32,28 +28,25 @@ import org.apache.jmeter.util.SlowSocket * Apache HttpClient protocol factory to generate "slow" SSL sockets for emulating dial-up modems */ -public class SlowHC4SSLSocketFactory extends SSLSocketFactory { - - private static final TrustStrategy TRUSTALL = new TrustStrategy(){ - public boolean isTrusted(X509Certificate[] chain, String authType) { - return true; - } - }; - - private static final AllowAllHostnameVerifier ALLOW_ALL_HOSTS = new AllowAllHostnameVerifier(); +public class SlowHC4SSLSocketFactory extends HC4TrustAllSSLSocketFactory { private final int CPS; // Characters per second to emulate /** * Create a factory - * @param cps - characters per second + * @param cps - characters per second, must be > 0 * @throws GeneralSecurityException if there's a problem setting up the security + * @throws IllegalArgumentException if cps ≤ 0 */ public SlowHC4SSLSocketFactory(final int cps) throws GeneralSecurityException { - super(TRUSTALL, ALLOW_ALL_HOSTS); + super(); + if (cps <= 0) { + throw new IllegalArgumentException("CPS must be > 0, but is "+cps); + } CPS = cps; } + // Override all the socket creation methods in SSLSocketFactory @Override public Socket createSocket(final HttpParams params) { return new SlowSocket(CPS); --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@jakarta.apache.org For additional commands, e-mail: notifications-h...@jakarta.apache.org