Skip to content

Commit 15134ad

Browse files
committed
Support for JDK8u252+ and E2E tests for it. Pulled from #84
1 parent 4b96d76 commit 15134ad

File tree

4 files changed

+325
-76
lines changed

4 files changed

+325
-76
lines changed

pom.xml

Lines changed: 45 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,6 @@
1212
<description>A library for communicating with the Apple Push Gateway in HTTP/2.</description>
1313
<url>https://github.com/CleverTap/apns-http2</url>
1414

15-
<properties>
16-
<sonar.organization>clevertap</sonar.organization>
17-
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
18-
<sonar.projectKey>CleverTap_apns-http2</sonar.projectKey>
19-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20-
<maven.compiler.source>1.8</maven.compiler.source>
21-
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
22-
</properties>
23-
24-
<dependencyManagement>
25-
<dependencies>
26-
<dependency>
27-
<groupId>org.junit</groupId>
28-
<artifactId>junit-bom</artifactId>
29-
<version>5.8.1</version>
30-
<type>pom</type>
31-
<scope>import</scope>
32-
</dependency>
33-
</dependencies>
34-
</dependencyManagement>
35-
3615
<dependencies>
3716
<dependency>
3817
<groupId>com.fasterxml.jackson.core</groupId>
@@ -55,81 +34,74 @@
5534
<dependency>
5635
<groupId>com.squareup.okhttp3</groupId>
5736
<artifactId>okhttp</artifactId>
58-
<version>3.2.0</version>
37+
<version>4.8.1</version>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>com.squareup.okhttp3</groupId>
42+
<artifactId>mockwebserver</artifactId>
43+
<version>4.8.1</version>
44+
<scope>test</scope>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>com.squareup.okhttp3</groupId>
49+
<artifactId>okhttp-tls</artifactId>
50+
<version>4.8.1</version>
51+
<scope>test</scope>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>junit</groupId>
56+
<artifactId>junit</artifactId>
57+
<version>4.11</version>
58+
<scope>test</scope>
5959
</dependency>
6060
<dependency>
6161
<groupId>org.junit.jupiter</groupId>
6262
<artifactId>junit-jupiter</artifactId>
63+
<version>RELEASE</version>
6364
<scope>test</scope>
6465
</dependency>
6566
</dependencies>
6667

67-
<profiles>
68-
<profile>
69-
<id>jacoco</id>
70-
<build>
71-
<plugins>
72-
<plugin>
73-
<groupId>org.jacoco</groupId>
74-
<artifactId>jacoco-maven-plugin</artifactId>
75-
<version>0.8.4</version>
76-
<executions>
77-
<execution>
78-
<goals>
79-
<goal>prepare-agent</goal>
80-
</goals>
81-
</execution>
82-
<execution>
83-
<id>jacoco-report</id>
84-
<goals>
85-
<goal>report</goal>
86-
</goals>
87-
</execution>
88-
</executions>
89-
</plugin>
90-
</plugins>
91-
</build>
92-
</profile>
93-
</profiles>
94-
9568
<build>
9669
<plugins>
9770
<plugin>
9871
<groupId>org.apache.maven.plugins</groupId>
9972
<artifactId>maven-compiler-plugin</artifactId>
100-
<version>3.8.1</version>
101-
</plugin>
102-
<plugin>
103-
<artifactId>maven-surefire-plugin</artifactId>
104-
<version>2.22.2</version>
73+
<configuration>
74+
<source>1.8</source>
75+
<target>1.8</target>
76+
</configuration>
10577
</plugin>
10678
<plugin>
10779
<groupId>org.apache.maven.plugins</groupId>
10880
<artifactId>maven-source-plugin</artifactId>
10981
<version>2.1.2</version>
11082
<executions>
11183
<execution>
112-
<id>attach-sources</id>
113-
<goals>
114-
<goal>jar-no-fork</goal>
115-
</goals>
84+
<id>attach-sources</id>
85+
<goals>
86+
<goal>jar-no-fork</goal>
87+
</goals>
11688
</execution>
117-
</executions>
89+
</executions>
11890
</plugin>
11991
<plugin>
120-
<groupId>org.apache.maven.plugins</groupId>
121-
<artifactId>maven-javadoc-plugin</artifactId>
122-
<version>2.7</version>
123-
<executions>
124-
<execution>
125-
<id>attach-javadocs</id>
126-
<goals>
127-
<goal>jar</goal>
128-
</goals>
129-
</execution>
130-
</executions>
92+
<groupId>org.apache.maven.plugins</groupId>
93+
<artifactId>maven-javadoc-plugin</artifactId>
94+
<version>2.7</version>
95+
<executions>
96+
<execution>
97+
<id>attach-javadocs</id>
98+
<goals>
99+
<goal>jar</goal>
100+
</goals>
101+
</execution>
102+
</executions>
131103
</plugin>
132-
<!-- this plugin is for creating fat jar (jar with embedded dependencies). -->
104+
<!-- this plugin is for creating fat jar (jar with embedded dependencies). -->
133105
<plugin>
134106
<groupId>org.apache.maven.plugins</groupId>
135107
<artifactId>maven-assembly-plugin</artifactId>
@@ -181,4 +153,4 @@
181153
<url>https://opensource.org/licenses/BSD-3-Clause</url>
182154
</license>
183155
</licenses>
184-
</project>
156+
</project>

