PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/codes-php/phpjakarta/WindowsAzure/Common/ServicesBuilder.php

http://bukuphpjs.codeplex.com
PHP | 413 lines | 222 code | 56 blank | 135 comment | 1 complexity | 7f685f9d485fda2fa8516a549e0fd643 MD5 | raw file
Possible License(s): Apache-2.0, MIT, LGPL-2.1
  1. <?php
  2. /**
  3. * LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. * PHP version 5
  15. *
  16. * @category Microsoft
  17. * @package WindowsAzure\Common
  18. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  19. * @copyright 2012 Microsoft Corporation
  20. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  21. * @link https://github.com/windowsazure/azure-sdk-for-php
  22. */
  23. namespace WindowsAzure\Common;
  24. use WindowsAzure\Blob\BlobRestProxy;
  25. use WindowsAzure\Common\Internal\Resources;
  26. use WindowsAzure\Common\Internal\Validate;
  27. use WindowsAzure\Common\Internal\Utilities;
  28. use WindowsAzure\Common\Internal\Http\HttpClient;
  29. use WindowsAzure\Common\Internal\Filters\DateFilter;
  30. use WindowsAzure\Common\Internal\Filters\HeadersFilter;
  31. use WindowsAzure\Common\Internal\Filters\AuthenticationFilter;
  32. use WindowsAzure\Common\Internal\Filters\WrapFilter;
  33. use WindowsAzure\Common\Internal\InvalidArgumentTypeException;
  34. use WindowsAzure\Common\Internal\Serialization\XmlSerializer;
  35. use WindowsAzure\Common\Internal\Authentication\SharedKeyAuthScheme;
  36. use WindowsAzure\Common\Internal\Authentication\TableSharedKeyLiteAuthScheme;
  37. use WindowsAzure\Common\Internal\StorageServiceSettings;
  38. use WindowsAzure\Common\Internal\ServiceManagementSettings;
  39. use WindowsAzure\Common\Internal\ServiceBusSettings;
  40. use WindowsAzure\Queue\QueueRestProxy;
  41. use WindowsAzure\ServiceBus\ServiceBusRestProxy;
  42. use WindowsAzure\ServiceBus\WrapRestProxy;
  43. use WindowsAzure\ServiceManagement\ServiceManagementRestProxy;
  44. use WindowsAzure\Table\TableRestProxy;
  45. use WindowsAzure\Table\Internal\AtomReaderWriter;
  46. use WindowsAzure\Table\Internal\MimeReaderWriter;
  47. /**
  48. * Builds azure service objects.
  49. *
  50. * @category Microsoft
  51. * @package WindowsAzure\Common
  52. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  53. * @copyright 2012 Microsoft Corporation
  54. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  55. * @version Release: @package_version@
  56. * @link https://github.com/windowsazure/azure-sdk-for-php
  57. */
  58. class ServicesBuilder
  59. {
  60. /**
  61. * @var ServicesBuilder
  62. */
  63. private static $_instance = null;
  64. /**
  65. * Gets the HTTP client used in the REST services construction.
  66. *
  67. * @return WindowsAzure\Common\Internal\Http\IHttpClient
  68. */
  69. protected function httpClient()
  70. {
  71. return new HttpClient();
  72. }
  73. /**
  74. * Gets the serializer used in the REST services construction.
  75. *
  76. * @return WindowsAzure\Common\Internal\Serialization\ISerializer
  77. */
  78. protected function serializer()
  79. {
  80. return new XmlSerializer();
  81. }
  82. /**
  83. * Gets the MIME serializer used in the REST services construction.
  84. *
  85. * @return \WindowsAzure\Table\Internal\IMimeReaderWriter
  86. */
  87. protected function mimeSerializer()
  88. {
  89. return new MimeReaderWriter();
  90. }
  91. /**
  92. * Gets the Atom serializer used in the REST services construction.
  93. *
  94. * @return \WindowsAzure\Table\Internal\IAtomReaderWriter
  95. */
  96. protected function atomSerializer()
  97. {
  98. return new AtomReaderWriter();
  99. }
  100. /**
  101. * Gets the Queue authentication scheme.
  102. *
  103. * @param string $accountName The account name.
  104. * @param string $accountKey The account key.
  105. *
  106. * @return \WindowsAzure\Common\Internal\Authentication\StorageAuthScheme
  107. */
  108. protected function queueAuthenticationScheme($accountName, $accountKey)
  109. {
  110. return new SharedKeyAuthScheme($accountName, $accountKey);
  111. }
  112. /**
  113. * Gets the Blob authentication scheme.
  114. *
  115. * @param string $accountName The account name.
  116. * @param string $accountKey The account key.
  117. *
  118. * @return \WindowsAzure\Common\Internal\Authentication\StorageAuthScheme
  119. */
  120. protected function blobAuthenticationScheme($accountName, $accountKey)
  121. {
  122. return new SharedKeyAuthScheme($accountName, $accountKey);
  123. }
  124. /**
  125. * Gets the Table authentication scheme.
  126. *
  127. * @param string $accountName The account name.
  128. * @param string $accountKey The account key.
  129. *
  130. * @return TableSharedKeyLiteAuthScheme
  131. */
  132. protected function tableAuthenticationScheme($accountName, $accountKey)
  133. {
  134. return new TableSharedKeyLiteAuthScheme($accountName, $accountKey);
  135. }
  136. /**
  137. * Builds a queue object.
  138. *
  139. * @param string $connectionString The configuration connection string.
  140. *
  141. * @return WindowsAzure\Queue\Internal\IQueue
  142. */
  143. public function createQueueService($connectionString)
  144. {
  145. $settings = StorageServiceSettings::createFromConnectionString(
  146. $connectionString
  147. );
  148. $httpClient = $this->httpClient();
  149. $serializer = $this->serializer();
  150. $uri = Utilities::tryAddUrlScheme(
  151. $settings->getQueueEndpointUri()
  152. );
  153. $queueWrapper = new QueueRestProxy(
  154. $httpClient,
  155. $uri,
  156. Resources::EMPTY_STRING,
  157. $serializer
  158. );
  159. // Adding headers filter
  160. $headers = array();
  161. $headers[Resources::X_MS_VERSION] = Resources::STORAGE_API_LATEST_VERSION;
  162. $headersFilter = new HeadersFilter($headers);
  163. $queueWrapper = $queueWrapper->withFilter($headersFilter);
  164. // Adding date filter
  165. $dateFilter = new DateFilter();
  166. $queueWrapper = $queueWrapper->withFilter($dateFilter);
  167. // Adding authentication filter
  168. $authFilter = new AuthenticationFilter(
  169. $this->queueAuthenticationScheme(
  170. $settings->getName(),
  171. $settings->getKey()
  172. )
  173. );
  174. $queueWrapper = $queueWrapper->withFilter($authFilter);
  175. return $queueWrapper;
  176. }
  177. /**
  178. * Builds a blob object.
  179. *
  180. * @param string $connectionString The configuration connection string.
  181. *
  182. * @return WindowsAzure\Blob\Internal\IBlob
  183. */
  184. public function createBlobService($connectionString)
  185. {
  186. $settings = StorageServiceSettings::createFromConnectionString(
  187. $connectionString
  188. );
  189. $httpClient = $this->httpClient();
  190. $serializer = $this->serializer();
  191. $uri = Utilities::tryAddUrlScheme(
  192. $settings->getBlobEndpointUri()
  193. );
  194. $blobWrapper = new BlobRestProxy(
  195. $httpClient,
  196. $uri,
  197. $settings->getName(),
  198. $serializer
  199. );
  200. // Adding headers filter
  201. $headers = array();
  202. $headers[Resources::X_MS_VERSION] = Resources::STORAGE_API_LATEST_VERSION;
  203. $headersFilter = new HeadersFilter($headers);
  204. $blobWrapper = $blobWrapper->withFilter($headersFilter);
  205. // Adding date filter
  206. $dateFilter = new DateFilter();
  207. $blobWrapper = $blobWrapper->withFilter($dateFilter);
  208. $authFilter = new AuthenticationFilter(
  209. $this->blobAuthenticationScheme(
  210. $settings->getName(),
  211. $settings->getKey()
  212. )
  213. );
  214. $blobWrapper = $blobWrapper->withFilter($authFilter);
  215. return $blobWrapper;
  216. }
  217. /**
  218. * Builds a table object.
  219. *
  220. * @param string $connectionString The configuration connection string.
  221. *
  222. * @return WindowsAzure\Table\Internal\ITable
  223. */
  224. public function createTableService($connectionString)
  225. {
  226. $settings = StorageServiceSettings::createFromConnectionString(
  227. $connectionString
  228. );
  229. $httpClient = $this->httpClient();
  230. $atomSerializer = $this->atomSerializer();
  231. $mimeSerializer = $this->mimeSerializer();
  232. $serializer = $this->serializer();
  233. $uri = Utilities::tryAddUrlScheme(
  234. $settings->getTableEndpointUri()
  235. );
  236. $tableWrapper = new TableRestProxy(
  237. $httpClient,
  238. $uri,
  239. $atomSerializer,
  240. $mimeSerializer,
  241. $serializer
  242. );
  243. // Adding headers filter
  244. $headers = array();
  245. $latestServicesVersion = Resources::STORAGE_API_LATEST_VERSION;
  246. $currentVersion = Resources::DATA_SERVICE_VERSION_VALUE;
  247. $maxVersion = Resources::MAX_DATA_SERVICE_VERSION_VALUE;
  248. $accept = Resources::ACCEPT_HEADER_VALUE;
  249. $acceptCharset = Resources::ACCEPT_CHARSET_VALUE;
  250. $headers[Resources::X_MS_VERSION] = $latestServicesVersion;
  251. $headers[Resources::DATA_SERVICE_VERSION] = $currentVersion;
  252. $headers[Resources::MAX_DATA_SERVICE_VERSION] = $maxVersion;
  253. $headers[Resources::MAX_DATA_SERVICE_VERSION] = $maxVersion;
  254. $headers[Resources::ACCEPT_HEADER] = $accept;
  255. $headers[Resources::ACCEPT_CHARSET] = $acceptCharset;
  256. $headersFilter = new HeadersFilter($headers);
  257. $tableWrapper = $tableWrapper->withFilter($headersFilter);
  258. // Adding date filter
  259. $dateFilter = new DateFilter();
  260. $tableWrapper = $tableWrapper->withFilter($dateFilter);
  261. // Adding authentication filter
  262. $authFilter = new AuthenticationFilter(
  263. $this->tableAuthenticationScheme(
  264. $settings->getName(),
  265. $settings->getKey()
  266. )
  267. );
  268. $tableWrapper = $tableWrapper->withFilter($authFilter);
  269. return $tableWrapper;
  270. }
  271. /**
  272. * Builds a Service Bus object.
  273. *
  274. * @param string $connectionString The configuration connection string.
  275. *
  276. * @return WindowsAzure\ServiceBus\Internal\IServiceBus
  277. */
  278. public function createServiceBusService($connectionString)
  279. {
  280. $settings = ServiceBusSettings::createFromConnectionString(
  281. $connectionString
  282. );
  283. $httpClient = $this->httpClient();
  284. $serializer = $this->serializer();
  285. $serviceBusWrapper = new ServiceBusRestProxy(
  286. $httpClient,
  287. $settings->getServiceBusEndpointUri(),
  288. $serializer
  289. );
  290. $wrapFilter = new WrapFilter(
  291. $settings->getWrapEndpointUri(),
  292. $settings->getWrapName(),
  293. $settings->getWrapPassword(),
  294. $this->createWrapService($settings->getWrapEndpointUri())
  295. );
  296. return $serviceBusWrapper->withFilter($wrapFilter);
  297. }
  298. /**
  299. * Builds a service management object.
  300. *
  301. * @param string $connectionString The configuration connection string.
  302. *
  303. * @return WindowsAzure\ServiceManagement\Internal\IServiceManagement
  304. */
  305. public function createServiceManagementService($connectionString)
  306. {
  307. $settings = ServiceManagementSettings::createFromConnectionString(
  308. $connectionString
  309. );
  310. $certificatePath = $settings->getCertificatePath();
  311. $httpClient = new HttpClient($certificatePath);
  312. $serializer = $this->serializer();
  313. $uri = Utilities::tryAddUrlScheme(
  314. $settings->getEndpointUri(),
  315. Resources::HTTPS_SCHEME
  316. );
  317. $serviceManagementWrapper = new ServiceManagementRestProxy(
  318. $httpClient,
  319. $settings->getSubscriptionId(),
  320. $uri,
  321. $serializer
  322. );
  323. // Adding headers filter
  324. $headers = array();
  325. $headers[Resources::X_MS_VERSION] = Resources::SM_API_LATEST_VERSION;
  326. $headersFilter = new HeadersFilter($headers);
  327. $serviceManagementWrapper = $serviceManagementWrapper->withFilter(
  328. $headersFilter
  329. );
  330. return $serviceManagementWrapper;
  331. }
  332. /**
  333. * Builds a WRAP client.
  334. *
  335. * @param string $wrapEndpointUri The WRAP endpoint uri.
  336. *
  337. * @return WindowsAzure\ServiceBus\Internal\IWrap
  338. */
  339. protected function createWrapService($wrapEndpointUri)
  340. {
  341. $httpClient = $this->httpClient();
  342. $wrapWrapper = new WrapRestProxy($httpClient, $wrapEndpointUri);
  343. return $wrapWrapper;
  344. }
  345. /**
  346. * Gets the static instance of this class.
  347. *
  348. * @return ServicesBuilder
  349. */
  350. public static function getInstance()
  351. {
  352. if (!isset(self::$instance)) {
  353. self::$_instance = new ServicesBuilder();
  354. }
  355. return self::$_instance;
  356. }
  357. }