SoapServerFormatterSink.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. using System.Collections;
  10. using System.IO;
  11. using System.Reflection;
  12. using System.Runtime.Remoting.Messaging;
  13. using System.Runtime.Serialization;
  14. using System.Runtime.Serialization.Formatters;
  15. using System.Runtime.Serialization.Formatters.Soap;
  16. using System.Runtime.InteropServices;
  17. namespace System.Runtime.Remoting.Channels {
  18. /// <summary>
  19. // The formatter sink that uses SoapFormatter
  20. /// </summary>
  21. // <remarks>
  22. // The formatter sink deserializes the message from the channel sink
  23. // and passes the result to the remoting infrastructure
  24. // </remark>
  25. //
  26. public class SoapServerFormatterSink : IServerChannelSink, IChannelSinkBase
  27. {
  28. IServerChannelSink next_sink;
  29. IChannelReceiver _receiver;
  30. private SoapCore _soapCore = SoapCore.DefaultInstance;
  31. public SoapServerFormatterSink (SoapServerFormatterSink.Protocol protocol,
  32. IServerChannelSink nextSink,
  33. IChannelReceiver receiver)
  34. {
  35. this.next_sink = nextSink;
  36. _receiver = receiver;
  37. }
  38. internal SoapCore SoapCore
  39. {
  40. get { return _soapCore; }
  41. set { _soapCore = value; }
  42. }
  43. /// <summary>
  44. // Gets the next channel sink in the channel sink chain
  45. // </summary>
  46. /// <value>
  47. // The next channel sink in the sink chain
  48. // </value>
  49. public IServerChannelSink NextChannelSink {
  50. get {
  51. return next_sink;
  52. }
  53. }
  54. public IDictionary Properties {
  55. get {
  56. return null;
  57. }
  58. }
  59. #if NET_1_1
  60. [ComVisible(false)]
  61. public TypeFilterLevel TypeFilterLevel
  62. {
  63. get { return _soapCore.TypeFilterLevel; }
  64. set
  65. {
  66. IDictionary props = (IDictionary) ((ICloneable)_soapCore.Properties).Clone ();
  67. props ["typeFilterLevel"] = value;
  68. _soapCore = new SoapCore (this, props, SoapServerFormatterSinkProvider.AllowedProperties);
  69. }
  70. }
  71. #endif
  72. public void AsyncProcessResponse (IServerResponseChannelSinkStack sinkStack, object state,
  73. IMessage msg, ITransportHeaders headers, Stream stream)
  74. {
  75. ITransportHeaders responseHeaders = new TransportHeaders();
  76. if(sinkStack != null) stream = sinkStack.GetResponseStream(msg, responseHeaders);
  77. if(stream == null) stream = new MemoryStream();
  78. SoapMessageFormatter soapMsgFormatter = (SoapMessageFormatter)state;
  79. SoapMessage soapMessage = (SoapMessage) soapMsgFormatter.BuildSoapMessageFromMethodResponse((IMethodReturnMessage)msg, out responseHeaders);
  80. _soapCore.Serializer.Serialize(stream, soapMessage, null);
  81. if(stream is MemoryStream) stream.Position = 0;
  82. sinkStack.AsyncProcessResponse (msg, responseHeaders, stream);
  83. }
  84. public Stream GetResponseStream (IServerResponseChannelSinkStack sinkStack, object state,
  85. IMessage msg, ITransportHeaders headers)
  86. {
  87. // this method shouldn't be called
  88. throw new NotSupportedException ();
  89. }
  90. public ServerProcessing ProcessMessage (IServerChannelSinkStack sinkStack,
  91. IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream,
  92. out IMessage responseMsg, out ITransportHeaders responseHeaders, out Stream responseStream)
  93. {
  94. responseMsg = null;
  95. responseHeaders = null;
  96. responseStream = null;
  97. Exception exception;
  98. ServerProcessing sp;
  99. SoapMessageFormatter soapMsgFormatter = new SoapMessageFormatter();
  100. sinkStack.Push(this, soapMsgFormatter);
  101. try {
  102. string url = (string)requestHeaders[CommonTransportKeys.RequestUri];
  103. string uri;
  104. _receiver.Parse(url, out uri);
  105. if(uri == null) uri = url;
  106. Type serverType = RemotingServices.GetServerTypeForUri(uri);
  107. if (serverType == null) throw new RemotingException ("No receiver for uri " + uri);
  108. SoapFormatter fm = _soapCore.GetSafeDeserializer ();
  109. SoapMessage soapMessage = soapMsgFormatter.CreateSoapMessage (true);
  110. fm.TopObject = soapMessage;
  111. requestStream.Position = 0;
  112. fm.Deserialize(requestStream);
  113. requestMsg = soapMsgFormatter.BuildMethodCallFromSoapMessage(soapMessage, uri);
  114. sp = next_sink.ProcessMessage(sinkStack, requestMsg, requestHeaders, null, out responseMsg, out responseHeaders, out responseStream);
  115. if(sp == ServerProcessing.Complete) {
  116. if(responseMsg != null && responseStream == null) {
  117. object rtnMessageObject = soapMsgFormatter.BuildSoapMessageFromMethodResponse((IMethodReturnMessage) responseMsg, out responseHeaders);
  118. responseStream = new MemoryStream();
  119. _soapCore.Serializer.Serialize(responseStream, rtnMessageObject);
  120. }
  121. }
  122. }
  123. catch(Exception e)
  124. {
  125. responseMsg = (IMethodReturnMessage)new ReturnMessage(e, (IMethodCallMessage)requestMsg);
  126. object rtnMessageObject = soapMsgFormatter.BuildSoapMessageFromMethodResponse((IMethodReturnMessage) responseMsg, out responseHeaders);
  127. responseStream = new MemoryStream();
  128. _soapCore.Serializer.Serialize(responseStream, rtnMessageObject);
  129. sp = ServerProcessing.Complete;
  130. }
  131. if (sp == ServerProcessing.Complete)
  132. sinkStack.Pop(this);
  133. return sp;
  134. }
  135. private object HH(Header[] headers) {
  136. foreach(Header h in headers) {
  137. Console.WriteLine("Name: {0} Value:{0}", h.Name, h.Value);
  138. }
  139. return null;
  140. }
  141. [Serializable]
  142. public enum Protocol
  143. {
  144. Http = 0,
  145. Other = 1,
  146. }
  147. }
  148. }