DiscoveryClientProtocol.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // System.Web.Services.Protocols.DiscoveryClientProtocol.cs
  3. //
  4. // Author:
  5. // Dave Bettin ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Copyright (C) Dave Bettin, 2002
  9. //
  10. using System.Collections;
  11. using System.IO;
  12. using System.Web.Services.Protocols;
  13. using System.Web.Services.Description;
  14. using System.Xml;
  15. using System.Xml.Schema;
  16. using System.Xml.Serialization;
  17. using System.Net;
  18. namespace System.Web.Services.Discovery {
  19. public class DiscoveryClientProtocol : HttpWebClientProtocol {
  20. #region Fields
  21. private IList additionalInformation;
  22. private DiscoveryClientDocumentCollection documents = new DiscoveryClientDocumentCollection();
  23. private DiscoveryExceptionDictionary errors = new DiscoveryExceptionDictionary();
  24. private DiscoveryClientReferenceCollection references = new DiscoveryClientReferenceCollection();
  25. #endregion // Fields
  26. #region Constructors
  27. public DiscoveryClientProtocol ()
  28. {
  29. }
  30. #endregion // Constructors
  31. #region Properties
  32. public IList AdditionalInformation {
  33. get { return additionalInformation; }
  34. }
  35. public DiscoveryClientDocumentCollection Documents {
  36. get { return documents; }
  37. }
  38. public DiscoveryExceptionDictionary Errors {
  39. get { return errors; }
  40. }
  41. public DiscoveryClientReferenceCollection References {
  42. get { return references; }
  43. }
  44. #endregion // Properties
  45. #region Methods
  46. public DiscoveryDocument Discover (string url)
  47. {
  48. Stream stream = Download (ref url);
  49. XmlTextReader reader = new XmlTextReader (stream);
  50. if (!DiscoveryDocument.CanRead (reader))
  51. throw new InvalidOperationException ("The url '" + url + "' does not point to a valid discovery document");
  52. DiscoveryDocument doc = DiscoveryDocument.Read (reader);
  53. reader.Close ();
  54. documents.Add (url, doc);
  55. foreach (DiscoveryReference re in doc.References)
  56. {
  57. re.ClientProtocol = this;
  58. references.Add (re);
  59. }
  60. return doc;
  61. }
  62. public DiscoveryDocument DiscoverAny (string url)
  63. {
  64. Stream stream = Download (ref url);
  65. XmlTextReader reader = new XmlTextReader (stream);
  66. reader.MoveToContent ();
  67. DiscoveryDocument doc;
  68. DiscoveryReference refe = null;
  69. if (DiscoveryDocument.CanRead (reader))
  70. {
  71. doc = DiscoveryDocument.Read (reader);
  72. documents.Add (url, doc);
  73. refe = new DiscoveryDocumentReference ();
  74. foreach (DiscoveryReference re in doc.References)
  75. {
  76. re.ClientProtocol = this;
  77. references.Add (re.Url, re);
  78. }
  79. }
  80. else if (ServiceDescription.CanRead (reader))
  81. {
  82. ServiceDescription wsdl = ServiceDescription.Read (reader);
  83. documents.Add (url, wsdl);
  84. doc = new DiscoveryDocument ();
  85. refe = new ContractReference ();
  86. doc.References.Add (refe);
  87. }
  88. else
  89. {
  90. XmlSchema schema = XmlSchema.Read (reader, null);
  91. documents.Add (url, schema);
  92. doc = new DiscoveryDocument ();
  93. refe = new SchemaReference ();
  94. doc.References.Add (refe);
  95. }
  96. refe.ClientProtocol = this;
  97. refe.Url = url;
  98. references.Add (url, refe);
  99. reader.Close ();
  100. return doc;
  101. }
  102. public Stream Download (ref string url)
  103. {
  104. string contentType = null;
  105. return Download (ref url, ref contentType);
  106. }
  107. public Stream Download (ref string url, ref string contentType)
  108. {
  109. WebRequest request = GetWebRequest (new Uri(url));
  110. WebResponse resp = request.GetResponse ();
  111. contentType = resp.ContentType;
  112. return resp.GetResponseStream ();
  113. }
  114. public DiscoveryClientResultCollection ReadAll (string topLevelFilename)
  115. {
  116. StreamReader sr = new StreamReader (topLevelFilename);
  117. XmlSerializer ser = new XmlSerializer (typeof (DiscoveryClientResultsFile));
  118. DiscoveryClientResultsFile resfile = (DiscoveryClientResultsFile) ser.Deserialize (sr);
  119. sr.Close ();
  120. foreach (DiscoveryClientResult dcr in resfile.Results)
  121. {
  122. Type type = Type.GetType (dcr.ReferenceTypeName);
  123. DiscoveryReference dr = (DiscoveryReference) Activator.CreateInstance (type);
  124. dr.Url = dcr.Url;
  125. FileStream fs = new FileStream (dcr.Filename, FileMode.Open, FileAccess.Read);
  126. Documents.Add (dr.Url, dr.ReadDocument (fs));
  127. fs.Close ();
  128. References.Add (dr.Url, dr);
  129. }
  130. return resfile.Results;
  131. }
  132. public void ResolveAll ()
  133. {
  134. ArrayList list = new ArrayList (References.Values);
  135. foreach (DiscoveryReference re in list)
  136. {
  137. if (re is DiscoveryDocumentReference)
  138. ((DiscoveryDocumentReference)re).ResolveAll ();
  139. else
  140. re.Resolve ();
  141. }
  142. }
  143. public void ResolveOneLevel ()
  144. {
  145. ArrayList list = new ArrayList (References.Values);
  146. foreach (DiscoveryReference re in list)
  147. re.Resolve ();
  148. }
  149. public DiscoveryClientResultCollection WriteAll (string directory, string topLevelFilename)
  150. {
  151. DiscoveryClientResultsFile resfile = new DiscoveryClientResultsFile();
  152. foreach (DiscoveryReference re in References.Values)
  153. {
  154. object doc = Documents [re.Url];
  155. if (doc == null) continue;
  156. resfile.Results.Add (new DiscoveryClientResult (re.GetType(), re.Url, re.DefaultFilename));
  157. string filepath = Path.Combine (directory, re.DefaultFilename);
  158. FileStream fs = new FileStream (filepath, FileMode.Create, FileAccess.Write);
  159. re.WriteDocument (doc, fs);
  160. fs.Close ();
  161. }
  162. StreamWriter sw = new StreamWriter (Path.Combine (directory, topLevelFilename));
  163. XmlSerializer ser = new XmlSerializer (typeof (DiscoveryClientResultsFile));
  164. ser.Serialize (sw, resfile);
  165. sw.Close ();
  166. return resfile.Results;
  167. }
  168. #endregion // Methods
  169. #region Classes
  170. public sealed class DiscoveryClientResultsFile {
  171. #region Fields
  172. private DiscoveryClientResultCollection results;
  173. #endregion // Fields
  174. #region Contructors
  175. public DiscoveryClientResultsFile ()
  176. {
  177. results = new DiscoveryClientResultCollection ();
  178. }
  179. #endregion // Constructors
  180. #region Properties
  181. public DiscoveryClientResultCollection Results {
  182. get { return results; }
  183. }
  184. #endregion // Properties
  185. }
  186. #endregion // Classes
  187. }
  188. }