svn path=/trunk/mcs/; revision=104772
[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.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;
48
49 namespace System.Web.Services.Protocols
50 {
51         internal class SoapDocumentationHandler: WebServiceHandler
52         {
53                 SoapTypeStubInfo _typeStubInfo;
54                 ServiceDescriptionCollection _descriptions;
55                 XmlSchemas _schemas;
56                 string _url;
57                 IHttpHandler _pageHandler = null;
58
59                 public SoapDocumentationHandler (Type type, HttpContext context): base (type)
60                 {
61                         _url = context.Request.Url.ToString();
62                         int i = _url.LastIndexOf ('?');
63                         if (i != -1) _url = _url.Substring (0,i);
64                         _typeStubInfo = (SoapTypeStubInfo) TypeStubManager.GetTypeStub (ServiceType, "Soap");
65                         
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];
72
73                                 if (key != null)
74                                         key = key.ToLower (CultureInfo.InvariantCulture);
75                         }
76                                 
77                         if (key == "wsdl" || key == "schema" || key == "code" || key == "disco")
78                                 return;
79                                 
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 = "/";
94                         }
95
96                         if (vpath.EndsWith ("/"))
97                                 vpath += help;
98                         else
99                                 vpath += "/" + help;
100
101                         string physPath = Path.Combine (path, help);
102                         
103 #if !TARGET_JVM
104                         if (!File.Exists (physPath))
105                                 throw new InvalidOperationException ("Documentation page '" + physPath + "' not found");
106 #endif
107
108 #if NET_2_0 && !TARGET_JVM
109                         // Since BuildManager expects the virtualPath to be mappable into the
110                         // application virtual root directory and the WSDL help generator possibly
111                         // lives outside the location (by default in $prefix/etc/mono/2.0/), we need
112                         // to use a fake virtual path which will be recognized by the page builder
113                         // and processed accordingly.
114                         // The fake virtual path prefix is defined in
115                         // BuildManager.FAKE_VIRTUAL_PATH_PREFIX constant
116                         _pageHandler = BuildManager.CreateInstanceFromVirtualPath ("/@@MonoFakeVirtualPath@@" + physPath, typeof (IHttpHandler)) as IHttpHandler;
117 #else
118                         _pageHandler = PageParser.GetCompiledPageInstance (vpath, physPath, context);
119 #endif
120                                 
121                 }
122
123                 internal IHttpHandler PageHandler {
124                         get { return _pageHandler; }
125                 }
126
127                 public override bool IsReusable 
128                 {
129                         get { return false; }
130                 }
131
132                 public override void ProcessRequest (HttpContext context)
133                 {
134                         if (_pageHandler != null)
135                         {
136                                 context.Items["wsdls"] = GetDescriptions ();
137                                 context.Items["schemas"] = GetSchemas ();
138                                 _pageHandler.ProcessRequest (context);
139                         }
140                         else
141                         {
142                                 HttpRequest req = context.Request;
143                                 string key = req.QueryString.GetKey (0);
144                                 if (key == null)
145                                         key = req.QueryString [0];
146
147                                 if (key != null)
148                                         key = key.ToLower (CultureInfo.InvariantCulture);
149
150                                 if (key  == "wsdl") GenerateWsdlDocument (context, req.QueryString ["wsdl"]);
151                                 else if (key == "schema") GenerateSchema (context, req.QueryString ["schema"]);
152 #if !TARGET_JVM //code generation is not supported
153                                 else if (key == "code") GenerateCode (context, req.QueryString ["code"]);
154 #else
155                                 else if (key == "code") throw new Exception("Code generation is not supported.");
156 #endif
157                                 else if (key == "disco") GenerateDiscoDocument (context);
158                                 else throw new Exception ("This should never happen");
159                         }
160                 }
161
162                 void GenerateWsdlDocument (HttpContext context, string wsdlId)
163                 {
164                         int di = 0;
165                         if (wsdlId != null && wsdlId != "") di = int.Parse (wsdlId);
166                         
167                         context.Response.ContentType = "text/xml; charset=utf-8";
168                         XmlTextWriter xtw = new XmlTextWriter (context.Response.OutputStream, new UTF8Encoding (false));
169                         xtw.Formatting = Formatting.Indented;
170                         GetDescriptions() [di].Write (xtw);
171                 }
172                 
173                 void GenerateDiscoDocument (HttpContext context)
174                 {
175                         ServiceDescriptionCollection descs = GetDescriptions ();
176                         
177                         DiscoveryDocument doc = new DiscoveryDocument ();
178                         ContractReference cref = new ContractReference ();
179                         cref.Ref = _url + "?wsdl";
180                         cref.DocRef = _url;
181                         doc.References.Add (cref);
182                         
183                         foreach (ServiceDescription desc in descs)
184                                 foreach (Service ser in desc.Services)
185                                         foreach (Port port in ser.Ports)
186                                         {
187                                                 SoapAddressBinding sab = port.Extensions.Find (typeof(SoapAddressBinding)) as SoapAddressBinding;
188                                                 if (sab != null)
189                                                 {
190                                                         System.Web.Services.Discovery.SoapBinding dsb = new System.Web.Services.Discovery.SoapBinding ();
191                                                         dsb.Address = sab.Location;
192                                                         dsb.Binding = port.Binding;
193                                                         doc.AdditionalInfo.Add (dsb);
194                                                 }
195                                         }
196
197                         context.Response.ContentType = "text/xml; charset=utf-8";
198                         XmlTextWriter xtw = new XmlTextWriter (context.Response.OutputStream, new UTF8Encoding (false));
199                         xtw.Formatting = Formatting.Indented;
200                         doc.Write (xtw);
201                 }
202                 
203                 void GenerateSchema (HttpContext context, string schemaId)
204                 {
205                         int di = -1;
206                         if (schemaId != null && schemaId != "") {
207                                 try {
208                                         di = int.Parse (schemaId);
209                                 } catch {
210                                         XmlSchemas xss = GetSchemas ();
211                                         for (int i = 0; i < xss.Count; i++) {
212                                                 if (xss [i].Id == schemaId) {
213                                                         di = i;
214                                                         break;
215                                                 }
216                                         }
217                                 }
218                                 if (di < 0)
219                                         throw new InvalidOperationException (String.Format ("HTTP parameter 'schema' needs to specify an Id of a schema in the schemas. {0} points to nowhere.", schemaId));
220                         }
221                         context.Response.ContentType = "text/xml; charset=utf-8";
222                         XmlTextWriter xtw = new XmlTextWriter (context.Response.OutputStream, new UTF8Encoding (false));
223                         xtw.Formatting = Formatting.Indented;
224                         GetSchemas() [di].Write (xtw);
225                 }
226
227 #if !TARGET_JVM         
228                 void GenerateCode (HttpContext context, string langId)
229                 {
230                         context.Response.ContentType = "text/plain; charset=utf-8";
231                         CodeNamespace codeNamespace = new CodeNamespace();
232                         CodeCompileUnit codeUnit = new CodeCompileUnit();
233                         
234                         codeUnit.Namespaces.Add (codeNamespace);
235
236                         ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
237                         
238                         foreach (ServiceDescription sd in GetDescriptions ())
239                                 importer.AddServiceDescription(sd, null, null);
240
241                         foreach (XmlSchema sc in GetSchemas())
242                                 importer.Schemas.Add (sc);
243
244                         importer.Import(codeNamespace, codeUnit);
245                         
246                         if (langId == null || langId == "") langId = "cs";
247                         CodeDomProvider provider = GetProvider (langId);
248                         ICodeGenerator generator = provider.CreateGenerator();
249                         CodeGeneratorOptions options = new CodeGeneratorOptions();
250                         
251                         generator.GenerateCodeFromCompileUnit(codeUnit, context.Response.Output, options);
252                 }
253                 
254                 private CodeDomProvider GetProvider(string langId)
255                 {
256                         // FIXME these should be loaded dynamically using reflection
257                         CodeDomProvider provider;
258                         
259                         switch (langId.ToUpper())
260                         {
261                             case "CS":
262                                     provider = new CSharpCodeProvider();
263                                     break;
264                             
265                             default:
266                                     throw new Exception("Unknown language: " + langId);
267                         }
268
269                         return provider;
270                 }
271 #endif
272                 
273                 internal ServiceDescriptionCollection GetDescriptions ()
274                 {
275                         if (_descriptions == null)
276                         {
277                                 ServiceDescriptionReflector reflector = new ServiceDescriptionReflector ();
278                                 reflector.Reflect (ServiceType,_url);
279                                 _schemas = reflector.Schemas;
280                                 _descriptions = reflector.ServiceDescriptions;
281                         }
282                         return _descriptions;
283                 }
284                 
285                 internal XmlSchemas GetSchemas()
286                 {
287                         GetDescriptions();
288                         return _schemas;
289                 }
290         }
291 }