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