PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/ResourceManagement.Client/DefaultClient.cs

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