Repository: jchambers/pushy
Pull request #890 (2021-06-05 23:25:56)

Include the HTTP status code in response objects
As requested in #840, this includes the HTTP status code from the APNs server in push notification responses.


Patch:
diff --git a/pushy/src/main/java/com/eatthepath/pushy/apns/ApnsClientHandler.java b/pushy/src/main/java/com/eatthepath/pushy/apns/ApnsClientHandler.java
index 9ecef91a..ed493697 100644
--- a/pushy/src/main/java/com/eatthepath/pushy/apns/ApnsClientHandler.java
+++ b/pushy/src/main/java/com/eatthepath/pushy/apns/ApnsClientHandler.java
@@ -325,7 +325,7 @@ class ApnsClientHandler extends Http2ConnectionHandler implements Http2FrameList
 
         if (HttpResponseStatus.OK.equals(status)) {
             responseFuture.complete(new SimplePushNotificationResponse<>(responseFuture.getPushNotification(),
-                    true, getApnsIdFromHeaders(headers), null, null));
+                    true, getApnsIdFromHeaders(headers), status.code(), null, null));
         } else {
             if (data != null) {
                 ErrorResponse errorResponse;
@@ -352,8 +352,8 @@ class ApnsClientHandler extends Http2ConnectionHandler implements Http2FrameList
         final HttpResponseStatus status = HttpResponseStatus.parseLine(headers.status());
 
         responseFuture.complete(new SimplePushNotificationResponse<>(responseFuture.getPushNotification(),
-                HttpResponseStatus.OK.equals(status), getApnsIdFromHeaders(headers), errorResponse.getReason(),
-                errorResponse.getTimestamp()));
+                HttpResponseStatus.OK.equals(status), getApnsIdFromHeaders(headers), status.code(),
+                errorResponse.getReason(), errorResponse.getTimestamp()));
     }
 
     private static UUID getApnsIdFromHeaders(final Http2Headers headers) {
diff --git a/pushy/src/main/java/com/eatthepath/pushy/apns/PushNotificationResponse.java b/pushy/src/main/java/com/eatthepath/pushy/apns/PushNotificationResponse.java
index 55c7a736..ca915703 100644
--- a/pushy/src/main/java/com/eatthepath/pushy/apns/PushNotificationResponse.java
+++ b/pushy/src/main/java/com/eatthepath/pushy/apns/PushNotificationResponse.java
@@ -62,6 +62,15 @@ public interface PushNotificationResponse<T extends ApnsPushNotification> {
      */
     UUID getApnsId();
 
+    /**
+     * Returns the HTTP status code reported by the APNs server.
+     *
+     * @return the HTTP status code reported by the APNs server
+     *
+     * @since 0.15.0
+     */
+    int getStatusCode();
+
     /**
      * Returns the reason for rejection reported by the APNs gateway. If the notification was accepted, the rejection
      * reason will be {@code null}.
diff --git a/pushy/src/main/java/com/eatthepath/pushy/apns/SimplePushNotificationResponse.java b/pushy/src/main/java/com/eatthepath/pushy/apns/SimplePushNotificationResponse.java
index 357a792c..c8b3956e 100644
--- a/pushy/src/main/java/com/eatthepath/pushy/apns/SimplePushNotificationResponse.java
+++ b/pushy/src/main/java/com/eatthepath/pushy/apns/SimplePushNotificationResponse.java
@@ -37,13 +37,15 @@ class SimplePushNotificationResponse<T extends ApnsPushNotification> implements
     private final T pushNotification;
     private final boolean success;
     private final UUID apnsId;
+    private final int statusCode;
     private final String rejectionReason;
     private final Instant tokenExpirationTimestamp;
 
-    SimplePushNotificationResponse(final T pushNotification, final boolean success, final UUID apnsId, final String rejectionReason, final Instant tokenExpirationTimestamp) {
+    SimplePushNotificationResponse(final T pushNotification, final boolean success, final UUID apnsId, final int statusCode, final String rejectionReason, final Instant tokenExpirationTimestamp) {
         this.pushNotification = pushNotification;
         this.success = success;
         this.apnsId = apnsId;
+        this.statusCode = statusCode;
         this.rejectionReason = rejectionReason;
         this.tokenExpirationTimestamp = tokenExpirationTimestamp;
     }
@@ -63,6 +65,11 @@ class SimplePushNotificationResponse<T extends ApnsPushNotification> implements
         return this.apnsId;
     }
 
+    @Override
+    public int getStatusCode() {
+        return this.statusCode;
+    }
+
     @Override
     public String getRejectionReason() {
         return this.rejectionReason;


Test patch:
diff --git a/pushy/src/test/java/com/eatthepath/pushy/apns/ApnsClientTest.java b/pushy/src/test/java/com/eatthepath/pushy/apns/ApnsClientTest.java
index 9fc7c966..fd2d99d0 100644
--- a/pushy/src/test/java/com/eatthepath/pushy/apns/ApnsClientTest.java
+++ b/pushy/src/test/java/com/eatthepath/pushy/apns/ApnsClientTest.java
@@ -396,6 +396,7 @@ public class ApnsClientTest extends AbstractClientServerTest {
                     "Clients must send notifications that conform to the APNs protocol specification.");
 
             assertNotNull(response.getApnsId());
+            assertEquals(200, response.getStatusCode());
         } finally {
             client.close().get();
             server.shutdown().get();