2

I am writing JUNIT for push notifications using auth tokens. Now, the Apple push API is based on HTTP2 and I want to use spring WebClient for that purpose.

private static WebClient webClient = WebClient
            .builder()
            .baseUrl("https://api.development.push.apple.com/3/device")
                .defaultHeader("apns-priority", "10")
                .defaultHeader("apns-expiration", "0")
                .build();

    class ApnsResponse {
        String reason;
        String timestamp;
    }

    @Test
    void test1() {
        Hooks.onOperatorDebug();
        String message = "{\"aps\":{\"alert\":\"hello\"}}";
        try {
        webClient
            .post()
                .uri("/6d268d12394f512dfa1212aa612c1822f44df4312116b5a52109f3a21d1")
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .header("authorization", "bearer "+authToken)
                .header("apns-topic", "com.demo.12316")
                .bodyValue(message)
                .retrieve()
                .bodyToMono(ApnsResponse.class)
                .block();
        } catch(Exception e) {
            System.out.println(e);
        }
    }

When I run the test case I get:

21:37:26.028 [reactor-http-nio-1] DEBUG io.netty.handler.ssl.SslHandler - [id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443] HANDSHAKEN: protocol:TLSv1.2 cipher suite:TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
21:37:26.030 [reactor-http-nio-1] DEBUG reactor.netty.resources.PooledConnectionProvider - [id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443] onStateChange(PooledConnection{channel=[id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443]}, [connected])
21:37:26.062 [reactor-http-nio-1] DEBUG reactor.netty.resources.PooledConnectionProvider - [id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443] onStateChange(GET{uri=/, connection=PooledConnection{channel=[id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443]}}, [configured])
21:37:26.070 [reactor-http-nio-1] DEBUG reactor.netty.http.client.HttpClientConnect - [id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443] Handler is being applied: {uri=https://api.development.push.apple.com/3/device/6d268d2d494f512dfa1212aa612c1822f44df4375e9116b5a52109f3a21d1, method=POST}
21:37:26.075 [reactor-http-nio-1] DEBUG reactor.netty.resources.PooledConnectionProvider - [id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443] onStateChange(POST{uri=/3/device/6d268d12394f512dfa1212aa612c1822f44df4312116b5a52109f3a21d1, connection=PooledConnection{channel=[id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443]}}, [request_prepared])
21:37:26.111 [reactor-http-nio-1] DEBUG org.springframework.core.codec.CharSequenceEncoder - [6304101a] Writing "{"aps":{"alert":"hello"}}"
21:37:26.183 [reactor-http-nio-1] DEBUG reactor.netty.resources.PooledConnectionProvider - [id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443] onStateChange(POST{uri=/3/device/6d268d12394f512dfa1212aa612c1822f44df4312116b5a52109f3a21d1, connection=PooledConnection{channel=[id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443]}}, [request_sent])
21:37:26.559 [reactor-http-nio-1] DEBUG reactor.netty.resources.PooledConnectionProvider - [id: 0xddac3403, L:/192.168.11.13:63108 ! R:api.development.push.apple.com/17.188.165.219:443] Channel closed, now 0 active connections and 0 inactive connections
21:37:26.569 [reactor-http-nio-1] DEBUG reactor.netty.resources.PooledConnectionProvider - [id: 0xddac3403, L:/192.168.11.13:63108 ! R:api.development.push.apple.com/17.188.165.219:443] onStateChange(POST{uri=/3/device/6d268d12394f512dfa1212aa612c1822f44df4312116b5a52109f3a21d1, connection=PooledConnection{channel=[id: 0xddac3403, L:/192.168.11.13:63108 ! R:api.development.push.apple.com/17.188.165.219:443]}}, [response_incomplete])
21:37:26.592 [reactor-http-nio-1] WARN reactor.netty.http.client.HttpClientConnect - [id: 0xddac3403, L:/192.168.11.13:63108 ! R:api.development.push.apple.com/17.188.165.219:443] The connection observed an error
reactor.netty.http.client.PrematureCloseException: Connection prematurely closed BEFORE response
reactor.core.Exceptions$ReactiveException: reactor.netty.http.client.PrematureCloseException: Connection prematurely closed BEFORE response

I was able to use WebClient for HTTP calls but in this case it just not working. I am using JAVA 1.8. I even tried adding ALPN in boot class path but still I get same error.

Does WebClient supports HTTP2? Or I have to start looking into OkHTTP2.

Meanwhile I am reading about clientConnector which is used while creating WebClient

quintin
  • 812
  • 1
  • 10
  • 35

0 Answers0