2 // WebHttpDispatchOperationSelector.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.Generic;
31 using System.ServiceModel;
32 using System.ServiceModel.Channels;
33 using System.ServiceModel.Description;
34 using System.ServiceModel.Web;
36 using TemplateTablePair = System.Collections.Generic.KeyValuePair<System.UriTemplate, object>;
38 namespace System.ServiceModel.Dispatcher
40 public class WebHttpDispatchOperationSelector : IDispatchOperationSelector
42 public const string HttpOperationSelectorUriMatchedPropertyName = "UriMatched";
44 Dictionary<string,UriTemplateTable> tables = new Dictionary<string,UriTemplateTable> ();
46 protected WebHttpDispatchOperationSelector ()
50 public WebHttpDispatchOperationSelector (ServiceEndpoint endpoint)
53 throw new ArgumentNullException ("endpoint");
54 if (endpoint.Address == null)
55 throw new InvalidOperationException ("EndpointAddress must be set in the argument ServiceEndpoint");
57 foreach (OperationDescription od in endpoint.Contract.Operations) {
58 WebAttributeInfo info = od.GetWebAttributeInfo ();
60 UriTemplateTable table;
61 if (!tables.TryGetValue (info.Method, out table)) {
62 table = new UriTemplateTable (endpoint.Address.Uri);
63 tables.Add (info.Method, table);
65 table.KeyValuePairs.Add (new TemplateTablePair (info.BuildUriTemplate (od, null), od));
70 public string SelectOperation (ref Message message)
72 // FIXME: uriMatched should be used to differentiate 404 and 405 (method not allowed)
74 return SelectOperation (ref message, out uriMatched);
77 protected virtual string SelectOperation (ref Message message, out bool uriMatched)
80 throw new ArgumentNullException ("message");
81 if (message.Properties.ContainsKey (UriMatchedProperty.Name))
82 throw new ArgumentException (String.Format ("There is already a message property for template-matched URI '{0}'", UriMatchedProperty.Name));
84 var hp = (HttpRequestMessageProperty) message.Properties [HttpRequestMessageProperty.Name];
86 OperationDescription od = null;
87 Message dummy = message; // since lambda expression prohibits ref variable...
89 Uri to = message.Headers.To;
90 // First, UriTemplateTable with corresponding HTTP method is tested. Then other tables can be tested for match.
91 UriTemplateTable table = null;
92 if (hp != null && hp.Method != null)
93 tables.TryGetValue (hp.Method, out table);
94 // FIXME: looks like To header does not matter on .NET. (But how to match then? I have no idea)
95 UriTemplateMatch match = to == null || table == null ? null : table.MatchSingle (to);
96 if (to != null && match == null) {
97 foreach (var tab in tables.Values)
98 if ((match = tab.MatchSingle (to)) != null) {
106 foreach (TemplateTablePair p in table.KeyValuePairs)
107 if (p.Key == match.Template)
108 od = p.Value as OperationDescription;
111 message.Properties.Add (UriMatchedProperty.Name, new UriMatchedProperty ());
113 return uriMatched && od != null ? od.Name : String.Empty;
116 internal class UriMatchedProperty
118 public static string Name = "UriMatched"; // this is what .NET uses for MessageProperty that represents a matched URI.