TL;DR Java 9 HttpClient does not allow custom Authorization header unless you resort to a hack.
Read more about my blog post on CleverTap’s blog.
Director of Engineering, CleverTap; founder of LetsTuneup (FbStart backed); obsessed with driving web scale stacks + R&D
TL;DR Java 9 HttpClient does not allow custom Authorization header unless you resort to a hack.
Read more about my blog post on CleverTap’s blog.
At CleverTap, we had the need to mock the new HTTP/2 Apple push gateway API. After trying to do this in Java, and then in Python, I succeeded in getting it to work in Go.
Go out of all languages! To me, Go has a completely strange syntax, compared to Java and C.
This mock server is hosted here, on Github.
HTTP/2 is still very much new to Java, and as such, there are just two libraries who support it – Jetty (from 9.3), and Netty (in alpha). If you’re going the Jetty way (as I have), you’ll need to add their ALPN library to your boot classpath.
Note: Jetty 9.3.x requires the use of Java 8.
A full library for this is available here, on GitHub.
Here’s a quick example:
package com.judepereira.jetty.apns.http2; | |
import org.eclipse.jetty.client.HttpClient; | |
import org.eclipse.jetty.client.api.ContentResponse; | |
import org.eclipse.jetty.client.api.Request; | |
import org.eclipse.jetty.client.util.StringContentProvider; | |
import org.eclipse.jetty.http2.client.HTTP2Client; | |
import org.eclipse.jetty.http2.client.http.HttpClientTransportOverHTTP2; | |
import org.eclipse.jetty.util.ssl.SslContextFactory; | |
import java.io.FileInputStream; | |
import java.security.KeyStore; | |
public class Main { | |
public static void main(String[] args) throws Exception { | |
HTTP2Client http2Client = new HTTP2Client(); | |
http2Client.start(); | |
KeyStore ks = KeyStore.getInstance("PKCS12"); | |
// Ensure that the password is the same as the one used later in setKeyStorePassword() | |
ks.load(new FileInputStream("MyProductionOrDevelopmentCertificate.p12"), "".toCharArray()); | |
SslContextFactory ssl = new SslContextFactory(true); | |
ssl.setKeyStore(ks); | |
ssl.setKeyStorePassword(""); | |
HttpClient client = new HttpClient(new HttpClientTransportOverHTTP2(http2Client), ssl); | |
client.start(); | |
// Change the API endpoint to api.development.push.apple.com if you're using a development certificate | |
Request req = client.POST("https://api.push.apple.com") | |
// Update your :path "/3/device/<your token>" | |
.path("/3/device/b2482deaf55521b2ccd755d5817a39784cc0044e24s3523a4708c2fa08983bdf") | |
.content(new StringContentProvider("{ \"aps\" : { \"alert\" : \"Hello\" } }")); | |
ContentResponse response = req.send(); | |
System.out.println("response code: " + response.getStatus()); | |
// The response body is empty for successful requests | |
System.out.println("response body: " + response.getContentAsString()); | |
} | |
} |