Skip to content

Commit f4eb730

Browse files
committed
Fix: Add configurable TLS version support to PusherOptions
1 parent dec90ac commit f4eb730

File tree

9 files changed

+39
-56
lines changed

9 files changed

+39
-56
lines changed

build.gradle

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,12 @@
1-
import org.apache.tools.ant.filters.ReplaceTokens
2-
3-
buildscript {
4-
repositories {
5-
mavenCentral()
6-
}
7-
dependencies {
8-
classpath 'org.ajoberstar:gradle-git:1.1.0'
9-
}
10-
}
11-
121
plugins {
132
id "eclipse"
143
id "jacoco"
154
id "java-library"
165
id "maven-publish"
17-
id "org.ajoberstar.github-pages" version "1.7.2"
18-
id "signing"
19-
}
20-
21-
def getProperty = { property ->
22-
if (!project.hasProperty(property)) {
23-
throw new GradleException("${property} property must be set")
24-
}
25-
return project.property(property)
266
}
277

288
group = "com.pusher"
29-
version = "2.4.4"
9+
version = "2.4.5"
3010
sourceCompatibility = "1.8"
3111
targetCompatibility = "1.8"
3212

@@ -55,20 +35,17 @@ dependencies {
5535
testImplementation "com.google.truth:truth:1.0.1"
5636
}
5737

38+
import org.apache.tools.ant.filters.ReplaceTokens
5839

5940
processResources {
6041
filter(ReplaceTokens, tokens: [
6142
version: project.version
6243
])
6344
}
6445

