Debian Codegraph @

proton-j/src/main/java/org/apache/qpid/proton/message/impl/MessageImpl.java

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 package org.apache.qpid.proton.message.impl;
23
24 import java.nio.ByteBuffer;
25 import java.util.Date;
26 import org.apache.qpid.proton.amqp.Binary;
27 import org.apache.qpid.proton.amqp.Symbol;
28 import org.apache.qpid.proton.amqp.UnsignedByte;
29 import org.apache.qpid.proton.amqp.UnsignedInteger;
30 import org.apache.qpid.proton.amqp.messaging.*;
31 import org.apache.qpid.proton.codec.*;
32 import org.apache.qpid.proton.message.*;
33
34 public class MessageImpl implements ProtonJMessage
35 {
36 private Header _header;
37 private DeliveryAnnotations _deliveryAnnotations;
38 private MessageAnnotations _messageAnnotations;
39 private Properties _properties;
40 private ApplicationProperties _applicationProperties;
41 private Section _body;
42 private Footer _footer;
43
44 private static class EncoderDecoderPair {
45 DecoderImpl decoder = new DecoderImpl();
46 EncoderImpl encoder = new EncoderImpl(decoder);
47 {
48 AMQPDefinedTypes.registerAllTypes(decoder, encoder);
49 }
50 }
51
52 private static final ThreadLocal<EncoderDecoderPair> tlsCodec = new ThreadLocal<EncoderDecoderPair>() {
53 @Override protected EncoderDecoderPair initialValue() {
54 return new EncoderDecoderPair();
55 }
56 };
57
58 /**
59 * Application code should use {@link org.apache.qpid.proton.message.Message.Factory#create()} instead.
60 */
61 public MessageImpl()
62 {
63 }
64
65 /**
66 * Application code should instead use
67 * {@link org.apache.qpid.proton.message.Message.Factory#create(Header, DeliveryAnnotations, MessageAnnotations, Properties, ApplicationProperties, Section, Footer)}
68 */
69 public MessageImpl(Header header, DeliveryAnnotations deliveryAnnotations, MessageAnnotations messageAnnotations,
70 Properties properties, ApplicationProperties applicationProperties, Section body, Footer footer)
71 {
72 _header = header;
73 _deliveryAnnotations = deliveryAnnotations;
74 _messageAnnotations = messageAnnotations;
75 _properties = properties;
76 _applicationProperties = applicationProperties;
77 _body = body;
78 _footer = footer;
79 }
80
81 @Override
82 public boolean isDurable()
83 {
84 return (_header == null || _header.getDurable() == null) ? false : _header.getDurable();
85 }
86
87
88 @Override
89 public long getDeliveryCount()
90 {
91 return (_header == null || _header.getDeliveryCount() == null) ? 0l : _header.getDeliveryCount().longValue();
92 }
93
94
95 @Override
96 public short getPriority()
97 {
98 return (_header == null || _header.getPriority() == null)
99 ? DEFAULT_PRIORITY
100 : _header.getPriority().shortValue();
101 }
102
103 @Override
104 public boolean isFirstAcquirer()
105 {
106 return (_header == null || _header.getFirstAcquirer() == null) ? false : _header.getFirstAcquirer();
107 }
108
109 @Override
110 public long getTtl()
111 {
112 return (_header == null || _header.getTtl() == null) ? 0l : _header.getTtl().longValue();
113 }
114
115 @Override
116 public void setDurable(boolean durable)
117 {
118 if (_header == null)
119 {
120 if (durable)
121 {
122 _header = new Header();
123 }
124 else
125 {
126 return;
127 }
128 }
129 _header.setDurable(durable);
130 }
131
132 @Override
133 public void setTtl(long ttl)
134 {
135
136 if (_header == null)
137 {
138 if (ttl != 0l)
139 {
140 _header = new Header();
141 }
142 else
143 {
144 return;
145 }
146 }
147 _header.setTtl(UnsignedInteger.valueOf(ttl));
148 }
149
150 @Override
151 public void setDeliveryCount(long deliveryCount)
152 {
153 if (_header == null)
154 {
155 if (deliveryCount == 0l)
156 {
157 return;
158 }
159 _header = new Header();
160 }
161 _header.setDeliveryCount(UnsignedInteger.valueOf(deliveryCount));
162 }
163
164
165 @Override
166 public void setFirstAcquirer(boolean firstAcquirer)
167 {
168
169 if (_header == null)
170 {
171 if (!firstAcquirer)
172 {
173 return;
174 }
175 _header = new Header();
176 }
177 _header.setFirstAcquirer(firstAcquirer);
178 }
179
180 @Override
181 public void setPriority(short priority)
182 {
183
184 if (_header == null)
185 {
186 if (priority == DEFAULT_PRIORITY)
187 {
188 return;
189 }
190 _header = new Header();
191 }
192 _header.setPriority(UnsignedByte.valueOf((byte) priority));
193 }
194
195 @Override
196 public Object getMessageId()
197 {
198 return _properties == null ? null : _properties.getMessageId();
199 }
200
201 @Override
202 public long getGroupSequence()
203 {
204 return (_properties == null || _properties.getGroupSequence() == null) ? 0l : _properties.getGroupSequence().intValue();
205 }
206
207 @Override
208 public String getReplyToGroupId()
209 {
210 return _properties == null ? null : _properties.getReplyToGroupId();
211 }
212
213 @Override
214 public long getCreationTime()
215 {
216 return (_properties == null || _properties.getCreationTime() == null) ? 0l : _properties.getCreationTime().getTime();
217 }
218
219 @Override
220 public String getAddress()
221 {
222 return _properties == null ? null : _properties.getTo();
223 }
224
225 @Override
226 public byte[] getUserId()
227 {
228 if(_properties == null || _properties.getUserId() == null)
229 {
230 return null;
231 }
232 else
233 {
234 final Binary userId = _properties.getUserId();
235 byte[] id = new byte[userId.getLength()];
236 System.arraycopy(userId.getArray(),userId.getArrayOffset(),id,0,userId.getLength());
237 return id;
238 }
239
240 }
241
242 @Override
243 public String getReplyTo()
244 {
245 return _properties == null ? null : _properties.getReplyTo();
246 }
247
248 @Override
249 public String getGroupId()
250 {
251 return _properties == null ? null : _properties.getGroupId();
252 }
253
254 @Override
255 public String getContentType()
256 {
257 return (_properties == null || _properties.getContentType() == null) ? null : _properties.getContentType().toString();
258 }
259
260 @Override
261 public long getExpiryTime()
262 {
263 return (_properties == null || _properties.getAbsoluteExpiryTime() == null) ? 0l : _properties.getAbsoluteExpiryTime().getTime();
264 }
265
266 @Override
267 public Object getCorrelationId()
268 {
269 return (_properties == null) ? null : _properties.getCorrelationId();
270 }
271
272 @Override
273 public String getContentEncoding()
274 {
275 return (_properties == null || _properties.getContentEncoding() == null) ? null : _properties.getContentEncoding().toString();
276 }
277
278 @Override
279 public String getSubject()
280 {
281 return _properties == null ? null : _properties.getSubject();
282 }
283
284 @Override
285 public void setGroupSequence(long groupSequence)
286 {
287 if(_properties == null)
288 {
289 if(groupSequence == 0l)
290 {
291 return;
292 }
293 else
294 {
295 _properties = new Properties();
296 }
297 }
298 _properties.setGroupSequence(UnsignedInteger.valueOf((int) groupSequence));
299 }
300
301 @Override
302 public void setUserId(byte[] userId)
303 {
304 if(userId == null)
305 {
306 if(_properties != null)
307 {
308 _properties.setUserId(null);
309 }
310
311 }
312 else
313 {
314 if(_properties == null)
315 {
316 _properties = new Properties();
317 }
318 byte[] id = new byte[userId.length];
319 System.arraycopy(userId, 0, id,0, userId.length);
320 _properties.setUserId(new Binary(id));
321 }
322 }
323
324 @Override
325 public void setCreationTime(long creationTime)
326 {
327 if(_properties == null)
328 {
329 if(creationTime == 0l)
330 {
331 return;
332 }
333 _properties = new Properties();
334
335 }
336 _properties.setCreationTime(new Date(creationTime));
337 }
338
339 @Override
340 public void setSubject(String subject)
341 {
342 if(_properties == null)
343 {
344 if(subject == null)
345 {
346 return;
347 }
348 _properties = new Properties();
349 }
350 _properties.setSubject(subject);
351 }
352
353 @Override
354 public void setGroupId(String groupId)
355 {
356 if(_properties == null)
357 {
358 if(groupId == null)
359 {
360 return;
361 }
362 _properties = new Properties();
363 }
364 _properties.setGroupId(groupId);
365 }
366
367 @Override
368 public void setAddress(String to)
369 {
370 if(_properties == null)
371 {
372 if(to == null)
373 {
374 return;
375 }
376 _properties = new Properties();
377 }
378 _properties.setTo(to);
379 }
380
381 @Override
382 public void setExpiryTime(long absoluteExpiryTime)
383 {
384 if(_properties == null)
385 {
386 if(absoluteExpiryTime == 0l)
387 {
388 return;
389 }
390 _properties = new Properties();
391
392 }
393 _properties.setAbsoluteExpiryTime(new Date(absoluteExpiryTime));
394 }
395
396 @Override
397 public void setReplyToGroupId(String replyToGroupId)
398 {
399 if(_properties == null)
400 {
401 if(replyToGroupId == null)
402 {
403 return;
404 }
405 _properties = new Properties();
406 }
407 _properties.setReplyToGroupId(replyToGroupId);
408 }
409
410 @Override
411 public void setContentEncoding(String contentEncoding)
412 {
413 if(_properties == null)
414 {
415 if(contentEncoding == null)
416 {
417 return;
418 }
419 _properties = new Properties();
420 }
421 _properties.setContentEncoding(Symbol.valueOf(contentEncoding));
422 }
423
424 @Override
425 public void setContentType(String contentType)
426 {
427 if(_properties == null)
428 {
429 if(contentType == null)
430 {
431 return;
432 }
433 _properties = new Properties();
434 }
435 _properties.setContentType(Symbol.valueOf(contentType));
436 }
437
438 @Override
439 public void setReplyTo(String replyTo)
440 {
441
442 if(_properties == null)
443 {
444 if(replyTo == null)
445 {
446 return;
447 }
448 _properties = new Properties();
449 }
450 _properties.setReplyTo(replyTo);
451 }
452
453 @Override
454 public void setCorrelationId(Object correlationId)
455 {
456
457 if(_properties == null)
458 {
459 if(correlationId == null)
460 {
461 return;
462 }
463 _properties = new Properties();
464 }
465 _properties.setCorrelationId(correlationId);
466 }
467
468 @Override
469 public void setMessageId(Object messageId)
470 {
471
472 if(_properties == null)
473 {
474 if(messageId == null)
475 {
476 return;
477 }
478 _properties = new Properties();
479 }
480 _properties.setMessageId(messageId);
481 }
482
483
484 @Override
485 public Header getHeader()
486 {
487 return _header;
488 }
489
490 @Override
491 public DeliveryAnnotations getDeliveryAnnotations()
492 {
493 return _deliveryAnnotations;
494 }
495
496 @Override
497 public MessageAnnotations getMessageAnnotations()
498 {
499 return _messageAnnotations;
500 }
501
502 @Override
503 public Properties getProperties()
504 {
505 return _properties;
506 }
507
508 @Override
509 public ApplicationProperties getApplicationProperties()
510 {
511 return _applicationProperties;
512 }
513
514 @Override
515 public Section getBody()
516 {
517 return _body;
518 }
519
520 @Override
521 public Footer getFooter()
522 {
523 return _footer;
524 }
525
526 @Override
527 public void setHeader(Header header)
528 {
529 _header = header;
530 }
531
532 @Override
533 public void setDeliveryAnnotations(DeliveryAnnotations deliveryAnnotations)
534 {
535 _deliveryAnnotations = deliveryAnnotations;
536 }
537
538 @Override
539 public void setMessageAnnotations(MessageAnnotations messageAnnotations)
540 {
541 _messageAnnotations = messageAnnotations;
542 }
543
544 @Override
545 public void setProperties(Properties properties)
546 {
547 _properties = properties;
548 }
549
550 @Override
551 public void setApplicationProperties(ApplicationProperties applicationProperties)
552 {
553 _applicationProperties = applicationProperties;
554 }
555
556 @Override
557 public void setBody(Section body)
558 {
559 _body = body;
560 }
561
562 @Override
563 public void setFooter(Footer footer)
564 {
565 _footer = footer;
566 }
567
568 @Override
569 public int decode(byte[] data, int offset, int length)
570 {
571 final ByteBuffer buffer = ByteBuffer.wrap(data, offset, length);
572 decode(buffer);
573
574 return length-buffer.remaining();
575 }
576
577 public void decode(ByteBuffer buffer)
578 {
579 decode(ReadableBuffer.ByteBufferReader.wrap(buffer));
580 }
581
582 public void decode(ReadableBuffer buffer)
583 {
584 DecoderImpl decoder = tlsCodec.get().decoder;
585 decoder.setBuffer(buffer);
586
587 _header = null;
588 _deliveryAnnotations = null;
589 _messageAnnotations = null;
590 _properties = null;
591 _applicationProperties = null;
592 _body = null;
593 _footer = null;
594 Section section = null;
595
596 if(buffer.hasRemaining())
597 {
598 section = (Section) decoder.readObject();
599 }
600 if(section instanceof Header)
601 {
602 _header = (Header) section;
603 if(buffer.hasRemaining())
604 {
605 section = (Section) decoder.readObject();
606 }
607 else
608 {
609 section = null;
610 }
611
612 }
613 if(section instanceof DeliveryAnnotations)
614 {
615 _deliveryAnnotations = (DeliveryAnnotations) section;
616
617 if(buffer.hasRemaining())
618 {
619 section = (Section) decoder.readObject();
620 }
621 else
622 {
623 section = null;
624 }
625
626 }
627 if(section instanceof MessageAnnotations)
628 {
629 _messageAnnotations = (MessageAnnotations) section;
630
631 if(buffer.hasRemaining())
632 {
633 section = (Section) decoder.readObject();
634 }
635 else
636 {
637 section = null;
638 }
639
640 }
641 if(section instanceof Properties)
642 {
643 _properties = (Properties) section;
644
645 if(buffer.hasRemaining())
646 {
647 section = (Section) decoder.readObject();
648 }
649 else
650 {
651 section = null;
652 }
653
654 }
655 if(section instanceof ApplicationProperties)
656 {
657 _applicationProperties = (ApplicationProperties) section;
658
659 if(buffer.hasRemaining())
660 {
661 section = (Section) decoder.readObject();
662 }
663 else
664 {
665 section = null;
666 }
667
668 }
669 if(section != null && !(section instanceof Footer))
670 {
671 _body = section;
672
673 if(buffer.hasRemaining())
674 {
675 section = (Section) decoder.readObject();
676 }
677 else
678 {
679 section = null;
680 }
681
682 }
683 if(section instanceof Footer)
684 {
685 _footer = (Footer) section;
686
687 }
688
689 decoder.setBuffer(null);
690 }
691
692 @Override
693 public int encode(byte[] data, int offset, int length)
694 {
695 ByteBuffer buffer = ByteBuffer.wrap(data, offset, length);
696 return encode(new WritableBuffer.ByteBufferWrapper(buffer));
697 }
698
699 @Override
700 public int encode2(byte[] data, int offset, int length)
701 {
702 ByteBuffer buffer = ByteBuffer.wrap(data, offset, length);
703 WritableBuffer.ByteBufferWrapper first = new WritableBuffer.ByteBufferWrapper(buffer);
704 DroppingWritableBuffer second = new DroppingWritableBuffer();
705 CompositeWritableBuffer composite = new CompositeWritableBuffer(first, second);
706 int start = composite.position();
707 encode(composite);
708 return composite.position() - start;
709 }
710
711 @Override
712 public int encode(WritableBuffer buffer)
713 {
714 int length = buffer.remaining();
715 EncoderImpl encoder = tlsCodec.get().encoder;
716 encoder.setByteBuffer(buffer);
717
718 if(getHeader() != null)
719 {
720 encoder.writeObject(getHeader());
721 }
722 if(getDeliveryAnnotations() != null)
723 {
724 encoder.writeObject(getDeliveryAnnotations());
725 }
726 if(getMessageAnnotations() != null)
727 {
728 encoder.writeObject(getMessageAnnotations());
729 }
730 if(getProperties() != null)
731 {
732 encoder.writeObject(getProperties());
733 }
734 if(getApplicationProperties() != null)
735 {
736 encoder.writeObject(getApplicationProperties());
737 }
738 if(getBody() != null)
739 {
740 encoder.writeObject(getBody());
741 }
742 if(getFooter() != null)
743 {
744 encoder.writeObject(getFooter());
745 }
746 encoder.setByteBuffer((WritableBuffer)null);
747
748 return length - buffer.remaining();
749 }
750
751 @Override
752 public void clear()
753 {
754 _body = null;
755 }
756
757 @Override
758 public MessageError getError()
759 {
760 return MessageError.OK;
761 }
762
763 public String toString()
764 {
765 StringBuilder sb = new StringBuilder();
766 sb.append("Message{");
767 if (_header != null) {
768 sb.append("header=");
769 sb.append(_header);
770 }
771 if (_properties != null) {
772 sb.append("properties=");
773 sb.append(_properties);
774 }
775 if (_messageAnnotations != null) {
776 sb.append("message_annotations=");
777 sb.append(_messageAnnotations);
778 }
779 if (_body != null) {
780 sb.append("body=");
781 sb.append(_body);
782 }
783 sb.append("}");
784 return sb.toString();
785 }
786
787 }