Skip to content

Commit 4c18876

Browse files
author
ehennum
committed
socket factory delegates that set TCP keepalive #1274
(cherry picked from commit 6ac7109)
1 parent 4288c04 commit 4c18876

File tree

3 files changed

+141
-7
lines changed

3 files changed

+141
-7
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/impl/OkHttpServices.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
import javax.mail.MessagingException;
7878
import javax.mail.internet.MimeMultipart;
7979
import javax.mail.util.ByteArrayDataSource;
80+
import javax.net.SocketFactory;
8081
import javax.net.ssl.*;
8182
import javax.xml.bind.DatatypeConverter;
8283
import java.io.ByteArrayInputStream;
@@ -310,7 +311,7 @@ public boolean verify(String hostname, SSLSession session) {
310311
throw new IllegalArgumentException("no trust manager and default is not an X509TrustManager");
311312
trustManager = (X509TrustManager) trustMgrs[0];
312313
sslContext.init(null, trustMgrs, null);
313-
clientBldr.sslSocketFactory(sslContext.getSocketFactory(), trustManager);
314+
clientBldr.sslSocketFactory(new SSLSocketFactoryDelegator(sslContext.getSocketFactory()), trustManager);
314315
} catch (KeyStoreException e) {
315316
throw new IllegalArgumentException("no trust manager and cannot initialize factory for default", e);
316317
} catch (NoSuchAlgorithmException e) {
@@ -319,8 +320,10 @@ public boolean verify(String hostname, SSLSession session) {
319320
throw new IllegalArgumentException("no trust manager and cannot initialize context with default", e);
320321
}
321322
} else {
322-
clientBldr.sslSocketFactory(sslContext.getSocketFactory(), trustManager);
323+
clientBldr.sslSocketFactory(new SSLSocketFactoryDelegator(sslContext.getSocketFactory()), trustManager);
323324
}
325+
} else {
326+
clientBldr.socketFactory(new SocketFactoryDelegator(SocketFactory.getDefault()));
324327
}
325328

326329
if ( hostnameVerifier != null ) {
@@ -372,11 +375,10 @@ public void log(String message) {
372375

373376
this.client = clientBldr.build();
374377
// System.setProperty("javax.net.debug", "all"); // all or ssl
375-
/*
376-
// long-term alternative to isFirstRequest alive
377-
// HttpProtocolParams.setUseExpectContinue(httpParams, false);
378-
// httpParams.setIntParameter(CoreProtocolPNames.WAIT_FOR_CONTINUE, 1000);
379-
*/
378+
/* TODO: long-term alternative to isFirstRequest alive
379+
HttpProtocolParams.setUseExpectContinue(httpParams, false);
380+
httpParams.setIntParameter(CoreProtocolPNames.WAIT_FOR_CONTINUE, 1000);
381+
*/
380382
}
381383

382384
public OkHttpClient.Builder configureAuthentication(BasicAuthContext basicAuthContext, OkHttpClient.Builder clientBuilder) {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2020 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.marklogic.client.impl;
17+
18+
import javax.net.ssl.SSLSocketFactory;
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.net.InetAddress;
22+
import java.net.Socket;
23+
import java.net.SocketException;
24+
25+
class SSLSocketFactoryDelegator extends SSLSocketFactory {
26+
private final SSLSocketFactory delegate;
27+
28+
SSLSocketFactoryDelegator(SSLSocketFactory delegate) {
29+
super();
30+
this.delegate = delegate;
31+
}
32+
33+
@Override
34+
public String[] getDefaultCipherSuites() {
35+
return delegate.getDefaultCipherSuites();
36+
}
37+
@Override
38+
public String[] getSupportedCipherSuites() {
39+
return delegate.getSupportedCipherSuites();
40+
}
41+
42+
@Override
43+
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
44+
return configureSocket(delegate.createSocket(s, host, port, autoClose));
45+
}
46+
@Override
47+
public Socket createSocket(String host, int port) throws IOException {
48+
return configureSocket(delegate.createSocket(host, port));
49+
}
50+
@Override
51+
public Socket createSocket(String host, int port, InetAddress localAddress, int localPort) throws IOException {
52+
return configureSocket(delegate.createSocket(host, port, localAddress, localPort));
53+
}
54+
@Override
55+
public Socket createSocket(InetAddress address, int port) throws IOException {
56+
return configureSocket(delegate.createSocket(address, port));
57+
}
58+
@Override
59+
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
60+
return configureSocket(delegate.createSocket(address, port, localAddress, localPort));
61+
}
62+
@Override
63+
public Socket createSocket(Socket s, InputStream consumed, boolean autoClose) throws IOException {
64+
return configureSocket(delegate.createSocket(s, consumed, autoClose));
65+
}
66+
@Override
67+
public Socket createSocket() throws IOException {
68+
return configureSocket(delegate.createSocket());
69+
}
70+
71+
private Socket configureSocket(Socket socket) throws SocketException {
72+
socket.setKeepAlive(true);
73+
return socket;
74+
}
75+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2020 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.marklogic.client.impl;
17+
18+
import javax.net.SocketFactory;
19+
import java.io.IOException;
20+
import java.net.InetAddress;
21+
import java.net.Socket;
22+
import java.net.SocketException;
23+
24+
class SocketFactoryDelegator extends SocketFactory {
25+
private final SocketFactory delegate;
26+
27+
SocketFactoryDelegator(SocketFactory delegate) {
28+
super();
29+
this.delegate = delegate;
30+
}
31+
32+
@Override
33+
public Socket createSocket(String host, int port) throws IOException {
34+
return configureSocket(delegate.createSocket(host, port));
35+
}
36+
@Override
37+
public Socket createSocket(String host, int port, InetAddress localAddress, int localPort) throws IOException {
38+
return configureSocket(delegate.createSocket(host, port, localAddress, localPort));
39+
}
40+
@Override
41+
public Socket createSocket(InetAddress address, int port) throws IOException {
42+
return configureSocket(delegate.createSocket(address, port));
43+
}
44+
@Override
45+
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
46+
return configureSocket(delegate.createSocket(address, port, localAddress, localPort));
47+
}
48+
@Override
49+
public Socket createSocket() throws IOException {
50+
return configureSocket(delegate.createSocket());
51+
}
52+
53+
private Socket configureSocket(Socket socket) throws SocketException {
54+
socket.setKeepAlive(true);
55+
return socket;
56+
}
57+
}

0 commit comments

Comments
 (0)