WebMessageEncoder.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // WebMessageEncoder.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
  8. //
  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:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  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.
  27. //
  28. using System;
  29. using System.IO;
  30. using System.Net.Mime;
  31. using System.Runtime.Serialization.Json;
  32. using System.ServiceModel;
  33. using System.ServiceModel.Dispatcher;
  34. using System.Text;
  35. using System.Xml;
  36. namespace System.ServiceModel.Channels
  37. {
  38. internal class WebMessageEncoder : MessageEncoder
  39. {
  40. internal const string ScriptPropertyName = "618BC2B0-38AA-21A3-DB4A-404FC87B9B11"; // randomly generated
  41. WebMessageEncodingBindingElement source;
  42. public WebMessageEncoder (WebMessageEncodingBindingElement source)
  43. {
  44. this.source = source;
  45. }
  46. public override string ContentType {
  47. #if NET_2_1
  48. get { return MediaType; }
  49. #else
  50. get { return MediaType + "; charset=" + source.WriteEncoding.HeaderName; }
  51. #endif
  52. }
  53. // FIXME: find out how it can be customized.
  54. public override string MediaType {
  55. get { return "application/xml"; }
  56. }
  57. public override MessageVersion MessageVersion {
  58. get { return MessageVersion.None; }
  59. }
  60. public override bool IsContentTypeSupported (string contentType)
  61. {
  62. if (contentType == null)
  63. throw new ArgumentNullException ("contentType");
  64. return true; // anything is accepted.
  65. }
  66. public override Message ReadMessage (ArraySegment<byte> buffer, BufferManager bufferManager, string contentType)
  67. {
  68. throw new NotImplementedException ();
  69. }
  70. public override Message ReadMessage (Stream stream, int maxSizeOfHeaders, string contentType)
  71. {
  72. if (stream == null)
  73. throw new ArgumentNullException ("stream");
  74. contentType = contentType ?? "application/octet-stream";
  75. Encoding enc = Encoding.UTF8;
  76. ContentType ct = new ContentType (contentType);
  77. if (ct.CharSet != null)
  78. enc = Encoding.GetEncoding (ct.CharSet);
  79. WebContentFormat fmt = WebContentFormat.Xml;
  80. if (source.ContentTypeMapper != null)
  81. fmt = source.ContentTypeMapper.GetMessageFormatForContentType (contentType);
  82. else {
  83. switch (ct.MediaType) {
  84. case "application/json":
  85. fmt = WebContentFormat.Json;
  86. break;
  87. case "application/xml":
  88. fmt = WebContentFormat.Xml;
  89. break;
  90. default:
  91. fmt = WebContentFormat.Raw;
  92. break;
  93. }
  94. }
  95. Message msg = null;
  96. WebBodyFormatMessageProperty wp = null;
  97. switch (fmt) {
  98. case WebContentFormat.Xml:
  99. // FIXME: is it safe/unsafe/required to keep XmlReader open?
  100. msg = Message.CreateMessage (MessageVersion.None, null, XmlReader.Create (new StreamReader (stream, enc)));
  101. wp = new WebBodyFormatMessageProperty (WebContentFormat.Xml);
  102. break;
  103. case WebContentFormat.Json:
  104. // FIXME: is it safe/unsafe/required to keep XmlReader open?
  105. #if NET_2_1
  106. msg = Message.CreateMessage (MessageVersion.None, null, JsonReaderWriterFactory.CreateJsonReader (stream, source.ReaderQuotas));
  107. #else
  108. msg = Message.CreateMessage (MessageVersion.None, null, JsonReaderWriterFactory.CreateJsonReader (stream, enc, source.ReaderQuotas, null));
  109. #endif
  110. wp = new WebBodyFormatMessageProperty (WebContentFormat.Json);
  111. break;
  112. case WebContentFormat.Raw:
  113. msg = new WebMessageFormatter.RawMessage (stream);
  114. wp = new WebBodyFormatMessageProperty (WebContentFormat.Raw);
  115. break;
  116. default:
  117. throw new SystemException ("INTERNAL ERROR: cannot determine content format");
  118. }
  119. if (wp != null)
  120. msg.Properties.Add (WebBodyFormatMessageProperty.Name, wp);
  121. return msg;
  122. }
  123. WebContentFormat GetContentFormat (Message message)
  124. {
  125. string name = WebBodyFormatMessageProperty.Name;
  126. if (message.Properties.ContainsKey (name))
  127. return ((WebBodyFormatMessageProperty) message.Properties [name]).Format;
  128. switch (MediaType) {
  129. case "application/xml":
  130. case "text/xml":
  131. return WebContentFormat.Xml;
  132. case "application/json":
  133. case "text/json":
  134. return WebContentFormat.Json;
  135. case "application/octet-stream":
  136. return WebContentFormat.Raw;
  137. default:
  138. return WebContentFormat.Default;
  139. }
  140. }
  141. public override void WriteMessage (Message message, Stream stream)
  142. {
  143. if (message == null)
  144. throw new ArgumentNullException ("message");
  145. // Handle /js and /jsdebug as the special cases.
  146. var script = message.Properties [ScriptPropertyName] as string;
  147. if (script != null) {
  148. var bytes = source.WriteEncoding.GetBytes (script);
  149. stream.Write (bytes, 0, bytes.Length);
  150. return;
  151. }
  152. if (!MessageVersion.Equals (message.Version))
  153. throw new ProtocolException (String.Format ("MessageVersion {0} is not supported", message.Version));
  154. if (stream == null)
  155. throw new ArgumentNullException ("stream");
  156. switch (GetContentFormat (message)) {
  157. case WebContentFormat.Xml:
  158. #if NET_2_1
  159. using (XmlWriter w = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (new StreamWriter (stream, source.WriteEncoding))))
  160. message.WriteMessage (w);
  161. #else
  162. using (XmlWriter w = XmlDictionaryWriter.CreateTextWriter (stream, source.WriteEncoding))
  163. message.WriteMessage (w);
  164. #endif
  165. break;
  166. case WebContentFormat.Json:
  167. using (XmlWriter w = JsonReaderWriterFactory.CreateJsonWriter (stream, source.WriteEncoding))
  168. message.WriteMessage (w);
  169. break;
  170. case WebContentFormat.Raw:
  171. var rmsg = (WebMessageFormatter.RawMessage) message;
  172. var src = rmsg.Stream;
  173. if (src == null) // null output
  174. break;
  175. int len = 0;
  176. byte [] buffer = new byte [4096];
  177. while ((len = src.Read (buffer, 0, buffer.Length)) > 0)
  178. stream.Write (buffer, 0, len);
  179. break;
  180. case WebContentFormat.Default:
  181. throw new SystemException ("INTERNAL ERROR: cannot determine content format");
  182. }
  183. }
  184. public override ArraySegment<byte> WriteMessage (Message message, int maxMessageSize, BufferManager bufferManager,
  185. int messageOffset)
  186. {
  187. throw new NotImplementedException ();
  188. }
  189. }
  190. }