1 /*
2  * Copyright 2002-2014 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.SimpleMessageConverter;
18 
19 import hunt.stomp.converter.MessageConverter;
20 
21 import hunt.stomp.Message;
22 import hunt.stomp.MessageHeaders;
23 import hunt.stomp.support.GenericMessage;
24 import hunt.stomp.support.MessageBuilder;
25 import hunt.stomp.support.MessageHeaderAccessor;
26 
27 import hunt.logging;
28 import hunt.Exceptions;
29 
30 /**
31  * A simple converter that simply unwraps the message payload as long as it matches the
32  * expected target class. Or reversely, simply wraps the payload in a message.
33  *
34  * <p>Note that this converter ignores any content type information that may be present in
35  * message headers and should not be used if payload conversion is actually required.
36  *
37  * @author Rossen Stoyanchev
38  * @since 4.0
39  */
40 class SimpleMessageConverter : MessageConverter {
41 
42 	override
43 	Object fromMessage(MessageBase message, TypeInfo targetClass) {
44 		TypeInfo payloadType = message.payloadType;
45 
46 		GenericMessage!(string) gm = cast(GenericMessage!(string))message;
47 		if(gm is null)
48 			return null;
49 
50 		string payload = gm.getPayload();
51 		// return (ClassUtils.isAssignableValue(targetClass, payload) ? payload : null);
52 		implementationMissing(false);
53 		return null;
54 	}
55 
56 	override
57 	MessageBase toMessage(Object payload, MessageHeaders headers) {
58 		if (headers !is null) {
59 			MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor!(MessageHeaderAccessor)(headers);
60 			if (accessor !is null && accessor.isMutable()) {
61 				return MessageHelper.createMessage!(Object)(payload, accessor.getMessageHeaders());
62 			}
63 		}
64 		return MessageHelper.withPayload(payload).copyHeaders(headers).build();
65 	}
66 
67 }