DiscoveryClientProtocol.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. ((ContractReference)refe).ResolveInternal (this, wsdl);
  88. }
  89. else
  90. {
  91. XmlSchema schema = XmlSchema.Read (reader, null);
  92. documents.Add (url, schema);
  93. doc = new DiscoveryDocument ();
  94. refe = new SchemaReference ();
  95. doc.References.Add (refe);
  96. }
  97. refe.ClientProtocol = this;
  98. refe.Url = url;
  99. references.Add (url, refe);
  100. reader.Close ();
  101. return doc;
  102. }
  103. public Stream Download (ref string url)
  104. {
  105. string contentType = null;
  106. return Download (ref url, ref contentType);
  107. }
  108. public Stream Download (ref string url, ref string contentType)
  109. {
  110. WebRequest request = GetWebRequest (new Uri(url));
  111. WebResponse resp = request.GetResponse ();
  112. contentType = resp.ContentType;
  113. return resp.GetResponseStream ();
  114. }
  115. public DiscoveryClientResultCollection ReadAll (string topLevelFilename)
  116. {
  117. StreamReader sr = new StreamReader (topLevelFilename);
  118. XmlSerializer ser = new XmlSerializer (typeof (DiscoveryClientResultsFile));
  119. DiscoveryClientResultsFile resfile = (DiscoveryClientResultsFile) ser.Deserialize (sr);
  120. sr.Close ();
  121. foreach (DiscoveryClientResult dcr in resfile.Results)
  122. {
  123. Type type = Type.GetType (dcr.ReferenceTypeName);
  124. DiscoveryReference dr = (DiscoveryReference) Activator.CreateInstance (type);
  125. dr.Url = dcr.Url;
  126. FileStream fs = new FileStream (dcr.Filename, FileMode.Open, FileAccess.Read);
  127. Documents.Add (dr.Url, dr.ReadDocument (fs));
  128. fs.Close ();
  129. References.Add (dr.Url, dr);
  130. }
  131. return resfile.Results;
  132. }
  133. public void ResolveAll ()
  134. {
  135. ArrayList list = new ArrayList (References.Values);
  136. foreach (DiscoveryReference re in list)
  137. {
  138. if (re is DiscoveryDocumentReference)
  139. ((DiscoveryDocumentReference)re).ResolveAll ();
  140. else
  141. re.Resolve ();
  142. }
  143. }
  144. public void ResolveOneLevel ()
  145. {
  146. ArrayList list = new ArrayList (References.Values);
  147. foreach (DiscoveryReference re in list)
  148. re.Resolve ();
  149. }
  150. public DiscoveryClientResultCollection WriteAll (string directory, string topLevelFilename)
  151. {
  152. DiscoveryClientResultsFile resfile = new DiscoveryClientResultsFile();
  153. foreach (DiscoveryReference re in References.Values)
  154. {
  155. object doc = Documents [re.Url];
  156. if (doc == null) continue;
  157. string fileName = FindValidName (resfile, re.DefaultFilename);
  158. resfile.Results.Add (new DiscoveryClientResult (re.GetType(), re.Url, fileName));
  159. string filepath = Path.Combine (directory, fileName);
  160. FileStream fs = new FileStream (filepath, FileMode.Create, FileAccess.Write);
  161. re.WriteDocument (doc, fs);
  162. fs.Close ();
  163. }
  164. StreamWriter sw = new StreamWriter (Path.Combine (directory, topLevelFilename));
  165. XmlSerializer ser = new XmlSerializer (typeof (DiscoveryClientResultsFile));
  166. ser.Serialize (sw, resfile);
  167. sw.Close ();
  168. return resfile.Results;
  169. }
  170. string FindValidName (DiscoveryClientResultsFile resfile, string baseName)
  171. {
  172. string name = baseName;
  173. int id = 0;
  174. bool found;
  175. do
  176. {
  177. found = false;
  178. foreach (DiscoveryClientResult res in resfile.Results)
  179. {
  180. if (name == res.Filename) {
  181. found = true; break;
  182. }
  183. }
  184. if (found)
  185. name = Path.GetFileNameWithoutExtension (baseName) + (++id) + Path.GetExtension (baseName);
  186. }
  187. while (found);
  188. return name;
  189. }
  190. #endregion // Methods
  191. #region Classes
  192. public sealed class DiscoveryClientResultsFile {
  193. #region Fields
  194. private DiscoveryClientResultCollection results;
  195. #endregion // Fields
  196. #region Contructors
  197. public DiscoveryClientResultsFile ()
  198. {
  199. results = new DiscoveryClientResultCollection ();
  200. }
  201. #endregion // Constructors
  202. #region Properties
  203. public DiscoveryClientResultCollection Results {
  204. get { return results; }
  205. }
  206. #endregion // Properties
  207. }
  208. #endregion // Classes
  209. }
  210. }