英文:
Configure Apache HttpClient 4.5 as a transport sender in Axis2 stub
问题
以下是需要迁移到HttpClient 4.5版本的完整代码:
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.client.methods.HttpUriRequest;
// 迁移到HttpClient 4.5版本的代码
final Options clientOptions = stub._getServiceClient().getOptions();
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(new SSLConnectionSocketFactory(new TLSSocketFactory(),
new String[]{"TLSv1.1", "TLSv1.2"}, null, NoopHostnameVerifier.INSTANCE))
.build();
clientOptions.setProperty(HTTPConstants.CUSTOM_HTTP_CLIENT, httpClient);
// TLSSocketFactory 类
public class TLSSocketFactory extends SSLSocketFactory {
public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException {
super(null, null, null, null, null, null);
}
@Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
return enableTLSOnSocket(super.createSocket(s, host, port, autoClose));
}
@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
return enableTLSOnSocket(super.createSocket(host, port));
}
@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
return enableTLSOnSocket(super.createSocket(host, port, localHost, localPort));
}
@Override
public Socket createSocket(String s, int i, InetAddress inetAddress, int i1, HttpConnectionParams httpConnectionParams) throws IOException, UnknownHostException, ConnectTimeoutException {
return enableTLSOnSocket(super.createSocket(s, i, inetAddress, i1));
}
@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
return enableTLSOnSocket(super.createSocket(host, port));
}
@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
return enableTLSOnSocket(super.createSocket(address, port, localAddress, localPort));
}
private Socket enableTLSOnSocket(Socket socket) {
if (socket != null && (socket instanceof SSLSocket)) {
((SSLSocket) socket).setEnabledProtocols(new String[]{"TLSv1.1", "TLSv1.2"});
}
return socket;
}
}
注意:由于您只需要翻译代码部分,因此我只提供了代码的翻译部分。如果您有任何进一步的问题或需要解释,请随时提问。
英文:
I am in the process of replacing Apache Httpclient 3.1 to 4.5 version, Our application is using AXIS 2 SOAP Web Service stub which underneath is using HTTPClient 3.1 TransportSender. I need to migrate it to use HttpClient 4.5 version. Below is the complete code that need to be migrated to HttpClient 4.5 version :
import org.apache.commons.httpclient.protocol.Protocol; // 3.1 version
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory; // 3.1 version
final Options clientOptions = stub._getServiceClient().getOptions();
clientOptions.setProperty(HTTPConstants.CUSTOM_PROTOCOL_HANDLER, new Protocol("https", new TLSSocketFactory(), 443));
public class TLSSocketFactory extends SSLSocketFactory implements SecureProtocolSocketFactory {
private SSLSocketFactory internalSSLSocketFactory;
public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, null, null);
internalSSLSocketFactory = context.getSocketFactory();
}
@Override
public String[] getDefaultCipherSuites() {
return internalSSLSocketFactory.getDefaultCipherSuites();
}
@Override
public String[] getSupportedCipherSuites() {
return internalSSLSocketFactory.getSupportedCipherSuites();
}
@Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(s, host, port, autoClose));
}
@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port));
}
@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port, localHost, localPort));
}
@Override
public Socket createSocket(String s, int i, InetAddress inetAddress, int i1, HttpConnectionParams httpConnectionParams) throws IOException, UnknownHostException, ConnectTimeoutException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(s, i, inetAddress, i1));
}
@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port));
}
@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(address, port, localAddress, localPort));
}
private Socket enableTLSOnSocket(Socket socket) {
if(socket != null && (socket instanceof SSLSocket)) {
((SSLSocket)socket).setEnabledProtocols(new String[] {"TLSv1.1", "TLSv1.2"});
}
return socket;
}
}
I came across this StackOverflow post : How to configure SSL with Axis2 using httpClient4
But its clearly mentioned in the post that it is only compatible upto httpclient 4.4.1.
> Axis2 1.7.0 supports Apache HttpClient 4.x in addition to the no
> longer maintained Commons HttpClient 3.x. To enable the support for
> HttpClient 4.x, use
> org.apache.axis2.transport.http.impl.httpclient4.HTTPClient4TransportSender
> instead of org.apache.axis2.transport.http.CommonsHTTPTransportSender
> in axis2.xml. Please note that the code was written for HttpClient
> 4.2.x and should work with 4.3.x and 4.4.x, but is incompatible with 4.5.x.
We are using HttpClient 4.5 and it clearly says it's incompatible with 4.5.x
I am really stuck and need help on migrating the above piece of code to use HttpClient 4.5.
Thanks in advance.
答案1
得分: 0
以下SO帖子中提到的步骤对于HttpClient 4.5.x同样适用。
英文:
The steps mentioned in the below SO Post works for HttpClient 4.5.x as well.
专注分享java语言的经验与见解,让所有开发者获益!
评论