PageRenderTime 34ms CodeModel.GetById 12ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/ResourceManagement.Client/DefaultClient.cs

#
C# | 315 lines | 218 code | 28 blank | 69 comment | 15 complexity | 8f945f5759c1da69b0997e7216b32ccf MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Globalization;
  4using System.Text;
  5using System.Xml.Schema;
  6using System.ServiceModel;
  7using Microsoft.ResourceManagement.Client.WsTransfer;
  8using Microsoft.ResourceManagement.Client.WsEnumeration;
  9using Microsoft.ResourceManagement.ObjectModel;
 10using Microsoft.ResourceManagement.ObjectModel.ResourceTypes;
 11
 12namespace Microsoft.ResourceManagement.Client
 13{
 14    /// <summary>
 15    /// The DefaultClient is a thin wrapper over the WsTransfer, WsTransferFactory, and WsEnumeration clients.
 16    /// 
 17    /// It is intended to be useful only for common scenarios. For more advanced scenarios, please use the other clients.
 18    /// </summary>
 19    public class DefaultClient : IDisposable
 20    {
 21        // underlying clients
 22        WsTransferClient wsTransferClient;
 23        WsTransferFactoryClient wsTransferFactoryClient;
 24        MexClient mexClient;
 25        WsEnumerationClient wsEnumerationClient;
 26
 27        // factories used to construct resources and requests
 28        RmResourceFactory resourceFactory;
 29        RmRequestFactory requestFactory;
 30
 31        bool schemaCached;
 32
 33        public DefaultClient()
 34        {
 35            this.wsTransferClient = new WsTransferClient();
 36            this.wsTransferFactoryClient = new WsTransferFactoryClient();
 37            this.wsEnumerationClient = new WsEnumerationClient();
 38            this.mexClient = new MexClient();
 39
 40            this.resourceFactory = new RmResourceFactory();
 41            this.requestFactory = new RmRequestFactory();
 42        }
 43
 44        public DefaultClient(
 45            String wsTransferConfigurationName,
 46            String wsTransferFactoryConfigurationName,
 47            String wsEnumerationConfigurationName,
 48            String mexConfigurationName
 49            )
 50        {
 51            this.wsTransferClient = new WsTransferClient(wsTransferConfigurationName);
 52            this.wsTransferFactoryClient = new WsTransferFactoryClient(wsTransferFactoryConfigurationName);
 53            this.wsEnumerationClient = new WsEnumerationClient(wsEnumerationConfigurationName);
 54            this.mexClient = new MexClient(mexConfigurationName);
 55
 56            this.resourceFactory = new RmResourceFactory();
 57            this.requestFactory = new RmRequestFactory();
 58        }
 59
 60        public DefaultClient(
 61            String wsTransferConfigurationName,
 62            String wsTransferEndpointAddress,
 63            String wsTransferFactoryConfigurationName,
 64            String wsTransferFactoryEndpointAddress,
 65            String wsEnumerationConfigurationName,
 66            String wsEnumerationEndpointAddress,
 67            String mexConfigurationName,
 68            String mexEndpointAddress
 69            )
 70        {
 71            this.wsTransferClient = new WsTransferClient(wsTransferConfigurationName, wsTransferEndpointAddress);
 72            this.wsTransferFactoryClient = new WsTransferFactoryClient(wsTransferFactoryConfigurationName, wsTransferFactoryEndpointAddress);
 73            this.wsEnumerationClient = new WsEnumerationClient(wsEnumerationConfigurationName, wsEnumerationEndpointAddress);
 74            this.mexClient = new MexClient(mexConfigurationName, mexEndpointAddress);
 75
 76            this.resourceFactory = new RmResourceFactory();
 77            this.requestFactory = new RmRequestFactory();
 78        }
 79
 80        /// <summary>
 81        /// Refreshes the metadata from the service.
 82        /// </summary>
 83        public XmlSchemaSet RefreshSchema()
 84        {
 85            XmlSchemaSet metadata = this.mexClient.Get();
 86            lock (this.requestFactory)
 87            {
 88                this.requestFactory = new RmRequestFactory(metadata);
 89            }
 90            lock (this.resourceFactory)
 91            {
 92                this.resourceFactory = new RmResourceFactory(metadata);
 93            }
 94            this.schemaCached = true;
 95            return metadata;
 96        }
 97        #region WsTransfer
 98        /// <summary>
 99        /// Creates the given resource and returns its ObjectId.
100        /// This method does not set the ObjectId of newResource.
101        /// </summary>
102        /// <param name="newResource">The resource to create.</param>
103        /// <returns>The ObjectId of the resource.</returns>
104        public RmReference Create(RmResource newResource)
105        {
106            if (newResource == null)
107                throw new ArgumentNullException("newResource");
108            CreateRequest request = this.requestFactory.CreateCreateRequest(newResource);
109            CreateResponse response = this.wsTransferFactoryClient.Create(request);
110            try
111            {
112                RmReference reference = new RmReference(response.ResourceCreated.EndpointReference.ReferenceProperties.ResourceReferenceProperty.Value);
113                newResource.ObjectID = reference;
114                return reference;
115            }
116            catch (NullReferenceException)
117            {
118                return new RmReference();
119            }
120            catch (FormatException)
121            {
122                return new RmReference();
123            }
124        }
125
126        /// <summary>
127        /// Retrieves the object with the given ObjectId
128        /// </summary>
129        /// <param name="objectId">The ObjectId of the requested object.</param>
130        /// <returns>The object or null if not found</returns>
131        /// <exception cref="System.ServiceModel.FaultException">System.ServiceModel.FaultException thrown when failures occur.</exception>
132        public RmResource Get(RmReference objectId)
133        {
134            return Get(objectId, null, null);
135        }
136
137        /// <summary>
138        /// Retrieves the representation of an object with the given ObjectId in the given culture.
139        /// </summary>
140        /// <param name="objectId">The ObjectId of the requested object.</param>
141        /// <param name="culture">The requested culture representation of the object.</param>
142        /// <returns>The object or null if not found.</returns>
143        public RmResource Get(RmReference objectId, CultureInfo culture)
144        {
145            return Get(objectId, culture, null);
146        }
147
148        /// <summary>
149        /// Retrieves the object and the specified attributes with the given ObjectId.
150        /// </summary>
151        /// <param name="objectId">The ObjectId of the requested object.</param>
152        /// <param name="attributes">The list of attributes on the object to return.</param>
153        /// <returns></returns>
154        public RmResource Get(RmReference objectId, String[] attributes)
155        {
156            return Get(objectId, null, attributes);
157        }
158
159        protected RmResource Get(RmReference objectId, CultureInfo culture, String[] attributes)
160        {
161            GetRequest request = this.requestFactory.CreateGetRequest(objectId, culture, attributes);
162            GetResponse response = this.wsTransferClient.Get(request);
163            return this.resourceFactory.CreateResource(response);
164        }
165
166        /// <summary>
167        /// Saves changes made to an object recorded by the transaction to the service.
168        /// </summary>
169        /// <param name="transaction">The transaction object which recorded changes made to a Resource object.</param>
170        /// <returns>True upon successful operation.</returns>
171        public bool Put(RmResourceChanges transaction)
172        {
173            if (transaction == null)
174            {
175                throw new ArgumentNullException("transaction");
176            }
177
178            PutRequest request = this.requestFactory.CreatePutRequest(transaction);
179            PutResponse response = this.wsTransferClient.Put(request);
180            if (response == null)
181                return false;
182            else
183                return true;
184        }
185
186        /// <summary>
187        /// Deletes the object with the given ObjectId.
188        /// </summary>
189        /// <param name="objectId">The ObjectId of the object to delete.</param>
190        /// <returns>True upon successful deletion.</returns>
191        public bool Delete(RmReference objectId)
192        {
193            DeleteRequest request = this.requestFactory.CreateDeleteRequest(objectId);
194            DeleteResponse response = this.wsTransferClient.Delete(request);
195            if (response == null)
196                return false;
197            else
198                return true;
199        }
200        #endregion
201
202        #region Enumeration
203        /// <summary>
204        /// Returns an enumerator that can traverse all objects matching the given filter.
205        /// </summary>
206        /// <param name="filter">The XPath filter of which objects to select.</param>
207        /// <returns>An enumerator object which can be consumed in foreach statements.</returns>
208        public IEnumerable<RmResource> Enumerate(String filter)
209        {
210            return this.Enumerate(filter, new string[] {});
211        }
212
213        /// <summary>
214        /// Returns an enumerator that can traverse all objects matching the given filter.
215        /// 
216        /// Each object only contains the specified attributes.
217        /// </summary>
218        /// <param name="filter">The XPath filter of which objects to select.</param>
219        /// <param name="attributes">A list of attribute names to include in returned objects.</param>
220        /// <returns>An enumerator object which can be consumed in foreach statements.</returns>
221        public IEnumerable<RmResource> Enumerate(String filter, String[] attributes)
222        {
223            if (String.IsNullOrEmpty(filter))
224            {
225                throw new ArgumentNullException("filter");
226            }
227
228            return this.requestFactory.CreateEnumeration(this.wsEnumerationClient, this.resourceFactory, filter, attributes);
229        }
230        #endregion
231
232        #region IDisposable Members
233
234        public void Dispose()
235        {
236            this.wsTransferClient.Close();
237            this.wsTransferFactoryClient.Close();
238            this.mexClient.Close();
239            this.wsEnumerationClient.Close();
240
241            GC.SuppressFinalize(this);
242        }
243
244        #endregion
245
246        #region Promoted Properties
247        /// <summary>
248        /// Gets or Sets the credential with which the underlying clients use to communicate with the service.
249        /// </summary>
250        public System.Net.NetworkCredential ClientCredential
251        {
252            get
253            {
254                return this.wsTransferClient.ClientCredentials.Windows.ClientCredential;
255            }
256            set
257            {
258                if (value == null)
259                {
260                    throw new ArgumentNullException("value");
261                }
262                this.wsTransferClient.ClientCredentials.Windows.ClientCredential = value;
263                this.wsTransferFactoryClient.ClientCredentials.Windows.ClientCredential = value;
264                this.wsEnumerationClient.ClientCredentials.Windows.ClientCredential = value;
265                this.mexClient.ClientCredentials.Windows.ClientCredential = value;
266            }
267        }
268
269        /// <summary>
270        /// Gets or Sets the RmResourceFactory object used to construct resources returned.
271        /// </summary>
272        public RmResourceFactory ResourceFactory
273        {
274            get
275            {
276                return this.resourceFactory;
277            }
278            set
279            {
280                if (value == null)
281                    throw new ArgumentNullException("value");
282                this.resourceFactory = value;
283            }
284        }
285
286        /// <summary>
287        /// Gets or Sets the RmRequestFactory object used to construct request messages.
288        /// </summary>
289        public RmRequestFactory RequestFactory
290        {
291            get
292            {
293                return this.requestFactory;
294            }
295            set
296            {
297                if (value == null)
298                    throw new ArgumentNullException("value");
299                this.requestFactory = value;
300            }
301        }
302
303        /// <summary>
304        /// Returns true if the schema has been cached for this client.
305        /// </summary>
306        public bool SchemaCached
307        {
308            get
309            {
310                return this.schemaCached;
311            }
312        }
313        #endregion
314    }
315}