* System.Web.Services.dll.sources: Added
[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 using System;
11 using System.Reflection;
12 using System.IO;
13 using System.Web.Services;
14
15 namespace System.Web.Services.Protocols 
16 {
17         internal class HttpSimpleWebServiceHandler: WebServiceHandler 
18         {
19                 HttpSimpleTypeStubInfo _typeInfo;
20                 
21                 public HttpSimpleWebServiceHandler (Type type, string protocolName): base (type)
22                 {
23                         _typeInfo = (HttpSimpleTypeStubInfo) TypeStubManager.GetTypeStub (type, protocolName);
24                 }
25                 
26                 protected HttpSimpleTypeStubInfo TypeStub
27                 {
28                         get { return _typeInfo; }
29                 }
30                 
31                 public override void ProcessRequest (HttpContext context)
32                 {
33                         string name = context.Request.PathInfo;
34                         if (name.StartsWith ("/")) name = name.Substring (1);
35                         
36                         Stream respStream = null;
37                         Exception error = null;
38                         try
39                         {
40                                 HttpSimpleMethodStubInfo method = (HttpSimpleMethodStubInfo) _typeInfo.GetMethod (name);
41                                 if (method == null) throw new InvalidOperationException ("Method " + name + " not defined in service " + ServiceType.Name);
42                         
43                                 MimeParameterReader parameterReader = (MimeParameterReader) method.ParameterReaderType.Create ();
44                                 object[] parameters = parameterReader.Read (context.Request);
45                 
46                                 MimeReturnWriter returnWriter = (MimeReturnWriter) method.ReturnWriterType.Create ();
47                                 object result = Invoke (method.MethodInfo, parameters);
48                                 respStream = context.Response.OutputStream;
49                                 returnWriter.Write (context.Response, respStream, result);
50                         }
51                         catch (Exception ex)
52                         {
53                                 error = ex;
54                         }
55                         
56                         if (error != null)
57                         {
58                                 try
59                                 {
60                                         context.Response.ContentType = "text/plain";
61                                         context.Response.StatusCode = 500;
62                                         context.Response.Write (error.Message);
63                                 }
64                                 catch {}
65                         }
66                         
67                         if (respStream != null)
68                                 respStream.Close ();
69                 }
70                 
71                 object Invoke (LogicalMethodInfo method, object[] parameters)
72                 {
73                         try
74                         {
75                                 object server = CreateServerInstance ();
76                                 object[] res = method.Invoke (server, parameters);
77                                 if (!method.IsVoid) return res[0];
78                                 else return null;
79                         }
80                         catch (TargetInvocationException ex)
81                         {
82                                 throw ex.InnerException;
83                         }
84                 }
85         }
86 }