Merge pull request #498 from Unroll-Me/master
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Description / CustomPolicyConversionContext.cs
1 //
2 // CustomPolicyConversionContext.cs
3 //
4 // Author:
5 //       Martin Baulig <martin.baulig@xamarin.com>
6 //
7 // Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26
27 using System;
28 using System.Xml;
29 using System.ServiceModel.Channels;
30 using WS = System.Web.Services.Description;
31
32 namespace System.ServiceModel.Description {
33
34         internal class CustomPolicyConversionContext : PolicyConversionContext {
35                 WS.Binding binding;
36                 PolicyAssertionCollection assertions;
37                 BindingElementCollection binding_elements;
38
39                 internal WS.Binding WsdlBinding {
40                         get { return binding; }
41                 }
42
43                 #region implemented abstract members of PolicyConversionContext
44
45                 public override PolicyAssertionCollection GetBindingAssertions ()
46                 {
47                         return assertions;
48                 }
49
50                 public override PolicyAssertionCollection GetFaultBindingAssertions (FaultDescription fault)
51                 {
52                         throw new NotImplementedException ();
53                 }
54
55                 public override PolicyAssertionCollection GetMessageBindingAssertions (MessageDescription message)
56                 {
57                         throw new NotImplementedException ();
58                 }
59
60                 public override PolicyAssertionCollection GetOperationBindingAssertions (OperationDescription operation)
61                 {
62                         throw new NotImplementedException ();
63                 }
64
65                 public override BindingElementCollection BindingElements {
66                         get {
67                                 return binding_elements;
68                         }
69                 }
70
71                 #endregion
72
73                 public CustomPolicyConversionContext (WS.Binding binding, ServiceEndpoint endpoint)
74                         : base (endpoint)
75                 {
76                         this.binding = binding;
77                         assertions = new PolicyAssertionCollection ();
78                         binding_elements = ((CustomBinding)endpoint.Binding).Elements;
79                 }
80
81                 public void AddPolicyAssertion (XmlElement element)
82                 {
83                         /*
84                          * http://www.w3.org/Submission/WS-Policy/#Policy_Assertion:
85                          *
86                          * <wsp:Policy … >
87                          *   <wsp:ExactlyOne>
88                          *     ( <wsp:All> ( <Assertion …> … </Assertion> )* </wsp:All> )*
89                          *   </wsp:ExactlyOne>
90                          * </wsp:Policy> 
91                          * 
92                          */
93
94                         var exactlyOne = element.FirstChild as XmlElement;
95                         if (exactlyOne == null) {
96                                 // OOPS
97                                 return;
98                         }
99
100                         if (!exactlyOne.NamespaceURI.Equals (Constants.WspNamespace) ||
101                                 !exactlyOne.LocalName.Equals ("ExactlyOne")) {
102                                 // FIXME: What to do with this ... ?
103                                 return;
104                         }
105
106                         foreach (var node in exactlyOne.ChildNodes) {
107                                 var child = node as XmlElement;
108                                 if (child == null)
109                                         continue;
110
111                                 if (!child.NamespaceURI.Equals (Constants.WspNamespace) ||
112                                     !child.LocalName.Equals ("All")) {
113                                         // FIXME: Can assertions go here ... ?
114                                         continue;
115                                 }
116
117                                 foreach (var node2 in child.ChildNodes) {
118                                         var assertion = node2 as XmlElement;
119                                         if (assertion == null)
120                                                 continue;
121
122                                         assertions.Add (assertion);
123                                 }
124                         }
125                 }
126         }
127 }
128