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