SoapDocumentationHandler.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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.Globalization;
  33. using System.Xml;
  34. using System.Text;
  35. using System.Xml.Serialization;
  36. using System.Xml.Schema;
  37. using System.Web.Compilation;
  38. using System.Web.Services.Description;
  39. using System.Web.Services.Discovery;
  40. using System.Web.Services.Configuration;
  41. using System.Configuration;
  42. using System.CodeDom;
  43. using System.CodeDom.Compiler;
  44. using Microsoft.CSharp;
  45. using System.Web.UI;
  46. namespace System.Web.Services.Protocols
  47. {
  48. internal class SoapDocumentationHandler: WebServiceHandler
  49. {
  50. SoapTypeStubInfo _typeStubInfo;
  51. ServiceDescriptionCollection _descriptions;
  52. XmlSchemas _schemas;
  53. string _url;
  54. IHttpHandler _pageHandler = null;
  55. public SoapDocumentationHandler (Type type, HttpContext context): base (type)
  56. {
  57. _url = context.Request.Url.ToString();
  58. int i = _url.LastIndexOf ('?');
  59. if (i != -1) _url = _url.Substring (0,i);
  60. _typeStubInfo = (SoapTypeStubInfo) TypeStubManager.GetTypeStub (ServiceType, "Soap");
  61. HttpRequest req = context.Request;
  62. string key = null;
  63. if (req.QueryString.Count == 1) {
  64. key = req.QueryString.GetKey (0);
  65. if (key == null)
  66. key = req.QueryString [0];
  67. if (key != null)
  68. key = key.ToLower (CultureInfo.InvariantCulture);
  69. }
  70. if (key == "wsdl" || key == "schema" || key == "code" || key == "disco")
  71. return;
  72. #if NET_2_0
  73. string help = WebServicesSection.Current.WsdlHelpGenerator.Href;
  74. string path = Path.GetDirectoryName (ConfigurationManager.OpenMachineConfiguration().FilePath);
  75. #else
  76. string help = WSConfig.Instance.WsdlHelpPage;
  77. string path = Path.GetDirectoryName (WSConfig.Instance.ConfigFilePath);
  78. #endif
  79. string appPath = AppDomain.CurrentDomain.GetData (".appPath").ToString ();
  80. string vpath;
  81. if (path.StartsWith (appPath)) {
  82. vpath = path.Substring (appPath.Length);
  83. vpath = vpath.Replace ("\\", "/");
  84. } else {
  85. vpath = "/";
  86. }
  87. if (vpath.EndsWith ("/"))
  88. vpath += help;
  89. else
  90. vpath += "/" + help;
  91. string physPath = Path.Combine (path, help);
  92. #if !TARGET_JVM
  93. if (!File.Exists (physPath))
  94. throw new InvalidOperationException ("Documentation page '" + physPath + "' not found");
  95. #endif
  96. _pageHandler = PageParser.GetCompiledPageInstance (vpath, physPath, context);
  97. }
  98. internal IHttpHandler PageHandler {
  99. get { return _pageHandler; }
  100. }
  101. public override bool IsReusable
  102. {
  103. get { return false; }
  104. }
  105. public override void ProcessRequest (HttpContext context)
  106. {
  107. if (_pageHandler != null)
  108. {
  109. context.Items["wsdls"] = GetDescriptions ();
  110. context.Items["schemas"] = GetSchemas ();
  111. _pageHandler.ProcessRequest (context);
  112. }
  113. else
  114. {
  115. HttpRequest req = context.Request;
  116. string key = req.QueryString.GetKey (0);
  117. if (key == null)
  118. key = req.QueryString [0];
  119. if (key != null)
  120. key = key.ToLower (CultureInfo.InvariantCulture);
  121. if (key == "wsdl") GenerateWsdlDocument (context, req.QueryString ["wsdl"]);
  122. else if (key == "schema") GenerateSchema (context, req.QueryString ["schema"]);
  123. #if !TARGET_JVM //code generation is not supported
  124. else if (key == "code") GenerateCode (context, req.QueryString ["code"]);
  125. #else
  126. else if (key == "code") throw new Exception("Code generation is not supported.");
  127. #endif
  128. else if (key == "disco") GenerateDiscoDocument (context);
  129. else throw new Exception ("This should never happen");
  130. }
  131. }
  132. void GenerateWsdlDocument (HttpContext context, string wsdlId)
  133. {
  134. int di = 0;
  135. if (wsdlId != null && wsdlId != "") di = int.Parse (wsdlId);
  136. context.Response.ContentType = "text/xml; charset=utf-8";
  137. XmlTextWriter xtw = new XmlTextWriter (context.Response.OutputStream, new UTF8Encoding (false));
  138. xtw.Formatting = Formatting.Indented;
  139. GetDescriptions() [di].Write (xtw);
  140. }
  141. void GenerateDiscoDocument (HttpContext context)
  142. {
  143. ServiceDescriptionCollection descs = GetDescriptions ();
  144. DiscoveryDocument doc = new DiscoveryDocument ();
  145. ContractReference cref = new ContractReference ();
  146. cref.Ref = _url + "?wsdl";
  147. cref.DocRef = _url;
  148. doc.References.Add (cref);
  149. foreach (ServiceDescription desc in descs)
  150. foreach (Service ser in desc.Services)
  151. foreach (Port port in ser.Ports)
  152. {
  153. SoapAddressBinding sab = port.Extensions.Find (typeof(SoapAddressBinding)) as SoapAddressBinding;
  154. if (sab != null)
  155. {
  156. System.Web.Services.Discovery.SoapBinding dsb = new System.Web.Services.Discovery.SoapBinding ();
  157. dsb.Address = sab.Location;
  158. dsb.Binding = port.Binding;
  159. doc.AdditionalInfo.Add (dsb);
  160. }
  161. }
  162. context.Response.ContentType = "text/xml; charset=utf-8";
  163. XmlTextWriter xtw = new XmlTextWriter (context.Response.OutputStream, new UTF8Encoding (false));
  164. xtw.Formatting = Formatting.Indented;
  165. doc.Write (xtw);
  166. }
  167. void GenerateSchema (HttpContext context, string schemaId)
  168. {
  169. int di = -1;
  170. if (schemaId != null && schemaId != "") {
  171. try {
  172. di = int.Parse (schemaId);
  173. } catch {
  174. XmlSchemas xss = GetSchemas ();
  175. for (int i = 0; i < xss.Count; i++) {
  176. if (xss [i].Id == schemaId) {
  177. di = i;
  178. break;
  179. }
  180. }
  181. }
  182. if (di < 0)
  183. throw new InvalidOperationException (String.Format ("HTTP parameter 'schema' needs to specify an Id of a schema in the schemas. {0} points to nowhere.", schemaId));
  184. }
  185. context.Response.ContentType = "text/xml; charset=utf-8";
  186. XmlTextWriter xtw = new XmlTextWriter (context.Response.OutputStream, new UTF8Encoding (false));
  187. xtw.Formatting = Formatting.Indented;
  188. GetSchemas() [di].Write (xtw);
  189. }
  190. #if !TARGET_JVM
  191. void GenerateCode (HttpContext context, string langId)
  192. {
  193. context.Response.ContentType = "text/plain; charset=utf-8";
  194. CodeNamespace codeNamespace = new CodeNamespace();
  195. CodeCompileUnit codeUnit = new CodeCompileUnit();
  196. codeUnit.Namespaces.Add (codeNamespace);
  197. ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
  198. foreach (ServiceDescription sd in GetDescriptions ())
  199. importer.AddServiceDescription(sd, null, null);
  200. foreach (XmlSchema sc in GetSchemas())
  201. importer.Schemas.Add (sc);
  202. importer.Import(codeNamespace, codeUnit);
  203. if (langId == null || langId == "") langId = "cs";
  204. CodeDomProvider provider = GetProvider (langId);
  205. ICodeGenerator generator = provider.CreateGenerator();
  206. CodeGeneratorOptions options = new CodeGeneratorOptions();
  207. generator.GenerateCodeFromCompileUnit(codeUnit, context.Response.Output, options);
  208. }
  209. private CodeDomProvider GetProvider(string langId)
  210. {
  211. // FIXME these should be loaded dynamically using reflection
  212. CodeDomProvider provider;
  213. switch (langId.ToUpper())
  214. {
  215. case "CS":
  216. provider = new CSharpCodeProvider();
  217. break;
  218. default:
  219. throw new Exception("Unknown language: " + langId);
  220. }
  221. return provider;
  222. }
  223. #endif
  224. internal ServiceDescriptionCollection GetDescriptions ()
  225. {
  226. if (_descriptions == null)
  227. {
  228. ServiceDescriptionReflector reflector = new ServiceDescriptionReflector ();
  229. reflector.Reflect (ServiceType,_url);
  230. _schemas = reflector.Schemas;
  231. _descriptions = reflector.ServiceDescriptions;
  232. }
  233. return _descriptions;
  234. }
  235. internal XmlSchemas GetSchemas()
  236. {
  237. GetDescriptions();
  238. return _schemas;
  239. }
  240. }
  241. }