@Test void testNoTypeSupplied() throws Exception { ObjectNode jsonObject = JsonNodeFactory.instance.objectNode(); jsonObject.put("Message", "Hello World!"); String payload = jsonObject.toString(); assertThatThrownBy( () -> new NotificationRequestConverter(new StringMessageConverter()) .fromMessage(MessageBuilder.withPayload(payload).build(), String.class)) .isInstanceOf(MessageConversionException.class) .hasMessageContaining( "does not contain a Type attribute"); }
@Override public Object fromMessage(Message<?> message, Class<?> targetClass) { Assert.notNull(message, "message must not be null"); Assert.notNull(targetClass, "target class must not be null"); JsonNode jsonNode; try { jsonNode = this.jsonMapper.readTree(message.getPayload().toString()); } catch (Exception e) { throw new MessageConversionException("Could not read JSON", e); } if (!jsonNode.has("Type")) { throw new MessageConversionException("Payload: '" + message.getPayload() + "' does not contain a Type attribute", null); } if (!"Notification".equals(jsonNode.get("Type").asText())) { throw new MessageConversionException( "Payload: '" + message.getPayload() + "' is not a valid notification", null); } if (!jsonNode.has("Message")) { throw new MessageConversionException( "Payload: '" + message.getPayload() + "' does not contain a message", null); } String messagePayload = jsonNode.get("Message").asText(); GenericMessage<String> genericMessage = new GenericMessage<>(messagePayload, getMessageAttributesAsMessageHeaders(jsonNode.path("MessageAttributes"))); return new NotificationRequest(jsonNode.path("Subject").asText(), this.payloadConverter.fromMessage(genericMessage, targetClass)); }
NotificationRequestConverter implements MessageConverter { @Override public Object fromMessage(Message<?> message, Class<?> targetClass) { Assert.notNull(message, "message must not be null"); Assert.notNull(targetClass, "target class must not be null"); JsonNode jsonNode; try { jsonNode = this.jsonMapper.readTree(message.getPayload().toString()); } catch (Exception e) { throw new MessageConversionException("Could not read JSON", e); } if (!jsonNode.has("Type")) { throw new MessageConversionException("Payload: '" + message.getPayload() + "' does not contain a Type attribute", null); } if (!"Notification".equals(jsonNode.get("Type").asText())) { throw new MessageConversionException( "Payload: '" + message.getPayload() + "' is not a valid notification", null); } if (!jsonNode.has("Message")) { throw new MessageConversionException( "Payload: '" + message.getPayload() + "' does not contain a message", null); } String messagePayload = jsonNode.get("Message").asText(); GenericMessage<String> genericMessage = new GenericMessage<>(messagePayload, getMessageAttributesAsMessageHeaders(jsonNode.path("MessageAttributes"))); return new NotificationRequest(jsonNode.path("Subject").asText(), this.payloadConverter.fromMessage(genericMessage, targetClass)); } }
NotificationRequestConverter implements MessageConverter { @Override public Object fromMessage(Message<?> message, Class<?> targetClass) { Assert.notNull(message, "message must not be null"); Assert.notNull(targetClass, "target class must not be null"); JsonNode jsonNode; try { jsonNode = this.jsonMapper.readTree(message.getPayload().toString()); } catch (Exception e) { throw new MessageConversionException("Could not read JSON", e); } if (!jsonNode.has("Type")) { throw new MessageConversionException("Payload: '" + message.getPayload() + "' does not contain a Type attribute", null); } if (!"Notification".equals(jsonNode.get("Type").asText())) { throw new MessageConversionException( "Payload: '" + message.getPayload() + "' is not a valid notification", null); } if (!jsonNode.has("Message")) { throw new MessageConversionException( "Payload: '" + message.getPayload() + "' does not contain a message", null); } String messagePayload = jsonNode.get("Message").asText(); GenericMessage<String> genericMessage = new GenericMessage<>(messagePayload, getMessageAttributesAsMessageHeaders(jsonNode.path("MessageAttributes"))); return new NotificationRequest(jsonNode.path("Subject").asText(), this.payloadConverter.fromMessage(genericMessage, targetClass)); } NotificationRequestConverter(MessageConverter payloadConverter); }
NotificationRequestConverter implements MessageConverter { @Override public Object fromMessage(Message<?> message, Class<?> targetClass) { Assert.notNull(message, "message must not be null"); Assert.notNull(targetClass, "target class must not be null"); JsonNode jsonNode; try { jsonNode = this.jsonMapper.readTree(message.getPayload().toString()); } catch (Exception e) { throw new MessageConversionException("Could not read JSON", e); } if (!jsonNode.has("Type")) { throw new MessageConversionException("Payload: '" + message.getPayload() + "' does not contain a Type attribute", null); } if (!"Notification".equals(jsonNode.get("Type").asText())) { throw new MessageConversionException( "Payload: '" + message.getPayload() + "' is not a valid notification", null); } if (!jsonNode.has("Message")) { throw new MessageConversionException( "Payload: '" + message.getPayload() + "' does not contain a message", null); } String messagePayload = jsonNode.get("Message").asText(); GenericMessage<String> genericMessage = new GenericMessage<>(messagePayload, getMessageAttributesAsMessageHeaders(jsonNode.path("MessageAttributes"))); return new NotificationRequest(jsonNode.path("Subject").asText(), this.payloadConverter.fromMessage(genericMessage, targetClass)); } NotificationRequestConverter(MessageConverter payloadConverter); @Override Object fromMessage(Message<?> message, Class<?> targetClass); @Override Message<?> toMessage(Object payload, MessageHeaders headers); }
NotificationRequestConverter implements MessageConverter { @Override public Object fromMessage(Message<?> message, Class<?> targetClass) { Assert.notNull(message, "message must not be null"); Assert.notNull(targetClass, "target class must not be null"); JsonNode jsonNode; try { jsonNode = this.jsonMapper.readTree(message.getPayload().toString()); } catch (Exception e) { throw new MessageConversionException("Could not read JSON", e); } if (!jsonNode.has("Type")) { throw new MessageConversionException("Payload: '" + message.getPayload() + "' does not contain a Type attribute", null); } if (!"Notification".equals(jsonNode.get("Type").asText())) { throw new MessageConversionException( "Payload: '" + message.getPayload() + "' is not a valid notification", null); } if (!jsonNode.has("Message")) { throw new MessageConversionException( "Payload: '" + message.getPayload() + "' does not contain a message", null); } String messagePayload = jsonNode.get("Message").asText(); GenericMessage<String> genericMessage = new GenericMessage<>(messagePayload, getMessageAttributesAsMessageHeaders(jsonNode.path("MessageAttributes"))); return new NotificationRequest(jsonNode.path("Subject").asText(), this.payloadConverter.fromMessage(genericMessage, targetClass)); } NotificationRequestConverter(MessageConverter payloadConverter); @Override Object fromMessage(Message<?> message, Class<?> targetClass); @Override Message<?> toMessage(Object payload, MessageHeaders headers);  }