5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005-2006 Novell, Inc. http://www.novell.com
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Runtime.Serialization;
32 using System.Xml.Schema;
35 namespace System.ServiceModel.Channels
37 public abstract class Message : IDisposable
42 protected Message () {
43 State = MessageState.Created;
46 public abstract MessageHeaders Headers { get; }
48 internal string BodyId {
49 get { return body_id; }
50 set { body_id = value; }
53 public virtual bool IsEmpty {
57 public virtual bool IsFault {
61 public abstract MessageProperties Properties { get; }
63 MessageState ___state;
64 public MessageState State {
65 get { return ___state; }
66 private set { ___state = value; }
69 public abstract MessageVersion Version { get; }
71 protected bool IsDisposed {
72 get { return disposed; }
79 State = MessageState.Closed;
83 public MessageBuffer CreateBufferedCopy (int maxBufferSize)
85 if (State != MessageState.Created)
86 throw new InvalidOperationException (String.Format ("The message is already at {0} state", State));
88 return OnCreateBufferedCopy (maxBufferSize);
90 State = MessageState.Copied;
94 void IDisposable.Dispose ()
99 public T GetBody<T> ()
101 return GetBody<T> (new DataContractSerializer (typeof (T)));
104 public T GetBody<T> (XmlObjectSerializer xmlFormatter)
106 return (T) xmlFormatter.ReadObject (GetReaderAtBodyContents ());
109 public string GetBodyAttribute (string localName, string ns)
111 return OnGetBodyAttribute (localName, ns);
114 public XmlDictionaryReader GetReaderAtBodyContents ()
116 return OnGetReaderAtBodyContents ();
119 public override string ToString ()
121 MessageState tempState = State;
124 StringWriter sw = new StringWriter ();
125 XmlWriterSettings settings = new XmlWriterSettings ();
126 settings.Indent = true;
127 settings.OmitXmlDeclaration = true;
128 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
131 return sw.ToString ();
138 void WriteXsiNil (XmlDictionaryWriter writer)
140 writer.WriteStartElement ("z", "anyType", Constants.MSSerialization);
141 writer.WriteAttributeString ("i", "nil", "http://www.w3.org/2001/XMLSchema-instance", "true");
142 writer.WriteEndElement ();
145 public void WriteBody (XmlDictionaryWriter writer)
147 if (Version.Envelope != EnvelopeVersion.None)
148 WriteStartBody (writer);
149 WriteBodyContents (writer);
150 if (Version.Envelope != EnvelopeVersion.None)
151 writer.WriteEndElement ();
154 public void WriteBody (XmlWriter writer)
156 WriteBody (XmlDictionaryWriter.CreateDictionaryWriter (writer));
159 public void WriteBodyContents (XmlDictionaryWriter writer)
162 OnWriteBodyContents (writer);
163 else if (Version.Envelope == EnvelopeVersion.None)
164 WriteXsiNil (writer);
165 State = MessageState.Written;
168 public void WriteMessage (XmlDictionaryWriter writer)
170 if (State != MessageState.Created)
171 throw new InvalidOperationException (String.Format ("The message is already at {0} state", State));
173 OnWriteMessage (writer);
176 public void WriteMessage (XmlWriter writer)
178 WriteMessage (XmlDictionaryWriter.CreateDictionaryWriter (writer));
181 public void WriteStartBody (XmlDictionaryWriter writer)
183 if (State != MessageState.Created)
184 throw new InvalidOperationException (String.Format ("The message is already at {0} state", State));
186 OnWriteStartBody (writer);
189 public void WriteStartBody (XmlWriter writer)
192 XmlDictionaryWriter.CreateDictionaryWriter (writer));
195 public void WriteStartEnvelope (XmlDictionaryWriter writer)
197 if (State != MessageState.Created)
198 throw new InvalidOperationException (String.Format ("The message is already at {0} state", State));
200 OnWriteStartEnvelope (writer);
204 protected virtual void OnBodyToString (
205 XmlDictionaryWriter writer)
207 throw new NotImplementedException ();
210 protected virtual void OnClose ()
214 [MonoTODO ("use maxBufferSize")]
215 protected virtual MessageBuffer OnCreateBufferedCopy (
218 var s = new XmlWriterSettings ();
219 s.OmitXmlDeclaration = true;
220 s.ConformanceLevel = ConformanceLevel.Auto;
221 StringWriter sw = new StringWriter ();
222 using (XmlDictionaryWriter w = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (sw, s)))
223 WriteBodyContents (w);
224 var headers = new MessageHeaders (Headers);
225 var props = new MessageProperties (Properties);
226 return new DefaultMessageBuffer (maxBufferSize, headers, props, new XmlReaderBodyWriter (sw.ToString ()), false);
229 protected virtual string OnGetBodyAttribute (
230 string localName, string ns)
232 // other than XmlReaderMessage it cannot return anything
236 protected virtual XmlDictionaryReader OnGetReaderAtBodyContents ()
238 var ws = new XmlWriterSettings ();
239 ws.ConformanceLevel = ConformanceLevel.Auto;
240 StringWriter sw = new StringWriter ();
241 using (XmlDictionaryWriter body = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (sw, ws))) {
242 WriteBodyContents (body);
245 var rs = new XmlReaderSettings ();
246 rs.ConformanceLevel = ConformanceLevel.Auto;
247 return XmlDictionaryReader.CreateDictionaryReader (XmlReader.Create (new StringReader (sw.ToString ()), rs));
250 protected abstract void OnWriteBodyContents (
251 XmlDictionaryWriter writer);
253 protected virtual void OnWriteMessage (
254 XmlDictionaryWriter writer)
256 if (Version.Envelope != EnvelopeVersion.None) {
257 WriteStartEnvelope (writer);
258 if (Headers.Count > 0) {
259 OnWriteStartHeaders (writer);
260 for (int i = 0, count = Headers.Count; i < count; i++)
261 Headers.WriteHeader (i, writer);
262 writer.WriteEndElement ();
266 if (Version.Envelope != EnvelopeVersion.None)
267 writer.WriteEndElement ();
270 protected virtual void OnWriteStartBody (
271 XmlDictionaryWriter writer)
273 writer.WriteStartElement ("s", "Body", Version.Envelope.Namespace);
275 writer.WriteAttributeString ("u", "Id", Constants.WsuNamespace, BodyId);
278 protected virtual void OnWriteStartEnvelope (
279 XmlDictionaryWriter writer)
281 writer.WriteStartElement ("s", "Envelope", Version.Envelope.Namespace);
282 if (Headers.Action != null && Version.Addressing.Namespace != MessageVersion.None.Addressing.Namespace)
283 writer.WriteXmlnsAttribute ("a", Version.Addressing.Namespace);
284 foreach (MessageHeaderInfo h in Headers)
286 writer.WriteXmlnsAttribute ("u", Constants.WsuNamespace);
291 protected virtual void OnWriteStartHeaders (
292 XmlDictionaryWriter writer)
294 writer.WriteStartElement ("s", "Header", Version.Envelope.Namespace);
297 #region factory methods
301 // 3) fault, action -> 10
302 // 4) version, fault -> 10
303 // 5) version, action -> EmptyMessage
304 // 6) action, body -> 12
305 // 7) action, xmlReader -> 8
306 // 8) action, reader -> 16
307 // 10) version, fault, action -> 20
308 // 11) version, action, body -> 14
309 // 12) action, body, formatter -> 14
310 // 13) version, action, body -> 14
311 // 14) version, action, body, formatter -> 20
312 // 15) version, action, xmlReader -> 16
313 // 16) version, action, reader -> 20
314 // 17) xmlReader, maxSizeOfHeaders, version -> 18
315 // 18) reader, maxSizeOfHeaders, version -> ForwardingMessage
316 // 19) action, bodyWriter -> 20
317 // 20) version, action, bodyWriter -> SimpleMessage
319 public static Message CreateMessage (MessageVersion version,
320 FaultCode code, string reason, string action)
322 MessageFault fault = MessageFault.CreateFault (code, reason);
323 return CreateMessage (version, fault, action);
326 public static Message CreateMessage (MessageVersion version,
327 FaultCode code, string reason, object detail,
330 MessageFault fault = MessageFault.CreateFault (
331 code, new FaultReason (reason), detail);
332 return CreateMessage (version, fault, action);
335 public static Message CreateMessage (MessageVersion version,
336 MessageFault fault, string action)
338 return new SimpleMessage (version, action,
339 new MessageFaultBodyWriter (fault, version), true);
342 public static Message CreateMessage (MessageVersion version,
343 string action, object body)
345 return body == null ?
346 CreateMessage (version, action) :
347 CreateMessage (version, action, body, new DataContractSerializer (body.GetType ()));
350 public static Message CreateMessage (MessageVersion version,
351 string action, object body, XmlObjectSerializer xmlFormatter)
353 return body == null ?
354 CreateMessage (version, action) :
357 new XmlObjectSerializerBodyWriter (body, xmlFormatter));
360 public static Message CreateMessage (MessageVersion version,
361 string action, XmlReader body)
363 return CreateMessage (version, action,
364 XmlDictionaryReader.CreateDictionaryReader (body));
367 public static Message CreateMessage (MessageVersion version,
368 string action, XmlDictionaryReader body)
370 return CreateMessage (version, action,
371 new XmlReaderBodyWriter (body));
374 public static Message CreateMessage (XmlReader envelopeReader,
375 int maxSizeOfHeaders, MessageVersion version)
377 return CreateMessage (
378 XmlDictionaryReader.CreateDictionaryReader (envelopeReader),
383 // Core implementations of CreateMessage.
385 public static Message CreateMessage (MessageVersion version,
386 string action, BodyWriter body)
389 throw new ArgumentNullException ("version");
391 throw new ArgumentNullException ("body");
392 return new SimpleMessage (version, action, body, false);
395 public static Message CreateMessage (MessageVersion version,
399 throw new ArgumentNullException ("version");
400 return new EmptyMessage (version, action);
403 public static Message CreateMessage (
404 XmlDictionaryReader envelopeReader,
405 int maxSizeOfHeaders,
406 MessageVersion version)
408 if (envelopeReader == null)
409 throw new ArgumentNullException ("envelopeReader");
411 throw new ArgumentNullException ("version");
412 return new XmlReaderMessage (version,
413 envelopeReader, maxSizeOfHeaders);