ContractReference.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // System.Web.Services.Discovery.ContractReference.cs
  3. //
  4. // Author:
  5. // Dave Bettin ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Copyright (C) Dave Bettin, 2002
  9. //
  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.IO;
  31. using System.Web.Services.Description;
  32. using System.Xml.Serialization;
  33. using System.Xml;
  34. using System.Xml.Schema;
  35. namespace System.Web.Services.Discovery {
  36. [XmlRootAttribute("contractRef", Namespace="http://schemas.xmlsoap.org/disco/scl/", IsNullable=true)]
  37. public class ContractReference : DiscoveryReference {
  38. #region Fields
  39. public const string Namespace = "http://schemas.xmlsoap.org/disco/scl/";
  40. private ServiceDescription contract;
  41. private string defaultFilename;
  42. private string docRef;
  43. private string href;
  44. #endregion // Fields
  45. #region Constructors
  46. public ContractReference ()
  47. {
  48. }
  49. public ContractReference (string href) : this()
  50. {
  51. this.href = href;
  52. }
  53. public ContractReference (string href, string docRef)
  54. {
  55. this.href = href;
  56. this.docRef = docRef;
  57. }
  58. #endregion // Constructors
  59. #region Properties
  60. [XmlIgnore]
  61. public ServiceDescription Contract {
  62. get {
  63. if (ClientProtocol == null)
  64. throw new InvalidOperationException ("The ClientProtocol property is a null reference");
  65. ServiceDescription desc = ClientProtocol.Documents [Url] as ServiceDescription;
  66. if (desc == null)
  67. throw new Exception ("The Documents property of ClientProtocol does not contain a WSDL document with the url " + Url);
  68. return desc;
  69. }
  70. }
  71. [XmlIgnore]
  72. public override string DefaultFilename {
  73. get { return FilenameFromUrl (Url) + ".wsdl"; }
  74. }
  75. [XmlAttribute("docRef")]
  76. public string DocRef {
  77. get { return docRef; }
  78. set { docRef = value; }
  79. }
  80. [XmlAttribute("ref")]
  81. public string Ref {
  82. get { return href; }
  83. set { href = value; }
  84. }
  85. [XmlIgnore]
  86. public override string Url {
  87. get { return href;}
  88. set { href = value; }
  89. }
  90. #endregion // Properties
  91. #region Methods
  92. public override object ReadDocument (Stream stream)
  93. {
  94. return ServiceDescription.Read (stream);
  95. }
  96. protected internal override void Resolve (string contentType, Stream stream)
  97. {
  98. ServiceDescription wsdl = ServiceDescription.Read (stream);
  99. if (!ClientProtocol.References.Contains (Url))
  100. ClientProtocol.References.Add (this);
  101. ClientProtocol.Documents.Add (Url, wsdl);
  102. ResolveInternal (ClientProtocol, wsdl);
  103. }
  104. internal void ResolveInternal (DiscoveryClientProtocol prot, ServiceDescription wsdl)
  105. {
  106. if (wsdl.Imports == null) return;
  107. foreach (Import import in wsdl.Imports)
  108. {
  109. // Make relative uris to absoulte
  110. Uri uri = new Uri (BaseUri, import.Location);
  111. string url = uri.ToString ();
  112. if (prot.Documents.Contains (url)) // Already resolved
  113. continue;
  114. try
  115. {
  116. string contentType = null;
  117. Stream stream = prot.Download (ref url, ref contentType);
  118. XmlTextReader reader = new XmlTextReader (stream);
  119. reader.MoveToContent ();
  120. DiscoveryReference refe;
  121. if (ServiceDescription.CanRead (reader))
  122. {
  123. ServiceDescription refWsdl = ServiceDescription.Read (reader);
  124. refe = new ContractReference ();
  125. refe.ClientProtocol = prot;
  126. refe.Url = url;
  127. ((ContractReference)refe).ResolveInternal (prot, refWsdl);
  128. prot.Documents.Add (url, refWsdl);
  129. }
  130. else
  131. {
  132. XmlSchema schema = XmlSchema.Read (reader, null);
  133. refe = new SchemaReference ();
  134. refe.ClientProtocol = prot;
  135. refe.Url = url;
  136. prot.Documents.Add (url, schema);
  137. }
  138. if (!prot.References.Contains (url))
  139. prot.References.Add (refe);
  140. reader.Close ();
  141. }
  142. catch (Exception ex)
  143. {
  144. ReportError (url, ex);
  145. }
  146. }
  147. }
  148. public override void WriteDocument (object document, Stream stream)
  149. {
  150. ((ServiceDescription)document).Write (stream);
  151. }
  152. #endregion // Methods
  153. }
  154. }