SoapServerFormatterSink.cs 5.0 KB

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