SoapServerFormatterSink.cs 6.3 KB

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