2006-11-02 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / HttpSimpleWebServiceHandler.cs
1 // 
2 // System.Web.Services.Protocols.HttpSimpleWebServiceHandler.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.Reflection;
33 using System.IO;
34 using System.Web.Services;
35
36 namespace System.Web.Services.Protocols 
37 {
38         internal class HttpSimpleWebServiceHandler: WebServiceHandler 
39         {
40                 HttpSimpleTypeStubInfo _typeInfo;
41                 HttpSimpleMethodStubInfo method;
42                 
43                 public HttpSimpleWebServiceHandler (Type type, string protocolName)
44                 : base (type)
45                 {
46                         _typeInfo = (HttpSimpleTypeStubInfo) TypeStubManager.GetTypeStub (type, protocolName);
47                 }
48                 
49                 protected HttpSimpleTypeStubInfo TypeStub
50                 {
51                         get { return _typeInfo; }
52                 }
53                 
54                 internal override MethodStubInfo GetRequestMethod (HttpContext context)
55                 {
56                         string name = context.Request.PathInfo;
57                         if (name.StartsWith ("/"))
58                                 name = name.Substring (1);
59
60                         method = (HttpSimpleMethodStubInfo) _typeInfo.GetMethod (name);
61                         if (method == null) 
62                                 WriteError (context, "Method " + name + " not defined in service " + ServiceType.Name);
63                         
64                         return method;
65                 }
66                 
67                 public override void ProcessRequest (HttpContext context)
68                 {
69                         Context = context;
70                         Stream respStream = null;
71                         Exception error = null;
72                         
73                         try
74                         {
75                                 if (method == null) 
76                                         method = (HttpSimpleMethodStubInfo) GetRequestMethod (context);
77
78                                 if (method.MethodInfo.EnableSession)
79                                         Session = context.Session;
80                         
81                                 MimeParameterReader parameterReader = (MimeParameterReader) method.ParameterReaderType.Create ();
82                                 object[] parameters = parameterReader.Read (context.Request);
83                 
84                                 MimeReturnWriter returnWriter = (MimeReturnWriter) method.ReturnWriterType.Create ();
85                                 object result = Invoke (method.MethodInfo, parameters);
86                                 respStream = context.Response.OutputStream;
87                                 returnWriter.Write (context.Response, respStream, result);
88                         }
89                         catch (Exception ex)
90                         {
91                                 error = ex;
92                         }
93                         
94                         if (error != null)
95                                 WriteError (context, error.Message);
96                         
97                         if (respStream != null)
98                                 respStream.Close ();
99                 }
100                 
101                 void WriteError (HttpContext context, string msg)
102                 {
103                         try
104                         {
105                                 context.Response.ContentType = "text/plain";
106                                 context.Response.StatusCode = 500;
107                                 context.Response.Write (msg);
108                                 context.Response.End ();
109                         }
110                         catch {}
111                 }
112                 
113                 object Invoke (LogicalMethodInfo method, object[] parameters)
114                 {
115                         try
116                         {
117                                 object server = CreateServerInstance ();
118                                 object[] res = method.Invoke (server, parameters);
119                                 if (!method.IsVoid) return res[0];
120                                 else return null;
121                         }
122                         catch (TargetInvocationException ex)
123                         {
124                                 throw ex.InnerException;
125                         }
126                 }
127         }
128 }