@Override public T getPayload() { return delegate.getPayload(); }
@Override @SuppressWarnings("unchecked") public <E> Stream<E> filterCandidates(T message, Stream<E> candidates) { Property routingProperty = getProperty(message.getPayloadType(), routingKey); if (routingProperty == null) { return Stream.empty(); } Object routingValue = routingProperty.getValue(message.getPayload()); return candidates.filter(candidate -> matchesInstance(candidate, routingValue)); }
private static Object invokeAnnotated(Message<?> command, Class<? extends Annotation> annotation) throws InvocationTargetException, IllegalAccessException { for (Method m : methodsOf(command.getPayloadType())) { if (AnnotationUtils.isAnnotationPresent(m, annotation)) { ensureAccessible(m); return m.invoke(command.getPayload()); } } for (Field f : fieldsOf(command.getPayloadType())) { if (AnnotationUtils.isAnnotationPresent(f, annotation)) { return getFieldValue(f, command.getPayload()); } } return null; }
@Override public Object resolveParameterValue(Message message) { return message.getPayload(); }
@SuppressWarnings("unchecked") public <T> SerializedObject<T> serializePayload(Serializer serializer, Class<T> expectedRepresentation) { synchronized (payloadGuard) { SerializedObject existingForm = serializedPayload.get(serializer); if (existingForm == null) { SerializedObject<T> serialized = serializer.serialize(message.getPayload(), expectedRepresentation); if (message.getPayload() == null) { // make sure the payload type is maintained serialized = new SimpleSerializedObject<>(serialized.getData(), serialized.getContentType(), serializer.typeForClass(message.getPayloadType())); } serializedPayload.put(serializer, serialized); return serialized; } else { return serializer.getConverter().convert(existingForm, expectedRepresentation); } } }
@Override public boolean matches(Object item) { return Message.class.isInstance(item) && payloadMatcher.matches(((Message) item).getPayload()); }
@Override @SuppressWarnings("unchecked") public <E> Stream<E> filterCandidates(T message, Stream<E> candidates) { Property routingProperty = getProperty(message.getPayloadType(), routingKey); if (routingProperty == null) { return Stream.empty(); } Object routingValue = routingProperty.getValue(message.getPayload()); return candidates.filter(candidate -> matchesInstance(candidate, routingValue)); }
@Override public boolean matches(Object item) { if (!List.class.isInstance(item)) { return false; } List<Object> payloads = new ArrayList<>(); for (Object listItem : (List) item) { if (Message.class.isInstance(listItem)) { payloads.add(((Message) listItem).getPayload()); } else { payloads.add(item); } } return matcher.matches(payloads); }
@Override @SuppressWarnings("unchecked") public <E> Stream<E> filterCandidates(T message, Stream<E> candidates) { Property routingProperty = getProperty(message.getPayloadType(), routingKey); if (routingProperty == null) { return Stream.empty(); } Object routingValue = routingProperty.getValue(message.getPayload()); return candidates.filter(candidate -> matchesInstance(candidate, routingValue)); }
/** * Serialize the payload of this message to the {@code expectedRepresentation} using given {@code serializer}. This * method <em>should</em> return the same SerializedObject instance when invoked multiple times using the same * serializer. * * @param serializer The serializer to serialize payload with * @param expectedRepresentation The type of data to serialize to * @param <R> The type of the serialized data * @return a SerializedObject containing the serialized representation of the message's payload */ default <R> SerializedObject<R> serializePayload(Serializer serializer, Class<R> expectedRepresentation) { return serializer.serialize(getPayload(), expectedRepresentation); }
@Override public BiFunction<Integer, T, T> handle(List<? extends T> messages) { return (index, message) -> { Validator validator = validatorFactory.getValidator(); Set<ConstraintViolation<Object>> violations = validateMessage(message.getPayload(), validator); if (violations != null && !violations.isEmpty()) { throw new JSR303ViolationException(violations); } return message; }; }
/** * Returns the given {@code result} as a {@link ResultMessage} instance. If {@code result} already implements {@link * ResultMessage}, it is returned as-is. If {@code result} implements {@link Message}, payload and meta data will be * used to construct new {@link GenericResultMessage}. Otherwise, the given {@code result} is wrapped into a {@link * GenericResultMessage} as its payload. * * @param result the command result to be wrapped as {@link ResultMessage} * @param <T> The type of the payload contained in returned Message * @return a Message containing given {@code result} as payload, or {@code result} if already * implements {@link ResultMessage} */ @SuppressWarnings("unchecked") public static <T> ResultMessage<T> asResultMessage(Object result) { if (ResultMessage.class.isInstance(result)) { return (ResultMessage<T>) result; } else if (Message.class.isInstance(result)) { Message<?> resultMessage = (Message<?>) result; return new GenericResultMessage<>((T) resultMessage.getPayload(), resultMessage.getMetaData()); } return new GenericResultMessage<>((T) result); }
@Override public TestExecutor<T> andGiven(List<?> domainEvents) { for (Object event : domainEvents) { Object payload = event; MetaData metaData = null; if (event instanceof Message) { payload = ((Message) event).getPayload(); metaData = ((Message) event).getMetaData(); } this.givenEvents.add(new GenericDomainEventMessage<>(aggregateType.getSimpleName(), aggregateIdentifier, sequenceNumber++, new GenericMessage<>(payload, metaData), deadlineManager.getCurrentDateTime())); } return this; }
/** * Apply a new event message to the aggregate and then publish this message to external systems. If the given {@code * payloadOrMessage} is an instance of a {@link Message} an event message is applied with the payload and metadata * of the given message, otherwise an event message is applied with given payload and empty metadata. * * @param payloadOrMessage defines the payload and optionally metadata to apply to the aggregate */ protected void applyMessageOrPayload(Object payloadOrMessage) { if (payloadOrMessage instanceof Message) { Message message = (Message) payloadOrMessage; apply(message.getPayload(), message.getMetaData()); } else if (payloadOrMessage != null) { apply(payloadOrMessage, MetaData.emptyInstance()); } }
if (identifier != null) { return new GenericDomainEventMessage<>(getType(), getAggregateIdentifier(), getSequenceNumber(), getPayload(), getMetaData(), getIdentifier(), getTimestamp()); } else { return new LazyIdentifierDomainEventMessage<>(getType(), getSequenceNumber(), getPayload(), MetaData.from(newMetaData)); if (identifier != null) { return new GenericDomainEventMessage<>(getType(), getAggregateIdentifier(), getSequenceNumber(), getPayload(), getMetaData(), getIdentifier(), getTimestamp()) .andMetaData(additionalMetaData); } else { return new LazyIdentifierDomainEventMessage<>(getType(), getSequenceNumber(), getPayload(), getMetaData().mergedWith(additionalMetaData));
/** * Returns the given {@code commandResult} as a {@link CommandResultMessage} instance. If {@code commandResult} * already implements {@link CommandResultMessage}, it is returned as-is. If {@code commandResult} implements {@link * Message}, payload and meta data will be used to construct new {@link GenericCommandResultMessage}. Otherwise, the * given {@code commandResult} is wrapped into a {@link GenericCommandResultMessage} as its payload. * * @param commandResult the command result to be wrapped as {@link CommandResultMessage} * @param <T> The type of the payload contained in returned Message * @return a Message containing given {@code commandResult} as payload, or {@code commandResult} if already * implements {@link CommandResultMessage} */ @SuppressWarnings("unchecked") public static <T> CommandResultMessage<T> asCommandResultMessage(Object commandResult) { if (CommandResultMessage.class.isInstance(commandResult)) { return (CommandResultMessage<T>) commandResult; } else if (ResultMessage.class.isInstance(commandResult)) { ResultMessage<T> resultMessage = (ResultMessage<T>) commandResult; if (resultMessage.isExceptional()) { Throwable cause = resultMessage.exceptionResult(); return new GenericCommandResultMessage<>(cause, resultMessage.getMetaData()); } return new GenericCommandResultMessage<>(resultMessage.getPayload(), resultMessage.getMetaData()); } else if (Message.class.isInstance(commandResult)) { Message<T> commandResultMessage = (Message<T>) commandResult; return new GenericCommandResultMessage<>(commandResultMessage.getPayload(), commandResultMessage.getMetaData()); } return new GenericCommandResultMessage<>((T) commandResult); }
/** * Creates a QueryResponseMessage for the given {@code result}. If result already implements QueryResponseMessage, * it is returned directly. Otherwise a new QueryResponseMessage is created with the result as payload. * * @param result The result of a Query, to be wrapped in a QueryResponseMessage * @param <R> The type of response expected * @return a QueryResponseMessage for the given {@code result}, or the result itself, if already a * QueryResponseMessage. */ @SuppressWarnings("unchecked") public static <R> QueryResponseMessage<R> asResponseMessage(Object result) { if (result instanceof QueryResponseMessage) { return (QueryResponseMessage<R>) result; } else if (result instanceof ResultMessage) { ResultMessage<R> resultMessage = (ResultMessage<R>) result; return new GenericQueryResponseMessage<>(resultMessage.getPayload(), resultMessage.getMetaData()); } else if (result instanceof Message) { Message<R> message = (Message<R>) result; return new GenericQueryResponseMessage<>(message.getPayload(), message.getMetaData()); } else { return new GenericQueryResponseMessage(result); } }
} else if (result instanceof Message) { Message<R> message = (Message<R>) result; return new GenericQueryResponseMessage<>(message.getPayload(), message.getMetaData()); } else { return new GenericQueryResponseMessage(declaredType, result);
@Override public BiFunction<Integer, T, T> handle(List<? extends T> messages) { return (index, message) -> { Validator validator = validatorFactory.getValidator(); Set<ConstraintViolation<Object>> violations = validateMessage(message.getPayload(), validator); if (violations != null && !violations.isEmpty()) { throw new JSR303ViolationException(violations); } return message; }; }
@Override public BiFunction<Integer, T, T> handle(List<? extends T> messages) { return (index, message) -> { Validator validator = validatorFactory.getValidator(); Set<ConstraintViolation<Object>> violations = validateMessage(message.getPayload(), validator); if (violations != null && !violations.isEmpty()) { throw new JSR303ViolationException(violations); } return message; }; }