move to from olive to mcs
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Description / MetadataExchangeClient.cs
1 //
2 // MetadataExchangeClient.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //      Ankit Jain <jankit@novell.com>
7 //
8 // Copyright (C) 2005 Novell, Inc.  http://www.novell.com
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;
30 using System.Collections.Generic;
31 using System.Collections.ObjectModel;
32 using System.ServiceModel;
33 using System.ServiceModel.Channels;
34 using System.ServiceModel.Description;
35 using System.Web.Services.Description;
36 using System.Web.Services.Discovery;
37 using System.Web.Services.Protocols;
38 using System.Xml;
39 using System.Xml.Serialization;
40 using System.IO;
41 using System.Net;
42 using System.Text;
43
44 using SMBinding = System.ServiceModel.Channels.Binding;
45 using SMMessage = System.ServiceModel.Channels.Message;
46
47 namespace System.ServiceModel.Description
48 {
49         [MonoTODO]
50         public class MetadataExchangeClient
51         {
52                 string scheme;
53
54                 EndpointAddress address;
55                 SMBinding binding;
56
57                 public MetadataExchangeClient ()
58                 {
59                         //FIXME: Look for config element, implementing
60                         //      IMetadataExchange contract
61                         //      Use Channel<IMetadataExchange> .. ?
62                         
63                         throw new NotImplementedException ();
64                 }
65
66                 public MetadataExchangeClient (SMBinding mexBinding)
67                 {
68                         binding = mexBinding;
69                 }
70
71                 public MetadataExchangeClient (EndpointAddress address)
72                 {
73                         this.address = address;
74                 }
75
76                 public MetadataExchangeClient (string endpointConfigurationName)
77                 {
78                         throw new NotImplementedException ();
79                 }
80
81                 public MetadataExchangeClient (Uri address, MetadataExchangeClientMode mode)
82                 {
83                         this.address = new EndpointAddress (address.AbsoluteUri);
84                 }
85
86                 public MetadataSet GetMetadata ()
87                 {
88                         return GetMetadata (address);
89                 }
90
91                 public MetadataSet GetMetadata (EndpointAddress address)
92                 {
93                         //FIXME: default mode?
94                         return GetMetadataInternal (address, MetadataExchangeClientMode.MetadataExchange);
95                 }
96
97                 public MetadataSet GetMetadata (Uri address, MetadataExchangeClientMode mode)
98                 {
99                         return GetMetadataInternal (new EndpointAddress (address.AbsoluteUri), mode);
100                 }
101
102                 MetadataSet GetMetadataInternal (EndpointAddress address, MetadataExchangeClientMode mode)
103                 {
104                         if (binding == null)
105                                 binding = MetadataExchangeBindings.CreateMexHttpBinding ();
106
107                         MetadataProxy proxy = new MetadataProxy (binding, address);
108                         proxy.Open ();
109
110                         SMMessage msg = SMMessage.CreateMessage ( 
111                                         MessageVersion.Soap12WSAddressing10, 
112                                         "http://schemas.xmlsoap.org/ws/2004/09/transfer/Get");
113
114                         msg.Headers.ReplyTo = new EndpointAddress (
115                                         "http://www.w3.org/2005/08/addressing/anonymous");
116                         //msg.Headers.From = new EndpointAddress ("http://localhost");
117                         msg.Headers.To = address.Uri;
118                         msg.Headers.MessageId = new UniqueId ();
119
120                         SMMessage ret;
121                         try {
122                                 ret = proxy.Get (msg);
123                         } catch (Exception e) {
124                                 throw new InvalidOperationException (
125                                                 "Metadata contains a reference that cannot be resolved : " + address.Uri.AbsoluteUri, e);
126                         }
127
128                         return MetadataSet.ReadFrom (ret.GetReaderAtBodyContents ());
129                 }
130         }
131         
132         internal class MetadataProxy : ClientBase<IMetadataExchange>, IMetadataExchange
133         {
134                 public MetadataProxy (SMBinding binding, EndpointAddress address)
135                         : base (binding, address)
136                 {
137                 }
138
139                 public SMMessage Get (SMMessage msg)
140                 {
141                         return Channel.Get (msg);
142                 }
143
144                 public IAsyncResult BeginGet (SMMessage request, AsyncCallback callback , object state)
145                 {
146                         throw new NotImplementedException ();
147                 }
148
149                 public SMMessage EndGet (IAsyncResult result)
150                 {
151                         throw new NotImplementedException ();
152                 }
153         }
154
155 }