WebHttpDispatchOperationSelector.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // WebHttpDispatchOperationSelector.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Linq;
  31. using System.ServiceModel;
  32. using System.ServiceModel.Channels;
  33. using System.ServiceModel.Description;
  34. using System.ServiceModel.Web;
  35. using TemplateTablePair = System.Collections.Generic.KeyValuePair<System.UriTemplate, object>;
  36. namespace System.ServiceModel.Dispatcher
  37. {
  38. public class WebHttpDispatchOperationSelector : IDispatchOperationSelector
  39. {
  40. public const string HttpOperationSelectorUriMatchedPropertyName = "UriMatched";
  41. Dictionary<string,UriTemplateTable> tables = new Dictionary<string,UriTemplateTable> ();
  42. protected WebHttpDispatchOperationSelector ()
  43. {
  44. }
  45. public WebHttpDispatchOperationSelector (ServiceEndpoint endpoint)
  46. {
  47. if (endpoint == null)
  48. throw new ArgumentNullException ("endpoint");
  49. if (endpoint.Address == null)
  50. throw new InvalidOperationException ("EndpointAddress must be set in the argument ServiceEndpoint");
  51. foreach (OperationDescription od in endpoint.Contract.Operations) {
  52. WebAttributeInfo info = od.GetWebAttributeInfo ();
  53. if (info != null) {
  54. UriTemplateTable table;
  55. if (!tables.TryGetValue (info.Method, out table)) {
  56. table = new UriTemplateTable (endpoint.Address.Uri);
  57. tables.Add (info.Method, table);
  58. }
  59. table.KeyValuePairs.Add (new TemplateTablePair (info.BuildUriTemplate (od, null), od));
  60. }
  61. }
  62. }
  63. public string SelectOperation (ref Message message)
  64. {
  65. // FIXME: uriMatched should be used to differentiate 404 and 405 (method not allowed)
  66. bool uriMatched;
  67. return SelectOperation (ref message, out uriMatched);
  68. }
  69. protected virtual string SelectOperation (ref Message message, out bool uriMatched)
  70. {
  71. if (message == null)
  72. throw new ArgumentNullException ("message");
  73. if (message.Properties.ContainsKey (UriMatchedProperty.Name))
  74. throw new ArgumentException (String.Format ("There is already a message property for template-matched URI '{0}'", UriMatchedProperty.Name));
  75. uriMatched = false;
  76. var hp = (HttpRequestMessageProperty) message.Properties [HttpRequestMessageProperty.Name];
  77. OperationDescription od = null;
  78. Message dummy = message; // since lambda expression prohibits ref variable...
  79. Uri to = message.Headers.To;
  80. // First, UriTemplateTable with corresponding HTTP method is tested. Then other tables can be tested for match.
  81. UriTemplateTable table = null;
  82. if (hp != null && hp.Method != null)
  83. tables.TryGetValue (hp.Method, out table);
  84. // FIXME: looks like To header does not matter on .NET. (But how to match then? I have no idea)
  85. UriTemplateMatch match = to == null || table == null ? null : table.MatchSingle (to);
  86. if (to != null && match == null) {
  87. foreach (var tab in tables.Values)
  88. if ((match = tab.MatchSingle (to)) != null) {
  89. table = tab;
  90. break;
  91. }
  92. }
  93. if (match != null) {
  94. uriMatched = true;
  95. foreach (TemplateTablePair p in table.KeyValuePairs)
  96. if (p.Key == match.Template)
  97. od = p.Value as OperationDescription;
  98. }
  99. if (od != null)
  100. message.Properties.Add (UriMatchedProperty.Name, new UriMatchedProperty ());
  101. return uriMatched && od != null ? od.Name : String.Empty;
  102. }
  103. internal class UriMatchedProperty
  104. {
  105. public static string Name = "UriMatched"; // this is what .NET uses for MessageProperty that represents a matched URI.
  106. }
  107. }
  108. }