2010-06-21 Marek Habersack <mhabersack@novell.com>
[mcs.git] / class / System.Web.Services / System.Web.Services.Protocols / SoapDocumentationHandler.cs
blob178161b1c95c0c7a836f3d770efff2511fb2f7cd
1 //
2 // System.Web.Services.Protocols.SoapDocumentationHandler.cs
3 //
4 // Author:
5 // Lluis Sanchez Gual (lluis@ximian.com)
6 //
7 // Copyright (C) Ximian, Inc. 2003
8 //
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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.
31 using System;
32 using System.Web;
33 using System.IO;
34 using System.Globalization;
35 using System.Xml;
36 using System.Text;
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;
44 using System.CodeDom;
45 using System.CodeDom.Compiler;
46 using Microsoft.CSharp;
47 using System.Web.UI;
49 namespace System.Web.Services.Protocols
51 internal class SoapDocumentationHandler: WebServiceHandler
53 SoapTypeStubInfo _typeStubInfo;
54 ServiceDescriptionCollection _descriptions;
55 XmlSchemas _schemas;
56 string _url;
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;
67 string key = null;
68 if (req.QueryString.Count == 1) {
69 key = req.QueryString.GetKey (0);
70 if (key == null)
71 key = req.QueryString [0];
73 if (key != null)
74 key = key.ToLower (CultureInfo.InvariantCulture);
77 if (key == "wsdl" || key == "schema" || key == "code" || key == "disco")
78 return;
80 #if NET_2_0
81 string help = WebServicesSection.Current.WsdlHelpGenerator.Href;
82 string path = Path.GetDirectoryName (ConfigurationManager.OpenMachineConfiguration().FilePath);
83 #else
84 string help = WSConfig.Instance.WsdlHelpPage;
85 string path = Path.GetDirectoryName (WSConfig.Instance.ConfigFilePath);
86 #endif
87 string appPath = AppDomain.CurrentDomain.GetData (".appPath").ToString ();
88 string vpath;
89 if (path.StartsWith (appPath)) {
90 vpath = path.Substring (appPath.Length);
91 vpath = vpath.Replace ("\\", "/");
92 } else {
93 vpath = "/";
96 if (vpath.EndsWith ("/"))
97 vpath += help;
98 else
99 vpath += "/" + help;
101 string physPath = Path.Combine (path, help);
103 #if !TARGET_JVM
104 if (!File.Exists (physPath))
105 throw new InvalidOperationException ("Documentation page '" + physPath + "' not found");
106 #endif
108 _pageHandler = PageParser.GetCompiledPageInstance (vpath, physPath, context);
111 internal IHttpHandler PageHandler {
112 get { return _pageHandler; }
115 public override bool IsReusable
117 get { return false; }
120 public override void ProcessRequest (HttpContext context)
122 if (_pageHandler != null)
124 context.Items["wsdls"] = GetDescriptions ();
125 context.Items["schemas"] = GetSchemas ();
126 _pageHandler.ProcessRequest (context);
128 else
130 HttpRequest req = context.Request;
131 string key = req.QueryString.GetKey (0);
132 if (key == null)
133 key = req.QueryString [0];
135 if (key != null)
136 key = key.ToLower (CultureInfo.InvariantCulture);
138 if (key == "wsdl") GenerateWsdlDocument (context, req.QueryString ["wsdl"]);
139 else if (key == "schema") GenerateSchema (context, req.QueryString ["schema"]);
140 #if !TARGET_JVM //code generation is not supported
141 else if (key == "code") GenerateCode (context, req.QueryString ["code"]);
142 #else
143 else if (key == "code") throw new Exception("Code generation is not supported.");
144 #endif
145 else if (key == "disco") GenerateDiscoDocument (context);
146 else throw new Exception ("This should never happen");
150 void GenerateWsdlDocument (HttpContext context, string wsdlId)
152 int di = 0;
153 if (wsdlId != null && wsdlId != "") di = int.Parse (wsdlId);
155 context.Response.ContentType = "text/xml; charset=utf-8";
156 XmlTextWriter xtw = new XmlTextWriter (context.Response.OutputStream, new UTF8Encoding (false));
157 xtw.Formatting = Formatting.Indented;
158 GetDescriptions() [di].Write (xtw);
161 void GenerateDiscoDocument (HttpContext context)
163 ServiceDescriptionCollection descs = GetDescriptions ();
165 DiscoveryDocument doc = new DiscoveryDocument ();
166 ContractReference cref = new ContractReference ();
167 cref.Ref = _url + "?wsdl";
168 cref.DocRef = _url;
169 doc.References.Add (cref);
171 foreach (ServiceDescription desc in descs)
172 foreach (Service ser in desc.Services)
173 foreach (Port port in ser.Ports)
175 SoapAddressBinding sab = port.Extensions.Find (typeof(SoapAddressBinding)) as SoapAddressBinding;
176 if (sab != null)
178 System.Web.Services.Discovery.SoapBinding dsb = new System.Web.Services.Discovery.SoapBinding ();
179 dsb.Address = sab.Location;
180 dsb.Binding = port.Binding;
181 doc.AdditionalInfo.Add (dsb);
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 doc.Write (xtw);
191 void GenerateSchema (HttpContext context, string schemaId)
193 int di = -1;
194 if (schemaId != null && schemaId != "") {
195 try {
196 di = int.Parse (schemaId);
197 } catch {
198 XmlSchemas xss = GetSchemas ();
199 for (int i = 0; i < xss.Count; i++) {
200 if (xss [i].Id == schemaId) {
201 di = i;
202 break;
206 if (di < 0)
207 throw new InvalidOperationException (String.Format ("HTTP parameter 'schema' needs to specify an Id of a schema in the schemas. {0} points to nowhere.", schemaId));
209 context.Response.ContentType = "text/xml; charset=utf-8";
210 XmlTextWriter xtw = new XmlTextWriter (context.Response.OutputStream, new UTF8Encoding (false));
211 xtw.Formatting = Formatting.Indented;
212 GetSchemas() [di].Write (xtw);
215 #if !TARGET_JVM
216 void GenerateCode (HttpContext context, string langId)
218 context.Response.ContentType = "text/plain; charset=utf-8";
219 CodeNamespace codeNamespace = new CodeNamespace();
220 CodeCompileUnit codeUnit = new CodeCompileUnit();
222 codeUnit.Namespaces.Add (codeNamespace);
224 ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
226 foreach (ServiceDescription sd in GetDescriptions ())
227 importer.AddServiceDescription(sd, null, null);
229 foreach (XmlSchema sc in GetSchemas())
230 importer.Schemas.Add (sc);
232 importer.Import(codeNamespace, codeUnit);
234 if (langId == null || langId == "") langId = "cs";
235 CodeDomProvider provider = GetProvider (langId);
236 ICodeGenerator generator = provider.CreateGenerator();
237 CodeGeneratorOptions options = new CodeGeneratorOptions();
239 generator.GenerateCodeFromCompileUnit(codeUnit, context.Response.Output, options);
242 private CodeDomProvider GetProvider(string langId)
244 // FIXME these should be loaded dynamically using reflection
245 CodeDomProvider provider;
247 switch (langId.ToUpper())
249 case "CS":
250 provider = new CSharpCodeProvider();
251 break;
253 default:
254 throw new Exception("Unknown language: " + langId);
257 return provider;
259 #endif
261 internal ServiceDescriptionCollection GetDescriptions ()
263 if (_descriptions == null)
265 ServiceDescriptionReflector reflector = new ServiceDescriptionReflector ();
266 reflector.Reflect (ServiceType,_url);
267 _schemas = reflector.Schemas;
268 _descriptions = reflector.ServiceDescriptions;
270 return _descriptions;
273 internal XmlSchemas GetSchemas()
275 GetDescriptions();
276 return _schemas;