PageRenderTime 18ms CodeModel.GetById 1ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/ResourceManagement.Client/WsTransfer/WsTransferClient.cs

#
C# | 221 lines | 197 code | 17 blank | 7 comment | 30 complexity | 4e1e778965e1f771ac89b6770a953dac MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.ServiceModel;
  4using System.ServiceModel.Channels;
  5using System.Xml;
  6using Microsoft.ResourceManagement.Client.WsTransfer;
  7using Microsoft.ResourceManagement.Client.Faults;
  8using System.Text;
  9
 10namespace Microsoft.ResourceManagement.Client.WsTransfer
 11{
 12    public class WsTransferClient : System.ServiceModel.ClientBase<IResource>, IResource
 13    {
 14        public WsTransferClient()
 15            : base()
 16        {
 17        }
 18
 19        public WsTransferClient(string endpointConfigurationName) :
 20            base(endpointConfigurationName)
 21        {
 22        }
 23
 24        public WsTransferClient(string endpointConfigurationName, string remoteAddress) :
 25            base(endpointConfigurationName, remoteAddress)
 26        {
 27        }
 28
 29        public WsTransferClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
 30            base(endpointConfigurationName, remoteAddress)
 31        {
 32        }
 33
 34        public WsTransferClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
 35            base(binding, remoteAddress)
 36        {
 37        }
 38
 39        public Message Get(Message request)
 40        {
 41            IResource channel = base.ChannelFactory.CreateChannel();
 42            try
 43            {
 44                Message ret = channel.Get(request);
 45                ((IDisposable)channel).Dispose();
 46                return ret;
 47            }
 48            catch (Exception)
 49            {
 50                if (((ICommunicationObject)channel).State == CommunicationState.Faulted)
 51                {
 52                    ((ICommunicationObject)channel).Abort();
 53                }
 54                throw;
 55            }
 56        }
 57        
 58        public GetResponse Get(GetRequest request)
 59        {
 60            if (request == null)
 61            {
 62                throw new ArgumentNullException("request");
 63            }
 64            Message getRequest = null;
 65            bool isBaseObjectSearchRequest = false;
 66            lock (request)
 67            {
 68                if (request.BaseObjectSearchRequest == null || request.BaseObjectSearchRequest.AttributeTypes.Count == 0)
 69                {
 70                    getRequest = Message.CreateMessage(MessageVersion.Default, Constants.WsTransfer.GetAction);
 71                }
 72                else
 73                {
 74                    isBaseObjectSearchRequest = true;
 75                    getRequest = Message.CreateMessage(MessageVersion.Default, Constants.WsTransfer.GetAction, request.BaseObjectSearchRequest, new ClientSerializer(typeof(BaseObjectSearchRequest)));
 76                    ClientHelper.AddImdaHeaders(request as ImdaRequest, getRequest);
 77                }
 78
 79                ClientHelper.AddRmHeaders(request as WsTransfer.TransferRequest, getRequest);
 80            }
 81            Message getResponse = Get(getRequest);
 82            if(getResponse.IsFault)
 83            {
 84                // handle fault will throw a .NET exception
 85                ClientHelper.HandleFault(getResponse);
 86            }
 87
 88            GetResponse getResponseTyped = new GetResponse();
 89            if(isBaseObjectSearchRequest)
 90            {
 91                BaseObjectSearchResponse baseObjectSearchResponse = getResponse.GetBody<BaseObjectSearchResponse>(new ClientSerializer(typeof(BaseObjectSearchResponse)));
 92                getResponseTyped.BaseObjectSearchResponse = baseObjectSearchResponse;
 93            }
 94            else
 95            {
 96                // we manually construct the partial attributes as if the client had selected all attributes
 97                // the purpose is to unify the API of the get request
 98                XmlNode retObject = getResponse.GetBody<XmlNode>(new ClientSerializer(typeof(XmlNode)));
 99                Dictionary<String, List<XmlNode>> seenAttributes = new Dictionary<string, List<XmlNode>>();
100                foreach (XmlNode child in retObject.ChildNodes)
101                {
102                    if (child.NodeType == XmlNodeType.Element)
103                    {
104                        if (seenAttributes.ContainsKey(child.Name) == false)
105                        {
106                            seenAttributes[child.Name] = new List<XmlNode>();
107                        }
108                        seenAttributes[child.Name].Add(child);
109                    }
110                }
111
112                getResponseTyped.BaseObjectSearchResponse = new BaseObjectSearchResponse();
113                foreach (KeyValuePair<String, List<XmlNode>> item in seenAttributes)
114                {
115                    PartialAttributeType partialAttribute = new PartialAttributeType();
116                    partialAttribute.Values.AddRange(item.Value);
117                    getResponseTyped.BaseObjectSearchResponse.PartialAttributes.Add(partialAttribute);
118                }             
119            }
120            return getResponseTyped;
121        }
122        
123        public Message Put(Message request)
124        {
125            IResource channel = base.ChannelFactory.CreateChannel();
126            try
127            {
128                Message ret = channel.Put(request);
129                ((IDisposable)channel).Dispose();
130                return ret;
131            }
132            catch (Exception)
133            {
134                if (((ICommunicationObject)channel).State == CommunicationState.Faulted)
135                {
136                    ((ICommunicationObject)channel).Abort();
137                }
138                throw;
139            }
140        }
141
142        public PutResponse Put(PutRequest request)
143        {
144            if (request == null)
145            {
146                throw new ArgumentNullException("request");
147            }
148            if (request.ModifyRequest == null)
149            {
150                throw new ArgumentNullException("ModifyRequest");
151            }
152            if (request.ResourceReferenceProperty == null)
153            {
154                throw new ArgumentNullException("ResourceReferenceProperty");
155            }
156
157            Message putRequest = null;
158            lock (request)
159            {
160                putRequest = Message.CreateMessage(MessageVersion.Default, Constants.WsTransfer.PutAction, request.ModifyRequest, new ClientSerializer(typeof(ModifyRequest)));
161                ClientHelper.AddImdaHeaders(request as ImdaRequest, putRequest);
162                ClientHelper.AddRmHeaders(request as WsTransfer.TransferRequest, putRequest);
163            }
164            Message putResponse = Put(putRequest);
165            if (putResponse.IsFault)
166            {
167                ClientHelper.HandleFault(putResponse);
168            }
169            
170            // the response has no information if it isn't a fault
171            // PutResponse putResponseTyped = putResponse.GetBody<PutResponse>(new ClientSerializer(typeof(PutResponse)));
172            return new PutResponse() ;
173        }
174
175        public Message Delete(Message request)
176        {
177            IResource channel = base.ChannelFactory.CreateChannel();
178            try
179            {
180                Message ret = channel.Delete(request);
181                ((IDisposable)channel).Dispose();
182                return ret;
183            }
184            catch (Exception)
185            {
186                if (((ICommunicationObject)channel).State == CommunicationState.Faulted)
187                {
188                    ((ICommunicationObject)channel).Abort();
189                }
190                throw;
191            }
192        }
193
194        public DeleteResponse Delete(DeleteRequest request)
195        {
196            if (request == null)
197            {
198                throw new ArgumentNullException("request");
199            }
200            if (request.ResourceReferenceProperty == null)
201            {
202                throw new ArgumentNullException("ResourceReferenceProperty");
203            }
204            Message deleteRequest = null;
205            lock (request)
206            {
207                deleteRequest = Message.CreateMessage(MessageVersion.Default, Constants.WsTransfer.DeleteAction, request, new ClientSerializer(typeof(DeleteRequest)));
208                ClientHelper.AddRmHeaders(request as WsTransfer.TransferRequest, deleteRequest);
209            }
210            Message deleteResponse = Delete(deleteRequest);
211            if (deleteResponse.IsFault)
212            {
213                ClientHelper.HandleFault(deleteResponse);
214            }
215
216            // the response has no information if it isn't a fault
217            // return deleteResponse.GetBody<DeleteResponse>(new ClientSerializer(typeof(DeleteResponse)));
218            return new DeleteResponse();
219        }
220    }
221}