Skip to content

Commit 809d139

Browse files
committed
GH-8983: Add MongoDB DocumentToMessageHistoryConverter
Fixes: #8983 Since collections in MongoDB cannot be deserialized to custom types anymore, add a new `DocumentToMessageHistoryConverter` to convert the `Document` with `components` into a `MessageHistory` instance back **Auto-cherry-pick to `6.2.x`**
1 parent 2731e94 commit 809d139

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

Diff for: spring-integration-mongodb/src/main/java/org/springframework/integration/mongodb/store/MongoDbMessageStore.java

+45-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.integration.mongodb.store;
1818

19+
import java.lang.reflect.Constructor;
1920
import java.util.ArrayList;
2021
import java.util.Arrays;
2122
import java.util.Collection;
@@ -25,7 +26,6 @@
2526
import java.util.List;
2627
import java.util.Map;
2728
import java.util.Map.Entry;
28-
import java.util.Properties;
2929
import java.util.UUID;
3030
import java.util.stream.Collectors;
3131
import java.util.stream.Stream;
@@ -36,6 +36,7 @@
3636
import org.bson.conversions.Bson;
3737
import org.bson.types.Binary;
3838

39+
import org.springframework.beans.BeanUtils;
3940
import org.springframework.beans.BeansException;
4041
import org.springframework.beans.DirectFieldAccessor;
4142
import org.springframework.beans.factory.BeanClassLoaderAware;
@@ -342,7 +343,7 @@ public void removeMessagesFromGroup(Object groupId, Collection<Message<?>> messa
342343
ids.clear();
343344
}
344345
}
345-
if (ids.size() > 0) {
346+
if (!ids.isEmpty()) {
346347
bulkRemove(groupId, ids);
347348
}
348349
updateGroup(groupId, lastModifiedUpdate());
@@ -573,6 +574,7 @@ void setCustomConverters(Object... customConverters) {
573574
public void afterPropertiesSet() {
574575
List<Object> converters = new ArrayList<>();
575576
converters.add(new MessageHistoryToDocumentConverter());
577+
converters.add(new DocumentToMessageHistoryConverter());
576578
converters.add(new DocumentToGenericMessageConverter());
577579
converters.add(new DocumentToMutableMessageConverter());
578580
DocumentToErrorMessageConverter docToErrorMessageConverter = new DocumentToErrorMessageConverter();
@@ -720,30 +722,55 @@ private Object extractPayload(Bson source) {
720722

721723

722724
@WritingConverter
723-
private static class MessageHistoryToDocumentConverter implements Converter<MessageHistory, Document> {
725+
private static final class MessageHistoryToDocumentConverter implements Converter<MessageHistory, Document> {
724726

725727
MessageHistoryToDocumentConverter() {
726728
}
727729

728730
@Override
729731
public Document convert(MessageHistory source) {
730-
BasicDBList dbList = new BasicDBList();
731-
for (Properties properties : source) {
732-
Document historyProperty = new Document()
733-
.append(MessageHistory.NAME_PROPERTY, properties.getProperty(MessageHistory.NAME_PROPERTY))
734-
.append(MessageHistory.TYPE_PROPERTY, properties.getProperty(MessageHistory.TYPE_PROPERTY))
735-
.append(MessageHistory.TIMESTAMP_PROPERTY,
736-
properties.getProperty(MessageHistory.TIMESTAMP_PROPERTY));
737-
dbList.add(historyProperty);
738-
}
739-
return new Document("components", dbList)
732+
return new Document("components", source)
740733
.append("_class", MessageHistory.class.getName());
741734
}
742735

743736
}
744737

745738
@ReadingConverter
746-
private class DocumentToGenericMessageConverter implements Converter<Document, GenericMessage<?>> {
739+
private static final class DocumentToMessageHistoryConverter implements Converter<Document, MessageHistory> {
740+
741+
private static final Constructor<MessageHistory> MESSAGE_HISTORY_CONSTRUCTOR;
742+
743+
static {
744+
try {
745+
MESSAGE_HISTORY_CONSTRUCTOR = MessageHistory.class.getDeclaredConstructor(List.class);
746+
}
747+
catch (NoSuchMethodException ex) {
748+
throw new IllegalStateException(ex);
749+
}
750+
}
751+
752+
DocumentToMessageHistoryConverter() {
753+
}
754+
755+
@Override
756+
@SuppressWarnings("unchecked")
757+
public MessageHistory convert(Document source) {
758+
List<Document> components = (List<Document>) source.get("components");
759+
List<MessageHistory.Entry> historyEntries = new ArrayList<>(components.size());
760+
for (Document component : components) {
761+
MessageHistory.Entry entry = new MessageHistory.Entry();
762+
for (Entry<String, Object> componentEntry : component.entrySet()) {
763+
entry.setProperty(componentEntry.getKey(), componentEntry.getValue().toString());
764+
}
765+
historyEntries.add(entry);
766+
}
767+
return BeanUtils.instantiateClass(MESSAGE_HISTORY_CONSTRUCTOR, historyEntries);
768+
}
769+
770+
}
771+
772+
@ReadingConverter
773+
private final class DocumentToGenericMessageConverter implements Converter<Document, GenericMessage<?>> {
747774

748775
DocumentToGenericMessageConverter() {
749776
}
@@ -783,7 +810,7 @@ public MutableMessage<?> convert(Document source) {
783810
}
784811

785812
@ReadingConverter
786-
private class DocumentToAdviceMessageConverter implements Converter<Document, AdviceMessage<?>> {
813+
private final class DocumentToAdviceMessageConverter implements Converter<Document, AdviceMessage<?>> {
787814

788815
DocumentToAdviceMessageConverter() {
789816
}
@@ -820,7 +847,7 @@ public AdviceMessage<?> convert(Document source) {
820847
}
821848

822849
@ReadingConverter
823-
private class DocumentToErrorMessageConverter implements Converter<Document, ErrorMessage> {
850+
private final class DocumentToErrorMessageConverter implements Converter<Document, ErrorMessage> {
824851

825852
private final AllowListDeserializingConverter deserializingConverter = new AllowListDeserializingConverter();
826853

@@ -843,7 +870,7 @@ public ErrorMessage convert(Document source) {
843870
}
844871

845872
@WritingConverter
846-
private static class ThrowableToBytesConverter implements Converter<Throwable, byte[]> {
873+
private static final class ThrowableToBytesConverter implements Converter<Throwable, byte[]> {
847874

848875
private final Converter<Object, byte[]> serializingConverter = new SerializingConverter();
849876

0 commit comments

Comments
 (0)