@Override public Date asDate() { if (!data.canConvertToLong()) { return null; } long seconds = data.asLong(); return new Date(seconds * 1000); }
Date getDateFromSeconds(Map<String, JsonNode> tree, String claimName) { JsonNode node = tree.get(claimName); if (node == null || node.isNull()) { return null; } if (!node.canConvertToLong()) { throw new JWTDecodeException(String.format("The claim '%s' contained a non-numeric date value.", claimName)); } final long ms = node.asLong() * 1000; return new Date(ms); }
/** * Test whether a numeric instance is a long * * <p>We use both a test on the instance type and Jackson's {@link * JsonNode#canConvertToLong()}. The first test is needed since the * latter method will also return true if the value is a decimal which * integral part fits into a long, and we don't want that.</p> * * @param node the node to test * @return true if both conditions are true */ private static boolean valueIsLong(final JsonNode node) { return NodeType.getNodeType(node) == NodeType.INTEGER && node.canConvertToLong(); } }
@Override public Object apply(JsonNode node) { if (node == null || node.isMissingNode() || node.isNull()) { return null; } if (node.isIntegralNumber()) { if (node.canConvertToLong()) { return node.asLong(); } else { return node.asDouble(); } } if (node.isFloatingPointNumber()) { return node.asDouble(); } final String s = node.asText(); final CharsetEncoder enc = StandardCharsets.UTF_8.newEncoder(); if (s != null && !enc.canEncode(s)) { // Some whacky characters are in this string (e.g. \uD900). These are problematic because they are decodeable // by new String(...) but will not encode into the same character. This dance here will replace these // characters with something more sane. return StringUtils.fromUtf8(StringUtils.toUtf8(s)); } else { return s; } } };
private static boolean valueIsLong(final JsonNode node) { if (!node.canConvertToLong()) return false; if (NodeType.getNodeType(node) == NodeType.INTEGER) return true; return node.decimalValue().remainder(BigDecimal.ONE) .compareTo(BigDecimal.ZERO) == 0; }
@Override public Date asDate() { if (!data.canConvertToLong()) { return null; } long seconds = data.asLong(); return new Date(seconds * 1000); }
Date getDateFromSeconds(Map<String, JsonNode> tree, String claimName) { JsonNode node = tree.get(claimName); if (node == null || node.isNull()) { return null; } if (!node.canConvertToLong()) { throw new JWTDecodeException(String.format("The claim '%s' contained a non-numeric date value.", claimName)); } final long ms = node.asLong() * 1000; return new Date(ms); }
@Override public WampMessage fromObjectArray(ArrayNode messageNode) throws WampError { if (messageNode.size() != 3 || !messageNode.get(1).canConvertToLong() || !messageNode.get(2).canConvertToLong()) throw new WampError(ApplicationError.INVALID_MESSAGE); long requestId = messageNode.get(1).asLong(); long subscriptionId = messageNode.get(2).asLong(); return new SubscribedMessage(requestId, subscriptionId); } }
@Override public WampMessage fromObjectArray(ArrayNode messageNode) throws WampError { if (messageNode.size() != 3 || !messageNode.get(1).canConvertToLong() || !messageNode.get(2).canConvertToLong()) throw new WampError(ApplicationError.INVALID_MESSAGE); long requestId = messageNode.get(1).asLong(); long subscriptionId = messageNode.get(2).asLong(); return new SubscribedMessage(requestId, subscriptionId); } }
@Override public WampMessage fromObjectArray(ArrayNode messageNode) throws WampError { if (messageNode.size() != 3 || !messageNode.get(1).canConvertToLong() || !messageNode.get(2).canConvertToLong()) throw new WampError(ApplicationError.INVALID_MESSAGE); long requestId = messageNode.get(1).asLong(); long subscriptionId = messageNode.get(2).asLong(); return new UnsubscribeMessage(requestId, subscriptionId); } }
@Override public WampMessage fromObjectArray(ArrayNode messageNode) throws WampError { if (messageNode.size() != 3 || !messageNode.get(1).canConvertToLong() || !messageNode.get(2).canConvertToLong()) throw new WampError(ApplicationError.INVALID_MESSAGE); long requestId = messageNode.get(1).asLong(); long subscriptionId = messageNode.get(2).asLong(); return new UnsubscribeMessage(requestId, subscriptionId); } }
@Override public WampMessage fromObjectArray(ArrayNode messageNode) throws WampError { if (messageNode.size() != 3 || !messageNode.get(1).canConvertToLong() || !messageNode.get(2).canConvertToLong()) throw new WampError(ApplicationError.INVALID_MESSAGE); long requestId = messageNode.get(1).asLong(); long registrationId = messageNode.get(2).asLong(); return new UnregisterMessage(requestId, registrationId); } }
@Override public WampMessage fromObjectArray(ArrayNode messageNode) throws WampError { if (messageNode.size() != 3 || !messageNode.get(1).canConvertToLong() || !messageNode.get(2).canConvertToLong()) throw new WampError(ApplicationError.INVALID_MESSAGE); long requestId = messageNode.get(1).asLong(); long subscriptionId = messageNode.get(2).asLong(); return new UnsubscribeMessage(requestId, subscriptionId); } }
@Override public WampMessage fromObjectArray(ArrayNode messageNode) throws WampError { if (messageNode.size() != 2 || !messageNode.get(1).canConvertToLong()) throw new WampError(ApplicationError.INVALID_MESSAGE); long requestId = messageNode.get(1).asLong(); return new UnsubscribedMessage(requestId); } }
@Override public WampMessage fromObjectArray(ArrayNode messageNode) throws WampError { if (messageNode.size() != 2 || !messageNode.get(1).canConvertToLong()) throw new WampError(ApplicationError.INVALID_MESSAGE); long requestId = messageNode.get(1).asLong(); return new UnsubscribedMessage(requestId); } }
@Override public WampMessage fromObjectArray(ArrayNode messageNode) throws WampError { if (messageNode.size() != 2 || !messageNode.get(1).canConvertToLong()) throw new WampError(ApplicationError.INVALID_MESSAGE); long requestId = messageNode.get(1).asLong(); return new UnregisteredMessage(requestId); } }
@Override public WampMessage fromObjectArray(ArrayNode messageNode) throws WampError { if (messageNode.size() != 2 || !messageNode.get(1).canConvertToLong()) throw new WampError(ApplicationError.INVALID_MESSAGE); long requestId = messageNode.get(1).asLong(); return new UnsubscribedMessage(requestId); } }
/** * Extracts long value from JsonNode if it is within bounds. * * <p>Throws {@link UnsupportedRowJsonException} if value is out of bounds. */ static ValueExtractor<Long> longValueExtractor() { return ValidatingValueExtractor.<Long>builder() .setExtractor(JsonNode::longValue) .setValidator(jsonNode -> jsonNode.isIntegralNumber() && jsonNode.canConvertToLong()) .build(); }
@Override public void validate(final ProcessingReport report, final MessageBundle bundle, final FullData data) throws ProcessingException { final JsonNode instance = data.getInstance().getNode(); if (!instance.canConvertToLong()) { report.warn(newMsg(data, bundle, "warn.format.int64.overflow") .put("key", "warn.format.int64.overflow") .putArgument("value", instance)); } } }
@Override public void validate(final ProcessingReport report, final MessageBundle bundle, final FullData data) throws ProcessingException { final JsonNode instance = data.getInstance().getNode(); if (!instance.canConvertToLong()) { report.warn(newMsg(data, bundle, "warn.format.int64.overflow") .put("key", "warn.format.int64.overflow") .putArgument("value", instance)); } } }