1 /*
2  * Copyright 2002-2016 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 module hunt.stomp.converter.CompositeMessageConverter;
18 
19 import hunt.stomp.converter.MessageConverter;
20 import hunt.stomp.converter.SmartMessageConverter;
21 import hunt.stomp.Message;
22 import hunt.stomp.MessageHeaders;
23 
24 import hunt.collection;
25 import std.conv;
26 
27 
28 /**
29  * A {@link MessageConverter} that delegates to a list of registered converters
30  * to be invoked until one of them returns a non-null result.
31  *
32  * <p>As of 4.2.1, this composite converter implements {@link SmartMessageConverter}
33  * in order to support the delegation of conversion hints.
34  *
35  * @author Rossen Stoyanchev
36  * @author Juergen Hoeller
37  * @since 4.0
38  */
39 class CompositeMessageConverter : SmartMessageConverter {
40 
41 	private MessageConverter[] converters;
42 
43 
44 	/**
45 	 * Create an instance with the given converters.
46 	 */
47 	this(MessageConverter[] converters) {
48 		assert(converters !is null, "Converters must not be empty");
49 		this.converters = converters;
50 	}
51 
52 
53 	override	
54 	Object fromMessage(MessageBase message, TypeInfo targetClass) {
55 		foreach (MessageConverter converter ; getConverters()) {
56 			Object result = converter.fromMessage(message, targetClass);
57 			if (result !is null) {
58 				return result;
59 			}
60 		}
61 		return null;
62 	}
63 
64 	override
65 	Object fromMessage(MessageBase message, TypeInfo targetClass, TypeInfo conversionHint) {
66 		foreach (MessageConverter converter ; getConverters()) {
67 			auto smc = cast(SmartMessageConverter) converter;
68 
69 			Object result = (smc is null ? converter.fromMessage(message, targetClass) :
70 				smc.fromMessage(message, targetClass, conversionHint));
71 			if (result !is null) {
72 				return result;
73 			}
74 		}
75 		return null;
76 	}
77 
78 	override
79 	MessageBase toMessage(Object payload, MessageHeaders headers) {
80 		foreach (MessageConverter converter ; getConverters()) {
81 			MessageBase result = converter.toMessage(payload, headers);
82 			if (result !is null) {
83 				return result;
84 			}
85 		}
86 		return null;
87 	}
88 
89 	override
90 	MessageBase toMessage(Object payload, MessageHeaders headers, TypeInfo conversionHint) {
91 		foreach (MessageConverter converter ; getConverters()) {
92 			auto smc = cast(SmartMessageConverter) converter;
93 			MessageBase result = (smc !is null ? smc.toMessage(payload, headers, conversionHint) :
94 					converter.toMessage(payload, headers));
95 			if (result !is null) {
96 				return result;
97 			}
98 		}
99 		return null;
100 	}
101 
102 
103 	/**
104 	 * Return the underlying list of delegate converters.
105 	 */
106 	MessageConverter[] getConverters() {
107 		return this.converters;
108 	}
109 
110 	override
111 	string toString() {
112 		return "CompositeMessageConverter[converters=" ~ to!string(getConverters()) ~ "]";
113 	}
114 
115 }