List of usage examples for io.netty.handler.codec.http2 Http2Headers getInt
Integer getInt(K name);
From source file:com.turo.pushy.apns.AbstractMockApnsServerHandler.java
License:Open Source License
protected void verifyHeaders(final Http2Headers headers) throws RejectedNotificationException { final String topic; {/*from ww w . j a v a 2s. co m*/ final CharSequence topicSequence = headers.get(APNS_TOPIC_HEADER); topic = (topicSequence != null) ? topicSequence.toString() : null; } if (topic == null) { throw new RejectedNotificationException(ErrorReason.MISSING_TOPIC); } { final Integer priorityCode = headers.getInt(APNS_PRIORITY_HEADER); if (priorityCode != null) { try { DeliveryPriority.getFromCode(priorityCode); } catch (final IllegalArgumentException e) { throw new RejectedNotificationException(ErrorReason.BAD_PRIORITY); } } } { final CharSequence pathSequence = headers.get(Http2Headers.PseudoHeaderName.PATH.value()); if (pathSequence != null) { final String pathString = pathSequence.toString(); if (pathString.startsWith(APNS_PATH_PREFIX)) { final String tokenString = pathString.substring(APNS_PATH_PREFIX.length()); final Matcher tokenMatcher = DEVICE_TOKEN_PATTERN.matcher(tokenString); if (!tokenMatcher.matches()) { throw new RejectedNotificationException(ErrorReason.BAD_DEVICE_TOKEN); } final boolean deviceTokenRegisteredForTopic; final Date expirationTimestamp; { final Map<String, Date> expirationTimestampsByDeviceToken = this.deviceTokenExpirationsByTopic .get(topic); expirationTimestamp = expirationTimestampsByDeviceToken != null ? expirationTimestampsByDeviceToken.get(tokenString) : null; deviceTokenRegisteredForTopic = expirationTimestampsByDeviceToken != null && expirationTimestampsByDeviceToken.containsKey(tokenString); } if (expirationTimestamp != null) { throw new RejectedNotificationException(ErrorReason.UNREGISTERED, expirationTimestamp); } if (!deviceTokenRegisteredForTopic) { throw new RejectedNotificationException(ErrorReason.DEVICE_TOKEN_NOT_FOR_TOPIC); } } } else { throw new RejectedNotificationException(ErrorReason.BAD_PATH); } } }
From source file:com.turo.pushy.apns.server.ParsingMockApnsServerListenerAdapter.java
License:Open Source License
private static ApnsPushNotification parsePushNotification(final Http2Headers headers, final ByteBuf payload) { final UUID apnsId; {/*from w ww . j a v a 2 s. c om*/ final CharSequence apnsIdSequence = headers.get(APNS_ID_HEADER); UUID apnsIdFromHeaders; try { apnsIdFromHeaders = apnsIdSequence != null ? FastUUID.parseUUID(apnsIdSequence) : null; } catch (final IllegalArgumentException e) { log.error("Failed to parse `apns-id` header: {}", apnsIdSequence, e); apnsIdFromHeaders = null; } apnsId = apnsIdFromHeaders; } final String deviceToken; { final CharSequence pathSequence = headers.get(Http2Headers.PseudoHeaderName.PATH.value()); if (pathSequence != null) { final String pathString = pathSequence.toString(); deviceToken = pathString.startsWith(APNS_PATH_PREFIX) ? pathString.substring(APNS_PATH_PREFIX.length()) : null; } else { deviceToken = null; } } final String topic; { final CharSequence topicSequence = headers.get(APNS_TOPIC_HEADER); topic = topicSequence != null ? topicSequence.toString() : null; } final DeliveryPriority deliveryPriority; { final Integer priorityCode = headers.getInt(APNS_PRIORITY_HEADER); DeliveryPriority priorityFromCode; try { priorityFromCode = priorityCode != null ? DeliveryPriority.getFromCode(priorityCode) : null; } catch (final IllegalArgumentException e) { priorityFromCode = null; } deliveryPriority = priorityFromCode; } final Date expiration; { final Integer expirationTimestamp = headers.getInt(APNS_EXPIRATION_HEADER); expiration = expirationTimestamp != null ? new Date(expirationTimestamp * 1000) : null; } final String collapseId; { final CharSequence collapseIdSequence = headers.get(APNS_COLLAPSE_ID_HEADER); collapseId = collapseIdSequence != null ? collapseIdSequence.toString() : null; } // One challenge here is that we don't actually know that a push notification is valid, even if it's // accepted by the push notification handler (since we might be using a handler that blindly accepts // everything), so we want to use a lenient push notification implementation. return new LenientApnsPushNotification(deviceToken, topic, payload != null ? payload.toString(StandardCharsets.UTF_8) : null, expiration, deliveryPriority, collapseId, apnsId); }
From source file:com.turo.pushy.apns.server.ValidatingPushNotificationHandler.java
License:Open Source License
@Override public void handlePushNotification(final Http2Headers headers, final ByteBuf payload) throws RejectedNotificationException { try {//from ww w.j a va 2 s . c o m final CharSequence apnsIdSequence = headers.get(APNS_ID_HEADER); if (apnsIdSequence != null) { FastUUID.parseUUID(apnsIdSequence); } } catch (final IllegalArgumentException e) { throw new RejectedNotificationException(RejectionReason.BAD_MESSAGE_ID); } if (!HttpMethod.POST.asciiName().contentEquals(headers.get(Http2Headers.PseudoHeaderName.METHOD.value()))) { throw new RejectedNotificationException(RejectionReason.METHOD_NOT_ALLOWED); } final String topic; { final CharSequence topicSequence = headers.get(APNS_TOPIC_HEADER); if (topicSequence == null) { throw new RejectedNotificationException(RejectionReason.MISSING_TOPIC); } topic = topicSequence.toString(); } { final CharSequence collapseIdSequence = headers.get(APNS_COLLAPSE_ID_HEADER); if (collapseIdSequence != null && collapseIdSequence.toString() .getBytes(StandardCharsets.UTF_8).length > MAX_COLLAPSE_ID_SIZE) { throw new RejectedNotificationException(RejectionReason.BAD_COLLAPSE_ID); } } { final Integer priorityCode = headers.getInt(APNS_PRIORITY_HEADER); if (priorityCode != null) { try { DeliveryPriority.getFromCode(priorityCode); } catch (final IllegalArgumentException e) { throw new RejectedNotificationException(RejectionReason.BAD_PRIORITY); } } } { final CharSequence pathSequence = headers.get(Http2Headers.PseudoHeaderName.PATH.value()); if (pathSequence != null) { final String pathString = pathSequence.toString(); if (pathSequence.toString().equals(APNS_PATH_PREFIX)) { throw new RejectedNotificationException(RejectionReason.MISSING_DEVICE_TOKEN); } else if (pathString.startsWith(APNS_PATH_PREFIX)) { final String deviceToken = pathString.substring(APNS_PATH_PREFIX.length()); final Matcher tokenMatcher = DEVICE_TOKEN_PATTERN.matcher(deviceToken); if (!tokenMatcher.matches()) { throw new RejectedNotificationException(RejectionReason.BAD_DEVICE_TOKEN); } final Date expirationTimestamp = this.expirationTimestampsByDeviceToken.get(deviceToken); if (expirationTimestamp != null) { throw new UnregisteredDeviceTokenException(expirationTimestamp); } final Set<String> allowedDeviceTokensForTopic = this.deviceTokensByTopic.get(topic); if (allowedDeviceTokensForTopic == null || !allowedDeviceTokensForTopic.contains(deviceToken)) { throw new RejectedNotificationException(RejectionReason.DEVICE_TOKEN_NOT_FOR_TOPIC); } } else { throw new RejectedNotificationException(RejectionReason.BAD_PATH); } } else { throw new RejectedNotificationException(RejectionReason.BAD_PATH); } } this.verifyAuthentication(headers); if (payload == null || payload.readableBytes() == 0) { throw new RejectedNotificationException(RejectionReason.PAYLOAD_EMPTY); } if (payload.readableBytes() > MAX_PAYLOAD_SIZE) { throw new RejectedNotificationException(RejectionReason.PAYLOAD_TOO_LARGE); } }