WebMessageEncoder.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.Text;
  34. using System.Xml;
  35. namespace System.ServiceModel.Channels
  36. {
  37. internal class WebMessageEncoder : MessageEncoder
  38. {
  39. WebMessageEncodingBindingElement source;
  40. public WebMessageEncoder (WebMessageEncodingBindingElement source)
  41. {
  42. this.source = source;
  43. }
  44. public override string ContentType {
  45. get { return MediaType + "; charset=" + source.WriteEncoding.HeaderName; }
  46. }
  47. // FIXME: find out how it can be customized.
  48. public override string MediaType {
  49. get { return "application/xml"; }
  50. }
  51. public override MessageVersion MessageVersion {
  52. get { return MessageVersion.None; }
  53. }
  54. public override Message ReadMessage (ArraySegment<byte> buffer, BufferManager bufferManager, string contentType)
  55. {
  56. throw new NotImplementedException ();
  57. }
  58. public override Message ReadMessage (Stream stream, int maxSizeOfHeaders, string contentType)
  59. {
  60. if (stream == null)
  61. throw new ArgumentNullException ("stream");
  62. if (contentType == null)
  63. throw new ArgumentNullException ("contentType");
  64. Encoding enc = Encoding.UTF8;
  65. ContentType ct = new ContentType (contentType);
  66. if (ct.CharSet != null)
  67. enc = Encoding.GetEncoding (ct.CharSet);
  68. WebContentFormat fmt = WebContentFormat.Xml;
  69. if (source.ContentTypeMapper != null)
  70. fmt = source.ContentTypeMapper.GetMessageFormatForContentType (contentType);
  71. else {
  72. switch (ct.MediaType) {
  73. case "application/json":
  74. fmt = WebContentFormat.Json;
  75. break;
  76. case "application/xml":
  77. fmt = WebContentFormat.Xml;
  78. break;
  79. case "application/octet-stream":
  80. fmt = WebContentFormat.Raw;
  81. break;
  82. }
  83. }
  84. Message msg = null;
  85. WebBodyFormatMessageProperty wp = null;
  86. switch (fmt) {
  87. case WebContentFormat.Xml:
  88. // FIXME: is it safe/unsafe/required to keep XmlReader open?
  89. msg = Message.CreateMessage (MessageVersion.None, null, XmlReader.Create (new StreamReader (stream, enc)));
  90. wp = new WebBodyFormatMessageProperty (WebContentFormat.Xml);
  91. break;
  92. case WebContentFormat.Json:
  93. // FIXME: is it safe/unsafe/required to keep XmlReader open?
  94. msg = Message.CreateMessage (MessageVersion.None, null, JsonReaderWriterFactory.CreateJsonReader (stream, enc, source.ReaderQuotas, null));
  95. wp = new WebBodyFormatMessageProperty (WebContentFormat.Json);
  96. break;
  97. case WebContentFormat.Raw:
  98. throw new NotImplementedException ();
  99. default:
  100. throw new SystemException ("INTERNAL ERROR: cannot determine content format");
  101. }
  102. if (wp != null)
  103. msg.Properties.Add (WebBodyFormatMessageProperty.Name, wp);
  104. return msg;
  105. }
  106. WebContentFormat GetContentFormat (Message message)
  107. {
  108. string name = WebBodyFormatMessageProperty.Name;
  109. if (message.Properties.ContainsKey (name))
  110. return ((WebBodyFormatMessageProperty) message.Properties [name]).Format;
  111. switch (MediaType) {
  112. case "application/xml":
  113. case "text/xml":
  114. return WebContentFormat.Xml;
  115. case "application/json":
  116. case "text/json":
  117. return WebContentFormat.Json;
  118. case "application/octet-stream":
  119. return WebContentFormat.Raw;
  120. default:
  121. return WebContentFormat.Default;
  122. }
  123. }
  124. public override void WriteMessage (Message message, Stream stream)
  125. {
  126. if (message == null)
  127. throw new ArgumentNullException ("message");
  128. if (!MessageVersion.Equals (message.Version))
  129. throw new ProtocolException (String.Format ("MessageVersion {0} is not supported", message.Version));
  130. if (stream == null)
  131. throw new ArgumentNullException ("stream");
  132. switch (GetContentFormat (message)) {
  133. case WebContentFormat.Xml:
  134. using (XmlWriter w = XmlDictionaryWriter.CreateTextWriter (stream, source.WriteEncoding))
  135. message.WriteMessage (w);
  136. break;
  137. case WebContentFormat.Json:
  138. using (XmlWriter w = JsonReaderWriterFactory.CreateJsonWriter (stream, source.WriteEncoding))
  139. message.WriteMessage (w);
  140. break;
  141. case WebContentFormat.Raw:
  142. throw new NotImplementedException ();
  143. case WebContentFormat.Default:
  144. throw new SystemException ("INTERNAL ERROR: cannot determine content format");
  145. }
  146. }
  147. public override ArraySegment<byte> WriteMessage (Message message, int maxMessageSize, BufferManager bufferManager,
  148. int messageOffset)
  149. {
  150. throw new NotImplementedException ();
  151. }
  152. }
  153. }