|
1 | 1 | /* |
2 | | - * Copyright 2005-2010 the original author or authors. |
| 2 | + * Copyright 2005-2012 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
6 | 6 | * You may obtain a copy of the License at |
7 | 7 | * |
8 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 | * |
10 | 10 | * Unless required by applicable law or agreed to in writing, software |
11 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
18 | 18 |
|
19 | 19 | import java.io.IOException; |
20 | 20 | import java.net.HttpURLConnection; |
21 | | -import java.net.URI; |
22 | | -import java.security.GeneralSecurityException; |
23 | 21 | import java.security.KeyManagementException; |
24 | 22 | import java.security.NoSuchAlgorithmException; |
25 | 23 | import java.security.NoSuchProviderException; |
26 | 24 | import java.security.SecureRandom; |
27 | | -import java.util.Arrays; |
28 | 25 | import javax.net.ssl.HostnameVerifier; |
29 | 26 | import javax.net.ssl.HttpsURLConnection; |
30 | 27 | import javax.net.ssl.KeyManager; |
31 | 28 | import javax.net.ssl.SSLContext; |
| 29 | +import javax.net.ssl.SSLSocketFactory; |
32 | 30 | import javax.net.ssl.TrustManager; |
33 | 31 |
|
34 | 32 | import org.springframework.beans.factory.InitializingBean; |
@@ -60,6 +58,8 @@ public class HttpsUrlConnectionMessageSender extends HttpUrlConnectionMessageSen |
60 | 58 |
|
61 | 59 | private SecureRandom rnd; |
62 | 60 |
|
| 61 | + private SSLSocketFactory sslSocketFactory; |
| 62 | + |
63 | 63 | /** |
64 | 64 | * Sets the SSL protocol to use. Default is {@code ssl}. |
65 | 65 | * |
@@ -119,51 +119,61 @@ public void setSecureRandom(SecureRandom rnd) { |
119 | 119 | this.rnd = rnd; |
120 | 120 | } |
121 | 121 |
|
| 122 | + /** |
| 123 | + * Specifies the SSLSocketFactory to use for this message sender. |
| 124 | + * |
| 125 | + * @see HttpsURLConnection#setSSLSocketFactory(SSLSocketFactory sf) |
| 126 | + */ |
| 127 | + public void setSslSocketFactory(SSLSocketFactory sslSocketFactory) { |
| 128 | + this.sslSocketFactory = sslSocketFactory; |
| 129 | + } |
| 130 | + |
122 | 131 | public void afterPropertiesSet() throws Exception { |
123 | | - Assert.isTrue(!(ObjectUtils.isEmpty(keyManagers) && ObjectUtils.isEmpty(trustManagers)), |
124 | | - "Setting either 'keyManagers' or 'trustManagers' is required"); |
| 132 | + Assert.isTrue( |
| 133 | + !(ObjectUtils.isEmpty(keyManagers) && ObjectUtils.isEmpty(trustManagers) && (sslSocketFactory == null)), |
| 134 | + "Setting either 'keyManagers', 'trustManagers' or 'sslSocketFactory' is required"); |
125 | 135 | } |
126 | 136 |
|
127 | 137 | @Override |
128 | 138 | protected void prepareConnection(HttpURLConnection connection) throws IOException { |
129 | 139 | super.prepareConnection(connection); |
130 | 140 | if (connection instanceof HttpsURLConnection) { |
131 | 141 | HttpsURLConnection httpsConnection = (HttpsURLConnection) connection; |
132 | | - try { |
133 | | - SSLContext sslContext = createSslContext(sslProtocol, sslProvider); |
134 | | - sslContext.init(keyManagers, trustManagers, rnd); |
135 | | - if (logger.isDebugEnabled()) { |
136 | | - logger.debug("Initialized SSL Context with key managers [" + |
137 | | - StringUtils.arrayToCommaDelimitedString(keyManagers) + "] trust managers [" + |
138 | | - StringUtils.arrayToCommaDelimitedString(trustManagers) + "] secure random [" + rnd + "]"); |
139 | | - } |
140 | | - |
141 | | - httpsConnection.setSSLSocketFactory(sslContext.getSocketFactory()); |
142 | | - |
143 | | - if (hostnameVerifier != null) { |
144 | | - httpsConnection.setHostnameVerifier(hostnameVerifier); |
145 | | - } |
146 | | - } |
147 | | - catch (NoSuchProviderException ex) { |
148 | | - throw new HttpsTransportException("Could not create SSLContext: " + ex.getMessage(), ex); |
149 | | - } |
150 | | - catch (NoSuchAlgorithmException ex) { |
151 | | - throw new HttpsTransportException("Could not create SSLContext: " + ex.getMessage(), ex); |
152 | | - } |
153 | | - catch (KeyManagementException ex) { |
154 | | - throw new HttpsTransportException("Could not initialize SSLContext: " + ex.getMessage(), ex); |
| 142 | + httpsConnection.setSSLSocketFactory(createSslSocketFactory()); |
| 143 | + |
| 144 | + if (hostnameVerifier != null) { |
| 145 | + httpsConnection.setHostnameVerifier(hostnameVerifier); |
155 | 146 | } |
156 | 147 | } |
157 | 148 | } |
158 | 149 |
|
159 | | - private SSLContext createSslContext(String protocol, String provider) |
160 | | - throws NoSuchProviderException, NoSuchAlgorithmException { |
161 | | - if (!StringUtils.hasLength(provider)) { |
162 | | - return SSLContext.getInstance(protocol); |
| 150 | + private SSLSocketFactory createSslSocketFactory() throws HttpsTransportException { |
| 151 | + if (this.sslSocketFactory != null) { |
| 152 | + return this.sslSocketFactory; |
| 153 | + } |
| 154 | + try { |
| 155 | + SSLContext sslContext = |
| 156 | + StringUtils.hasLength(sslProvider) ? SSLContext.getInstance(sslProtocol, sslProvider) : |
| 157 | + SSLContext.getInstance(sslProtocol); |
| 158 | + sslContext.init(keyManagers, trustManagers, rnd); |
| 159 | + if (logger.isDebugEnabled()) { |
| 160 | + logger.debug("Initialized SSL Context with key managers [" + |
| 161 | + StringUtils.arrayToCommaDelimitedString(keyManagers) + "] trust managers [" + |
| 162 | + StringUtils.arrayToCommaDelimitedString(trustManagers) + "] secure random [" + rnd + |
| 163 | + "]"); |
| 164 | + } |
| 165 | + return sslContext.getSocketFactory(); |
163 | 166 | } |
164 | | - else { |
165 | | - return SSLContext.getInstance(protocol, provider); |
| 167 | + catch (NoSuchAlgorithmException ex) { |
| 168 | + throw new HttpsTransportException("Could not create SSLContext: " + ex.getMessage(), ex); |
166 | 169 | } |
| 170 | + catch (NoSuchProviderException ex) { |
| 171 | + throw new HttpsTransportException("Could not create SSLContext: " + ex.getMessage(), ex); |
| 172 | + } |
| 173 | + catch (KeyManagementException ex) { |
| 174 | + throw new HttpsTransportException("Could not initialize SSLContext: " + ex.getMessage(), ex); |
| 175 | + } |
| 176 | + |
167 | 177 | } |
168 | 178 |
|
169 | 179 | } |
0 commit comments