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;
42 using System.Configuration;
44 using System.CodeDom.Compiler;
45 using Microsoft.CSharp;
48 namespace System.Web.Services.Protocols
50 internal class SoapDocumentationHandler: WebServiceHandler
52 SoapTypeStubInfo _typeStubInfo;
53 ServiceDescriptionCollection _descriptions;
56 IHttpHandler _pageHandler = null;
58 public SoapDocumentationHandler (Type type, HttpContext context): base (type)
60 _url = context.Request.Url.ToString();
61 int i = _url.LastIndexOf ('?');
62 if (i != -1) _url = _url.Substring (0,i);
63 _typeStubInfo = (SoapTypeStubInfo) TypeStubManager.GetTypeStub (ServiceType, "Soap");
65 HttpRequest req = context.Request;
67 if (req.QueryString.Count == 1) {
68 key = req.QueryString.GetKey (0);
70 key = req.QueryString [0];
73 key = key.ToLower (CultureInfo.InvariantCulture);
76 if (key == "wsdl" || key == "schema" || key == "code" || key == "disco")
80 string help = WebServicesSection.Instance.WsdlHelpGenerator.Href;
81 string path = Path.GetDirectoryName (ConfigurationManager.OpenMachineConfiguration().FilePath);
83 string help = WSConfig.Instance.WsdlHelpPage;
84 string path = Path.GetDirectoryName (WSConfig.Instance.ConfigFilePath);
86 string appPath = AppDomain.CurrentDomain.GetData (".appPath").ToString ();
88 if (path.StartsWith (appPath)) {
89 vpath = path.Substring (appPath.Length);
90 vpath = vpath.Replace ("\\", "/");
95 if (vpath.EndsWith ("/"))
100 string physPath = Path.Combine (path, help);
103 if (!File.Exists (physPath))
104 throw new InvalidOperationException ("Documentation page '" + physPath + "' not found");
106 _pageHandler = PageParser.GetCompiledPageInstance (vpath, physPath, context);
110 internal IHttpHandler PageHandler {
111 get { return _pageHandler; }
114 public override bool IsReusable
116 get { return false; }
119 public override void ProcessRequest (HttpContext context)
121 if (_pageHandler != null)
123 context.Items["wsdls"] = GetDescriptions ();
124 context.Items["schemas"] = GetSchemas ();
125 _pageHandler.ProcessRequest (context);
129 HttpRequest req = context.Request;
130 string key = req.QueryString.GetKey (0);
132 key = req.QueryString [0];
135 key = key.ToLower (CultureInfo.InvariantCulture);
137 if (key == "wsdl") GenerateWsdlDocument (context, req.QueryString ["wsdl"]);
138 else if (key == "schema") GenerateSchema (context, req.QueryString ["schema"]);
139 #if !TARGET_JVM //code generation is not supported
140 else if (key == "code") GenerateCode (context, req.QueryString ["code"]);
142 else if (key == "code") throw new Exception("Code generation is not supported.");
144 else if (key == "disco") GenerateDiscoDocument (context);
145 else throw new Exception ("This should never happen");
149 void GenerateWsdlDocument (HttpContext context, string wsdlId)
152 if (wsdlId != null && wsdlId != "") di = int.Parse (wsdlId);
154 context.Response.ContentType = "text/xml; charset=utf-8";
155 XmlTextWriter xtw = new XmlTextWriter (context.Response.OutputStream, new UTF8Encoding (false));
156 xtw.Formatting = Formatting.Indented;
157 GetDescriptions() [di].Write (xtw);
160 void GenerateDiscoDocument (HttpContext context)
162 ServiceDescriptionCollection descs = GetDescriptions ();
164 DiscoveryDocument doc = new DiscoveryDocument ();
165 ContractReference cref = new ContractReference ();
166 cref.Ref = _url + "?wsdl";
168 doc.References.Add (cref);
170 foreach (ServiceDescription desc in descs)
171 foreach (Service ser in desc.Services)
172 foreach (Port port in ser.Ports)
174 SoapAddressBinding sab = port.Extensions.Find (typeof(SoapAddressBinding)) as SoapAddressBinding;
177 System.Web.Services.Discovery.SoapBinding dsb = new System.Web.Services.Discovery.SoapBinding ();
178 dsb.Address = sab.Location;
179 dsb.Binding = port.Binding;
180 doc.AdditionalInfo.Add (dsb);
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;
190 void GenerateSchema (HttpContext context, string schemaId)
193 if (schemaId != null && schemaId != "") di = int.Parse (schemaId);
195 context.Response.ContentType = "text/xml; charset=utf-8";
196 XmlTextWriter xtw = new XmlTextWriter (context.Response.OutputStream, new UTF8Encoding (false));
197 xtw.Formatting = Formatting.Indented;
198 GetSchemas() [di].Write (xtw);
202 void GenerateCode (HttpContext context, string langId)
204 context.Response.ContentType = "text/plain; charset=utf-8";
205 CodeNamespace codeNamespace = new CodeNamespace();
206 CodeCompileUnit codeUnit = new CodeCompileUnit();
208 codeUnit.Namespaces.Add (codeNamespace);
210 ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
212 foreach (ServiceDescription sd in GetDescriptions ())
213 importer.AddServiceDescription(sd, null, null);
215 foreach (XmlSchema sc in GetSchemas())
216 importer.Schemas.Add (sc);
218 importer.Import(codeNamespace, codeUnit);
220 if (langId == null || langId == "") langId = "cs";
221 CodeDomProvider provider = GetProvider (langId);
222 ICodeGenerator generator = provider.CreateGenerator();
223 CodeGeneratorOptions options = new CodeGeneratorOptions();
225 generator.GenerateCodeFromCompileUnit(codeUnit, context.Response.Output, options);
228 private CodeDomProvider GetProvider(string langId)
230 // FIXME these should be loaded dynamically using reflection
231 CodeDomProvider provider;
233 switch (langId.ToUpper())
236 provider = new CSharpCodeProvider();
240 throw new Exception("Unknown language: " + langId);
247 internal ServiceDescriptionCollection GetDescriptions ()
249 if (_descriptions == null)
251 ServiceDescriptionReflector reflector = new ServiceDescriptionReflector ();
252 reflector.Reflect (ServiceType,_url);
253 _schemas = reflector.Schemas;
254 _descriptions = reflector.ServiceDescriptions;
256 return _descriptions;
259 internal XmlSchemas GetSchemas()