2 // System.Web.Services.Protocols.SoapDocumentationHandler.cs
5 // Lluis Sanchez Gual (lluis@ximian.com)
7 // Copyright (C) Ximian, Inc. 2003
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
34 using System.Globalization;
37 using System.Xml.Serialization;
38 using System.Xml.Schema;
39 using System.Web.Services.Description;
40 using System.Web.Services.Discovery;
41 using System.Web.Services.Configuration;
43 using System.CodeDom.Compiler;
44 using Microsoft.CSharp;
47 namespace System.Web.Services.Protocols
49 internal class SoapDocumentationHandler: WebServiceHandler
51 SoapTypeStubInfo _typeStubInfo;
52 ServiceDescriptionCollection _descriptions;
55 IHttpHandler _pageHandler = null;
57 public SoapDocumentationHandler (Type type, HttpContext context): base (type)
59 _url = context.Request.Url.ToString();
60 int i = _url.LastIndexOf ('?');
61 if (i != -1) _url = _url.Substring (0,i);
62 _typeStubInfo = (SoapTypeStubInfo) TypeStubManager.GetTypeStub (ServiceType, "Soap");
64 HttpRequest req = context.Request;
66 if (req.QueryString.Count == 1) {
67 key = req.QueryString.GetKey (0);
69 key = req.QueryString [0];
72 key = key.ToLower (CultureInfo.InvariantCulture);
75 if (key == "wsdl" || key == "schema" || key == "code" || key == "disco")
78 string help = WSConfig.Instance.WsdlHelpPage;
79 string path = Path.GetDirectoryName (WSConfig.Instance.ConfigFilePath);
80 string appPath = AppDomain.CurrentDomain.GetData (".appPath").ToString ();
82 if (path.StartsWith (appPath)) {
83 vpath = path.Substring (appPath.Length);
84 vpath = vpath.Replace ("\\", "/");
89 if (vpath.EndsWith ("/"))
94 string physPath = Path.Combine (path, help);
96 if (!File.Exists (physPath))
97 throw new InvalidOperationException ("Documentation page '" + physPath + "' not found");
99 _pageHandler = PageParser.GetCompiledPageInstance (vpath, physPath, context);
103 internal IHttpHandler PageHandler {
104 get { return _pageHandler; }
107 public override bool IsReusable
109 get { return false; }
112 public override void ProcessRequest (HttpContext context)
114 if (_pageHandler != null)
116 context.Items["wsdls"] = GetDescriptions ();
117 context.Items["schemas"] = GetSchemas ();
118 _pageHandler.ProcessRequest (context);
122 HttpRequest req = context.Request;
123 string key = req.QueryString.GetKey (0);
125 key = req.QueryString [0];
128 key = key.ToLower (CultureInfo.InvariantCulture);
130 if (key == "wsdl") GenerateWsdlDocument (context, req.QueryString ["wsdl"]);
131 else if (key == "schema") GenerateSchema (context, req.QueryString ["schema"]);
132 else if (key == "code") GenerateCode (context, req.QueryString ["code"]);
133 else if (key == "disco") GenerateDiscoDocument (context);
134 else throw new Exception ("This should never happen");
138 void GenerateWsdlDocument (HttpContext context, string wsdlId)
141 if (wsdlId != null && wsdlId != "") di = int.Parse (wsdlId);
143 context.Response.ContentType = "text/xml; charset=utf-8";
144 XmlTextWriter xtw = new XmlTextWriter (context.Response.OutputStream, new UTF8Encoding (false));
145 xtw.Formatting = Formatting.Indented;
146 GetDescriptions() [di].Write (xtw);
149 void GenerateDiscoDocument (HttpContext context)
151 ServiceDescriptionCollection descs = GetDescriptions ();
153 DiscoveryDocument doc = new DiscoveryDocument ();
154 ContractReference cref = new ContractReference ();
155 cref.Ref = _url + "?wsdl";
157 doc.References.Add (cref);
159 foreach (ServiceDescription desc in descs)
160 foreach (Service ser in desc.Services)
161 foreach (Port port in ser.Ports)
163 SoapAddressBinding sab = port.Extensions.Find (typeof(SoapAddressBinding)) as SoapAddressBinding;
166 System.Web.Services.Discovery.SoapBinding dsb = new System.Web.Services.Discovery.SoapBinding ();
167 dsb.Address = sab.Location;
168 dsb.Binding = port.Binding;
169 doc.AdditionalInfo.Add (dsb);
173 context.Response.ContentType = "text/xml; charset=utf-8";
174 XmlTextWriter xtw = new XmlTextWriter (context.Response.OutputStream, new UTF8Encoding (false));
175 xtw.Formatting = Formatting.Indented;
179 void GenerateSchema (HttpContext context, string schemaId)
182 if (schemaId != null && schemaId != "") di = int.Parse (schemaId);
184 context.Response.ContentType = "text/xml; charset=utf-8";
185 XmlTextWriter xtw = new XmlTextWriter (context.Response.OutputStream, new UTF8Encoding (false));
186 xtw.Formatting = Formatting.Indented;
187 GetSchemas() [di].Write (xtw);
190 void GenerateCode (HttpContext context, string langId)
192 context.Response.ContentType = "text/plain; charset=utf-8";
193 CodeNamespace codeNamespace = new CodeNamespace();
194 CodeCompileUnit codeUnit = new CodeCompileUnit();
196 codeUnit.Namespaces.Add (codeNamespace);
198 ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
200 foreach (ServiceDescription sd in GetDescriptions ())
201 importer.AddServiceDescription(sd, null, null);
203 foreach (XmlSchema sc in GetSchemas())
204 importer.Schemas.Add (sc);
206 importer.Import(codeNamespace, codeUnit);
208 if (langId == null || langId == "") langId = "cs";
209 CodeDomProvider provider = GetProvider (langId);
210 ICodeGenerator generator = provider.CreateGenerator();
211 CodeGeneratorOptions options = new CodeGeneratorOptions();
213 generator.GenerateCodeFromCompileUnit(codeUnit, context.Response.Output, options);
216 private CodeDomProvider GetProvider(string langId)
218 // FIXME these should be loaded dynamically using reflection
219 CodeDomProvider provider;
221 switch (langId.ToUpper())
224 provider = new CSharpCodeProvider();
228 throw new Exception("Unknown language: " + langId);
234 internal ServiceDescriptionCollection GetDescriptions ()
236 if (_descriptions == null)
238 ServiceDescriptionReflector reflector = new ServiceDescriptionReflector ();
239 reflector.Reflect (ServiceType,_url);
240 _schemas = reflector.Schemas;
241 _descriptions = reflector.ServiceDescriptions;
243 return _descriptions;
246 internal XmlSchemas GetSchemas()