Enable duplicate "UriMatched" error check, with couple of FIXMEs.
[mono.git] / mcs / class / System.ServiceModel.Web / System.ServiceModel.Dispatcher / WebHttpDispatchOperationSelector.cs
1 //
2 // WebHttpDispatchOperationSelector.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
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
36 using TemplateTablePair = System.Collections.Generic.KeyValuePair<System.UriTemplate, object>;
37
38 namespace System.ServiceModel.Dispatcher
39 {
40         public class WebHttpDispatchOperationSelector : IDispatchOperationSelector
41         {
42                 public const string HttpOperationSelectorUriMatchedPropertyName = "UriMatched";
43
44                 Dictionary<string,UriTemplateTable> tables = new Dictionary<string,UriTemplateTable> ();
45
46                 protected WebHttpDispatchOperationSelector ()
47                 {
48                 }
49
50                 public WebHttpDispatchOperationSelector (ServiceEndpoint endpoint)
51                 {
52                         if (endpoint == null)
53                                 throw new ArgumentNullException ("endpoint");
54                         if (endpoint.Address == null)
55                                 throw new InvalidOperationException ("EndpointAddress must be set in the argument ServiceEndpoint");
56
57                         foreach (OperationDescription od in endpoint.Contract.Operations) {
58                                 WebAttributeInfo info = od.GetWebAttributeInfo ();
59                                 if (info != null) {
60                                         UriTemplateTable table;
61                                         if (!tables.TryGetValue (info.Method, out table)) {
62                                                  table = new UriTemplateTable (endpoint.Address.Uri);
63                                                  tables.Add (info.Method, table);
64                                         }
65                                         table.KeyValuePairs.Add (new TemplateTablePair (info.BuildUriTemplate (od, null), od));
66                                 }
67                         }
68                 }
69
70                 public string SelectOperation (ref Message message)
71                 {
72                         // FIXME: uriMatched should be used to differentiate 404 and 405 (method not allowed)
73                         bool uriMatched;
74                         return SelectOperation (ref message, out uriMatched);
75                 }
76
77                 protected virtual string SelectOperation (ref Message message, out bool uriMatched)
78                 {
79                         if (message == null)
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));
83                         uriMatched = false;
84                         var hp = (HttpRequestMessageProperty) message.Properties [HttpRequestMessageProperty.Name];
85
86                         OperationDescription od = null;
87                         Message dummy = message; // since lambda expression prohibits ref variable...
88
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) {
99                                                 table = tab;
100                                                 break;
101                                         }
102                         }
103
104                         if (match != null) {
105                                 uriMatched = true;
106                                 foreach (TemplateTablePair p in table.KeyValuePairs)
107                                         if (p.Key == match.Template)
108                                                 od = p.Value as OperationDescription;
109                         }
110                         if (od != null)
111                                 message.Properties.Add (UriMatchedProperty.Name, new UriMatchedProperty ());
112
113                         return uriMatched && od != null ? od.Name : String.Empty;
114                 }
115
116                 internal class UriMatchedProperty
117                 {
118                         public static string Name = "UriMatched"; // this is what .NET uses for MessageProperty that represents a matched URI.
119                 }
120         }
121 }