2 // TextMessageEncoder.cs
4 // Author: Atsushi Enomoto (atsushi@ximian.com)
6 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections.ObjectModel;
30 using System.ServiceModel;
34 namespace System.ServiceModel.Channels
36 internal class TextMessageEncoder : MessageEncoder
39 MessageVersion version;
41 public TextMessageEncoder (MessageVersion version, Encoding encoding)
43 this.version = version;
44 this.encoding = encoding;
47 internal Encoding Encoding {
48 get { return encoding; }
51 public override string ContentType {
52 get { return String.Concat (MediaType, "; charset=", encoding.WebName); }
55 public override string MediaType {
56 get { return version.Envelope == EnvelopeVersion.Soap12 ? "application/soap+xml" : "text/xml"; }
59 public override MessageVersion MessageVersion {
60 get { return version; }
64 public override Message ReadMessage (ArraySegment<byte> buffer,
65 BufferManager bufferManager, string contentType)
67 if (bufferManager == null)
68 throw new ArgumentNullException ("bufferManager");
69 var ret = Message.CreateMessage (
70 XmlDictionaryReader.CreateDictionaryReader (
71 XmlReader.Create (new StreamReader (
73 buffer.Array, buffer.Offset,
74 buffer.Count), encoding))),
75 // FIXME: supply max header size
78 FillActionContentType (ret, contentType);
82 public override Message ReadMessage (Stream stream,
83 int maxSizeOfHeaders, string contentType)
86 throw new ArgumentNullException ("stream");
87 var ret = Message.CreateMessage (
88 XmlDictionaryReader.CreateDictionaryReader (
89 XmlReader.Create (new StreamReader (stream, encoding))),
92 ret.Properties.Encoder = this;
93 FillActionContentType (ret, contentType);
97 void FillActionContentType (Message msg, string contentType)
99 if (contentType.StartsWith ("application/soap+xml", StringComparison.Ordinal)) {
100 var ct = new ContentType (contentType);
101 if (ct.Parameters.ContainsKey ("action"))
102 msg.Headers.Action = ct.Parameters ["action"];
106 public override void WriteMessage (Message message, Stream stream)
109 throw new ArgumentNullException ("message");
111 throw new ArgumentNullException ("stream");
112 VerifyMessageVersion (message);
114 XmlWriterSettings s = new XmlWriterSettings ();
115 s.Encoding = encoding;
116 using (XmlWriter w = XmlWriter.Create (stream, s)) {
117 message.WriteMessage (
118 XmlDictionaryWriter.CreateDictionaryWriter (w));
123 public override ArraySegment<byte> WriteMessage (
124 Message message, int maxMessageSize,
125 BufferManager bufferManager, int messageOffset)
127 VerifyMessageVersion (message);
129 ArraySegment<byte> seg = new ArraySegment<byte> (
130 bufferManager.TakeBuffer (maxMessageSize),
131 messageOffset, maxMessageSize);
132 XmlWriterSettings s = new XmlWriterSettings ();
133 s.Encoding = encoding;
134 using (XmlWriter w = XmlWriter.Create (
135 new MemoryStream (seg.Array, seg.Offset, seg.Count), s)) {
136 message.WriteMessage (
137 XmlDictionaryWriter.CreateDictionaryWriter (w));