Merge pull request #495 from nicolas-raoul/fix-for-issue2907-with-no-formatting-changes
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / TransportBindingElement.cs
1 //
2 // TransportBindingElement.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //      Martin Baulig <martin.baulig@xamarin.com>
7 //
8 // Copyright (C) 2005, 2007 Novell, Inc.  http://www.novell.com
9 // Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 using System;
31 using System.Collections.Generic;
32 using System.Net;
33 using System.ServiceModel.Channels;
34 using System.ServiceModel.Description;
35 using System.ServiceModel.Security;
36 using System.Xml;
37
38 namespace System.ServiceModel.Channels
39 {
40         public abstract class TransportBindingElement : BindingElement
41         {
42                 bool manual_addressing;
43                 long max_buffer_pool_size = 0x80000;
44                 long max_recv_message_size = 0x10000;
45
46                 protected TransportBindingElement ()
47                 {
48                 }
49
50                 protected TransportBindingElement (
51                         TransportBindingElement other)
52                         : base (other)
53                 {
54                         manual_addressing = other.manual_addressing;
55                         max_buffer_pool_size = other.max_buffer_pool_size;
56                         max_recv_message_size = other.max_recv_message_size;
57                 }
58
59                 public bool ManualAddressing {
60                         get { return manual_addressing; }
61                         set { manual_addressing = value; }
62                 }
63
64                 public virtual long MaxBufferPoolSize {
65                         get { return max_buffer_pool_size; }
66                         set { max_buffer_pool_size = value; }
67                 }
68
69                 public virtual long MaxReceivedMessageSize {
70                         get { return max_recv_message_size; }
71                         set { max_recv_message_size = value; }
72                 }
73
74                 public abstract string Scheme { get; }
75
76                 public override T GetProperty<T> (BindingContext context)
77                 {
78                         if (typeof (T) == typeof (XmlDictionaryReaderQuotas)) {
79                                 XmlDictionaryReaderQuotas q =
80                                         new XmlDictionaryReaderQuotas ();
81                                 q.MaxStringContentLength = (int) MaxReceivedMessageSize;
82                                 return (T) (object) q;
83                         }
84 #if !NET_2_1
85                         if (typeof (T) == typeof (ChannelProtectionRequirements))
86                                 // blank one, basically it should not be used
87                                 // for any secure channels (
88                                 return (T) (object) new ChannelProtectionRequirements ();
89 #endif
90                         if (typeof (T) == typeof (MessageVersion))
91                                 return (T) (object) MessageVersion.Soap12WSAddressing10;
92                         return context.GetInnerProperty<T> ();
93                 }
94
95 #if !NET_2_1
96                 internal static XmlElement CreateTransportBinding (XmlElement transportToken)
97                 {
98                         var doc = new XmlDocument ();
99                         var transportBinding = doc.CreateElement (
100                                 "sp", "TransportBinding", PolicyImportHelper.SecurityPolicyNS);
101                         
102                         var token = doc.CreateElement (
103                                 "sp", "TransportToken", PolicyImportHelper.SecurityPolicyNS);
104                         PolicyImportHelper.AddWrappedPolicyElement (token, transportToken);
105                         
106                         var algorithmSuite = doc.CreateElement (
107                                 "sp", "AlgorithmSuite", PolicyImportHelper.SecurityPolicyNS);
108                         var basic256 = doc.CreateElement (
109                                 "sp", "Basic256", PolicyImportHelper.SecurityPolicyNS);
110                         PolicyImportHelper.AddWrappedPolicyElement (algorithmSuite, basic256);
111                         
112                         var layout = doc.CreateElement (
113                                 "sp", "Layout", PolicyImportHelper.SecurityPolicyNS);
114                         var strict = doc.CreateElement (
115                                 "sp", "Strict", PolicyImportHelper.SecurityPolicyNS);
116                         PolicyImportHelper.AddWrappedPolicyElement (layout, strict);
117                         
118                         PolicyImportHelper.AddWrappedPolicyElements (
119                                 transportBinding, token, algorithmSuite, layout);
120                         
121                         return transportBinding;
122                 }
123
124                 internal static MessageEncodingBindingElement ExportAddressingPolicy (
125                         PolicyConversionContext context)
126                 {
127                         MessageEncodingBindingElement messageEncodingElement = null;
128                         foreach (var element in context.BindingElements) {
129                                 var check = element as MessageEncodingBindingElement;
130                                 if (check == null)
131                                         continue;
132                                 messageEncodingElement = check;
133                                 break;
134                         }
135
136                         var doc = new XmlDocument ();
137                         var assertions = context.GetBindingAssertions ();
138                         
139                         if (messageEncodingElement == null) {
140                                 assertions.Add (doc.CreateElement (
141                                         "wsaw", "UsingAddressing",
142                                         "http://www.w3.org/2006/05/addressing/wsdl"));
143                                 return null;
144                         }
145
146                         var addressing = messageEncodingElement.MessageVersion.Addressing;
147                         if (addressing == AddressingVersion.WSAddressingAugust2004)
148                                 assertions.Add (doc.CreateElement (
149                                         "wsaw", "UsingAddressing",
150                                         "http://schemas.xmlsoap.org/ws/2004/08/addressing/policy"));
151                         else if (addressing != AddressingVersion.None)
152                                 assertions.Add (doc.CreateElement (
153                                         "wsaw", "UsingAddressing",
154                                         "http://www.w3.org/2006/05/addressing/wsdl"));
155
156                         return messageEncodingElement;
157                 }
158 #endif
159         }
160 }