ServiceMetadataExtension.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //
  2. // ServiceMetadataExtension.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. // Ankit Jain <[email protected]>
  7. // Gonzalo Paniagua Javier ([email protected])
  8. //
  9. // Copyright (C) 2005 Novell, Inc. http://www.novell.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;
  32. using System.Collections.Generic;
  33. using System.Collections.ObjectModel;
  34. using System.Collections.Specialized;
  35. using System.IO;
  36. using System.ServiceModel.Channels;
  37. using System.ServiceModel.Dispatcher;
  38. using System.Web;
  39. using System.Web.Services;
  40. using System.Web.Services.Description;
  41. using System.Xml;
  42. using System.Xml.Schema;
  43. using WSServiceDescription = System.Web.Services.Description.ServiceDescription;
  44. using WSMessage = System.Web.Services.Description.Message;
  45. using SMMessage = System.ServiceModel.Channels.Message;
  46. namespace System.ServiceModel.Description
  47. {
  48. public class ServiceMetadataExtension : IExtension<ServiceHostBase>
  49. {
  50. const string ServiceMetadataBehaviorHttpGetBinding = "ServiceMetadataBehaviorHttpGetBinding";
  51. MetadataSet metadata;
  52. ServiceHostBase owner;
  53. Dictionary<Uri, ChannelDispatcherBase> _serviceMetadataChanelDispatchers;
  54. [MonoTODO]
  55. public ServiceMetadataExtension ()
  56. {
  57. }
  58. [MonoTODO]
  59. public MetadataSet Metadata {
  60. get {
  61. if (metadata == null) {
  62. MetadataExporter exporter = new WsdlExporter ();
  63. foreach (ServiceEndpoint ep in owner.Description.Endpoints) {
  64. if (ep.Contract.Name == ServiceMetadataBehavior.MexContractName)
  65. continue;
  66. exporter.ExportEndpoint (ep);
  67. }
  68. metadata = exporter.GetGeneratedMetadata ();
  69. }
  70. return metadata;
  71. }
  72. }
  73. internal ServiceHostBase Owner {
  74. get { return owner; }
  75. }
  76. internal static ServiceMetadataExtension EnsureServiceMetadataExtension (ServiceDescription description, ServiceHostBase serviceHostBase) {
  77. ServiceMetadataExtension sme = serviceHostBase.Extensions.Find<ServiceMetadataExtension> ();
  78. if (sme == null) {
  79. sme = new ServiceMetadataExtension ();
  80. serviceHostBase.Extensions.Add (sme);
  81. }
  82. return sme;
  83. }
  84. internal static void EnsureServiceMetadataHttpChanelDispatcher (ServiceDescription description, ServiceHostBase serviceHostBase, ServiceMetadataExtension sme, Uri uri) {
  85. if (sme._serviceMetadataChanelDispatchers == null)
  86. sme._serviceMetadataChanelDispatchers = new Dictionary<Uri, ChannelDispatcherBase> ();
  87. else if (sme._serviceMetadataChanelDispatchers.ContainsKey (uri))
  88. return;
  89. CustomBinding cb = new CustomBinding (new BasicHttpBinding ())
  90. {
  91. Name = ServiceMetadataBehaviorHttpGetBinding,
  92. };
  93. cb.Elements.Find<MessageEncodingBindingElement> ().MessageVersion = MessageVersion.None;
  94. ServiceEndpoint se = new ServiceEndpoint (ContractDescription.GetContract (typeof (IHttpGetHelpPageAndMetadataContract)), cb, new EndpointAddress (uri))
  95. {
  96. ListenUri = uri,
  97. };
  98. ChannelDispatcher channelDispatcher = serviceHostBase.BuildChannelDispatcher (se, new BindingParameterCollection ());
  99. channelDispatcher.Endpoints [0].DispatchRuntime.InstanceContextProvider = new SingletonInstanceContextProvider (new InstanceContext (serviceHostBase, new HttpGetWsdl (sme, uri)));
  100. sme._serviceMetadataChanelDispatchers.Add (uri, channelDispatcher);
  101. serviceHostBase.ChannelDispatchers.Add (channelDispatcher);
  102. }
  103. internal static void EnsureServiceMetadataHttpsChanelDispatcher (ServiceDescription description, ServiceHostBase serviceHostBase, ServiceMetadataExtension sme, Uri uri) {
  104. throw new NotImplementedException ();
  105. }
  106. void IExtension<ServiceHostBase>.Attach (ServiceHostBase owner)
  107. {
  108. this.owner = owner;
  109. }
  110. [MonoTODO]
  111. void IExtension<ServiceHostBase>.Detach (ServiceHostBase owner)
  112. {
  113. throw new NotImplementedException ();
  114. }
  115. }
  116. [ServiceContract (Namespace = "http://schemas.microsoft.com/2006/04/http/metadata")]
  117. interface IHttpGetHelpPageAndMetadataContract
  118. {
  119. [OperationContract (Action = "*", ReplyAction = "*")]
  120. SMMessage Get (SMMessage req);
  121. }
  122. class HttpGetWsdl : IHttpGetHelpPageAndMetadataContract
  123. {
  124. ServiceMetadataExtension metadata_extn;
  125. Uri base_uri;
  126. Dictionary <string,WSServiceDescription> wsdl_documents =
  127. new Dictionary<string, WSServiceDescription> ();
  128. Dictionary <string, XmlSchema> schemas =
  129. new Dictionary<string, XmlSchema> ();
  130. public HttpGetWsdl (ServiceMetadataExtension metadata_extn, Uri base_uri)
  131. {
  132. this.metadata_extn = metadata_extn;
  133. this.base_uri = base_uri;
  134. GetMetadata (metadata_extn.Owner);
  135. }
  136. public SMMessage Get (SMMessage req)
  137. {
  138. HttpRequestMessageProperty prop = (HttpRequestMessageProperty) req.Properties [HttpRequestMessageProperty.Name];
  139. NameValueCollection query_string = CreateQueryString (prop.QueryString);
  140. if (query_string == null || query_string.AllKeys.Length != 1)
  141. return CreateHelpPage (req);
  142. if (query_string [null] == "wsdl") {
  143. WSServiceDescription wsdl = GetWsdl ("wsdl");
  144. if (wsdl != null)
  145. return CreateWsdlMessage (wsdl);
  146. } else if (query_string ["wsdl"] != null) {
  147. WSServiceDescription wsdl = GetWsdl (query_string ["wsdl"]);
  148. if (wsdl != null)
  149. return CreateWsdlMessage (wsdl);
  150. } else if (query_string ["xsd"] != null) {
  151. XmlSchema schema = GetXmlSchema (query_string ["xsd"]);
  152. if (schema != null) {
  153. //FIXME: Is this the correct way?
  154. MemoryStream ms = new MemoryStream ();
  155. schema.Write (ms);
  156. ms.Seek (0, SeekOrigin.Begin);
  157. SMMessage ret = SMMessage.CreateMessage (MessageVersion.None, "", XmlReader.Create (ms));
  158. return ret;
  159. }
  160. }
  161. return CreateHelpPage (req);
  162. }
  163. /* Code from HttpListenerRequest */
  164. NameValueCollection CreateQueryString (string query)
  165. {
  166. NameValueCollection query_string = new NameValueCollection ();
  167. if (query == null || query.Length == 0)
  168. return null;
  169. string [] components = query.Split ('&');
  170. foreach (string kv in components) {
  171. int pos = kv.IndexOf ('=');
  172. if (pos == -1) {
  173. query_string.Add (null, HttpUtility.UrlDecode (kv));
  174. } else {
  175. string key = HttpUtility.UrlDecode (kv.Substring (0, pos));
  176. string val = HttpUtility.UrlDecode (kv.Substring (pos + 1));
  177. query_string.Add (key, val);
  178. }
  179. }
  180. return query_string;
  181. }
  182. SMMessage CreateHelpPage (SMMessage request)
  183. {
  184. //FIXME Check for ServiceDebugBehavior.HttpHelpPage
  185. //else do what? Check
  186. throw new NotImplementedException ();
  187. }
  188. SMMessage CreateWsdlMessage (WSServiceDescription wsdl)
  189. {
  190. MemoryStream ms = new MemoryStream ();
  191. XmlWriter xw = XmlWriter.Create (ms);
  192. WSServiceDescription.Serializer.Serialize (xw, wsdl);
  193. ms.Seek (0, SeekOrigin.Begin);
  194. return SMMessage.CreateMessage (MessageVersion.None, "", XmlReader.Create (ms));
  195. }
  196. void GetMetadata (ServiceHostBase host)
  197. {
  198. MetadataSet metadata = metadata_extn.Metadata;
  199. int xs_i = 0, wsdl_i = 0;
  200. //Dictionary keyed by namespace
  201. StringDictionary wsdl_strings = new StringDictionary ();
  202. StringDictionary xsd_strings = new StringDictionary ();
  203. foreach (MetadataSection section in metadata.MetadataSections) {
  204. string key;
  205. XmlSchema xs = section.Metadata as XmlSchema;
  206. if (xs != null) {
  207. key = String.Format ("xsd{0}", xs_i ++);
  208. schemas [key] = xs;
  209. xsd_strings [xs.TargetNamespace] = key;
  210. continue;
  211. }
  212. WSServiceDescription wsdl = section.Metadata as WSServiceDescription;
  213. if (wsdl == null)
  214. continue;
  215. //if (wsdl.TargetNamespace == "http://tempuri.org/")
  216. if (wsdl.Services.Count > 0)
  217. key = "wsdl";
  218. else
  219. key = String.Format ("wsdl{0}", wsdl_i ++);
  220. wsdl_documents [key] = wsdl;
  221. wsdl_strings [wsdl.TargetNamespace] = key;
  222. }
  223. string base_url = base_uri.ToString ();
  224. foreach (WSServiceDescription wsdl in wsdl_documents.Values) {
  225. foreach (Import import in wsdl.Imports) {
  226. if (!String.IsNullOrEmpty (import.Location))
  227. continue;
  228. import.Location = String.Format ("{0}?wsdl={1}", base_url, wsdl_strings [import.Namespace]);
  229. }
  230. foreach (XmlSchema schema in wsdl.Types.Schemas) {
  231. foreach (XmlSchemaObject obj in schema.Includes) {
  232. XmlSchemaImport imp = obj as XmlSchemaImport;
  233. if (imp == null || imp.SchemaLocation != null)
  234. continue;
  235. imp.SchemaLocation = String.Format ("{0}?xsd={1}", base_url, xsd_strings [imp.Namespace]);
  236. }
  237. }
  238. }
  239. }
  240. WSServiceDescription GetWsdl (string which)
  241. {
  242. WSServiceDescription wsdl;
  243. wsdl_documents.TryGetValue (which, out wsdl);
  244. return wsdl;
  245. }
  246. XmlSchema GetXmlSchema (string which)
  247. {
  248. XmlSchema schema;
  249. schemas.TryGetValue (which, out schema);
  250. return schema;
  251. }
  252. }
  253. }