1 /*
2  * Copyright 2002-2017 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.simp.annotation.SubscriptionMethodReturnValueHandler;
18 
19 // import hunt.logging;
20 
21 // import hunt.framework.core.MethodParameter;
22 
23 // import hunt.stomp.Message;
24 // import hunt.stomp.MessageHeaders;
25 // import hunt.stomp.core.MessageSendingOperations;
26 // import hunt.stomp.handler.annotation.SendTo;
27 // import hunt.stomp.handler.invocation.HandlerMethodReturnValueHandler;
28 // 
29 // import hunt.stomp.simp.SimpMessageHeaderAccessor;
30 // import hunt.stomp.simp.SimpMessageType;
31 // import hunt.stomp.simp.SimpMessagingTemplate;
32 // import hunt.stomp.simp.annotation.SendToUser;
33 // import hunt.stomp.simp.annotation.SubscribeMapping;
34 // 
35 
36 
37 // /**
38 //  * {@code HandlerMethodReturnValueHandler} for replying directly to a
39 //  * subscription. It is supported on methods annotated with
40 //  * {@link hunt.stomp.simp.annotation.SubscribeMapping
41 //  * SubscribeMapping} such that the return value is treated as a response to be
42 //  * sent directly back on the session. This allows a client to implement
43 //  * a request-response pattern and use it for example to obtain some data upon
44 //  * initialization.
45 //  *
46 //  * <p>The value returned from the method is converted and turned into a
47 //  * {@link Message} that is then enriched with the sessionId, subscriptionId, and
48 //  * destination of the input message.
49 //  *
50 //  * <p><strong>Note:</strong> this default behavior for interpreting the return
51 //  * value from an {@code @SubscribeMapping} method can be overridden through use
52 //  * of the {@link SendTo} or {@link SendToUser} annotations in which case a
53 //  * message is prepared and sent to the broker instead.
54 //  *
55 //  * @author Rossen Stoyanchev
56 //  * @author Sebastien Deleuze
57 //  * @since 4.0
58 //  */
59 // class SubscriptionMethodReturnValueHandler : HandlerMethodReturnValueHandler {
60 
61 
62 
63 
64 // 	private final MessageSendingOperations!(string) messagingTemplate;
65 
66 	
67 // 	private MessageHeaderInitializer headerInitializer;
68 
69 
70 // 	/**
71 // 	 * Construct a new SubscriptionMethodReturnValueHandler.
72 // 	 * @param template a messaging template to send messages to,
73 // 	 * most likely the "clientOutboundChannel" (must not be {@code null})
74 // 	 */
75 // 	this(MessageSendingOperations!(string) template) {
76 // 		assert(template, "messagingTemplate must not be null");
77 // 		this.messagingTemplate = template;
78 // 	}
79 
80 
81 // 	/**
82 // 	 * Configure a {@link MessageHeaderInitializer} to apply to the headers of all
83 // 	 * messages sent to the client outbound channel.
84 // 	 * <p>By default this property is not set.
85 // 	 */
86 // 	void setHeaderInitializer(MessageHeaderInitializer headerInitializer) {
87 // 		this.headerInitializer = headerInitializer;
88 // 	}
89 
90 // 	/**
91 // 	 * Return the configured header initializer.
92 // 	 */
93 	
94 // 	MessageHeaderInitializer getHeaderInitializer() {
95 // 		return this.headerInitializer;
96 // 	}
97 
98 
99 // 	override
100 // 	bool supportsReturnType(MethodParameter returnType) {
101 // 		return (returnType.hasMethodAnnotation(SubscribeMapping.class) &&
102 // 				!returnType.hasMethodAnnotation(SendTo.class) &&
103 // 				!returnType.hasMethodAnnotation(SendToUser.class));
104 // 	}
105 
106 // 	override
107 // 	void handleReturnValue(Object returnValue, MethodParameter returnType, MessageBase message)
108 // 			throws Exception {
109 
110 // 		if (returnValue is null) {
111 // 			return;
112 // 		}
113 
114 // 		MessageHeaders headers = message.getHeaders();
115 // 		string sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
116 // 		string subscriptionId = SimpMessageHeaderAccessor.getSubscriptionId(headers);
117 // 		string destination = SimpMessageHeaderAccessor.getDestination(headers);
118 
119 // 		if (subscriptionId is null) {
120 // 			throw new IllegalStateException("No simpSubscriptionId in " ~ message +
121 // 					" returned by: " ~ returnType.getMethod());
122 // 		}
123 // 		if (destination is null) {
124 // 			throw new IllegalStateException("No simpDestination in " ~ message +
125 // 					" returned by: " ~ returnType.getMethod());
126 // 		}
127 
128 // 		version(HUNT_DEBUG) {
129 // 			trace("Reply to @SubscribeMapping: " ~ returnValue);
130 // 		}
131 // 		MessageHeaders headersToSend = createHeaders(sessionId, subscriptionId, returnType);
132 // 		this.messagingTemplate.convertAndSend(destination, returnValue, headersToSend);
133 // 	}
134 
135 // 	private MessageHeaders createHeaders(string sessionId, string subscriptionId, MethodParameter returnType) {
136 // 		SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
137 // 		if (getHeaderInitializer() !is null) {
138 // 			getHeaderInitializer().initHeaders(accessor);
139 // 		}
140 // 		if (sessionId !is null) {
141 // 			accessor.setSessionId(sessionId);
142 // 		}
143 // 		accessor.setSubscriptionId(subscriptionId);
144 // 		accessor.setHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER, returnType);
145 // 		accessor.setLeaveMutable(true);
146 // 		return accessor.getMessageHeaders();
147 // 	}
148 
149 // }