1 /* 2 * Copyright 2002-2018 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.support.ChannelInterceptor; 18 19 import hunt.stomp.Message; 20 import hunt.stomp.MessageChannel; 21 22 /** 23 * Interface for interceptors that are able to view and/or modify the 24 * {@link Message Messages} being sent-to and/or received-from a 25 * {@link MessageChannel}. 26 * 27 * @author Mark Fisher 28 * @author Rossen Stoyanchev 29 * @since 4.0 30 * @see Message 31 * @see MessageChannel 32 */ 33 abstract class ChannelInterceptor { 34 35 /** 36 * Invoked before the Message is actually sent to the channel. 37 * This allows for modification of the Message if necessary. 38 * If this method returns {@code null} then the actual 39 * send invocation will not occur. 40 */ 41 42 MessageBase preSend(MessageBase message, MessageChannel channel) { 43 return message; 44 } 45 46 /** 47 * Invoked immediately after the send invocation. The 48 * value argument represents the return value of that invocation. 49 */ 50 void postSend(MessageBase message, MessageChannel channel, bool sent) { 51 } 52 53 /** 54 * Invoked after the completion of a send regardless of any exception that 55 * have been raised thus allowing for proper resource cleanup. 56 * <p>Note that this will be invoked only if {@link #preSend} successfully 57 * completed and returned a Message, i.e. it did not return {@code null}. 58 * @since 4.1 59 */ 60 void afterSendCompletion( 61 MessageBase message, MessageChannel channel, bool sent, Exception ex) { 62 } 63 64 /** 65 * Invoked as soon as receive is called and before a Message is 66 * actually retrieved. If the return value is 'false', then no 67 * Message will be retrieved. This only applies to PollableChannels. 68 */ 69 bool preReceive(MessageChannel channel) { 70 return true; 71 } 72 73 /** 74 * Invoked immediately after a Message has been retrieved but before 75 * it is returned to the caller. The Message may be modified if 76 * necessary; {@code null} aborts further interceptor invocations. 77 * This only applies to PollableChannels. 78 */ 79 80 MessageBase postReceive(MessageBase message, MessageChannel channel) { 81 return message; 82 } 83 84 /** 85 * Invoked after the completion of a receive regardless of any exception that 86 * have been raised thus allowing for proper resource cleanup. 87 * <p>Note that this will be invoked only if {@link #preReceive} successfully 88 * completed and returned {@code true}. 89 * @since 4.1 90 */ 91 void afterReceiveCompletion(MessageBase message, MessageChannel channel, 92 Exception ex) { 93 } 94 95 }