BaseSerializationHandler.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.Kernel;
  3. using System;
  4. using System.IO;
  5. using System.Threading.Tasks;
  6. namespace MediaBrowser.Common.Net.Handlers
  7. {
  8. /// <summary>
  9. /// Class BaseSerializationHandler
  10. /// </summary>
  11. /// <typeparam name="TKernelType">The type of the T kernel type.</typeparam>
  12. /// <typeparam name="T"></typeparam>
  13. public abstract class BaseSerializationHandler<TKernelType, T> : BaseHandler<TKernelType>
  14. where TKernelType : IKernel
  15. where T : class
  16. {
  17. /// <summary>
  18. /// Gets the serialization format.
  19. /// </summary>
  20. /// <value>The serialization format.</value>
  21. public SerializationFormat SerializationFormat
  22. {
  23. get
  24. {
  25. var format = QueryString["dataformat"];
  26. if (string.IsNullOrEmpty(format))
  27. {
  28. return SerializationFormat.Json;
  29. }
  30. return (SerializationFormat)Enum.Parse(typeof(SerializationFormat), format, true);
  31. }
  32. }
  33. /// <summary>
  34. /// Gets the type of the content.
  35. /// </summary>
  36. /// <value>The type of the content.</value>
  37. protected string ContentType
  38. {
  39. get
  40. {
  41. switch (SerializationFormat)
  42. {
  43. case SerializationFormat.Protobuf:
  44. return "application/x-protobuf";
  45. default:
  46. return MimeTypes.JsonMimeType;
  47. }
  48. }
  49. }
  50. /// <summary>
  51. /// Gets the response info.
  52. /// </summary>
  53. /// <returns>Task{ResponseInfo}.</returns>
  54. protected override Task<ResponseInfo> GetResponseInfo()
  55. {
  56. return Task.FromResult(new ResponseInfo
  57. {
  58. ContentType = ContentType
  59. });
  60. }
  61. /// <summary>
  62. /// Called when [processing request].
  63. /// </summary>
  64. /// <param name="responseInfo">The response info.</param>
  65. /// <returns>Task.</returns>
  66. protected override async Task OnProcessingRequest(ResponseInfo responseInfo)
  67. {
  68. _objectToSerialize = await GetObjectToSerialize().ConfigureAwait(false);
  69. if (_objectToSerialize == null)
  70. {
  71. throw new ResourceNotFoundException();
  72. }
  73. await base.OnProcessingRequest(responseInfo).ConfigureAwait(false);
  74. }
  75. /// <summary>
  76. /// The _object to serialize
  77. /// </summary>
  78. private T _objectToSerialize;
  79. /// <summary>
  80. /// Gets the object to serialize.
  81. /// </summary>
  82. /// <returns>Task{`0}.</returns>
  83. protected abstract Task<T> GetObjectToSerialize();
  84. /// <summary>
  85. /// Writes the response to output stream.
  86. /// </summary>
  87. /// <param name="stream">The stream.</param>
  88. /// <param name="responseInfo">The response info.</param>
  89. /// <param name="contentLength">Length of the content.</param>
  90. /// <returns>Task.</returns>
  91. protected override Task WriteResponseToOutputStream(Stream stream, ResponseInfo responseInfo, long? contentLength)
  92. {
  93. return Task.Run(() =>
  94. {
  95. //switch (SerializationFormat)
  96. //{
  97. // case SerializationFormat.Protobuf:
  98. // Kernel.ProtobufSerializer.SerializeToStream(_objectToSerialize, stream);
  99. // break;
  100. // default:
  101. // JsonSerializer.SerializeToStream(_objectToSerialize, stream);
  102. // break;
  103. //}
  104. });
  105. }
  106. }
  107. /// <summary>
  108. /// Enum SerializationFormat
  109. /// </summary>
  110. public enum SerializationFormat
  111. {
  112. /// <summary>
  113. /// The json
  114. /// </summary>
  115. Json,
  116. /// <summary>
  117. /// The protobuf
  118. /// </summary>
  119. Protobuf
  120. }
  121. }