RestHandler.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.Serialization;
  4. using System.Threading.Tasks;
  5. using MediaBrowser.Model.Logging;
  6. using MediaBrowser.Model.Services;
  7. namespace ServiceStack.Host
  8. {
  9. public class RestHandler
  10. {
  11. public string RequestName { get; set; }
  12. public async Task<object> HandleResponseAsync(object response)
  13. {
  14. var taskResponse = response as Task;
  15. if (taskResponse == null)
  16. {
  17. return response;
  18. }
  19. await taskResponse.ConfigureAwait(false);
  20. var taskResult = ServiceStackHost.Instance.GetTaskResult(taskResponse, RequestName);
  21. var subTask = taskResult as Task;
  22. if (subTask != null)
  23. {
  24. taskResult = ServiceStackHost.Instance.GetTaskResult(subTask, RequestName);
  25. }
  26. return taskResult;
  27. }
  28. protected static object CreateContentTypeRequest(IRequest httpReq, Type requestType, string contentType)
  29. {
  30. if (!string.IsNullOrEmpty(contentType) && httpReq.ContentLength > 0)
  31. {
  32. var deserializer = ContentTypes.Instance.GetStreamDeserializer(contentType);
  33. if (deserializer != null)
  34. {
  35. return deserializer(requestType, httpReq.InputStream);
  36. }
  37. }
  38. return ServiceStackHost.Instance.CreateInstance(requestType); //Return an empty DTO, even for empty request bodies
  39. }
  40. protected static object GetCustomRequestFromBinder(IRequest httpReq, Type requestType)
  41. {
  42. Func<IRequest, object> requestFactoryFn;
  43. ServiceStackHost.Instance.ServiceController.RequestTypeFactoryMap.TryGetValue(
  44. requestType, out requestFactoryFn);
  45. return requestFactoryFn != null ? requestFactoryFn(httpReq) : null;
  46. }
  47. public static RestPath FindMatchingRestPath(string httpMethod, string pathInfo, ILogger logger, out string contentType)
  48. {
  49. pathInfo = GetSanitizedPathInfo(pathInfo, out contentType);
  50. return ServiceStackHost.Instance.ServiceController.GetRestPathForRequest(httpMethod, pathInfo, logger);
  51. }
  52. public static string GetSanitizedPathInfo(string pathInfo, out string contentType)
  53. {
  54. contentType = null;
  55. var pos = pathInfo.LastIndexOf('.');
  56. if (pos >= 0)
  57. {
  58. var format = pathInfo.Substring(pos + 1);
  59. contentType = GetFormatContentType(format);
  60. if (contentType != null)
  61. {
  62. pathInfo = pathInfo.Substring(0, pos);
  63. }
  64. }
  65. return pathInfo;
  66. }
  67. private static string GetFormatContentType(string format)
  68. {
  69. //built-in formats
  70. if (format == "json")
  71. return "application/json";
  72. if (format == "xml")
  73. return "application/xml";
  74. return null;
  75. }
  76. public RestPath GetRestPath(string httpMethod, string pathInfo)
  77. {
  78. if (this.RestPath == null)
  79. {
  80. string contentType;
  81. this.RestPath = FindMatchingRestPath(httpMethod, pathInfo, new NullLogger(), out contentType);
  82. if (contentType != null)
  83. ResponseContentType = contentType;
  84. }
  85. return this.RestPath;
  86. }
  87. public RestPath RestPath { get; set; }
  88. // Set from SSHHF.GetHandlerForPathInfo()
  89. public string ResponseContentType { get; set; }
  90. public async Task ProcessRequestAsync(IRequest httpReq, IResponse httpRes, string operationName)
  91. {
  92. var appHost = ServiceStackHost.Instance;
  93. var restPath = GetRestPath(httpReq.Verb, httpReq.PathInfo);
  94. if (restPath == null)
  95. {
  96. throw new NotSupportedException("No RestPath found for: " + httpReq.Verb + " " + httpReq.PathInfo);
  97. }
  98. httpReq.SetRoute(restPath);
  99. if (ResponseContentType != null)
  100. httpReq.ResponseContentType = ResponseContentType;
  101. var request = httpReq.Dto = CreateRequest(httpReq, restPath);
  102. appHost.ApplyRequestFilters(httpReq, httpRes, request);
  103. var rawResponse = await ServiceStackHost.Instance.ServiceController.Execute(request, httpReq).ConfigureAwait(false);
  104. var response = await HandleResponseAsync(rawResponse).ConfigureAwait(false);
  105. appHost.ApplyResponseFilters(httpReq, httpRes, response);
  106. await httpRes.WriteToResponse(httpReq, response).ConfigureAwait(false);
  107. }
  108. public static object CreateRequest(IRequest httpReq, RestPath restPath)
  109. {
  110. var dtoFromBinder = GetCustomRequestFromBinder(httpReq, restPath.RequestType);
  111. if (dtoFromBinder != null)
  112. return dtoFromBinder;
  113. var requestParams = httpReq.GetFlattenedRequestParams();
  114. return CreateRequest(httpReq, restPath, requestParams);
  115. }
  116. public static object CreateRequest(IRequest httpReq, RestPath restPath, Dictionary<string, string> requestParams)
  117. {
  118. var requestDto = CreateContentTypeRequest(httpReq, restPath.RequestType, httpReq.ContentType);
  119. return CreateRequest(httpReq, restPath, requestParams, requestDto);
  120. }
  121. public static object CreateRequest(IRequest httpReq, RestPath restPath, Dictionary<string, string> requestParams, object requestDto)
  122. {
  123. string contentType;
  124. var pathInfo = !restPath.IsWildCardPath
  125. ? GetSanitizedPathInfo(httpReq.PathInfo, out contentType)
  126. : httpReq.PathInfo;
  127. return restPath.CreateRequest(pathInfo, requestParams, requestDto);
  128. }
  129. /// <summary>
  130. /// Used in Unit tests
  131. /// </summary>
  132. /// <returns></returns>
  133. public object CreateRequest(IRequest httpReq, string operationName)
  134. {
  135. if (this.RestPath == null)
  136. throw new ArgumentNullException("No RestPath found");
  137. return CreateRequest(httpReq, this.RestPath);
  138. }
  139. }
  140. }