SoapDocumentationHandler.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Web;
  31. using System.IO;
  32. using System.Xml.Serialization;
  33. using System.Xml.Schema;
  34. using System.Web.Services.Description;
  35. using System.Web.Services.Discovery;
  36. using System.Web.Services.Configuration;
  37. using System.CodeDom;
  38. using System.CodeDom.Compiler;
  39. using Microsoft.CSharp;
  40. using System.Web.UI;
  41. namespace System.Web.Services.Protocols
  42. {
  43. internal class SoapDocumentationHandler: WebServiceHandler
  44. {
  45. SoapTypeStubInfo _typeStubInfo;
  46. ServiceDescriptionCollection _descriptions;
  47. XmlSchemas _schemas;
  48. string _url;
  49. IHttpHandler _pageHandler = null;
  50. public SoapDocumentationHandler (Type type, HttpContext context): base (type)
  51. {
  52. _url = context.Request.Url.ToString();
  53. int i = _url.LastIndexOf ('?');
  54. if (i != -1) _url = _url.Substring (0,i);
  55. _typeStubInfo = (SoapTypeStubInfo) TypeStubManager.GetTypeStub (ServiceType, "Soap");
  56. HttpRequest req = context.Request;
  57. string key = null;
  58. if (req.QueryString.Count == 1)
  59. key = req.QueryString.GetKey(0).ToLower();
  60. if (key == "wsdl" || key == "schema" || key == "code" || key == "disco")
  61. return;
  62. string help = WSConfig.Instance.WsdlHelpPage;
  63. string path = Path.GetDirectoryName (WSConfig.Instance.ConfigFilePath);
  64. string file = Path.GetFileName (WSConfig.Instance.ConfigFilePath);
  65. string appPath = AppDomain.CurrentDomain.GetData (".appPath").ToString ();
  66. string vpath;
  67. if (path.StartsWith (appPath)) {
  68. vpath = path.Substring (appPath.Length);
  69. vpath = vpath.Replace ("\\", "/");
  70. } else {
  71. vpath = "/";
  72. }
  73. if (vpath.EndsWith ("/"))
  74. vpath += help;
  75. else
  76. vpath += "/" + help;
  77. string physPath = Path.Combine (path, help);
  78. if (!File.Exists (physPath))
  79. throw new InvalidOperationException ("Documentation page '" + physPath + "' not found");
  80. _pageHandler = PageParser.GetCompiledPageInstance (vpath, physPath, context);
  81. }
  82. internal IHttpHandler PageHandler {
  83. get { return _pageHandler; }
  84. }
  85. public override bool IsReusable
  86. {
  87. get { return false; }
  88. }
  89. public override void ProcessRequest (HttpContext context)
  90. {
  91. if (_pageHandler != null)
  92. {
  93. context.Items["wsdls"] = GetDescriptions ();
  94. context.Items["schemas"] = GetSchemas ();
  95. _pageHandler.ProcessRequest (context);
  96. }
  97. else
  98. {
  99. HttpRequest req = context.Request;
  100. string key = req.QueryString.GetKey(0).ToLower();
  101. if (key == "wsdl") GenerateWsdlDocument (context, req.QueryString ["wsdl"]);
  102. else if (key == "schema") GenerateSchema (context, req.QueryString ["schema"]);
  103. else if (key == "code") GenerateCode (context, req.QueryString ["code"]);
  104. else if (key == "disco") GenerateDiscoDocument (context);
  105. else throw new Exception ("This should never happen");
  106. }
  107. }
  108. void GenerateWsdlDocument (HttpContext context, string wsdlId)
  109. {
  110. int di = 0;
  111. if (wsdlId != null && wsdlId != "") di = int.Parse (wsdlId);
  112. context.Response.ContentType = "text/xml; charset=utf-8";
  113. GetDescriptions() [di].Write (context.Response.OutputStream);
  114. }
  115. void GenerateDiscoDocument (HttpContext context)
  116. {
  117. ServiceDescriptionCollection descs = GetDescriptions ();
  118. DiscoveryDocument doc = new DiscoveryDocument ();
  119. ContractReference cref = new ContractReference ();
  120. cref.Ref = _url + "?wsdl";
  121. cref.DocRef = _url;
  122. doc.References.Add (cref);
  123. foreach (ServiceDescription desc in descs)
  124. foreach (Service ser in desc.Services)
  125. foreach (Port port in ser.Ports)
  126. {
  127. SoapAddressBinding sab = port.Extensions.Find (typeof(SoapAddressBinding)) as SoapAddressBinding;
  128. if (sab != null)
  129. {
  130. System.Web.Services.Discovery.SoapBinding dsb = new System.Web.Services.Discovery.SoapBinding ();
  131. dsb.Address = sab.Location;
  132. dsb.Binding = port.Binding;
  133. doc.AdditionalInfo.Add (dsb);
  134. }
  135. }
  136. context.Response.ContentType = "text/xml; charset=utf-8";
  137. doc.Write (context.Response.OutputStream);
  138. }
  139. void GenerateSchema (HttpContext context, string schemaId)
  140. {
  141. int di = 0;
  142. if (schemaId != null && schemaId != "") di = int.Parse (schemaId);
  143. context.Response.ContentType = "text/xml; charset=utf-8";
  144. GetSchemas() [di].Write (context.Response.OutputStream);
  145. }
  146. void GenerateCode (HttpContext context, string langId)
  147. {
  148. context.Response.ContentType = "text/plain; charset=utf-8";
  149. CodeNamespace codeNamespace = new CodeNamespace();
  150. CodeCompileUnit codeUnit = new CodeCompileUnit();
  151. codeUnit.Namespaces.Add (codeNamespace);
  152. ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
  153. foreach (ServiceDescription sd in GetDescriptions ())
  154. importer.AddServiceDescription(sd, null, null);
  155. foreach (XmlSchema sc in GetSchemas())
  156. importer.Schemas.Add (sc);
  157. importer.Import(codeNamespace, codeUnit);
  158. if (langId == null || langId == "") langId = "cs";
  159. CodeDomProvider provider = GetProvider (langId);
  160. ICodeGenerator generator = provider.CreateGenerator();
  161. CodeGeneratorOptions options = new CodeGeneratorOptions();
  162. generator.GenerateCodeFromCompileUnit(codeUnit, context.Response.Output, options);
  163. }
  164. private CodeDomProvider GetProvider(string langId)
  165. {
  166. // FIXME these should be loaded dynamically using reflection
  167. CodeDomProvider provider;
  168. switch (langId.ToUpper())
  169. {
  170. case "CS":
  171. provider = new CSharpCodeProvider();
  172. break;
  173. default:
  174. throw new Exception("Unknown language: " + langId);
  175. }
  176. return provider;
  177. }
  178. internal ServiceDescriptionCollection GetDescriptions ()
  179. {
  180. if (_descriptions == null)
  181. {
  182. ServiceDescriptionReflector reflector = new ServiceDescriptionReflector ();
  183. reflector.Reflect (ServiceType,_url);
  184. _schemas = reflector.Schemas;
  185. _descriptions = reflector.ServiceDescriptions;
  186. }
  187. return _descriptions;
  188. }
  189. internal XmlSchemas GetSchemas()
  190. {
  191. GetDescriptions();
  192. return _schemas;
  193. }
  194. }
  195. }