@Override public int onDataRead(final ChannelHandlerContext context, final int streamId, final ByteBuf data, final int padding, final boolean endOfStream) throws Http2Exception { log.trace("Received data from APNs gateway on stream {}: {}", streamId, data.toString(StandardCharsets.UTF_8)); final int bytesProcessed = data.readableBytes() + padding; if (endOfStream) { final Http2Headers headers = ApnsClientHandler.this.headersByStreamId.remove(streamId); final String authenticationToken = ApnsClientHandler.this.authenticationTokensByStreamId.remove(streamId); final ApnsPushNotification pushNotification = ApnsClientHandler.this.pushNotificationsByStreamId.remove(streamId); final HttpResponseStatus status = HttpResponseStatus.parseLine(headers.status()); final String responseBody = data.toString(StandardCharsets.UTF_8); if (HttpResponseStatus.INTERNAL_SERVER_ERROR.equals(status)) { ApnsClientHandler.this.apnsClient.handleServerError(pushNotification, responseBody); } else { final ErrorResponse errorResponse = gson.fromJson(responseBody, ErrorResponse.class); if (ApnsClient.EXPIRED_AUTH_TOKEN_REASON.equals(errorResponse.getReason())) { try { ApnsClientHandler.this.apnsClient.getAuthenticationTokenSupplierForTopic(pushNotification.getTopic()).invalidateToken(authenticationToken); } catch (final NoKeyForTopicException e) { // This should only happen if somebody de-registered the topic after a notification was sent log.warn("Authentication token expired, but no key registered for topic {}", pushNotification.getTopic()); } } ApnsClientHandler.this.apnsClient.handlePushNotificationResponse( new SimplePushNotificationResponse<>(pushNotification, HttpResponseStatus.OK.equals(status), errorResponse.getReason(), errorResponse.getTimestamp())); } } else { log.error("Gateway sent a DATA frame that was not the end of a stream."); } return bytesProcessed; }
authenticationToken = this.apnsClient.getAuthenticationTokenSupplierForTopic(pushNotification.getTopic()).getToken(); headers.add(APNS_AUTHORIZATION_HEADER, "bearer " + authenticationToken); } else {