2 // System.Runtime.Remoting.Channels.SoapServerFormatterSink.cs
\r
4 // Authors: Duncan Mak (duncan@ximian.com)
\r
5 // Jean-Marc Andre (jean-marc.andre@polymtl.ca)
\r
7 // 2002 (C) Copyright, Ximian, Inc.
\r
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
\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
41 namespace System.Runtime.Remoting.Channels {
\r
44 // The formatter sink that uses SoapFormatter
\r
47 // The formatter sink deserializes the message from the channel sink
\r
48 // and passes the result to the remoting infrastructure
\r
51 public class SoapServerFormatterSink : IServerChannelSink, IChannelSinkBase
\r
53 IServerChannelSink next_sink;
\r
54 IChannelReceiver _receiver;
\r
55 private SoapCore _soapCore = SoapCore.DefaultInstance;
\r
57 public SoapServerFormatterSink (SoapServerFormatterSink.Protocol protocol,
\r
58 IServerChannelSink nextSink,
\r
59 IChannelReceiver receiver)
\r
61 this.next_sink = nextSink;
\r
62 _receiver = receiver;
\r
65 internal SoapCore SoapCore
\r
67 get { return _soapCore; }
\r
68 set { _soapCore = value; }
\r
72 // Gets the next channel sink in the channel sink chain
\r
75 // The next channel sink in the sink chain
\r
77 public IServerChannelSink NextChannelSink {
\r
83 public IDictionary Properties {
\r
91 public TypeFilterLevel TypeFilterLevel
\r
93 get { return _soapCore.TypeFilterLevel; }
\r
96 IDictionary props = (IDictionary) ((ICloneable)_soapCore.Properties).Clone ();
\r
97 props ["typeFilterLevel"] = value;
\r
98 _soapCore = new SoapCore (this, props, SoapServerFormatterSinkProvider.AllowedProperties);
\r
103 public void AsyncProcessResponse (IServerResponseChannelSinkStack sinkStack, object state,
\r
104 IMessage msg, ITransportHeaders headers, Stream stream)
\r
107 ITransportHeaders responseHeaders = new TransportHeaders();
\r
109 if(sinkStack != null) stream = sinkStack.GetResponseStream(msg, responseHeaders);
\r
110 if(stream == null) stream = new MemoryStream();
\r
112 SoapMessageFormatter soapMsgFormatter = (SoapMessageFormatter)state;
\r
114 SoapMessage soapMessage = (SoapMessage) soapMsgFormatter.BuildSoapMessageFromMethodResponse((IMethodReturnMessage)msg, out responseHeaders);
\r
116 _soapCore.Serializer.Serialize(stream, soapMessage, null);
\r
118 if(stream is MemoryStream) stream.Position = 0;
\r
119 sinkStack.AsyncProcessResponse (msg, responseHeaders, stream);
\r
122 public Stream GetResponseStream (IServerResponseChannelSinkStack sinkStack, object state,
\r
123 IMessage msg, ITransportHeaders headers)
\r
125 // this method shouldn't be called
\r
126 throw new NotSupportedException ();
\r
129 public ServerProcessing ProcessMessage (IServerChannelSinkStack sinkStack,
\r
130 IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream,
\r
131 out IMessage responseMsg, out ITransportHeaders responseHeaders, out Stream responseStream)
\r
133 // Check whether the request was already processed by another
134 // formatter sink and pass the request to the next sink if so.
135 if (requestMsg != null)
136 return next_sink.ProcessMessage (sinkStack,
144 // Check whether the request is suitable for this formatter
145 // and pass the request to the next sink if not.
146 // Note that a null content-type is handled as suitable,
147 // otherwise no other sink will be able to handle the request.
148 string contentType = requestHeaders["Content-Type"] as string;
149 if (contentType != null && !contentType.StartsWith ("text/xml")) {
151 return next_sink.ProcessMessage (sinkStack,
159 // Let this formatter handle the exception.
163 responseMsg = null;
\r
164 responseHeaders = null;
\r
165 responseStream = null;
\r
167 ServerProcessing sp;
\r
168 SoapMessageFormatter soapMsgFormatter = new SoapMessageFormatter();
\r
169 sinkStack.Push(this, soapMsgFormatter);
\r
172 string url = (string)requestHeaders[CommonTransportKeys.RequestUri];
\r
174 _receiver.Parse(url, out uri);
\r
175 if(uri == null) uri = url;
\r
176 Type serverType = RemotingServices.GetServerTypeForUri(uri);
\r
177 if (serverType == null) throw new RemotingException ("No receiver for uri " + uri);
\r
179 SoapFormatter fm = _soapCore.GetSafeDeserializer ();
\r
180 SoapMessage soapMessage = soapMsgFormatter.CreateSoapMessage (true);
\r
181 fm.TopObject = soapMessage;
\r
182 requestStream.Position = 0;
\r
183 fm.Deserialize(requestStream);
\r
185 requestMsg = soapMsgFormatter.BuildMethodCallFromSoapMessage(soapMessage, uri);
\r
187 sp = next_sink.ProcessMessage(sinkStack, requestMsg, requestHeaders, null, out responseMsg, out responseHeaders, out responseStream);
\r
189 if(sp == ServerProcessing.Complete) {
\r
190 if(responseMsg != null && responseStream == null) {
\r
192 object rtnMessageObject = soapMsgFormatter.BuildSoapMessageFromMethodResponse((IMethodReturnMessage) responseMsg, out responseHeaders);
\r
193 responseStream = new MemoryStream();
\r
194 _soapCore.Serializer.Serialize(responseStream, rtnMessageObject);
\r
200 responseMsg = (IMethodReturnMessage)new ReturnMessage(e, (IMethodCallMessage)requestMsg);
\r
201 object rtnMessageObject = soapMsgFormatter.BuildSoapMessageFromMethodResponse((IMethodReturnMessage) responseMsg, out responseHeaders);
\r
202 responseStream = new MemoryStream();
\r
203 _soapCore.Serializer.Serialize(responseStream, rtnMessageObject);
\r
204 sp = ServerProcessing.Complete;
\r
207 if (sp == ServerProcessing.Complete)
\r
208 sinkStack.Pop(this);
\r
215 public enum Protocol
\r