Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for JDK8u252+ and E2E tests for it #89

Merged
merged 13 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 34 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,23 @@
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.2.0</version>
<version>4.8.1</version>
mohdabbas378 marked this conversation as resolved.
Show resolved Hide resolved
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<version>4.8.1</version>
mohdabbas378 marked this conversation as resolved.
Show resolved Hide resolved
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp-tls</artifactId>
<version>4.8.1</version>
mohdabbas378 marked this conversation as resolved.
Show resolved Hide resolved
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down Expand Up @@ -109,27 +124,27 @@
<version>2.1.2</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- this plugin is for creating fat jar (jar with embedded dependencies). -->
<!-- this plugin is for creating fat jar (jar with embedded dependencies). -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
Expand Down Expand Up @@ -181,4 +196,4 @@
<url>https://opensource.org/licenses/BSD-3-Clause</url>
</license>
</licenses>
</project>
</project>
15 changes: 12 additions & 3 deletions src/main/java/com/clevertap/apns/clients/SyncOkHttpApnsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.security.spec.InvalidKeySpecException;
import java.util.Arrays;
import java.util.UUID;

/**
Expand Down Expand Up @@ -178,11 +179,19 @@ public SyncOkHttpApnsClient(InputStream certificate, String password, boolean pr

final TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init((KeyStore) null);
sslContext.init(keyManagers, tmf.getTrustManagers(), null);

// check if there is an existing TrustManager configured in the builder
TrustManager[] trustManagers = (builder.getX509TrustManagerOrNull$okhttp() != null) ?
new TrustManager[] {builder.getX509TrustManagerOrNull$okhttp()} : tmf.getTrustManagers();
sslContext.init(keyManagers, trustManagers, null);

if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
judepereira marked this conversation as resolved.
Show resolved Hide resolved
throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
mohdabbas378 marked this conversation as resolved.
Show resolved Hide resolved
}

final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

builder.sslSocketFactory(sslSocketFactory);
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustManagers[0]);

client = builder.build();

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

return new NotificationResponse(error, statusCode, contentBody, null);
}
}
}
66 changes: 66 additions & 0 deletions src/test/java/com/clevertap/apns/LocalHttpServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.clevertap.apns;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class LocalHttpServer {

private final ExecutorService executorService = Executors.newFixedThreadPool(10);
HttpServer httpServer;
int port;
String LOCAL_HOST = "http://127.0.0.1";
String CONTEXT = "/serveRequest";

public static int nextFreePort() {
try {
try (ServerSocket tempSocket = new ServerSocket(0)) {
return tempSocket.getLocalPort();
}
} catch (IOException e) {
return -1;
}
}

public int init() throws Exception {
port = nextFreePort();
baseConfig(port);
return port;
}

public String getUrl() {
return LOCAL_HOST + ":" + port + CONTEXT;
}

public void baseConfig(int port) throws Exception {
httpServer = HttpServer.create(new InetSocketAddress(port), 0);
httpServer.createContext(CONTEXT, new RequestHandler());
httpServer.setExecutor(executorService); // creates a default executor
httpServer.start();
System.out.println("Local Http server created on port " + port);
}

public void shutDownServer() {
httpServer.stop(1);
executorService.shutdown();
}

static class RequestHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
String response = "Request Executed";
t.getResponseHeaders().set("Content-Type", "text/plain");
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}
Loading