2009-05-21 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / RabbitMQ.Client / src / client / content / BasicMessageBuilder.cs
1 // This source code is dual-licensed under the Apache License, version
2 // 2.0, and the Mozilla Public License, version 1.1.
3 //
4 // The APL v2.0:
5 //
6 //---------------------------------------------------------------------------
7 //   Copyright (C) 2007-2009 LShift Ltd., Cohesive Financial
8 //   Technologies LLC., and Rabbit Technologies Ltd.
9 //
10 //   Licensed under the Apache License, Version 2.0 (the "License");
11 //   you may not use this file except in compliance with the License.
12 //   You may obtain a copy of the License at
13 //
14 //       http://www.apache.org/licenses/LICENSE-2.0
15 //
16 //   Unless required by applicable law or agreed to in writing, software
17 //   distributed under the License is distributed on an "AS IS" BASIS,
18 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 //   See the License for the specific language governing permissions and
20 //   limitations under the License.
21 //---------------------------------------------------------------------------
22 //
23 // The MPL v1.1:
24 //
25 //---------------------------------------------------------------------------
26 //   The contents of this file are subject to the Mozilla Public License
27 //   Version 1.1 (the "License"); you may not use this file except in
28 //   compliance with the License. You may obtain a copy of the License at
29 //   http://www.rabbitmq.com/mpl.html
30 //
31 //   Software distributed under the License is distributed on an "AS IS"
32 //   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
33 //   License for the specific language governing rights and limitations
34 //   under the License.
35 //
36 //   The Original Code is The RabbitMQ .NET Client.
37 //
38 //   The Initial Developers of the Original Code are LShift Ltd,
39 //   Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd.
40 //
41 //   Portions created before 22-Nov-2008 00:00:00 GMT by LShift Ltd,
42 //   Cohesive Financial Technologies LLC, or Rabbit Technologies Ltd
43 //   are Copyright (C) 2007-2008 LShift Ltd, Cohesive Financial
44 //   Technologies LLC, and Rabbit Technologies Ltd.
45 //
46 //   Portions created by LShift Ltd are Copyright (C) 2007-2009 LShift
47 //   Ltd. Portions created by Cohesive Financial Technologies LLC are
48 //   Copyright (C) 2007-2009 Cohesive Financial Technologies
49 //   LLC. Portions created by Rabbit Technologies Ltd are Copyright
50 //   (C) 2007-2009 Rabbit Technologies Ltd.
51 //
52 //   All Rights Reserved.
53 //
54 //   Contributor(s): ______________________________________.
55 //
56 //---------------------------------------------------------------------------
57 using System;
58 using System.IO;
59 using System.Collections;
60
61 using RabbitMQ.Client;
62 using RabbitMQ.Util;
63
64 namespace RabbitMQ.Client.Content {
65     ///<summary>Framework for constructing various types of AMQP
66     ///Basic-class application messages.</summary>
67     public class BasicMessageBuilder: IMessageBuilder {
68         ///<summary>By default, new instances of BasicMessageBuilder and its
69         ///subclasses will have this much initial buffer
70         ///space.</summary>
71         public const int DefaultAccumulatorSize = 1024;
72
73         protected IBasicProperties m_properties;
74         protected MemoryStream m_accumulator;
75
76         protected NetworkBinaryWriter m_writer = null;
77
78         ///<summary>Retrieve this instance's NetworkBinaryWriter writing to BodyStream.</summary>
79         ///<remarks>
80         /// If no NetworkBinaryWriter instance exists, one is created,
81         /// pointing at the beginning of the accumulator. If one
82         /// already exists, the existing instance is returned. The
83         /// instance is not reset.
84         ///</remarks>
85         public NetworkBinaryWriter Writer {
86             get {
87                 if (m_writer == null) {
88                     m_writer = new NetworkBinaryWriter(m_accumulator);
89                 }
90                 return m_writer;
91             }
92         }
93
94         ///<summary>Construct an instance ready for writing.</summary>
95         ///<remarks>
96         /// The DefaultAccumulatorSize is used for the initial accumulator buffer size.
97         ///</remarks>
98         public BasicMessageBuilder(IModel model): this(model, DefaultAccumulatorSize) {}
99
100         ///<summary>Construct an instance ready for writing.</summary>
101         public BasicMessageBuilder(IModel model, int initialAccumulatorSize) {
102             m_properties = model.CreateBasicProperties();
103             m_accumulator = new MemoryStream(initialAccumulatorSize);
104
105             string contentType = GetDefaultContentType();
106             if (contentType != null) {
107                 Properties.ContentType = contentType;
108             }
109         }
110
111         ///<summary>Retrieve the IBasicProperties associated with this instance.</summary>
112         public IBasicProperties Properties {
113             get {
114                 return m_properties;
115             }
116         }
117
118         ///<summary>Implement IMessageBuilder.Headers</summary>
119         public IDictionary Headers {
120             get {
121                 if (Properties.Headers == null) {
122                     Properties.Headers = new Hashtable();
123                 }
124                 return Properties.Headers;
125             }
126         }
127
128         ///<summary>Implement IMessageBuilder.BodyStream</summary>
129         public Stream BodyStream {
130             get {
131                 return m_accumulator;
132             }
133         }
134
135         ///<summary>Implement
136         ///IMessageBuilder.GetDefaultContentType(). Returns null;
137         ///overridden in subclasses.</summary>
138         public virtual string GetDefaultContentType() {
139             return null;
140         }
141
142         ///<summary>Implement IMessageBuilder.RawWrite</summary>
143         public IMessageBuilder RawWrite(byte b) {
144             BodyStream.WriteByte(b);
145             return this;
146         }
147
148         ///<summary>Implement IMessageBuilder.RawWrite</summary>
149         public IMessageBuilder RawWrite(byte[] bytes) {
150             return RawWrite(bytes, 0, bytes.Length);
151         }
152
153         ///<summary>Implement IMessageBuilder.RawWrite</summary>
154         public IMessageBuilder RawWrite(byte[] bytes, int offset, int length) {
155             BodyStream.Write(bytes, offset, length);
156             return this;
157         }
158
159         ///<summary>Implement IMessageBuilder.GetContentHeader</summary>
160         public virtual IContentHeader GetContentHeader() {
161             return m_properties;
162         }
163
164         ///<summary>Implement IMessageBuilder.GetContentBody</summary>
165         public virtual byte[] GetContentBody() {
166             return m_accumulator.ToArray();
167         }
168     }
169 }