PageRenderTime 14ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/ResourceManagement.Client/WsTransfer/AlternateClient.cs

#
C# | 181 lines | 157 code | 17 blank | 7 comment | 24 complexity | 8490ee119bb576c2c892a622e5af995c 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 AlternateClient : System.ServiceModel.ClientBase<IAlternate>, IAlternate
 13    {
 14        public AlternateClient()
 15            : base()
 16        {
 17
 18        }
 19        public AlternateClient(string endpointConfigurationName) :
 20            base(endpointConfigurationName)
 21        {
 22        }
 23
 24        public AlternateClient(string endpointConfigurationName, string remoteAddress) :
 25            base(endpointConfigurationName, remoteAddress)
 26        {
 27        }
 28
 29        public AlternateClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
 30            base(endpointConfigurationName, remoteAddress)
 31        {
 32        }
 33
 34        public AlternateClient(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            return channel.Get(request);
 43        }
 44        
 45        public GetResponse Get(GetRequest request)
 46        {
 47            if (request == null)
 48            {
 49                throw new ArgumentNullException("request");
 50            }
 51            Message getRequest = null;
 52            bool isBaseObjectSearchRequest = false;
 53            lock (request)
 54            {
 55                if (request.BaseObjectSearchRequest == null || request.BaseObjectSearchRequest.AttributeTypes.Count == 0)
 56                {
 57                    getRequest = Message.CreateMessage(MessageVersion.Default, Constants.WsTransfer.GetAction);
 58                }
 59                else
 60                {
 61                    isBaseObjectSearchRequest = true;
 62                    getRequest = Message.CreateMessage(MessageVersion.Default, Constants.WsTransfer.GetAction, request.BaseObjectSearchRequest, new ClientSerializer(typeof(BaseObjectSearchRequest)));
 63                    ClientHelper.AddImdaHeaders(request as ImdaRequest, getRequest);
 64                }
 65
 66                ClientHelper.AddRmHeaders(request as WsTransfer.TransferRequest, getRequest);
 67            }
 68            Message getResponse = Get(getRequest);
 69            if(getResponse.IsFault)
 70            {
 71                // handle fault will throw a .NET exception
 72                ClientHelper.HandleFault(getResponse);
 73            }
 74
 75            GetResponse getResponseTyped = new GetResponse();
 76            if(isBaseObjectSearchRequest)
 77            {
 78                BaseObjectSearchResponse baseObjectSearchResponse = getResponse.GetBody<BaseObjectSearchResponse>(new ClientSerializer(typeof(BaseObjectSearchResponse)));
 79                getResponseTyped.BaseObjectSearchResponse = baseObjectSearchResponse;
 80            }
 81            else
 82            {
 83                // we manually construct the partial attributes as if the client had selected all attributes
 84                // the purpose is to unify the API of the get request
 85                XmlNode retObject = getResponse.GetBody<XmlNode>(new ClientSerializer(typeof(XmlNode)));
 86                Dictionary<String, List<XmlNode>> seenAttributes = new Dictionary<string, List<XmlNode>>();
 87                foreach (XmlNode child in retObject.ChildNodes)
 88                {
 89                    if (child.NodeType == XmlNodeType.Element)
 90                    {
 91                        if (seenAttributes.ContainsKey(child.Name) == false)
 92                        {
 93                            seenAttributes[child.Name] = new List<XmlNode>();
 94                        }
 95                        seenAttributes[child.Name].Add(child);
 96                    }
 97                }
 98
 99                getResponseTyped.BaseObjectSearchResponse = new BaseObjectSearchResponse();
100                foreach (KeyValuePair<String, List<XmlNode>> item in seenAttributes)
101                {
102                    PartialAttributeType partialAttribute = new PartialAttributeType();
103                    partialAttribute.Values.AddRange(item.Value);
104                }             
105            }
106            return getResponseTyped;
107        }
108        
109        public Message Put(Message request)
110        {
111            IResource channel = base.ChannelFactory.CreateChannel();
112            return channel.Put(request);
113        }
114
115        public PutResponse Put(PutRequest request)
116        {
117            if (request == null)
118            {
119                throw new ArgumentNullException("request");
120            }
121            if (request.ModifyRequest == null)
122            {
123                throw new ArgumentNullException("ModifyRequest");
124            }
125            if (request.ResourceReferenceProperty == null)
126            {
127                throw new ArgumentNullException("ResourceReferenceProperty");
128            }
129
130            Message putRequest = null;
131            lock (request)
132            {
133                putRequest = Message.CreateMessage(MessageVersion.Default, Constants.WsTransfer.PutAction, request.ModifyRequest, new ClientSerializer(typeof(ModifyRequest)));
134                ClientHelper.AddImdaHeaders(request as ImdaRequest, putRequest);
135                ClientHelper.AddRmHeaders(request as WsTransfer.TransferRequest, putRequest);
136            }
137            Message putResponse = Put(putRequest);
138            if (putResponse.IsFault)
139            {
140                ClientHelper.HandleFault(putResponse);
141            }
142            
143            // the response has no information if it isn't a fault
144            // PutResponse putResponseTyped = putResponse.GetBody<PutResponse>(new ClientSerializer(typeof(PutResponse)));
145            return new PutResponse() ;
146        }
147
148        public Message Delete(Message request)
149        {
150            IResource channel = base.ChannelFactory.CreateChannel();
151            return channel.Delete(request);
152        }
153
154        public DeleteResponse Delete(DeleteRequest request)
155        {
156            if (request == null)
157            {
158                throw new ArgumentNullException("request");
159            }
160            if (request.ResourceReferenceProperty == null)
161            {
162                throw new ArgumentNullException("ResourceReferenceProperty");
163            }
164            Message deleteRequest = null;
165            lock (request)
166            {
167                deleteRequest = Message.CreateMessage(MessageVersion.Default, Constants.WsTransfer.DeleteAction, request, new ClientSerializer(typeof(DeleteRequest)));
168                ClientHelper.AddRmHeaders(request as WsTransfer.TransferRequest, deleteRequest);
169            }
170            Message deleteResponse = Delete(deleteRequest);
171            if (deleteResponse.IsFault)
172            {
173                ClientHelper.HandleFault(deleteResponse);
174            }
175
176            // the response has no information if it isn't a fault
177            // return deleteResponse.GetBody<DeleteResponse>(new ClientSerializer(typeof(DeleteResponse)));
178            return new DeleteResponse();
179        }
180    }
181}