PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ProtocolBuffers.Serialization/Http/MessageFormatOptions.cs

https://code.google.com/p/protobuf-csharp-port/
C# | 167 lines | 102 code | 21 blank | 44 comment | 0 complexity | 938092399a033f94157561ca481d7a44 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, GPL-2.0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using Google.ProtocolBuffers.Collections;
  5. namespace Google.ProtocolBuffers.Serialization.Http
  6. {
  7. /// <summary>
  8. /// Defines control information for the various formatting used with HTTP services
  9. /// </summary>
  10. public class MessageFormatOptions
  11. {
  12. /// <summary>The mime type for xml content</summary>
  13. /// <remarks>Other valid xml mime types include: application/binary, application/x-protobuf</remarks>
  14. public const string ContentTypeProtoBuffer = "application/vnd.google.protobuf";
  15. /// <summary>The mime type for xml content</summary>
  16. /// <remarks>Other valid xml mime types include: text/xml</remarks>
  17. public const string ContentTypeXml = "application/xml";
  18. /// <summary>The mime type for json content</summary>
  19. /// <remarks>
  20. /// Other valid json mime types include: application/json, application/x-json,
  21. /// application/x-javascript, text/javascript, text/x-javascript, text/x-json, text/json
  22. /// </remarks>
  23. public const string ContentTypeJson = "application/json";
  24. /// <summary>The mime type for query strings and x-www-form-urlencoded content</summary>
  25. /// <remarks>This mime type is input-only</remarks>
  26. public const string ContentFormUrlEncoded = "application/x-www-form-urlencoded";
  27. /// <summary>
  28. /// Default mime-type handling for input
  29. /// </summary>
  30. private static readonly IDictionary<string, Converter<Stream, ICodedInputStream>> MimeInputDefaults =
  31. new ReadOnlyDictionary<string, Converter<Stream, ICodedInputStream>>(
  32. new Dictionary<string, Converter<Stream, ICodedInputStream>>(StringComparer.OrdinalIgnoreCase)
  33. {
  34. {"application/json", JsonFormatReader.CreateInstance},
  35. {"application/x-json", JsonFormatReader.CreateInstance},
  36. {"application/x-javascript", JsonFormatReader.CreateInstance},
  37. {"text/javascript", JsonFormatReader.CreateInstance},
  38. {"text/x-javascript", JsonFormatReader.CreateInstance},
  39. {"text/x-json", JsonFormatReader.CreateInstance},
  40. {"text/json", JsonFormatReader.CreateInstance},
  41. {"text/xml", XmlFormatReader.CreateInstance},
  42. {"application/xml", XmlFormatReader.CreateInstance},
  43. {"application/binary", CodedInputStream.CreateInstance},
  44. {"application/x-protobuf", CodedInputStream.CreateInstance},
  45. {"application/vnd.google.protobuf", CodedInputStream.CreateInstance},
  46. {"application/x-www-form-urlencoded", FormUrlEncodedReader.CreateInstance},
  47. }
  48. );
  49. /// <summary>
  50. /// Default mime-type handling for output
  51. /// </summary>
  52. private static readonly IDictionary<string, Converter<Stream, ICodedOutputStream>> MimeOutputDefaults =
  53. new ReadOnlyDictionary<string, Converter<Stream, ICodedOutputStream>>(
  54. new Dictionary<string, Converter<Stream, ICodedOutputStream>>(StringComparer.OrdinalIgnoreCase)
  55. {
  56. {"application/json", JsonFormatWriter.CreateInstance},
  57. {"application/x-json", JsonFormatWriter.CreateInstance},
  58. {"application/x-javascript", JsonFormatWriter.CreateInstance},
  59. {"text/javascript", JsonFormatWriter.CreateInstance},
  60. {"text/x-javascript", JsonFormatWriter.CreateInstance},
  61. {"text/x-json", JsonFormatWriter.CreateInstance},
  62. {"text/json", JsonFormatWriter.CreateInstance},
  63. {"text/xml", XmlFormatWriter.CreateInstance},
  64. {"application/xml", XmlFormatWriter.CreateInstance},
  65. {"application/binary", CodedOutputStream.CreateInstance},
  66. {"application/x-protobuf", CodedOutputStream.CreateInstance},
  67. {"application/vnd.google.protobuf", CodedOutputStream.CreateInstance},
  68. }
  69. );
  70. private string _defaultContentType;
  71. private string _xmlReaderRootElementName;
  72. private string _xmlWriterRootElementName;
  73. private ExtensionRegistry _extensionRegistry;
  74. private Dictionary<string, Converter<Stream, ICodedInputStream>> _mimeInputTypes;
  75. private Dictionary<string, Converter<Stream, ICodedOutputStream>> _mimeOutputTypes;
  76. /// <summary> Provides access to modify the mime-type input stream construction </summary>
  77. public IDictionary<string, Converter<Stream, ICodedInputStream>> MimeInputTypes
  78. {
  79. get
  80. {
  81. return _mimeInputTypes ??
  82. (_mimeInputTypes = new Dictionary<string, Converter<Stream, ICodedInputStream>>(
  83. MimeInputDefaults, StringComparer.OrdinalIgnoreCase));
  84. }
  85. }
  86. /// <summary> Provides access to modify the mime-type input stream construction </summary>
  87. public IDictionary<string, Converter<Stream, ICodedOutputStream>> MimeOutputTypes
  88. {
  89. get
  90. {
  91. return _mimeOutputTypes ??
  92. (_mimeOutputTypes = new Dictionary<string, Converter<Stream, ICodedOutputStream>>(
  93. MimeOutputDefaults, StringComparer.OrdinalIgnoreCase));
  94. }
  95. }
  96. internal IDictionary<string, Converter<Stream, ICodedInputStream>> MimeInputTypesReadOnly
  97. { get { return _mimeInputTypes ?? MimeInputDefaults; } }
  98. internal IDictionary<string, Converter<Stream, ICodedOutputStream>> MimeOutputTypesReadOnly
  99. { get { return _mimeOutputTypes ?? MimeOutputDefaults; } }
  100. /// <summary>
  101. /// The default content type to use if the input type is null or empty. If this
  102. /// value is not supplied an ArgumentOutOfRangeException exception will be raised.
  103. /// </summary>
  104. public string DefaultContentType
  105. {
  106. get { return _defaultContentType ?? String.Empty; }
  107. set { _defaultContentType = value; }
  108. }
  109. /// <summary>
  110. /// The extension registry to use when reading messages
  111. /// </summary>
  112. public ExtensionRegistry ExtensionRegistry
  113. {
  114. get { return _extensionRegistry ?? ExtensionRegistry.Empty; }
  115. set { _extensionRegistry = value; }
  116. }
  117. /// <summary>
  118. /// The name of the xml root element when reading messages
  119. /// </summary>
  120. public string XmlReaderRootElementName
  121. {
  122. get { return _xmlReaderRootElementName ?? XmlFormatReader.DefaultRootElementName; }
  123. set { _xmlReaderRootElementName = value; }
  124. }
  125. /// <summary>
  126. /// Xml reader options
  127. /// </summary>
  128. public XmlReaderOptions XmlReaderOptions { get; set; }
  129. /// <summary>
  130. /// True to use formatted output including new-lines and default indentation
  131. /// </summary>
  132. public bool FormattedOutput { get; set; }
  133. /// <summary>
  134. /// The name of the xml root element when writing messages
  135. /// </summary>
  136. public string XmlWriterRootElementName
  137. {
  138. get { return _xmlWriterRootElementName ?? XmlFormatWriter.DefaultRootElementName; }
  139. set { _xmlWriterRootElementName = value; }
  140. }
  141. /// <summary>
  142. /// Xml writer options
  143. /// </summary>
  144. public XmlWriterOptions XmlWriterOptions { get; set; }
  145. }
  146. }