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
\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
19 // The above copyright notice and this permission notice shall be
\r
20 // included in all copies or substantial portions of the Software.
\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
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
90 public TypeFilterLevel TypeFilterLevel
\r
92 get { return _soapCore.TypeFilterLevel; }
\r
95 IDictionary props = (IDictionary) ((ICloneable)_soapCore.Properties).Clone ();
\r
96 props ["typeFilterLevel"] = value;
\r
97 _soapCore = new SoapCore (this, props, SoapServerFormatterSinkProvider.AllowedProperties);
\r
101 public void AsyncProcessResponse (IServerResponseChannelSinkStack sinkStack, object state,
\r
102 IMessage msg, ITransportHeaders headers, Stream stream)
\r
105 ITransportHeaders responseHeaders = new TransportHeaders();
\r
107 if(sinkStack != null) stream = sinkStack.GetResponseStream(msg, responseHeaders);
\r
108 if(stream == null) stream = new MemoryStream();
\r
110 SoapMessageFormatter soapMsgFormatter = (SoapMessageFormatter)state;
\r
112 SoapMessage soapMessage = (SoapMessage) soapMsgFormatter.BuildSoapMessageFromMethodResponse((IMethodReturnMessage)msg, out responseHeaders);
\r
114 _soapCore.Serializer.Serialize(stream, soapMessage, null);
\r
116 if(stream is MemoryStream) stream.Position = 0;
\r
117 sinkStack.AsyncProcessResponse (msg, responseHeaders, stream);
\r
120 public Stream GetResponseStream (IServerResponseChannelSinkStack sinkStack, object state,
\r
121 IMessage msg, ITransportHeaders headers)
\r
123 // this method shouldn't be called
\r
124 throw new NotSupportedException ();
\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
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
139 out responseHeaders,
\r
140 out responseStream);
\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
153 out responseHeaders,
\r
154 out responseStream);
\r
157 responseMsg = null;
\r
158 responseHeaders = null;
\r
159 responseStream = null;
\r
161 ServerProcessing sp;
\r
162 SoapMessageFormatter soapMsgFormatter = new SoapMessageFormatter();
\r
163 sinkStack.Push(this, soapMsgFormatter);
\r
166 string url = (string)requestHeaders[CommonTransportKeys.RequestUri];
\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
173 SoapFormatter fm = _soapCore.GetSafeDeserializer ();
\r
174 SoapMessage soapMessage = soapMsgFormatter.CreateSoapMessage (true);
\r
175 fm.TopObject = soapMessage;
\r
176 fm.Deserialize(requestStream);
\r
178 requestMsg = soapMsgFormatter.BuildMethodCallFromSoapMessage(soapMessage, uri);
\r
180 sp = next_sink.ProcessMessage(sinkStack, requestMsg, requestHeaders, null, out responseMsg, out responseHeaders, out responseStream);
\r
182 if(sp == ServerProcessing.Complete) {
\r
183 if(responseMsg != null && responseStream == null) {
\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
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
202 if (sp == ServerProcessing.Complete)
\r
203 sinkStack.Pop(this);
\r
210 public enum Protocol
\r