src/main/java/com/clevertap/apns/clients/SyncOkHttpApnsClient.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.security.cert.CertificateException;
4444
import java.security.cert.X509Certificate;
4545
import java.security.spec.InvalidKeySpecException;
46+
import java.util.Arrays;
4647
import java.util.UUID;
4748

4849
/**
@@ -178,11 +179,19 @@ public SyncOkHttpApnsClient(InputStream certificate, String password, boolean pr
178179

179180
final TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
180181
tmf.init((KeyStore) null);
181-
sslContext.init(keyManagers, tmf.getTrustManagers(), null);
182+
183+
// check if there is an existing TrustManager configured in the builder
184+
TrustManager[] trustManagers = (builder.getX509TrustManagerOrNull$okhttp() != null) ?
185+
new TrustManager[] {builder.getX509TrustManagerOrNull$okhttp()} : tmf.getTrustManagers();
186+
sslContext.init(keyManagers, trustManagers, null);
187+
188+
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
189+
throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
190+
}
182191

183192
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
184193

185-
builder.sslSocketFactory(sslSocketFactory);
194+
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustManagers[0]);
186195

187196
client = builder.build();
188197

@@ -342,4 +351,4 @@ protected NotificationResponse parseResponse(Response response) throws IOExcepti
342351

343352
return new NotificationResponse(error, statusCode, contentBody, null);
344353
}
345-
}
354+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.clevertap.apns;
2+
3+
import com.sun.net.httpserver.HttpExchange;
4+
import com.sun.net.httpserver.HttpHandler;
5+
import com.sun.net.httpserver.HttpServer;
6+
7+
import java.io.IOException;
8+
import java.io.OutputStream;
9+
import java.net.InetSocketAddress;
10+
import java.net.ServerSocket;
11+
import java.util.concurrent.ExecutorService;
12+
import java.util.concurrent.Executors;
13+
14+
public class LocalHttpServer {
15+
16+
private final ExecutorService executorService = Executors.newFixedThreadPool(10);
17+
HttpServer httpServer;
18+
int port;
19+
String LOCAL_HOST = "http://127.0.0.1";
20+
String CONTEXT = "/serveRequest";
21+
22+
public static int nextFreePort() {
23+
try {
24+
try (ServerSocket tempSocket = new ServerSocket(0)) {
25+
return tempSocket.getLocalPort();
26+
}
27+
} catch (IOException e) {
28+
return -1;
29+
}
30+
}
31+
32+
public int init() throws Exception {
33+
port = nextFreePort();
34+
baseConfig(port);
35+
return port;
36+
}
37+
38+
public String getUrl() {
39+
return LOCAL_HOST + ":" + port + CONTEXT;
40+
}
41+
42+
public void baseConfig(int port) throws Exception {
43+
httpServer = HttpServer.create(new InetSocketAddress(port), 0);
44+
httpServer.createContext(CONTEXT, new RequestHandler());
45+
httpServer.setExecutor(executorService); // creates a default executor
46+
httpServer.start();
47+
System.out.println("Local Http server created on port " + port);
48+
}
49+
50+
public void shutDownServer() {
51+
httpServer.stop(1);
52+
executorService.shutdown();
53+
}
54+
55+
static class RequestHandler implements HttpHandler {
56+
@Override
57+
public void handle(HttpExchange t) throws IOException {
58+
String response = "Request Executed";
59+
t.getResponseHeaders().set("Content-Type", "text/plain");
60+
t.sendResponseHeaders(200, response.length());
61+
OutputStream os = t.getResponseBody();
62+
os.write(response.getBytes());
63+
os.close();
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)