exceptions propagating incompatible with dotnet. Patch by roeie@mainsoft.com
[mono.git] / mcs / class / System.Runtime.Remoting / System.Runtime.Remoting.MetadataServices / SdlChannelSink.cs
1 //
2 // System.Runtime.Remoting.MetadataServices.SdlChannelSink
3 //
4 // Authors:
5 //      Martin Willemoes Hansen (mwh@sysrq.dk)
6 //
7 // (C) 2003 Martin Willemoes Hansen
8 //
9
10 //
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.
29 //
30
31 using System.Runtime.Remoting.Channels;
32 using System.Runtime.Remoting.Messaging;
33 using System.IO;
34 using System.Collections;
35 using System.Text;
36
37 namespace System.Runtime.Remoting.MetadataServices
38 {
39         public class SdlChannelSink : IServerChannelSink, IChannelSinkBase
40         {
41                 IServerChannelSink _next;
42                 IChannelReceiver _channel;
43                 
44                 public SdlChannelSink (IChannelReceiver channel, IServerChannelSink next)
45                 {
46                         _next = next;
47                         _channel = channel;
48                 }
49
50                 public IServerChannelSink NextChannelSink 
51                 {
52                         get { return _next; }
53                 }
54
55                 public IDictionary Properties 
56                 {
57                         get { return null; }
58                 }
59                 
60                 public void AsyncProcessResponse (IServerResponseChannelSinkStack sinkStack,
61                                                   object state,
62                                                   IMessage msg,
63                                                   ITransportHeaders headers,
64                                                   Stream stream)
65                 {
66                         throw new NotSupportedException ();     // Never called
67                 }
68
69                 public Stream GetResponseStream (IServerResponseChannelSinkStack sinkStack,
70                                                  object state,
71                                                  IMessage msg,
72                                                  ITransportHeaders headers)
73                 {
74                         return null;
75                 }
76
77                 public ServerProcessing ProcessMessage (IServerChannelSinkStack sinkStack,
78                                                         IMessage requestMsg,
79                                                         ITransportHeaders requestHeaders,
80                                                         Stream requestStream,
81                                                         out IMessage responseMsg,
82                                                         out ITransportHeaders responseHeaders,
83                                                         out Stream responseStream)
84                 {
85                         responseMsg = null;
86                         responseStream = null;
87                         responseHeaders = null;
88                         string verb = requestHeaders [CommonTransportKeys.RequestVerb] as string;
89                         string uri = (string) requestHeaders [CommonTransportKeys.RequestUri];
90                         
91                         if (verb == "GET" && uri.EndsWith ("?wsdl"))
92                         {
93                                 try
94                                 {
95                                         uri = uri.Substring (0, uri.Length - 5);
96                                         Type type = RemotingServices.GetServerTypeForUri (uri);
97                                         
98                                         string url = _channel.GetUrlsForUri (uri)[0];
99                                         ServiceType st = new ServiceType (type, url);
100                                         
101                                         responseStream = new MemoryStream ();
102                                         MetaData.ConvertTypesToSchemaToStream (new ServiceType[] {st}, SdlType.Wsdl, responseStream);
103                                         responseStream.Position = 0;
104                                         responseMsg = null;
105                                         responseHeaders = new TransportHeaders ();
106                                         responseHeaders [CommonTransportKeys.ContentType] = "text/xml";
107                                 }
108                                 catch (Exception ex)
109                                 {
110                                         responseHeaders = new TransportHeaders ();
111                                         responseHeaders [CommonTransportKeys.HttpStatusCode] = "400";
112                                         responseStream = new MemoryStream (Encoding.UTF8.GetBytes (ex.ToString ()));
113                                 }
114                                 return ServerProcessing.Complete;
115                         }
116                         else
117                                 return _next.ProcessMessage (sinkStack, requestMsg, requestHeaders, requestStream, out responseMsg, out responseHeaders, out responseStream);
118                 }
119         }
120 }