2005-09-26 Lluis Sanchez Gual <lluis@novell.com>
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / SoapDocumentationHandler.cs
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 //
9
10 //
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.
29 //
30
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.Services.Description;
40 using System.Web.Services.Discovery;
41 using System.Web.Services.Configuration;
42 using System.CodeDom;
43 using System.CodeDom.Compiler;
44 using Microsoft.CSharp;
45 using System.Web.UI;
46
47 namespace System.Web.Services.Protocols
48 {
49         internal class SoapDocumentationHandler: WebServiceHandler
50         {
51                 SoapTypeStubInfo _typeStubInfo;
52                 ServiceDescriptionCollection _descriptions;
53                 XmlSchemas _schemas;
54                 string _url;
55                 IHttpHandler _pageHandler = null;
56
57                 public SoapDocumentationHandler (Type type, HttpContext context): base (type)
58                 {
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");
63                         
64                         HttpRequest req = context.Request;
65                         string key = null;
66                         if (req.QueryString.Count == 1) {
67                                 key = req.QueryString.GetKey (0);
68                                 if (key == null)
69                                         key = req.QueryString [0];
70
71                                 if (key != null)
72                                         key = key.ToLower (CultureInfo.InvariantCulture);
73                         }
74                                 
75                         if (key == "wsdl" || key == "schema" || key == "code" || key == "disco")
76                                 return;
77                                 
78                         string help = WSConfig.Instance.WsdlHelpPage;
79                         string path = Path.GetDirectoryName (WSConfig.Instance.ConfigFilePath);
80                         string appPath = AppDomain.CurrentDomain.GetData (".appPath").ToString ();
81                         string vpath;
82                         if (path.StartsWith (appPath)) {
83                                 vpath = path.Substring (appPath.Length);
84                                 vpath = vpath.Replace ("\\", "/");
85                         } else {
86                                 vpath = "/";
87                         }
88
89                         if (vpath.EndsWith ("/"))
90                                 vpath += help;
91                         else
92                                 vpath += "/" + help;
93
94                         string physPath = Path.Combine (path, help);
95                         
96                         if (!File.Exists (physPath))
97                                 throw new InvalidOperationException ("Documentation page '" + physPath + "' not found");
98
99                         _pageHandler = PageParser.GetCompiledPageInstance (vpath, physPath, context);
100                                 
101                 }
102
103                 internal IHttpHandler PageHandler {
104                         get { return _pageHandler; }
105                 }
106
107                 public override bool IsReusable 
108                 {
109                         get { return false; }
110                 }
111
112                 public override void ProcessRequest (HttpContext context)
113                 {
114                         if (_pageHandler != null)
115                         {
116                                 context.Items["wsdls"] = GetDescriptions ();
117                                 context.Items["schemas"] = GetSchemas ();
118                                 _pageHandler.ProcessRequest (context);
119                         }
120                         else
121                         {
122                                 HttpRequest req = context.Request;
123                                 string key = req.QueryString.GetKey (0);
124                                 if (key == null)
125                                         key = req.QueryString [0];
126
127                                 if (key != null)
128                                         key = key.ToLower (CultureInfo.InvariantCulture);
129
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");
135                         }
136                 }
137
138                 void GenerateWsdlDocument (HttpContext context, string wsdlId)
139                 {
140                         int di = 0;
141                         if (wsdlId != null && wsdlId != "") di = int.Parse (wsdlId);
142                         
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);
147                 }
148                 
149                 void GenerateDiscoDocument (HttpContext context)
150                 {
151                         ServiceDescriptionCollection descs = GetDescriptions ();
152                         
153                         DiscoveryDocument doc = new DiscoveryDocument ();
154                         ContractReference cref = new ContractReference ();
155                         cref.Ref = _url + "?wsdl";
156                         cref.DocRef = _url;
157                         doc.References.Add (cref);
158                         
159                         foreach (ServiceDescription desc in descs)
160                                 foreach (Service ser in desc.Services)
161                                         foreach (Port port in ser.Ports)
162                                         {
163                                                 SoapAddressBinding sab = port.Extensions.Find (typeof(SoapAddressBinding)) as SoapAddressBinding;
164                                                 if (sab != null)
165                                                 {
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);
170                                                 }
171                                         }
172
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;
176                         doc.Write (xtw);
177                 }
178                 
179                 void GenerateSchema (HttpContext context, string schemaId)
180                 {
181                         int di = 0;
182                         if (schemaId != null && schemaId != "") di = int.Parse (schemaId);
183                         
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);
188                 }
189                 
190                 void GenerateCode (HttpContext context, string langId)
191                 {
192                         context.Response.ContentType = "text/plain; charset=utf-8";
193                         CodeNamespace codeNamespace = new CodeNamespace();
194                         CodeCompileUnit codeUnit = new CodeCompileUnit();
195                         
196                         codeUnit.Namespaces.Add (codeNamespace);
197
198                         ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
199                         
200                         foreach (ServiceDescription sd in GetDescriptions ())
201                                 importer.AddServiceDescription(sd, null, null);
202
203                         foreach (XmlSchema sc in GetSchemas())
204                                 importer.Schemas.Add (sc);
205
206                         importer.Import(codeNamespace, codeUnit);
207                         
208                         if (langId == null || langId == "") langId = "cs";
209                         CodeDomProvider provider = GetProvider (langId);
210                         ICodeGenerator generator = provider.CreateGenerator();
211                         CodeGeneratorOptions options = new CodeGeneratorOptions();
212                         
213                         generator.GenerateCodeFromCompileUnit(codeUnit, context.Response.Output, options);
214                 }
215                 
216                 private CodeDomProvider GetProvider(string langId)
217                 {
218                         // FIXME these should be loaded dynamically using reflection
219                         CodeDomProvider provider;
220                         
221                         switch (langId.ToUpper())
222                         {
223                             case "CS":
224                                     provider = new CSharpCodeProvider();
225                                     break;
226                             
227                             default:
228                                     throw new Exception("Unknown language: " + langId);
229                         }
230
231                         return provider;
232                 }
233                 
234                 internal ServiceDescriptionCollection GetDescriptions ()
235                 {
236                         if (_descriptions == null)
237                         {
238                                 ServiceDescriptionReflector reflector = new ServiceDescriptionReflector ();
239                                 reflector.Reflect (ServiceType,_url);
240                                 _schemas = reflector.Schemas;
241                                 _descriptions = reflector.ServiceDescriptions;
242                         }
243                         return _descriptions;
244                 }
245                 
246                 internal XmlSchemas GetSchemas()
247                 {
248                         GetDescriptions();
249                         return _schemas;
250                 }
251         }
252 }