BaseSerializationHandler.cs 4.0 KB

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