65-
6646
javadoc {
67-
/* info for JavaDoc options http://docs.oracle.com/javase/6/docs/technotes/tools/windows/javadoc.html#overviewcomment */
6847
title "Pusher Java Websocket API"
6948
options.overview = file("src/main/javadoc/overview.html")
70-
// uncomment this to use the custom javadoc styles
71-
//options.stylesheetFile = file("src/main/javadoc/css/styles.css")
7249
exclude "com/pusher/client/channel/impl/*"
7350
exclude "com/pusher/client/connection/impl/*"
7451
exclude "com/pusher/client/connection/websocket/*"
@@ -79,7 +56,6 @@ javadoc {
7956
options.linkSource = true
8057
}
8158

82-
8359
jar {
8460
manifest = project.manifest {
8561
from sharedManifest
@@ -97,32 +73,18 @@ task fatJar(type: Jar, dependsOn: configurations.runtimeClasspath) {
9773
}
9874
assemble.dependsOn fatJar
9975

100-
10176
task sourcesJar(type: Jar, dependsOn: classes) {
10277
archiveClassifier.set('sources')
10378
from sourceSets.main.allSource
10479
}
10580
assemble.dependsOn sourcesJar
10681

107-
10882
task javadocJar(type: Jar, dependsOn: javadoc) {
10983
archiveClassifier.set('javadoc')
11084
from javadoc.destinationDir
11185
}
11286
assemble.dependsOn javadocJar
11387

114-
githubPages {
115-
repoUri = 'https://github.com/pusher/pusher-websocket-java.git'
116-
pages {
117-
from javadoc.outputs.files
118-
}
119-
commitMessage = "JavaDoc gh-pages for ${version}"
120-
credentials {
121-
username = { getProperty("github.username") }
122-
password = { getProperty("github.password") }
123-
}
124-
}
125-
12688
artifacts {
12789
archives jar, fatJar, sourcesJar, javadocJar
12890
}
@@ -136,7 +98,6 @@ publishing {
13698
publications {
13799
mavenJava(MavenPublication) {
138100
artifactId = 'pusher-java-client'
139-
140101
from components.java
141102
pom {
142103
name = 'Pusher Java Client Library'
@@ -187,10 +148,6 @@ publishing {
187148
}
188149
}
189150

190-
signing {
191-
sign publishing.publications.mavenJava
192-
}
193-
194151
jacoco {
195152
toolVersion = "0.8.8"
196153
}
@@ -201,9 +158,8 @@ jacocoTestReport {
201158
reports.csv.required = false
202159
}
203160

204-
// Test Logging - https://stackoverflow.com/a/42425815/2623314
205161
test {
206162
testLogging {
207163
showStandardStreams = true
208164
}
209-
}
165+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

src/main/java/com/pusher/client/PusherOptions.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111
public class PusherOptions {
1212

13+
private String tlsVersion = "TLS";
1314
private static final String SRC_LIB_DEV_VERSION = "@version@";
1415
private static final String LIB_DEV_VERSION = "0.0.0-dev";
1516
public static final String LIB_VERSION = readVersionFromProperties();
@@ -44,6 +45,15 @@ public class PusherOptions {
4445
private int maxReconnectionAttempts = MAX_RECONNECTION_ATTEMPTS;
4546
private int maxReconnectGapInSeconds = MAX_RECONNECT_GAP_IN_SECONDS;
4647

48+
public PusherOptions setTlsVersion(String tlsVersion) {
49+
this.tlsVersion = tlsVersion;
50+
return this;
51+
}
52+
53+
public String getTlsVersion() {
54+
return tlsVersion;
55+
}
56+
4757
/**
4858
* @deprecated Please use isUseTLS
4959
*/

src/main/java/com/pusher/client/connection/websocket/WebSocketClientWrapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public class WebSocketClientWrapper extends WebSocketClient {
2424
private static final String WSS_SCHEME = "wss";
2525
private WebSocketListener webSocketListener;
2626

27-
public WebSocketClientWrapper(final URI uri, final Proxy proxy, final WebSocketListener webSocketListener)
27+
public WebSocketClientWrapper(final URI uri, final Proxy proxy, final WebSocketListener webSocketListener, String tlsVersion)
2828
throws SSLException {
2929
super(uri);
3030
if (uri.getScheme().equals(WSS_SCHEME)) {
3131
try {
32-
SSLContext sslContext = SSLContext.getInstance("TLS");
32+
SSLContext sslContext = SSLContext.getInstance(tlsVersion);
3333
sslContext.init(null, null, null); // will use java's default
3434
// key and trust store which
3535
// is sufficient unless you

src/main/java/com/pusher/client/util/BaseHttpAuthClient.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.pusher.client.util;
22

3+
import javax.net.ssl.SSLSocketFactory;
34
import java.io.BufferedReader;
45
import java.io.DataOutputStream;
56
import java.io.IOException;
@@ -22,6 +23,7 @@ abstract class BaseHttpAuthClient {
2223
private final URL endPoint;
2324
private Map<String, String> mHeaders = new HashMap<>();
2425
protected ConnectionFactory mConnectionFactory;
26+
protected SSLSocketFactory mSslSocketFactory = null;
2527

2628
/**
2729
* Creates a new auth client.
@@ -61,6 +63,15 @@ public void setHeaders(final Map<String, String> headers) {
6163
mHeaders = headers;
6264
}
6365

66+
/**
67+
* Set the SSL socket factory that is used for the connection. This allows to
68+
* configure custom certificate handling (self-signed certificates, mutual TLS, ...)
69+
* @param sslSocketFactory The SSL socket factory to use
70+
*/
71+
public void setSslSocketFactory(final SSLSocketFactory sslSocketFactory) {
72+
mSslSocketFactory = sslSocketFactory;
73+
}
74+
6475
/**
6576
* Identifies if the HTTP request will be sent over HTTPS.
6677
*
@@ -92,6 +103,10 @@ protected String performAuthRequest() {
92103
HttpURLConnection connection;
93104
if (isSSL()) {
94105
connection = (HttpsURLConnection) endPoint.openConnection();
106+
if(mSslSocketFactory != null) {
107+
((HttpsURLConnection) connection)
108+
.setSSLSocketFactory(mSslSocketFactory);
109+
}
95110
} else {
96111
connection = (HttpURLConnection) endPoint.openConnection();
97112
}

src/main/java/com/pusher/client/util/Factory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@ public class Factory {
5151
private ExecutorService eventQueue;
5252
private ScheduledExecutorService timers;
5353
private static final Object eventLock = new Object();
54+
private String tlsVersion = "TLS";
5455

5556
public synchronized InternalConnection getConnection(
5657
final String apiKey,
5758
final PusherOptions options,
5859
final Consumer<PusherEvent> eventHandler
5960
) {
6061
if (connection == null) {
62+
tlsVersion = options.getTlsVersion(); // ← add this line
6163
try {
6264
connection =
6365
new WebSocketConnection(
@@ -82,7 +84,7 @@ public WebSocketClientWrapper newWebSocketClientWrapper(
8284
final Proxy proxy,
8385
final WebSocketListener webSocketListener
8486
) throws SSLException {
85-
return new WebSocketClientWrapper(uri, proxy, webSocketListener);
87+
return new WebSocketClientWrapper(uri, proxy, webSocketListener, tlsVersion); // ← use field
8688
}
8789

8890
public synchronized ScheduledExecutorService getTimers() {

src/test/java/com/pusher/client/EndToEndTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public WebSocketClientWrapper answer(final InvocationOnMock invocation) throws T
111111
final URI uri = (URI) invocation.getArguments()[0];
112112
final Proxy proxy = (Proxy) invocation.getArguments()[1];
113113
final WebSocketListener webSocketListener = (WebSocketListener) invocation.getArguments()[2];
114-
testWebsocket = new TestWebSocketClientWrapper(uri, proxy, webSocketListener);
114+
testWebsocket = new TestWebSocketClientWrapper(uri, proxy, webSocketListener, "TLSv1.3");
115115
return testWebsocket;
116116
}
117117
}

src/test/java/com/pusher/client/TestWebSocketClientWrapper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
public class TestWebSocketClientWrapper extends WebSocketClientWrapper {
1919

20-
private final List<String> messagesSent = new ArrayList<String>();
20+
private final List<String> messagesSent = new ArrayList<>();
2121
private boolean connectCalled = false;
2222

23-
public TestWebSocketClientWrapper(final URI uri, final Proxy proxy, final WebSocketListener webSocketListener)
23+
public TestWebSocketClientWrapper(final URI uri, final Proxy proxy, final WebSocketListener webSocketListener, String tlsVersion)
2424
throws SSLException {
25-
super(uri, proxy, webSocketListener);
25+
super(uri, proxy, webSocketListener, tlsVersion);
2626
}
2727

2828
void assertConnectCalled() {

src/test/java/com/pusher/client/connection/websocket/WebSocketClientWrapperTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class WebSocketClientWrapperTest {
3131

3232
@Before
3333
public void setUp() throws URISyntaxException, SSLException {
34-
wrapper = new WebSocketClientWrapper(new URI("http://www.test.com"), mockProxy, mockListener);
34+
wrapper = new WebSocketClientWrapper(new URI("http://www.test.com"), mockProxy, mockListener,"TLSV13");
3535
}
3636

3737
@Test

0 commit comments

Comments
 (0)