DiscoveryRequestHandler.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // System.Web.Services.Discovery.DiscoveryRequestHandler.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.Web;
  31. using System.IO;
  32. using System.Web.Services.Discovery;
  33. namespace System.Web.Services.Discovery {
  34. public sealed class DiscoveryRequestHandler : IHttpHandler {
  35. #region Constructors
  36. public DiscoveryRequestHandler ()
  37. {
  38. }
  39. #endregion // Constructors
  40. #region Properties
  41. public bool IsReusable {
  42. get { return true; }
  43. }
  44. #endregion // Properties
  45. #region Methods
  46. public void ProcessRequest (HttpContext context)
  47. {
  48. string path = context.Request.PhysicalPath;
  49. FileStream fs = new FileStream (path, FileMode.Open);
  50. DynamicDiscoveryDocument ddoc = DynamicDiscoveryDocument.Load (fs);
  51. fs.Close ();
  52. string url = context.Request.Url.ToString();
  53. int i = url.LastIndexOf ('/');
  54. if (i != -1) url = url.Substring (0,i+1);
  55. DiscoveryDocument doc = new DiscoveryDocument ();
  56. GetFiles (url, "", Path.GetDirectoryName(path), doc, ddoc);
  57. context.Response.ContentType = "text/xml; charset=utf-8";
  58. doc.Write (context.Response.OutputStream);
  59. }
  60. void GetFiles (string baseUrl, string relPath, string path, DiscoveryDocument doc, DynamicDiscoveryDocument ddoc)
  61. {
  62. string url = baseUrl + relPath;
  63. if (!url.EndsWith ("/")) url += "/";
  64. string[] files = Directory.GetFiles (path);
  65. foreach (string file in files)
  66. {
  67. string ext = Path.GetExtension (file).ToLower();
  68. if (ext == ".asmx")
  69. {
  70. ContractReference cref = new ContractReference ();
  71. cref.DocRef = url + Path.GetFileName (file);
  72. cref.Ref = cref.DocRef + "?wsdl";
  73. doc.References.Add (cref);
  74. }
  75. else if (ext == ".disco")
  76. {
  77. DiscoveryDocumentReference dref = new DiscoveryDocumentReference ();
  78. dref.Ref = url + Path.GetFileName (file);
  79. doc.References.Add (dref);
  80. }
  81. }
  82. string[] dirs = Directory.GetDirectories (path);
  83. foreach (string dir in dirs)
  84. {
  85. string rel = Path.Combine (relPath, Path.GetFileName(dir));
  86. if (!ddoc.IsExcluded (rel))
  87. GetFiles (baseUrl, rel, Path.Combine (path, dir), doc, ddoc);
  88. }
  89. }
  90. #endregion // Methods
  91. }
  92. }