DiscoveryReference.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // System.Web.Services.Discovery.DiscoveryReference.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.Xml.Serialization;
  32. namespace System.Web.Services.Discovery {
  33. public abstract class DiscoveryReference {
  34. #region Fields
  35. private string defaultFilename;
  36. private DiscoveryClientProtocol clientProtocol;
  37. #endregion // Fields
  38. #region Constructors
  39. protected DiscoveryReference ()
  40. {
  41. }
  42. #endregion // Constructors
  43. #region Properties
  44. [XmlIgnore]
  45. public DiscoveryClientProtocol ClientProtocol {
  46. get { return clientProtocol; }
  47. set { clientProtocol = value; }
  48. }
  49. [XmlIgnore]
  50. public virtual string DefaultFilename {
  51. get { return FilenameFromUrl (Url); }
  52. }
  53. [XmlIgnore]
  54. public abstract string Url {
  55. get;
  56. set;
  57. }
  58. internal Uri BaseUri
  59. {
  60. get
  61. {
  62. int i = Url.IndexOf ("://");
  63. if (i == -1 || !Uri.CheckSchemeName (Url.Substring (0,i)))
  64. return new Uri ("file://" + Url);
  65. else
  66. return new Uri (Url);
  67. }
  68. }
  69. #endregion // Properties
  70. #region Methods
  71. protected static string FilenameFromUrl (string url)
  72. {
  73. if (url.ToLower().EndsWith ("/wsdl"))
  74. url = url.Substring (0,url.Length-5);
  75. else if (url.ToLower().EndsWith ("/soap"))
  76. url = url.Substring (0,url.Length-5);
  77. else if (url.ToLower().EndsWith ("/wsdl.jsp"))
  78. url = url.Substring (0,url.Length-9);
  79. else if (url.ToLower().EndsWith ("/soap.wsdl"))
  80. url = url.Substring (0,url.Length-10);
  81. int i = url.LastIndexOf ("/");
  82. if (i != -1) url = url.Substring (i+1);
  83. i = url.IndexOfAny (new char[] {'.','?','\\'});
  84. if (i != -1) url = url.Substring (0,i);
  85. System.Text.StringBuilder sb = new System.Text.StringBuilder ();
  86. for (int n=0; n<url.Length; n++)
  87. if (Char.IsLetterOrDigit (url[n]) || url[n] == '_') sb.Append (url[n]);
  88. return sb.ToString ();
  89. }
  90. public abstract object ReadDocument (Stream stream);
  91. public void Resolve ()
  92. {
  93. if (clientProtocol == null)
  94. throw new InvalidOperationException ("The ClientProtocol property is a null reference.");
  95. if (clientProtocol.Documents.Contains (Url)) // Already resolved
  96. return;
  97. try
  98. {
  99. string contentType = null;
  100. string url = Url;
  101. Stream stream = clientProtocol.Download (ref url, ref contentType);
  102. Resolve (contentType, stream);
  103. }
  104. catch (Exception ex)
  105. {
  106. ReportError (Url, ex);
  107. }
  108. }
  109. protected internal abstract void Resolve (string contentType, Stream stream);
  110. public abstract void WriteDocument (object document, Stream stream);
  111. internal void ReportError (string url, Exception ex)
  112. {
  113. if (ex is DiscoveryException) throw ex;
  114. else throw new DiscoveryException (url, ex);
  115. }
  116. #endregion // Methods
  117. }
  118. }