1 /*
2  * hunt-amqp-client: AMQP Client Library for D Programming Language. Support for RabbitMQ and other AMQP Server.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 module hunt.amqp.client.AmqpMessage;
12 
13 import hunt.amqp.client.impl.AmqpMessageBuilderImpl;
14 import hunt.proton.message.Message;
15 import hunt.collection.List;
16 import hunt.collection.Map;
17 import hunt.amqp.client.AmqpMessageBuilder;
18 import std.json;
19 
20 /**
21  * Represents an AMQP message.
22  * <p>
23  * Reference about the different metadata can be found on
24  * <a href="http://docs.oasis-open.org/amqp/core/v1.0/amqp-core-messaging-v1.0.html#type-properties">AMQP message properties</a>.
25  * <p>
26  * Note that the body is retrieved using {@code body*} method depending on the expected type.
27  */
28 interface AmqpMessage {
29 
30   /**
31    * @return a builder to create an {@link AmqpMessage}.
32    */
33   static AmqpMessageBuilder create() {
34     return new AmqpMessageBuilderImpl();
35   }
36 
37   /**
38    * Creates a builder to create a new {@link AmqpMessage} copying the metadata from the passed message.
39    *
40    * @param existing an existing message, must not be {@code null}.
41    * @return a builder to create an {@link AmqpMessage}.
42    */
43   static AmqpMessageBuilder create(AmqpMessage existing) {
44     return new AmqpMessageBuilderImpl(existing);
45   }
46 
47   /**
48    * Creates a builder to create a new {@link AmqpMessage} copying the metadata from the passed (Proton) message.
49    *
50    * @param existing an existing (Proton) message, must not be {@code null}.
51    * @return a builder to create an {@link AmqpMessage}.
52    */
53  // @GenIgnore
54   static AmqpMessageBuilder create(Message existing) {
55     return new AmqpMessageBuilderImpl(existing);
56   }
57 
58   /**
59    * @return whether or not the message is durable.
60    * @see <a href="http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-header">AMQP specification</a>
61    */
62   bool isDurable();
63 
64   /**
65    * @return if {@code true}, then this message has not been acquired by any other link. If {@code false}, then this
66    * message MAY have previously been acquired by another link or links.
67    * @see <a href="http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-header">AMQP specification</a>
68    */
69   bool isFirstAcquirer();
70 
71   /**
72    * @return the relative message priority. Higher numbers indicate higher priority messages. Messages with higher
73    * priorities MAY be delivered before those with lower priorities.
74    * @see <a href="http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-header">AMQP specification</a>
75    */
76   int priority();
77 
78   /**
79    * @return the number of unsuccessful previous attempts to deliver this message. If this value is non-zero it can be
80    * taken as an indication that the delivery might be a duplicate. On first delivery, the value is zero. It is
81    * incremented upon an outcome being settled at the sender, according to rules defined for each outcome.
82    * @see <a href="http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-header">AMQP specification</a>
83    */
84   int deliveryCount();
85 
86   /**
87    * @return the duration in milliseconds for which the message is to be considered "live".
88    * @see <a href="http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-properties">AMQP specification</a>
89    */
90   long ttl();
91 
92   /**
93    * @return the message id
94    * @see <a href="http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-properties">AMQP specification</a>
95    */
96   string id();
97 
98   /**
99    * @return the message address, also named {@code to} field
100    * @see <a href="http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-properties">AMQP specification</a>
101    */
102   string address();
103 
104   /**
105    * @return The address of the node to send replies to, if any.
106    * @see <a href="http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-properties">AMQP specification</a>
107    */
108   string replyTo();
109 
110   /**
111    * @return The client-specific id that can be used to mark or identify messages between clients.
112    * @see <a href="http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-properties">AMQP specification</a>
113    */
114   string correlationId();
115 
116   /**
117    * @return whether the body is {@code null}. This method returns {@code true} is the message does not contain a body or
118    * if the message contain a {@code null} AMQP value as body.
119    */
120   bool isBodyNull();
121 
122   /**
123    * @return the boolean value contained in the body. The value must be passed as AMQP value.
124    */
125   bool bodyAsBoolean();
126 
127   /**
128    * @return the byte value contained in the body. The value must be passed as AMQP value.
129    */
130   byte bodyAsByte();
131 
132   /**
133    * @return the short value contained in the body. The value must be passed as AMQP value.
134    */
135   short bodyAsShort();
136 
137   /**
138    * @return the integer value contained in the body. The value must be passed as AMQP value.
139    */
140   int bodyAsInteger();
141 
142   /**
143    * @return the long value contained in the body. The value must be passed as AMQP value.
144    */
145   long bodyAsLong();
146 
147   /**
148    * @return the float value contained in the body. The value must be passed as AMQP value.
149    */
150   float bodyAsFloat();
151 
152   /**
153    * @return the double value contained in the body. The value must be passed as AMQP value.
154    */
155   double bodyAsDouble();
156 
157   /**
158    * @return the character value contained in the body. The value must be passed as AMQP value.
159    */
160   char bodyAsChar();
161 
162   /**
163    * @return the timestamp value contained in the body. The value must be passed as AMQP value.
164    */
165   //@GenIgnore(GenIgnore.PERMITTED_TYPE)
166   //Instant bodyAsTimestamp();
167 
168   /**
169    * @return the UUID value contained in the body. The value must be passed as AMQP value.
170    */
171   //@GenIgnore(GenIgnore.PERMITTED_TYPE)
172   //UUID bodyAsUUID();
173 
174   /**
175    * @return the bytes contained in the body. The value must be passed as AMQP data.
176    */
177   byte[] bodyAsBinary();
178 
179   /**
180    * @return the string value contained in the body. The value must be passed as AMQP value.
181    */
182   string bodyAsString();
183 
184   /**
185    * @return the symbol value contained in the body. The value must be passed as AMQP value.
186    */
187   string bodyAsSymbol();
188 
189   /**
190    * @return the list of values contained in the body. The value must be passed as AMQP value.
191    */
192   List!Object bodyAsList();
193 
194   /**
195    * @return the map contained in the body. The value must be passed as AMQP value.
196    */
197   //@GenIgnore
198   //<K, V> Map<K, V> bodyAsMap();
199 
200   /**
201    * @return the JSON object contained in the body. The value must be passed as AMQP data.
202    */
203   //JsonObject bodyAsJsonObject();
204 
205   /**
206    * @return the JSON array contained in the body. The value must be passed as AMQP data.
207    */
208   //JsonArray bodyAsJsonArray();
209 
210   string subject();
211 
212   string contentType();
213 
214   string contentEncoding();
215 
216   long expiryTime();
217 
218   long creationTime();
219 
220   string groupId();
221 
222   string replyToGroupId();
223 
224   long groupSequence();
225 
226   /**
227    * @return the message properties as JSON object.
228    */
229  // JSONValue applicationProperties();
230 
231  // @GenIgnore
232   Message unwrap();
233 
234   /**
235    * When receiving a message, and when auto-acknowledgement is disabled, this method is used to acknowledge
236    * the incoming message. It marks the message as delivered with the {@code accepted} status.
237    *
238    * @return the current {@link AmqpMessage} object
239    * @throws IllegalStateException is the current message is not a received message.
240    */
241   AmqpMessage accepted();
242 
243   /**
244    * When receiving a message, and when auto-acknowledgement is disabled, this method is used to acknowledge
245    * the incoming message as {@code rejected}.
246    *
247    * @return the current {@link AmqpMessage} object
248    * @throws IllegalStateException is the current message is not a received message.
249    */
250   AmqpMessage rejected();
251 
252   /**
253    * When receiving a message, and when auto-acknowledgement is disabled, this method is used to acknowledge
254    * the incoming message as {@code released}.
255    *
256    * @return the current {@link AmqpMessage} object
257    * @throws IllegalStateException is the current message is not a received message.
258    */
259   AmqpMessage released();
260 
261   /**
262    * When receiving a message,  and when auto-acknowledgement is disabled, this method is used to acknowledge
263    * the incoming message as {@code modified}.
264    *
265    * @param didItFail          pass {@code true} to increase the failed delivery count
266    * @param wasItDeliveredHere pass {@code true} to prevent the re-delivery of the message
267    * @return the current {@link AmqpMessage} object
268    * @throws IllegalStateException is the current message is not a received message.
269    */
270   AmqpMessage modified(bool didItFail, bool wasItDeliveredHere);
271 
272 
273   //TODO What type should we use for delivery annotations and message annotations
274 
275 }