8dc1125601b2f8bca69ce96fece81c0418c8215c
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / HttpSimpleTypeStubInfo.cs
1 //
2 // HttpSimpleMethodStubInfo.cs: Information about a method and its mapping to a SOAP web service.
3 //
4 // Author:
5 //   Lluis Sanchez Gual (lluis@ximian.com)
6 //
7 // (C) 2003 Ximian, Inc.
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.Reflection;
32 using System.Collections;
33 using System.Xml;
34 using System.Xml.Serialization;
35 using System.Web.Services;
36 using System.Web.Services.Description;
37
38 namespace System.Web.Services.Protocols 
39 {
40         enum FormatterKind
41         {
42                 ParameterWriter,
43                 ParameterReader,
44                 ReturnReader,
45                 ReturnWriter
46         }
47         
48         internal class HttpSimpleMethodStubInfo : MethodStubInfo
49         {
50                 public MimeFormatterInfo ParameterWriterType;
51                 public MimeFormatterInfo ParameterReaderType;
52                 public MimeFormatterInfo ReturnReaderType;
53                 public MimeFormatterInfo ReturnWriterType;
54                 
55                 public MimeFormatterInfo GetFormatterInfo (FormatterKind kind)
56                 {
57                         switch (kind)
58                         {
59                                 case FormatterKind.ParameterWriter: return ParameterWriterType;
60                                 case FormatterKind.ParameterReader: return ParameterReaderType;
61                                 case FormatterKind.ReturnReader: return ReturnReaderType;
62                                 case FormatterKind.ReturnWriter: return ReturnWriterType;
63                         }
64                         return null;
65                 }
66                 
67                 public HttpSimpleMethodStubInfo (TypeStubInfo parent, LogicalMethodInfo source): base (parent, source)
68                 {
69                         object[] atts = source.CustomAttributeProvider.GetCustomAttributes (typeof(HttpMethodAttribute), true);
70                         if (atts.Length > 0)
71                         {
72                                 HttpMethodAttribute at = (HttpMethodAttribute) atts[0];
73                                 ParameterWriterType = new MimeFormatterInfo (at.ParameterFormatter);
74                                 ReturnReaderType = new MimeFormatterInfo (at.ReturnFormatter);
75                         }
76                         
77                         if (ReturnReaderType == null) {
78                                 if (source.IsVoid) ReturnReaderType = new MimeFormatterInfo (typeof(NopReturnReader));
79                                 else ReturnReaderType = new MimeFormatterInfo (typeof(XmlReturnReader));
80                         }
81                 }
82         }
83         
84         internal class MimeFormatterInfo
85         {
86                 public MimeFormatterInfo (Type type) {
87                         Type = type;
88                 }
89                 
90                 public MimeFormatter Create () {
91                         return MimeFormatter.CreateInstance (Type, Initializer);
92                 }
93                 
94                 public Type Type;
95                 public object Initializer;
96         }
97
98         internal class HttpSimpleTypeStubInfo : TypeStubInfo
99         {
100                 public HttpSimpleTypeStubInfo (LogicalTypeInfo logicalTypeInfo): base (logicalTypeInfo)
101                 {
102                 }
103
104                 protected override void BuildTypeMethods ()
105                 {
106                         base.BuildTypeMethods ();
107                         
108                         BuildInitializers (FormatterKind.ParameterWriter);
109                         BuildInitializers (FormatterKind.ParameterReader);
110                         BuildInitializers (FormatterKind.ReturnReader);
111                         BuildInitializers (FormatterKind.ReturnWriter);
112                 }
113                 
114                 void BuildInitializers (FormatterKind formatter)
115                 {
116                         Hashtable types = new Hashtable ();
117
118                         foreach (HttpSimpleMethodStubInfo met in Methods)
119                                 AddType (types, met.GetFormatterInfo (formatter).Type, met);
120                         
121                         foreach (DictionaryEntry ent in types)
122                         {
123                                 Type t = (Type)ent.Key;
124                                 ArrayList list = (ArrayList)ent.Value;
125                                 LogicalMethodInfo[] mets = new LogicalMethodInfo [list.Count];
126                                 for (int n=0; n<list.Count; n++) 
127                                         mets[n] = ((MethodStubInfo)list[n]).MethodInfo;
128
129                                 object[] inits = MimeFormatter.GetInitializers (t, mets);
130
131                                 for (int n=0; n<list.Count; n++)
132                                         ((HttpSimpleMethodStubInfo)list[n]).GetFormatterInfo (formatter).Initializer = inits[n];
133                         }
134                 }
135
136                 void AddType (Hashtable types, Type type, HttpSimpleMethodStubInfo method)
137                 {
138                         ArrayList list = (ArrayList) types [type];
139                         if (list == null)
140                         {
141                                 list = new ArrayList ();
142                                 types [type] = list;
143                         }
144                         list.Add (method);
145                 }
146                 
147                 protected override MethodStubInfo CreateMethodStubInfo (TypeStubInfo typeInfo, LogicalMethodInfo methodInfo, bool isClientProxy)
148                 {
149                         return new HttpSimpleMethodStubInfo (typeInfo, methodInfo);
150                 }
151         }
152 }