Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / TextMessageEncoder.cs
1 //
2 // TextMessageEncoder.cs
3 //
4 // Author: Atsushi Enomoto (atsushi@ximian.com)
5 //
6 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
7 //
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:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 // 
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.
26 //
27 using System;
28 using System.IO;
29 using System.Collections.ObjectModel;
30 using System.ServiceModel;
31 using System.Text;
32 using System.Xml;
33
34 namespace System.ServiceModel.Channels
35 {
36         internal class TextMessageEncoder : MessageEncoder
37         {
38                 Encoding encoding;
39                 MessageVersion version;
40
41                 public TextMessageEncoder (MessageVersion version, Encoding encoding)
42                 {
43                         this.version = version;
44                         this.encoding = encoding;
45                 }
46
47                 internal Encoding Encoding {
48                         get { return encoding; }
49                 }
50
51                 public override string ContentType {
52                         get { return String.Concat (MediaType, "; charset=", encoding.WebName); }
53                 }
54
55                 public override string MediaType {
56                         get { return version.Envelope == EnvelopeVersion.Soap12 ? "application/soap+xml" : "text/xml";  }
57                 }
58
59                 public override MessageVersion MessageVersion {
60                         get { return version; }
61                 }
62
63                 [MonoTODO]
64                 public override Message ReadMessage (ArraySegment<byte> buffer,
65                         BufferManager bufferManager, string contentType)
66                 {
67                         return Message.CreateMessage (
68                                 XmlDictionaryReader.CreateDictionaryReader (
69                                         XmlReader.Create (new StreamReader (
70                                                 new MemoryStream (
71                                                 buffer.Array, buffer.Offset,
72                                                 buffer.Count), encoding))),
73                                 // FIXME: supply max header size
74                                 int.MaxValue,
75                                 version);
76                 }
77
78                 [MonoTODO]
79                 public override Message ReadMessage (Stream stream,
80                         int maxSizeOfHeaders, string contentType)
81                 {
82                         return Message.CreateMessage (
83                                 XmlDictionaryReader.CreateDictionaryReader (
84                                         XmlReader.Create (new StreamReader (stream, encoding))),
85                                 maxSizeOfHeaders,
86                                 version);
87                 }
88
89                 public override void WriteMessage (Message message, Stream stream)
90                 {
91                         if (message == null)
92                                 throw new ArgumentNullException ("message");
93                         if (stream == null)
94                                 throw new ArgumentNullException ("stream");
95                         VerifyMessageVersion (message);
96
97                         XmlWriterSettings s = new XmlWriterSettings ();
98                         s.Encoding = encoding;
99                         using (XmlWriter w = XmlWriter.Create (stream, s)) {
100                                 message.WriteMessage (
101                                         XmlDictionaryWriter.CreateDictionaryWriter (w));
102                         }
103                 }
104
105                 [MonoTODO]
106                 public override ArraySegment<byte> WriteMessage (
107                         Message message, int maxMessageSize,
108                         BufferManager bufferManager, int messageOffset)
109                 {
110                         VerifyMessageVersion (message);
111
112                         ArraySegment<byte> seg = new ArraySegment<byte> (
113                                 bufferManager.TakeBuffer (maxMessageSize),
114                                 messageOffset, maxMessageSize);
115                         XmlWriterSettings s = new XmlWriterSettings ();
116                         s.Encoding = encoding;
117                         using (XmlWriter w = XmlWriter.Create (
118                                 new MemoryStream (seg.Array, seg.Offset, seg.Count), s)) {
119                                 message.WriteMessage (
120                                         XmlDictionaryWriter.CreateDictionaryWriter (w));
121                         }
122                         return seg;
123                 }
124         }
125 }