Skip to content

Commit 92eff95

Browse files
committed
SWS-808 - add support to pass in sslSocketFactory to org.springframework.ws.transport.http.HttpsUrlConnectionMessageSender
1 parent 5f79616 commit 92eff95

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

support/src/main/java/org/springframework/ws/transport/http/HttpsUrlConnectionMessageSender.java

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2005-2010 the original author or authors.
2+
* Copyright 2005-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -18,17 +18,15 @@
1818

1919
import java.io.IOException;
2020
import java.net.HttpURLConnection;
21-
import java.net.URI;
22-
import java.security.GeneralSecurityException;
2321
import java.security.KeyManagementException;
2422
import java.security.NoSuchAlgorithmException;
2523
import java.security.NoSuchProviderException;
2624
import java.security.SecureRandom;
27-
import java.util.Arrays;
2825
import javax.net.ssl.HostnameVerifier;
2926
import javax.net.ssl.HttpsURLConnection;
3027
import javax.net.ssl.KeyManager;
3128
import javax.net.ssl.SSLContext;
29+
import javax.net.ssl.SSLSocketFactory;
3230
import javax.net.ssl.TrustManager;
3331

3432
import org.springframework.beans.factory.InitializingBean;
@@ -60,6 +58,8 @@ public class HttpsUrlConnectionMessageSender extends HttpUrlConnectionMessageSen
6058

6159
private SecureRandom rnd;
6260

61+
private SSLSocketFactory sslSocketFactory;
62+
6363
/**
6464
* Sets the SSL protocol to use. Default is {@code ssl}.
6565
*
@@ -119,51 +119,61 @@ public void setSecureRandom(SecureRandom rnd) {
119119
this.rnd = rnd;
120120
}
121121

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+
122131
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");
125135
}
126136

127137
@Override
128138
protected void prepareConnection(HttpURLConnection connection) throws IOException {
129139
super.prepareConnection(connection);
130140
if (connection instanceof HttpsURLConnection) {
131141
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);
155146
}
156147
}
157148
}
158149

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();
163166
}
164-
else {
165-
return SSLContext.getInstance(protocol, provider);
167+
catch (NoSuchAlgorithmException ex) {
168+
throw new HttpsTransportException("Could not create SSLContext: " + ex.getMessage(), ex);
166169
}
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+
167177
}
168178

169179
}

0 commit comments

Comments
 (0)