2005-07-26 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Discovery / DiscoveryRequestHandler.cs
1 // \r
2 // System.Web.Services.Discovery.DiscoveryRequestHandler.cs\r
3 //\r
4 // Author:\r
5 //   Dave Bettin (javabettin@yahoo.com)\r
6 //   Lluis Sanchez Gual (lluis@ximian.com)\r
7 //\r
8 // Copyright (C) Dave Bettin, 2002\r
9 //\r
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31 \r
32 using System.Web;\r
33 using System.IO;\r
34 using System.Web.Services.Discovery;\r
35 \r
36 namespace System.Web.Services.Discovery {\r
37         public sealed class DiscoveryRequestHandler : IHttpHandler {\r
38                 \r
39                 #region Constructors\r
40 \r
41                 public DiscoveryRequestHandler () \r
42                 {\r
43                 }\r
44                 \r
45                 #endregion // Constructors\r
46 \r
47                 #region Properties\r
48 \r
49                 public bool IsReusable {\r
50                         get { return true; }                    \r
51                 }\r
52                 \r
53                 #endregion // Properties\r
54 \r
55                 #region Methods\r
56 \r
57                 public void ProcessRequest (HttpContext context)\r
58                 {\r
59                         string path = context.Request.PhysicalPath;\r
60                         FileStream fs = new FileStream (path, FileMode.Open);\r
61                         DynamicDiscoveryDocument ddoc = DynamicDiscoveryDocument.Load (fs);\r
62                         fs.Close ();\r
63                         \r
64                         string url = context.Request.Url.ToString();\r
65                         int i = url.LastIndexOf ('/');\r
66                         if (i != -1) url = url.Substring (0,i+1);\r
67                         \r
68                         DiscoveryDocument doc = new DiscoveryDocument ();\r
69                         GetFiles (url, "", Path.GetDirectoryName(path), doc, ddoc);\r
70                         \r
71                         context.Response.ContentType = "text/xml; charset=utf-8";\r
72                         doc.Write (context.Response.OutputStream);\r
73                 }\r
74                 \r
75                 void GetFiles (string baseUrl, string relPath, string path, DiscoveryDocument doc, DynamicDiscoveryDocument ddoc)\r
76                 {\r
77                         string url = baseUrl + relPath;\r
78                         if (!url.EndsWith ("/")) url += "/";\r
79                         \r
80                         string[] files = Directory.GetFiles (path);\r
81                         foreach (string file in files)\r
82                         {\r
83                                 string ext = Path.GetExtension (file).ToLower();\r
84                                 if (ext == ".asmx")\r
85                                 {\r
86                                         ContractReference cref = new ContractReference ();\r
87                                         cref.DocRef = url + Path.GetFileName (file);\r
88                                         cref.Ref = cref.DocRef + "?wsdl";\r
89                                         doc.References.Add (cref);\r
90                                 }\r
91                                 else if (ext == ".disco")\r
92                                 {\r
93                                         DiscoveryDocumentReference dref = new DiscoveryDocumentReference ();\r
94                                         dref.Ref = url + Path.GetFileName (file);\r
95                                         doc.References.Add (dref);\r
96                                 }\r
97                         }\r
98                         string[] dirs = Directory.GetDirectories (path);\r
99                         \r
100                         foreach (string dir in dirs)\r
101                         {\r
102                                 string rel = Path.Combine (relPath, Path.GetFileName(dir));\r
103                                 if (!ddoc.IsExcluded (rel))\r
104                                         GetFiles (baseUrl, rel, Path.Combine (path, dir), doc, ddoc);\r
105                         }\r
106                 }\r
107 \r
108                 #endregion // Methods\r
109         }\r
110 }\r