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
|
//
// ServiceMetadataExtension.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
// Ankit Jain <jankit@novell.com>
// Gonzalo Paniagua Javier (gonzalo@novell.com)
//
// Copyright (C) 2005 Novell, Inc. http://www.novell.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.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.IO;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.Web;
using System.Web.Services;
using System.Web.Services.Description;
using System.Xml;
using System.Xml.Schema;
using WSServiceDescription = System.Web.Services.Description.ServiceDescription;
using WSMessage = System.Web.Services.Description.Message;
using SMMessage = System.ServiceModel.Channels.Message;
namespace System.ServiceModel.Description
{
public class ServiceMetadataExtension : IExtension<ServiceHostBase>
{
const string ServiceMetadataBehaviorHttpGetBinding = "ServiceMetadataBehaviorHttpGetBinding";
MetadataSet metadata;
ServiceHostBase owner;
Dictionary<Uri, ChannelDispatcherBase> _serviceMetadataChanelDispatchers;
[MonoTODO]
public ServiceMetadataExtension ()
{
}
[MonoTODO]
public MetadataSet Metadata {
get {
if (metadata == null) {
MetadataExporter exporter = new WsdlExporter ();
foreach (ServiceEndpoint ep in owner.Description.Endpoints) {
if (ep.Contract.Name == ServiceMetadataBehavior.MexContractName)
continue;
exporter.ExportEndpoint (ep);
}
metadata = exporter.GetGeneratedMetadata ();
}
return metadata;
}
}
internal ServiceHostBase Owner {
get { return owner; }
}
internal static ServiceMetadataExtension EnsureServiceMetadataExtension (ServiceDescription description, ServiceHostBase serviceHostBase) {
ServiceMetadataExtension sme = serviceHostBase.Extensions.Find<ServiceMetadataExtension> ();
if (sme == null) {
sme = new ServiceMetadataExtension ();
serviceHostBase.Extensions.Add (sme);
}
return sme;
}
internal static void EnsureServiceMetadataHttpChanelDispatcher (ServiceDescription description, ServiceHostBase serviceHostBase, ServiceMetadataExtension sme, Uri uri) {
if (sme._serviceMetadataChanelDispatchers == null)
sme._serviceMetadataChanelDispatchers = new Dictionary<Uri, ChannelDispatcherBase> ();
else if (sme._serviceMetadataChanelDispatchers.ContainsKey (uri))
return;
CustomBinding cb = new CustomBinding (new BasicHttpBinding ())
{
Name = ServiceMetadataBehaviorHttpGetBinding,
};
cb.Elements.Find<MessageEncodingBindingElement> ().MessageVersion = MessageVersion.None;
ServiceEndpoint se = new ServiceEndpoint (ContractDescription.GetContract (typeof (IHttpGetHelpPageAndMetadataContract)), cb, new EndpointAddress (uri))
{
ListenUri = uri,
};
ChannelDispatcher channelDispatcher = serviceHostBase.BuildChannelDispatcher (se, new BindingParameterCollection ());
channelDispatcher.Endpoints [0].DispatchRuntime.InstanceContextProvider = new SingletonInstanceContextProvider (new InstanceContext (serviceHostBase, new HttpGetWsdl (sme, uri)));
sme._serviceMetadataChanelDispatchers.Add (uri, channelDispatcher);
serviceHostBase.ChannelDispatchers.Add (channelDispatcher);
}
internal static void EnsureServiceMetadataHttpsChanelDispatcher (ServiceDescription description, ServiceHostBase serviceHostBase, ServiceMetadataExtension sme, Uri uri) {
throw new NotImplementedException ();
}
void IExtension<ServiceHostBase>.Attach (ServiceHostBase owner)
{
this.owner = owner;
}
[MonoTODO]
void IExtension<ServiceHostBase>.Detach (ServiceHostBase owner)
{
throw new NotImplementedException ();
}
}
[ServiceContract (Namespace = "http://schemas.microsoft.com/2006/04/http/metadata")]
interface IHttpGetHelpPageAndMetadataContract
{
[OperationContract (Action = "*", ReplyAction = "*")]
SMMessage Get (SMMessage req);
}
class HttpGetWsdl : IHttpGetHelpPageAndMetadataContract
{
ServiceMetadataExtension metadata_extn;
Uri base_uri;
Dictionary <string,WSServiceDescription> wsdl_documents =
new Dictionary<string, WSServiceDescription> ();
Dictionary <string, XmlSchema> schemas =
new Dictionary<string, XmlSchema> ();
public HttpGetWsdl (ServiceMetadataExtension metadata_extn, Uri base_uri)
{
this.metadata_extn = metadata_extn;
this.base_uri = base_uri;
GetMetadata (metadata_extn.Owner);
}
public SMMessage Get (SMMessage req)
{
HttpRequestMessageProperty prop = (HttpRequestMessageProperty) req.Properties [HttpRequestMessageProperty.Name];
NameValueCollection query_string = CreateQueryString (prop.QueryString);
if (query_string == null || query_string.AllKeys.Length != 1)
return CreateHelpPage (req);
if (query_string [null] == "wsdl") {
WSServiceDescription wsdl = GetWsdl ("wsdl");
if (wsdl != null)
return CreateWsdlMessage (wsdl);
} else if (query_string ["wsdl"] != null) {
WSServiceDescription wsdl = GetWsdl (query_string ["wsdl"]);
if (wsdl != null)
return CreateWsdlMessage (wsdl);
} else if (query_string ["xsd"] != null) {
XmlSchema schema = GetXmlSchema (query_string ["xsd"]);
if (schema != null) {
//FIXME: Is this the correct way?
MemoryStream ms = new MemoryStream ();
schema.Write (ms);
ms.Seek (0, SeekOrigin.Begin);
SMMessage ret = SMMessage.CreateMessage (MessageVersion.None, "", XmlReader.Create (ms));
return ret;
}
}
return CreateHelpPage (req);
}
/* Code from HttpListenerRequest */
NameValueCollection CreateQueryString (string query)
{
NameValueCollection query_string = new NameValueCollection ();
if (query == null || query.Length == 0)
return null;
string [] components = query.Split ('&');
foreach (string kv in components) {
int pos = kv.IndexOf ('=');
if (pos == -1) {
query_string.Add (null, HttpUtility.UrlDecode (kv));
} else {
string key = HttpUtility.UrlDecode (kv.Substring (0, pos));
string val = HttpUtility.UrlDecode (kv.Substring (pos + 1));
query_string.Add (key, val);
}
}
return query_string;
}
SMMessage CreateHelpPage (SMMessage request)
{
//FIXME Check for ServiceDebugBehavior.HttpHelpPage
//else do what? Check
throw new NotImplementedException ();
}
SMMessage CreateWsdlMessage (WSServiceDescription wsdl)
{
MemoryStream ms = new MemoryStream ();
XmlWriter xw = XmlWriter.Create (ms);
WSServiceDescription.Serializer.Serialize (xw, wsdl);
ms.Seek (0, SeekOrigin.Begin);
return SMMessage.CreateMessage (MessageVersion.None, "", XmlReader.Create (ms));
}
void GetMetadata (ServiceHostBase host)
{
MetadataSet metadata = metadata_extn.Metadata;
int xs_i = 0, wsdl_i = 0;
//Dictionary keyed by namespace
StringDictionary wsdl_strings = new StringDictionary ();
StringDictionary xsd_strings = new StringDictionary ();
foreach (MetadataSection section in metadata.MetadataSections) {
string key;
XmlSchema xs = section.Metadata as XmlSchema;
if (xs != null) {
key = String.Format ("xsd{0}", xs_i ++);
schemas [key] = xs;
xsd_strings [xs.TargetNamespace] = key;
continue;
}
WSServiceDescription wsdl = section.Metadata as WSServiceDescription;
if (wsdl == null)
continue;
//if (wsdl.TargetNamespace == "http://tempuri.org/")
if (wsdl.Services.Count > 0)
key = "wsdl";
else
key = String.Format ("wsdl{0}", wsdl_i ++);
wsdl_documents [key] = wsdl;
wsdl_strings [wsdl.TargetNamespace] = key;
}
string base_url = base_uri.ToString ();
foreach (WSServiceDescription wsdl in wsdl_documents.Values) {
foreach (Import import in wsdl.Imports) {
if (!String.IsNullOrEmpty (import.Location))
continue;
import.Location = String.Format ("{0}?wsdl={1}", base_url, wsdl_strings [import.Namespace]);
}
foreach (XmlSchema schema in wsdl.Types.Schemas) {
foreach (XmlSchemaObject obj in schema.Includes) {
XmlSchemaImport imp = obj as XmlSchemaImport;
if (imp == null || imp.SchemaLocation != null)
continue;
imp.SchemaLocation = String.Format ("{0}?xsd={1}", base_url, xsd_strings [imp.Namespace]);
}
}
}
}
WSServiceDescription GetWsdl (string which)
{
WSServiceDescription wsdl;
wsdl_documents.TryGetValue (which, out wsdl);
return wsdl;
}
XmlSchema GetXmlSchema (string which)
{
XmlSchema schema;
schemas.TryGetValue (which, out schema);
return schema;
}
}
}
|