DiscoveryReference.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. using System.IO;
  11. using System.Xml.Serialization;
  12. namespace System.Web.Services.Discovery {
  13. public abstract class DiscoveryReference {
  14. #region Fields
  15. private string defaultFilename;
  16. private DiscoveryClientProtocol clientProtocol;
  17. #endregion // Fields
  18. #region Constructors
  19. protected DiscoveryReference ()
  20. {
  21. }
  22. #endregion // Constructors
  23. #region Properties
  24. [XmlIgnore]
  25. public DiscoveryClientProtocol ClientProtocol {
  26. get { return clientProtocol; }
  27. set { clientProtocol = value; }
  28. }
  29. public virtual string DefaultFilename {
  30. get { return FilenameFromUrl (Url); }
  31. }
  32. [XmlIgnore]
  33. public abstract string Url {
  34. get;
  35. set;
  36. }
  37. #endregion // Properties
  38. #region Methods
  39. protected static string FilenameFromUrl (string url)
  40. {
  41. if (url.ToLower().EndsWith ("/wsdl"))
  42. url = url.Substring (0,url.Length-5);
  43. else if (url.ToLower().EndsWith ("/soap"))
  44. url = url.Substring (0,url.Length-5);
  45. else if (url.ToLower().EndsWith ("/wsdl.jsp"))
  46. url = url.Substring (0,url.Length-9);
  47. else if (url.ToLower().EndsWith ("/soap.wsdl"))
  48. url = url.Substring (0,url.Length-10);
  49. int i = url.LastIndexOf ("/");
  50. if (i != -1) url = url.Substring (i+1);
  51. i = url.IndexOfAny (new char[] {'.','?','\\'});
  52. if (i != -1) url = url.Substring (0,i);
  53. System.Text.StringBuilder sb = new System.Text.StringBuilder ();
  54. for (int n=0; n<url.Length; n++)
  55. if (Char.IsLetterOrDigit (url[n]) || url[n] == '_') sb.Append (url[n]);
  56. return sb.ToString ();
  57. }
  58. public abstract object ReadDocument (Stream stream);
  59. public void Resolve ()
  60. {
  61. if (clientProtocol == null)
  62. throw new InvalidOperationException ("The ClientProtocol property is a null reference.");
  63. if (clientProtocol.Documents.Contains (Url)) // Already resolved
  64. return;
  65. try
  66. {
  67. string contentType = null;
  68. string url = Url;
  69. Stream stream = clientProtocol.Download (ref url, ref contentType);
  70. Resolve (contentType, stream);
  71. }
  72. catch (Exception ex)
  73. {
  74. ReportError (Url, ex);
  75. }
  76. }
  77. protected internal abstract void Resolve (string contentType, Stream stream);
  78. public abstract void WriteDocument (object document, Stream stream);
  79. internal void ReportError (string url, Exception ex)
  80. {
  81. if (ex is DiscoveryException) throw ex;
  82. else throw new DiscoveryException (url, ex);
  83. }
  84. #endregion // Methods
  85. }
  86. }