**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Runtime.Remoting / System.Runtime.Remoting.Channels / SoapServerFormatterSink.cs
blob166b93d35dc74aac6678913634f67fa4f67467b9
1 //
2 // System.Runtime.Remoting.Channels.SoapServerFormatterSink.cs
3 //
4 // Authors: Duncan Mak (duncan@ximian.com)
5 // Jean-Marc Andre (jean-marc.andre@polymtl.ca)
6 //
7 // 2002 (C) Copyright, Ximian, Inc.
8 //
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.
31 using System.Collections;
32 using System.IO;
33 using System.Reflection;
34 using System.Runtime.Remoting.Messaging;
35 using System.Runtime.Serialization;
36 using System.Runtime.Serialization.Formatters;
37 using System.Runtime.Serialization.Formatters.Soap;
38 using System.Runtime.InteropServices;
41 namespace System.Runtime.Remoting.Channels {
43 /// <summary>
44 // The formatter sink that uses SoapFormatter
45 /// </summary>
46 // <remarks>
47 // The formatter sink deserializes the message from the channel sink
48 // and passes the result to the remoting infrastructure
49 // </remark>
50 //
51 public class SoapServerFormatterSink : IServerChannelSink, IChannelSinkBase
53 IServerChannelSink next_sink;
54 IChannelReceiver _receiver;
55 private SoapCore _soapCore = SoapCore.DefaultInstance;
57 public SoapServerFormatterSink (SoapServerFormatterSink.Protocol protocol,
58 IServerChannelSink nextSink,
59 IChannelReceiver receiver)
61 this.next_sink = nextSink;
62 _receiver = receiver;
65 internal SoapCore SoapCore
67 get { return _soapCore; }
68 set { _soapCore = value; }
71 /// <summary>
72 // Gets the next channel sink in the channel sink chain
73 // </summary>
74 /// <value>
75 // The next channel sink in the sink chain
76 // </value>
77 public IServerChannelSink NextChannelSink {
78 get {
79 return next_sink;
83 public IDictionary Properties {
84 get {
85 return null;
89 #if NET_1_1
90 [ComVisible(false)]
91 public TypeFilterLevel TypeFilterLevel
93 get { return _soapCore.TypeFilterLevel; }
94 set
96 IDictionary props = (IDictionary) ((ICloneable)_soapCore.Properties).Clone ();
97 props ["typeFilterLevel"] = value;
98 _soapCore = new SoapCore (this, props, SoapServerFormatterSinkProvider.AllowedProperties);
101 #endif
103 public void AsyncProcessResponse (IServerResponseChannelSinkStack sinkStack, object state,
104 IMessage msg, ITransportHeaders headers, Stream stream)
107 ITransportHeaders responseHeaders = new TransportHeaders();
109 if(sinkStack != null) stream = sinkStack.GetResponseStream(msg, responseHeaders);
110 if(stream == null) stream = new MemoryStream();
112 SoapMessageFormatter soapMsgFormatter = (SoapMessageFormatter)state;
114 SoapMessage soapMessage = (SoapMessage) soapMsgFormatter.BuildSoapMessageFromMethodResponse((IMethodReturnMessage)msg, out responseHeaders);
116 _soapCore.Serializer.Serialize(stream, soapMessage, null);
118 if(stream is MemoryStream) stream.Position = 0;
119 sinkStack.AsyncProcessResponse (msg, responseHeaders, stream);
122 public Stream GetResponseStream (IServerResponseChannelSinkStack sinkStack, object state,
123 IMessage msg, ITransportHeaders headers)
125 // this method shouldn't be called
126 throw new NotSupportedException ();
129 public ServerProcessing ProcessMessage (IServerChannelSinkStack sinkStack,
130 IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream,
131 out IMessage responseMsg, out ITransportHeaders responseHeaders, out Stream responseStream)
133 responseMsg = null;
134 responseHeaders = null;
135 responseStream = null;
137 Exception exception;
138 ServerProcessing sp;
139 SoapMessageFormatter soapMsgFormatter = new SoapMessageFormatter();
140 sinkStack.Push(this, soapMsgFormatter);
142 try {
143 string url = (string)requestHeaders[CommonTransportKeys.RequestUri];
144 string uri;
145 _receiver.Parse(url, out uri);
146 if(uri == null) uri = url;
147 Type serverType = RemotingServices.GetServerTypeForUri(uri);
148 if (serverType == null) throw new RemotingException ("No receiver for uri " + uri);
150 SoapFormatter fm = _soapCore.GetSafeDeserializer ();
151 SoapMessage soapMessage = soapMsgFormatter.CreateSoapMessage (true);
152 fm.TopObject = soapMessage;
153 requestStream.Position = 0;
154 fm.Deserialize(requestStream);
156 requestMsg = soapMsgFormatter.BuildMethodCallFromSoapMessage(soapMessage, uri);
158 sp = next_sink.ProcessMessage(sinkStack, requestMsg, requestHeaders, null, out responseMsg, out responseHeaders, out responseStream);
160 if(sp == ServerProcessing.Complete) {
161 if(responseMsg != null && responseStream == null) {
163 object rtnMessageObject = soapMsgFormatter.BuildSoapMessageFromMethodResponse((IMethodReturnMessage) responseMsg, out responseHeaders);
164 responseStream = new MemoryStream();
165 _soapCore.Serializer.Serialize(responseStream, rtnMessageObject);
169 catch(Exception e)
171 responseMsg = (IMethodReturnMessage)new ReturnMessage(e, (IMethodCallMessage)requestMsg);
172 object rtnMessageObject = soapMsgFormatter.BuildSoapMessageFromMethodResponse((IMethodReturnMessage) responseMsg, out responseHeaders);
173 responseStream = new MemoryStream();
174 _soapCore.Serializer.Serialize(responseStream, rtnMessageObject);
175 sp = ServerProcessing.Complete;
178 if (sp == ServerProcessing.Complete)
179 sinkStack.Pop(this);
181 return sp;
185 [Serializable]
186 public enum Protocol
188 Http = 0,
189 Other = 1,