SoapDocumentationHandler.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // System.Web.Services.Protocols.SoapDocumentationHandler.cs
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // Copyright (C) Ximian, Inc. 2003
  8. //
  9. using System;
  10. using System.Web;
  11. using System.IO;
  12. using System.Xml.Serialization;
  13. using System.Xml.Schema;
  14. using System.Web.Services.Description;
  15. using System.Web.Services.Discovery;
  16. using System.Web.Services.Configuration;
  17. using System.CodeDom;
  18. using System.CodeDom.Compiler;
  19. using Microsoft.CSharp;
  20. using System.Web.UI;
  21. namespace System.Web.Services.Protocols
  22. {
  23. internal class SoapDocumentationHandler: WebServiceHandler
  24. {
  25. SoapTypeStubInfo _typeStubInfo;
  26. ServiceDescriptionCollection _descriptions;
  27. XmlSchemas _schemas;
  28. string _url;
  29. IHttpHandler _pageHandler = null;
  30. public SoapDocumentationHandler (Type type, HttpContext context): base (type)
  31. {
  32. _url = context.Request.Url.ToString();
  33. int i = _url.LastIndexOf ('?');
  34. if (i != -1) _url = _url.Substring (0,i);
  35. _typeStubInfo = (SoapTypeStubInfo) TypeStubManager.GetTypeStub (ServiceType, "Soap");
  36. HttpRequest req = context.Request;
  37. string key = null;
  38. if (req.QueryString.Count == 1)
  39. key = req.QueryString.GetKey(0).ToLower();
  40. if (key == "wsdl" || key == "schema" || key == "code" || key == "disco")
  41. return;
  42. string help = WSConfig.Instance.WsdlHelpPage;
  43. string path = Path.GetDirectoryName (WSConfig.Instance.ConfigFilePath);
  44. string file = Path.GetFileName (WSConfig.Instance.ConfigFilePath);
  45. string appPath = AppDomain.CurrentDomain.GetData (".appPath").ToString ();
  46. string vpath;
  47. if (path.StartsWith (appPath)) {
  48. vpath = path.Substring (appPath.Length);
  49. vpath = vpath.Replace ("\\", "/");
  50. } else {
  51. vpath = "/";
  52. }
  53. if (vpath.EndsWith ("/"))
  54. vpath += help;
  55. else
  56. vpath += "/" + help;
  57. string physPath = Path.Combine (path, help);
  58. if (!File.Exists (physPath))
  59. throw new InvalidOperationException ("Documentation page '" + physPath + "' not found");
  60. _pageHandler = PageParser.GetCompiledPageInstance (vpath, physPath, context);
  61. }
  62. public override bool IsReusable
  63. {
  64. get { return false; }
  65. }
  66. public override void ProcessRequest (HttpContext context)
  67. {
  68. if (_pageHandler != null)
  69. {
  70. context.Items["wsdls"] = GetDescriptions ();
  71. context.Items["schemas"] = GetSchemas ();
  72. _pageHandler.ProcessRequest (context);
  73. }
  74. else
  75. {
  76. HttpRequest req = context.Request;
  77. string key = req.QueryString.GetKey(0).ToLower();
  78. if (key == "wsdl") GenerateWsdlDocument (context, req.QueryString ["wsdl"]);
  79. else if (key == "schema") GenerateSchema (context, req.QueryString ["schema"]);
  80. else if (key == "code") GenerateCode (context, req.QueryString ["code"]);
  81. else if (key == "disco") GenerateDiscoDocument (context);
  82. else throw new Exception ("This should never happen");
  83. }
  84. }
  85. void GenerateWsdlDocument (HttpContext context, string wsdlId)
  86. {
  87. int di = 0;
  88. if (wsdlId != null && wsdlId != "") di = int.Parse (wsdlId);
  89. context.Response.ContentType = "text/xml; charset=utf-8";
  90. GetDescriptions() [di].Write (context.Response.OutputStream);
  91. }
  92. void GenerateDiscoDocument (HttpContext context)
  93. {
  94. DiscoveryDocument doc = new DiscoveryDocument ();
  95. ContractReference cref = new ContractReference ();
  96. cref.Ref = _url + "?wsdl";
  97. cref.DocRef = _url;
  98. doc.References.Add (cref);
  99. context.Response.ContentType = "text/xml; charset=utf-8";
  100. doc.Write (context.Response.OutputStream);
  101. }
  102. void GenerateSchema (HttpContext context, string schemaId)
  103. {
  104. int di = 0;
  105. if (schemaId != null && schemaId != "") di = int.Parse (schemaId);
  106. context.Response.ContentType = "text/xml; charset=utf-8";
  107. GetSchemas() [di].Write (context.Response.OutputStream);
  108. }
  109. void GenerateCode (HttpContext context, string langId)
  110. {
  111. context.Response.ContentType = "text/plain; charset=utf-8";
  112. CodeNamespace codeNamespace = new CodeNamespace();
  113. CodeCompileUnit codeUnit = new CodeCompileUnit();
  114. codeUnit.Namespaces.Add (codeNamespace);
  115. ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
  116. foreach (ServiceDescription sd in GetDescriptions ())
  117. importer.AddServiceDescription(sd, null, null);
  118. foreach (XmlSchema sc in GetSchemas())
  119. importer.Schemas.Add (sc);
  120. importer.Import(codeNamespace, codeUnit);
  121. if (langId == null || langId == "") langId = "cs";
  122. CodeDomProvider provider = GetProvider (langId);
  123. ICodeGenerator generator = provider.CreateGenerator();
  124. CodeGeneratorOptions options = new CodeGeneratorOptions();
  125. generator.GenerateCodeFromCompileUnit(codeUnit, context.Response.Output, options);
  126. }
  127. private CodeDomProvider GetProvider(string langId)
  128. {
  129. // FIXME these should be loaded dynamically using reflection
  130. CodeDomProvider provider;
  131. switch (langId.ToUpper())
  132. {
  133. case "CS":
  134. provider = new CSharpCodeProvider();
  135. break;
  136. default:
  137. throw new Exception("Unknown language: " + langId);
  138. }
  139. return provider;
  140. }
  141. internal ServiceDescriptionCollection GetDescriptions ()
  142. {
  143. if (_descriptions == null)
  144. {
  145. ServiceDescriptionReflector reflector = new ServiceDescriptionReflector ();
  146. reflector.Reflect (ServiceType,_url);
  147. _schemas = reflector.Schemas;
  148. _descriptions = reflector.ServiceDescriptions;
  149. }
  150. return _descriptions;
  151. }
  152. internal XmlSchemas GetSchemas()
  153. {
  154. GetDescriptions();
  155. return _schemas;
  156. }
  157. }
  158. }