ServiceStackHost.Runtime.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (c) Service Stack LLC. All Rights Reserved.
  2. // License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
  3. using MediaBrowser.Model.Services;
  4. using ServiceStack.Support.WebHost;
  5. namespace ServiceStack
  6. {
  7. public abstract partial class ServiceStackHost
  8. {
  9. /// <summary>
  10. /// Applies the request filters. Returns whether or not the request has been handled
  11. /// and no more processing should be done.
  12. /// </summary>
  13. /// <returns></returns>
  14. public virtual bool ApplyRequestFilters(IRequest req, IResponse res, object requestDto)
  15. {
  16. if (res.IsClosed) return res.IsClosed;
  17. //Exec all RequestFilter attributes with Priority < 0
  18. var attributes = FilterAttributeCache.GetRequestFilterAttributes(requestDto.GetType());
  19. var i = 0;
  20. for (; i < attributes.Length && attributes[i].Priority < 0; i++)
  21. {
  22. var attribute = attributes[i];
  23. attribute.RequestFilter(req, res, requestDto);
  24. if (res.IsClosed) return res.IsClosed;
  25. }
  26. if (res.IsClosed) return res.IsClosed;
  27. //Exec global filters
  28. foreach (var requestFilter in GlobalRequestFilters)
  29. {
  30. requestFilter(req, res, requestDto);
  31. if (res.IsClosed) return res.IsClosed;
  32. }
  33. //Exec remaining RequestFilter attributes with Priority >= 0
  34. for (; i < attributes.Length && attributes[i].Priority >= 0; i++)
  35. {
  36. var attribute = attributes[i];
  37. attribute.RequestFilter(req, res, requestDto);
  38. if (res.IsClosed) return res.IsClosed;
  39. }
  40. return res.IsClosed;
  41. }
  42. /// <summary>
  43. /// Applies the response filters. Returns whether or not the request has been handled
  44. /// and no more processing should be done.
  45. /// </summary>
  46. /// <returns></returns>
  47. public virtual bool ApplyResponseFilters(IRequest req, IResponse res, object response)
  48. {
  49. if (response != null)
  50. {
  51. if (res.IsClosed) return res.IsClosed;
  52. }
  53. //Exec global filters
  54. foreach (var responseFilter in GlobalResponseFilters)
  55. {
  56. responseFilter(req, res, response);
  57. if (res.IsClosed) return res.IsClosed;
  58. }
  59. return res.IsClosed;
  60. }
  61. }
  62. }