Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / System.Runtime.Remoting / System.Runtime.Remoting.Channels / SoapServerFormatterSink.cs
1 //\r
2 // System.Runtime.Remoting.Channels.SoapServerFormatterSink.cs\r
3 //\r
4 // Authors:     Duncan Mak (duncan@ximian.com)\r
5 //              Jean-Marc Andre (jean-marc.andre@polymtl.ca)\r
6 //\r
7 // 2002 (C) Copyright, Ximian, Inc.\r
8 //\r
9 \r
10 //\r
11 // Permission is hereby granted, free of charge, to any person obtaining\r
12 // a copy of this software and associated documentation files (the\r
13 // "Software"), to deal in the Software without restriction, including\r
14 // without limitation the rights to use, copy, modify, merge, publish,\r
15 // distribute, sublicense, and/or sell copies of the Software, and to\r
16 // permit persons to whom the Software is furnished to do so, subject to\r
17 // the following conditions:\r
18 // \r
19 // The above copyright notice and this permission notice shall be\r
20 // included in all copies or substantial portions of the Software.\r
21 // \r
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
29 //\r
30 \r
31 using System.Collections;\r
32 using System.IO;\r
33 using System.Reflection;\r
34 using System.Runtime.Remoting.Messaging;\r
35 using System.Runtime.Serialization;\r
36 using System.Runtime.Serialization.Formatters;\r
37 using System.Runtime.Serialization.Formatters.Soap;\r
38 using System.Runtime.InteropServices;\r
39 \r
40 \r
41 namespace System.Runtime.Remoting.Channels {\r
42 \r
43         /// <summary>\r
44         //      The formatter sink that uses SoapFormatter\r
45         /// </summary>\r
46         // <remarks>\r
47         //      The formatter sink deserializes the message from the channel sink\r
48         //      and passes the result to the remoting infrastructure\r
49         // </remark>\r
50         //      \r
51         public class SoapServerFormatterSink : IServerChannelSink, IChannelSinkBase\r
52         {\r
53                 IServerChannelSink next_sink;\r
54                 IChannelReceiver _receiver;\r
55                 private SoapCore _soapCore = SoapCore.DefaultInstance;\r
56                 \r
57                 public SoapServerFormatterSink (SoapServerFormatterSink.Protocol protocol,\r
58                                                 IServerChannelSink nextSink,\r
59                                                 IChannelReceiver receiver)\r
60                 {\r
61                         this.next_sink = nextSink;\r
62                         _receiver = receiver;\r
63                 }\r
64 \r
65                 internal SoapCore SoapCore\r
66                 {\r
67                         get { return _soapCore; }\r
68                         set { _soapCore = value; }\r
69                 }\r
70                 \r
71                 /// <summary>\r
72                 //      Gets the next channel sink in the channel sink chain\r
73                 //  </summary>\r
74                 /// <value>\r
75                 //      The next channel sink in the sink chain\r
76                 //  </value>\r
77                 public IServerChannelSink NextChannelSink {\r
78                         get {\r
79                                 return next_sink;\r
80                         }\r
81                 }\r
82 \r
83                 public IDictionary Properties {\r
84                         get {\r
85                                 return null;\r
86                         }\r
87                 }\r
88 \r
89                 [ComVisible(false)]\r
90                 public TypeFilterLevel TypeFilterLevel\r
91                 {\r
92                         get { return _soapCore.TypeFilterLevel; }\r
93                         set \r
94                         {\r
95                                 IDictionary props = (IDictionary) ((ICloneable)_soapCore.Properties).Clone ();\r
96                                 props ["typeFilterLevel"] = value;\r
97                                 _soapCore = new SoapCore (this, props, SoapServerFormatterSinkProvider.AllowedProperties);\r
98                         }\r
99                 }\r
100 \r
101                 public void AsyncProcessResponse (IServerResponseChannelSinkStack sinkStack, object state,\r
102                                                   IMessage msg, ITransportHeaders headers, Stream stream)\r
103                                                   \r
104                 {\r
105                         ITransportHeaders responseHeaders = new TransportHeaders();\r
106 \r
107                         if(sinkStack != null) stream = sinkStack.GetResponseStream(msg, responseHeaders);\r
108                         if(stream == null) stream = new MemoryStream();\r
109 \r
110                         SoapMessageFormatter soapMsgFormatter = (SoapMessageFormatter)state;\r
111 \r
112                         SoapMessage soapMessage = (SoapMessage) soapMsgFormatter.BuildSoapMessageFromMethodResponse((IMethodReturnMessage)msg, out responseHeaders);\r
113 \r
114                         _soapCore.Serializer.Serialize(stream, soapMessage, null);\r
115 \r
116                         if(stream is MemoryStream) stream.Position = 0;\r
117                         sinkStack.AsyncProcessResponse (msg, responseHeaders, stream);\r
118                 }\r
119 \r
120                 public Stream GetResponseStream (IServerResponseChannelSinkStack sinkStack, object state,\r
121                                                 IMessage msg, ITransportHeaders headers)\r
122                 {\r
123                         // this method shouldn't be called\r
124                         throw new NotSupportedException ();\r
125                 }\r
126                 \r
127                 public ServerProcessing ProcessMessage (IServerChannelSinkStack sinkStack,\r
128                                                         IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream,\r
129                                                         out IMessage responseMsg, out ITransportHeaders responseHeaders, out Stream responseStream)\r
130                 {\r
131                         // Check whether the request was already processed by another\r
132                         // formatter sink and pass the request to the next sink if so.\r
133                         if (requestMsg != null)\r
134                                 return next_sink.ProcessMessage (sinkStack,\r
135                                                                  requestMsg,\r
136                                                                  requestHeaders,\r
137                                                                  requestStream,\r
138                                                                  out responseMsg,\r
139                                                                  out responseHeaders,\r
140                                                                  out responseStream);\r
141 \r
142                         // Check whether the request is suitable for this formatter\r
143                         // and pass the request to the next sink if not.\r
144                         // Note that a null content-type is handled as suitable,\r
145                         // otherwise no other sink will be able to handle the request.\r
146                         string contentType = requestHeaders["Content-Type"] as string;\r
147                         if (contentType == null || !contentType.StartsWith ("text/xml") || requestHeaders["SOAPAction"] == null) {\r
148                                 return next_sink.ProcessMessage (sinkStack,\r
149                                         requestMsg,\r
150                                         requestHeaders,\r
151                                         requestStream,\r
152                                         out responseMsg,\r
153                                         out responseHeaders,\r
154                                         out responseStream);\r
155                         }\r
156 \r
157                         responseMsg = null;\r
158                         responseHeaders = null;\r
159                         responseStream = null;\r
160 \r
161                         ServerProcessing sp;\r
162                         SoapMessageFormatter soapMsgFormatter = new SoapMessageFormatter();\r
163                         sinkStack.Push(this, soapMsgFormatter);\r
164 \r
165                         try {\r
166                                 string url = (string)requestHeaders[CommonTransportKeys.RequestUri];\r
167                                 string uri;\r
168                                 _receiver.Parse(url, out uri);\r
169                                 if(uri == null) uri = url;\r
170                                 Type serverType = RemotingServices.GetServerTypeForUri(uri);\r
171                                 if (serverType == null) throw new RemotingException ("No receiver for uri " + uri);\r
172                         \r
173                                 SoapFormatter fm = _soapCore.GetSafeDeserializer ();\r
174                                 SoapMessage soapMessage = soapMsgFormatter.CreateSoapMessage (true);\r
175                                 fm.TopObject = soapMessage;\r
176                                 fm.Deserialize(requestStream);\r
177 \r
178                                 requestMsg = soapMsgFormatter.BuildMethodCallFromSoapMessage(soapMessage, uri);\r
179                                 \r
180                                 sp = next_sink.ProcessMessage(sinkStack, requestMsg, requestHeaders, null, out responseMsg, out responseHeaders, out responseStream);\r
181                                 \r
182                                 if(sp == ServerProcessing.Complete) {\r
183                                         if(responseMsg != null && responseStream == null) {\r
184 \r
185                                                 object rtnMessageObject = soapMsgFormatter.BuildSoapMessageFromMethodResponse((IMethodReturnMessage) responseMsg, out responseHeaders);\r
186                                                 responseStream = new MemoryStream();\r
187                                                 _soapCore.Serializer.Serialize(responseStream, rtnMessageObject);\r
188                                                 responseStream.Position = 0;\r
189                                         }\r
190                                 }\r
191                         }\r
192                         catch(Exception e)\r
193                         {\r
194                                 responseMsg = (IMethodReturnMessage)new ReturnMessage(e, (IMethodCallMessage)requestMsg);\r
195                                 object rtnMessageObject = soapMsgFormatter.BuildSoapMessageFromMethodResponse((IMethodReturnMessage) responseMsg, out responseHeaders);\r
196                                 responseStream = new MemoryStream();\r
197                                 _soapCore.Serializer.Serialize(responseStream, rtnMessageObject);\r
198                                 responseStream.Position = 0;\r
199                                 sp = ServerProcessing.Complete;\r
200                         }\r
201 \r
202                         if (sp == ServerProcessing.Complete)\r
203                                 sinkStack.Pop(this);\r
204 \r
205                         return sp;\r
206                         \r
207                 }\r
208 \r
209                 [Serializable]\r
210                 public enum Protocol\r
211                 {\r
212                         Http = 0,\r
213                         Other = 1,\r
214                 }\r
215         }\r
216 \r
217 }\r