1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
//
// StandardBindingImporter.cs
//
// Author:
// Martin Baulig <martin.baulig@xamarin.com>
//
// Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Net;
using System.Xml;
using System.Xml.Schema;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using WS = System.Web.Services.Description;
using QName = System.Xml.XmlQualifiedName;
namespace System.ServiceModel.Channels {
public class StandardBindingImporter : IWsdlImportExtension {
#region IWsdlImportExtension implementation
public void BeforeImport (WS.ServiceDescriptionCollection wsdlDocuments, XmlSchemaSet xmlSchemas,
ICollection<XmlElement> policy)
{
}
public void ImportContract (WsdlImporter importer, WsdlContractConversionContext contractContext)
{
}
WS.Port LookupPort (WsdlImporter importer, QName name)
{
foreach (WS.ServiceDescription doc in importer.WsdlDocuments) {
foreach (WS.Service service in doc.Services) {
foreach (WS.Port port in service.Ports) {
if (!name.Namespace.Equals (port.Binding.Namespace))
continue;
if (!name.Name.Equals (port.Binding.Name))
continue;
return port;
}
}
}
return null;
}
public void ImportEndpoint (WsdlImporter importer, WsdlEndpointConversionContext context)
{
var custom = context.Endpoint.Binding as CustomBinding;
if (custom == null)
return;
var soapHttp = GetHttpSoapBinding (context.WsdlBinding);
if (soapHttp != null) {
ImportBasicHttpBinding (importer, context, custom, soapHttp);
return;
}
var soapTcp = GetTcpSoapBinding (context.WsdlBinding);
if (soapTcp != null) {
ImportNetTcpBinding (importer, context, custom, soapTcp);
return;
}
}
internal static WS.SoapBinding GetHttpSoapBinding (WS.Binding binding)
{
WS.SoapBinding soap = null;
foreach (var extension in binding.Extensions) {
var check = extension as WS.SoapBinding;
if (check != null) {
soap = check;
break;
}
}
if (soap == null)
return null;
if (soap.Transport != WS.SoapBinding.HttpTransport)
return null;
if (soap.Style != WS.SoapBindingStyle.Document)
return null;
return soap;
}
const string TcpTransport = "http://schemas.microsoft.com/soap/tcp";
internal static WS.Soap12Binding GetTcpSoapBinding (WS.Binding binding)
{
WS.Soap12Binding soap = null;
foreach (var extension in binding.Extensions) {
var check = extension as WS.Soap12Binding;
if (check != null) {
soap = check;
break;
}
}
if (soap == null)
return null;
if (soap.Transport != TcpTransport)
return null;
if (soap.Style != WS.SoapBindingStyle.Document)
return null;
return soap;
}
bool ImportBasicHttpBinding (
WsdlImporter importer, WsdlEndpointConversionContext context,
CustomBinding custom, WS.SoapBinding soap)
{
TransportBindingElement transportElement = null;
MtomMessageEncodingBindingElement mtomElement = null;
TextMessageEncodingBindingElement textElement = null;
bool foundUnknownElement = false;
foreach (var element in custom.Elements) {
if (element is TransportBindingElement)
transportElement = (TransportBindingElement)element;
else if (element is MtomMessageEncodingBindingElement)
mtomElement = (MtomMessageEncodingBindingElement)element;
else if (element is TextMessageEncodingBindingElement)
textElement = (TextMessageEncodingBindingElement)element;
else {
importer.AddWarning (
"Found unknown binding element `{0}' while attempting " +
"to import binding `{0}'.", element.GetType (),
custom.Name);
foundUnknownElement = true;
}
}
if (foundUnknownElement)
return false;
if ((mtomElement != null) && (textElement != null)) {
// FIXME: Should never happen
importer.AddWarning (
"Found both MtomMessageEncodingBindingElement and " +
"TextMessageEncodingBindingElement while attempting to " +
"import binding `{0}'.", custom.Name);
return false;
}
BasicHttpBinding httpBinding;
AuthenticationSchemes authScheme;
/*
* FIXME: Maybe make the BasicHttpBinding use the transport element
* that we created with the TransportBindingElementImporter ?
*
* There seems to be no public API to do that, so maybe add a private .ctor ?
*
*/
var httpsTransport = transportElement as HttpsTransportBindingElement;
var httpTransport = transportElement as HttpTransportBindingElement;
if (httpsTransport != null) {
httpBinding = new BasicHttpBinding (BasicHttpSecurityMode.Transport);
authScheme = httpsTransport.AuthenticationScheme;
} else if (httpTransport != null) {
authScheme = httpTransport.AuthenticationScheme;
if ((authScheme != AuthenticationSchemes.None) &&
(authScheme != AuthenticationSchemes.Anonymous))
httpBinding = new BasicHttpBinding (
BasicHttpSecurityMode.TransportCredentialOnly);
else
httpBinding = new BasicHttpBinding ();
} else {
httpBinding = new BasicHttpBinding ();
authScheme = AuthenticationSchemes.Anonymous;
}
if (mtomElement != null)
httpBinding.MessageEncoding = WSMessageEncoding.Mtom;
else if (textElement != null)
httpBinding.MessageEncoding = WSMessageEncoding.Text;
else {
importer.AddWarning (
"Found neither MtomMessageEncodingBindingElement nor " +
"TextMessageEncodingBindingElement while attempting to " +
"import binding `{0}'.", custom.Name);
return false;
}
httpBinding.Name = context.Endpoint.Binding.Name;
httpBinding.Namespace = context.Endpoint.Binding.Namespace;
switch (authScheme) {
case AuthenticationSchemes.None:
case AuthenticationSchemes.Anonymous:
httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
break;
case AuthenticationSchemes.Basic:
httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
break;
case AuthenticationSchemes.Digest:
httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Digest;
break;
case AuthenticationSchemes.Ntlm:
httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
break;
case AuthenticationSchemes.Negotiate:
httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
break;
default:
importer.AddWarning ("Invalid auth scheme: {0}", authScheme);
return false;
}
if ((httpsTransport != null) && httpsTransport.RequireClientCertificate) {
if (httpBinding.Security.Transport.ClientCredentialType != HttpClientCredentialType.None) {
importer.AddWarning ("Cannot use both client certificate and explicit auth type.");
return false;
}
httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
}
context.Endpoint.Binding = httpBinding;
return true;
}
bool ImportNetTcpBinding (
WsdlImporter importer, WsdlEndpointConversionContext context,
CustomBinding custom, WS.Soap12Binding soap)
{
TcpTransportBindingElement transportElement = null;
BinaryMessageEncodingBindingElement binaryElement = null;
TransactionFlowBindingElement transactionFlowElement = null;
WindowsStreamSecurityBindingElement windowsStreamElement = null;
SslStreamSecurityBindingElement sslStreamElement = null;
bool foundUnknownElement = false;
foreach (var element in custom.Elements) {
if (element is TcpTransportBindingElement)
transportElement = (TcpTransportBindingElement)element;
else if (element is BinaryMessageEncodingBindingElement)
binaryElement = (BinaryMessageEncodingBindingElement)element;
else if (element is TransactionFlowBindingElement)
transactionFlowElement = (TransactionFlowBindingElement)element;
else if (element is WindowsStreamSecurityBindingElement)
windowsStreamElement = (WindowsStreamSecurityBindingElement)element;
else if (element is SslStreamSecurityBindingElement)
sslStreamElement = (SslStreamSecurityBindingElement)element;
else {
importer.AddWarning (
"Found unknown binding element `{0}' while importing " +
"binding `{1}'.", element.GetType (), custom.Name);
foundUnknownElement = true;
}
}
if (foundUnknownElement)
return false;
if (transportElement == null) {
importer.AddWarning (
"Missing TcpTransportBindingElement while importing " +
"binding `{0}'.", custom.Name);
return false;
}
if (binaryElement == null) {
importer.AddWarning (
"Missing BinaryMessageEncodingBindingElement while importing " +
"binding `{0}'.", custom.Name);
return false;
}
if ((windowsStreamElement != null) && (sslStreamElement != null)) {
importer.AddWarning (
"Found both WindowsStreamSecurityBindingElement and " +
"SslStreamSecurityBindingElement while importing binding `{0}.",
custom.Name);
return false;
}
NetTcpSecurity security;
if (windowsStreamElement != null) {
security = new NetTcpSecurity (SecurityMode.Transport);
security.Transport.ProtectionLevel = windowsStreamElement.ProtectionLevel;
} else if (sslStreamElement != null) {
security = new NetTcpSecurity (SecurityMode.TransportWithMessageCredential);
} else {
security = new NetTcpSecurity (SecurityMode.None);
}
var netTcp = new NetTcpBinding (transportElement, security, false);
netTcp.Name = context.Endpoint.Binding.Name;
netTcp.Namespace = context.Endpoint.Binding.Namespace;
context.Endpoint.Binding = netTcp;
return true;
}
#endregion
}
}
|