@Override public Object resolveParameterValue(Message message) { return message.getMetaData(); }
@Override public MetaData getMetaData() { return delegate.getMetaData(); }
@Override public boolean matches(Message<?> message) { return !(parameterType.isPrimitive() || (boolean) metaDataValue.get(REQUIRED_PROPERTY)) || ( message.getMetaData().containsKey(metaDataValue.get(META_DATA_VALUE_PROPERTY).toString()) && parameterType.isInstance(message.getMetaData().get(metaDataValue.get(META_DATA_VALUE_PROPERTY).toString())) ); } }
@Override public Object resolveParameterValue(Message<?> message) { return message.getMetaData().get(metaDataValue.get(META_DATA_VALUE_PROPERTY).toString()); }
/** * Serialize the meta data 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 meta data 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 meta data */ default <R> SerializedObject<R> serializeMetaData(Serializer serializer, Class<R> expectedRepresentation) { return serializer.serialize(getMetaData(), expectedRepresentation); } }
@Override public Map<String, ?> correlationDataFor(Message<?> message) { Map<String, Object> result = new HashMap<>(); result.put(correlationKey, message.getIdentifier()); result.put(traceKey, message.getMetaData().getOrDefault(traceKey, message.getIdentifier())); return result; }
@Override public Map<String, ?> correlationDataFor(Message<?> message) { if (headerNames.length == 0) { return Collections.emptyMap(); } Map<String, Object> data = new HashMap<>(); final MetaData metaData = message.getMetaData(); for (String headerName : headerNames) { if (metaData.containsKey(headerName)) { data.put(headerName, metaData.get(headerName)); } } return data; } }
@SuppressWarnings("unchecked") public <T> SerializedObject<T> serializeMetaData(Serializer serializer, Class<T> expectedRepresentation) { synchronized (metaDataGuard) { SerializedObject existingForm = serializedMetaData.get(serializer); if (existingForm == null) { SerializedObject<T> serialized = serializer.serialize(message.getMetaData(), expectedRepresentation); serializedMetaData.put(serializer, serialized); return serialized; } else { return serializer.getConverter().convert(existingForm, expectedRepresentation); } } } }
/** * 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()); } }
/** * 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); }
if (identifier != null) { return new GenericDomainEventMessage<>(getType(), getAggregateIdentifier(), getSequenceNumber(), getPayload(), getMetaData(), getIdentifier(), getTimestamp()); } else { return new LazyIdentifierDomainEventMessage<>(getType(), getSequenceNumber(), getPayload(), 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));
/** * 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); } }
resultMessage = new GenericResultMessage<>(result, ((Message) result).getMetaData()); } else { resultMessage = new GenericResultMessage<>(result);
resultMessage = (ResultMessage<R>) result; } else if(result instanceof Message) { resultMessage = new GenericResultMessage<>(result, ((Message) result).getMetaData()); } else { resultMessage = new GenericResultMessage<>(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 Object resolveParameterValue(Message message) { return message.getMetaData(); }
@Override public boolean matches(Message<?> message) { return !(parameterType.isPrimitive() || (boolean) metaDataValue.get(REQUIRED_PROPERTY)) || ( message.getMetaData().containsKey(metaDataValue.get(META_DATA_VALUE_PROPERTY).toString()) && parameterType.isInstance(message.getMetaData().get(metaDataValue.get(META_DATA_VALUE_PROPERTY).toString())) ); } }
@Override public Map<String, ?> correlationDataFor(Message<?> message) { Map<String, Object> result = new HashMap<>(); result.put(correlationKey, message.getIdentifier()); result.put(traceKey, message.getMetaData().getOrDefault(traceKey, message.getIdentifier())); return result; }