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.Compilation;
40 using System.Web.Services.Description;
41 using System.Web.Services.Discovery;
42 using System.Web.Services.Configuration;
43 using System.Configuration;
45 using System.CodeDom.Compiler;
46 using Microsoft.CSharp;
49 namespace System.Web.Services.Protocols
51 internal class SoapDocumentationHandler: WebServiceHandler
53 SoapTypeStubInfo _typeStubInfo;
54 ServiceDescriptionCollection _descriptions;
57 IHttpHandler _pageHandler = null;
59 public SoapDocumentationHandler (Type type, HttpContext context): base (type)
61 _url = context.Request.Url.ToString();
62 int i = _url.IndexOf ('?');
63 if (i != -1) _url = _url.Substring (0,i);
64 _typeStubInfo = (SoapTypeStubInfo) TypeStubManager.GetTypeStub (ServiceType, "Soap");
66 HttpRequest req = context.Request;
68 if (req.QueryString.Count == 1) {
69 key = req.QueryString.GetKey (0);
71 key = req.QueryString [0];
74 key = key.ToLower (CultureInfo.InvariantCulture);
77 if (key == "wsdl" || key == "schema" || key == "code" || key == "disco")
81 string help = WebServicesSection.Current.WsdlHelpGenerator.Href;
82 string path = Path.GetDirectoryName (ConfigurationManager.OpenMachineConfiguration().FilePath);
84 string help = WSConfig.Instance.WsdlHelpPage;
85 string path = Path.GetDirectoryName (WSConfig.Instance.ConfigFilePath);
87 string appPath = AppDomain.CurrentDomain.GetData (".appPath").ToString ();
89 if (path.StartsWith (appPath)) {
90 vpath = path.Substring (appPath.Length);
91 vpath = vpath.Replace ("\\", "/");
96 if (vpath.EndsWith ("/"))
101 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);
109 internal IHttpHandler PageHandler {
110 get { return _pageHandler; }
113 public override bool IsReusable
115 get { return false; }
118 public override void ProcessRequest (HttpContext context)
120 if (_pageHandler != null)
122 context.Items["wsdls"] = GetDescriptions ();
123 context.Items["schemas"] = GetSchemas ();
124 _pageHandler.ProcessRequest (context);
128 HttpRequest req = context.Request;
129 string key = req.QueryString.GetKey (0);
131 key = req.QueryString [0];
134 key = key.ToLower (CultureInfo.InvariantCulture);
136 if (key == "wsdl") GenerateWsdlDocument (context, req.QueryString ["wsdl"]);
137 else if (key == "schema") GenerateSchema (context, req.QueryString ["schema"]);
138 else if (key == "code") GenerateCode (context, req.QueryString ["code"]);
139 else if (key == "disco") GenerateDiscoDocument (context);
140 else throw new Exception ("This should never happen");
144 void GenerateWsdlDocument (HttpContext context, string wsdlId)
147 if (wsdlId != null && wsdlId != "") di = int.Parse (wsdlId);
149 context.Response.ContentType = "text/xml; charset=utf-8";
150 XmlTextWriter xtw = new XmlTextWriter (context.Response.OutputStream, new UTF8Encoding (false));
151 xtw.Formatting = Formatting.Indented;
152 GetDescriptions() [di].Write (xtw);
155 void GenerateDiscoDocument (HttpContext context)
157 ServiceDescriptionCollection descs = GetDescriptions ();
159 DiscoveryDocument doc = new DiscoveryDocument ();
160 ContractReference cref = new ContractReference ();
161 cref.Ref = _url + "?wsdl";
163 doc.References.Add (cref);
165 foreach (ServiceDescription desc in descs)
166 foreach (Service ser in desc.Services)
167 foreach (Port port in ser.Ports)
169 SoapAddressBinding sab = port.Extensions.Find (typeof(SoapAddressBinding)) as SoapAddressBinding;
172 System.Web.Services.Discovery.SoapBinding dsb = new System.Web.Services.Discovery.SoapBinding ();
173 dsb.Address = sab.Location;
174 dsb.Binding = port.Binding;
175 doc.AdditionalInfo.Add (dsb);
179 context.Response.ContentType = "text/xml; charset=utf-8";
180 XmlTextWriter xtw = new XmlTextWriter (context.Response.OutputStream, new UTF8Encoding (false));
181 xtw.Formatting = Formatting.Indented;
185 void GenerateSchema (HttpContext context, string schemaId)
188 if (schemaId != null && schemaId != "") {
190 di = int.Parse (schemaId);
192 XmlSchemas xss = GetSchemas ();
193 for (int i = 0; i < xss.Count; i++) {
194 if (xss [i].Id == schemaId) {
201 throw new InvalidOperationException (String.Format ("HTTP parameter 'schema' needs to specify an Id of a schema in the schemas. {0} points to nowhere.", schemaId));
203 context.Response.ContentType = "text/xml; charset=utf-8";
204 XmlTextWriter xtw = new XmlTextWriter (context.Response.OutputStream, new UTF8Encoding (false));
205 xtw.Formatting = Formatting.Indented;
206 GetSchemas() [di].Write (xtw);
209 void GenerateCode (HttpContext context, string langId)
211 context.Response.ContentType = "text/plain; charset=utf-8";
212 CodeNamespace codeNamespace = new CodeNamespace();
213 CodeCompileUnit codeUnit = new CodeCompileUnit();
215 codeUnit.Namespaces.Add (codeNamespace);
217 ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
219 foreach (ServiceDescription sd in GetDescriptions ())
220 importer.AddServiceDescription(sd, null, null);
222 foreach (XmlSchema sc in GetSchemas())
223 importer.Schemas.Add (sc);
225 importer.Import(codeNamespace, codeUnit);
227 if (langId == null || langId == "") langId = "cs";
228 CodeDomProvider provider = GetProvider (langId);
229 ICodeGenerator generator = provider.CreateGenerator();
230 CodeGeneratorOptions options = new CodeGeneratorOptions();
232 generator.GenerateCodeFromCompileUnit(codeUnit, context.Response.Output, options);
235 private CodeDomProvider GetProvider(string langId)
237 // FIXME these should be loaded dynamically using reflection
238 CodeDomProvider provider;
240 switch (langId.ToUpper())
243 provider = new CSharpCodeProvider();
247 throw new Exception("Unknown language: " + langId);
253 internal ServiceDescriptionCollection GetDescriptions ()
255 if (_descriptions == null)
257 ServiceDescriptionReflector reflector = new ServiceDescriptionReflector ();
258 reflector.Reflect (ServiceType,_url);
259 _schemas = reflector.Schemas;
260 _descriptions = reflector.ServiceDescriptions;
262 return _descriptions;
265 internal XmlSchemas GetSchemas()