PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/storage-2009-04-14/library/Microsoft/WindowsAzure/Storage/Queue.php

#
PHP | 560 lines | 299 code | 57 blank | 204 comment | 84 complexity | c1e929e0f2767bc29021218b5fdd2f98 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Copyright (c) 2009, RealDolmen
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of RealDolmen nor the
  14. * names of its contributors may be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * @category Microsoft
  29. * @package Microsoft_WindowsAzure
  30. * @subpackage Storage
  31. * @copyright Copyright (c) 2009, RealDolmen (http://www.realdolmen.com)
  32. * @license http://todo name_todo
  33. * @version $Id: Blob.php 24241 2009-07-22 09:43:13Z unknown $
  34. */
  35. /**
  36. * @see Microsoft_WindowsAzure_Credentials_SharedKey
  37. */
  38. require_once 'Microsoft/WindowsAzure/Credentials/SharedKey.php';
  39. /**
  40. * @see Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract
  41. */
  42. require_once 'Microsoft/WindowsAzure/RetryPolicy/RetryPolicyAbstract.php';
  43. /**
  44. * @see Microsoft_Http_Client
  45. */
  46. require_once 'Microsoft/Http/Client.php';
  47. /**
  48. * @see Microsoft_Http_Response
  49. */
  50. require_once 'Microsoft/Http/Response.php';
  51. /**
  52. * @see Microsoft_WindowsAzure_Storage
  53. */
  54. require_once 'Microsoft/WindowsAzure/Storage.php';
  55. /**
  56. * Microsoft_WindowsAzure_Storage_QueueInstance
  57. */
  58. require_once 'Microsoft/WindowsAzure/Storage/QueueInstance.php';
  59. /**
  60. * Microsoft_WindowsAzure_Storage_QueueMessage
  61. */
  62. require_once 'Microsoft/WindowsAzure/Storage/QueueMessage.php';
  63. /**
  64. * @see Microsoft_WindowsAzure_Exception
  65. */
  66. require_once 'Microsoft/WindowsAzure/Exception.php';
  67. /**
  68. * @category Microsoft
  69. * @package Microsoft_WindowsAzure
  70. * @subpackage Storage
  71. * @copyright Copyright (c) 2009, RealDolmen (http://www.realdolmen.com)
  72. * @license http://phpazure.codeplex.com/license
  73. */
  74. class Microsoft_WindowsAzure_Storage_Queue extends Microsoft_WindowsAzure_Storage
  75. {
  76. /**
  77. * Maximal message size (in bytes)
  78. */
  79. const MAX_MESSAGE_SIZE = 8388608;
  80. /**
  81. * Maximal message ttl (in seconds)
  82. */
  83. const MAX_MESSAGE_TTL = 604800;
  84. /**
  85. * Creates a new Microsoft_WindowsAzure_Storage_Queue instance
  86. *
  87. * @param string $host Storage host name
  88. * @param string $accountName Account name for Windows Azure
  89. * @param string $accountKey Account key for Windows Azure
  90. * @param boolean $usePathStyleUri Use path-style URI's
  91. * @param Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
  92. */
  93. public function __construct($host = Microsoft_WindowsAzure_Storage::URL_DEV_QUEUE, $accountName = Microsoft_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_ACCOUNT, $accountKey = Microsoft_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_KEY, $usePathStyleUri = false, Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null)
  94. {
  95. parent::__construct($host, $accountName, $accountKey, $usePathStyleUri, $retryPolicy);
  96. // API version
  97. $this->_apiVersion = '2009-04-14';
  98. }
  99. /**
  100. * Check if a queue exists
  101. *
  102. * @param string $queueName Queue name
  103. * @return boolean
  104. */
  105. public function queueExists($queueName = '')
  106. {
  107. if ($queueName === '') {
  108. throw new Microsoft_WindowsAzure_Exception('Queue name is not specified.');
  109. }
  110. if (!self::isValidQueueName($queueName)) {
  111. throw new Microsoft_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
  112. }
  113. // List queues
  114. $queues = $this->listQueues($queueName, 1);
  115. foreach ($queues as $queue) {
  116. if ($queue->Name == $queueName) {
  117. return true;
  118. }
  119. }
  120. return false;
  121. }
  122. /**
  123. * Create queue
  124. *
  125. * @param string $queueName Queue name
  126. * @param array $metadata Key/value pairs of meta data
  127. * @return object Queue properties
  128. * @throws Microsoft_WindowsAzure_Exception
  129. */
  130. public function createQueue($queueName = '', $metadata = array())
  131. {
  132. if ($queueName === '') {
  133. throw new Microsoft_WindowsAzure_Exception('Queue name is not specified.');
  134. }
  135. if (!self::isValidQueueName($queueName)) {
  136. throw new Microsoft_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
  137. }
  138. // Create metadata headers
  139. $headers = array();
  140. $headers = array_merge($headers, $this->_generateMetadataHeaders($metadata));
  141. // Perform request
  142. $response = $this->_performRequest($queueName, '', Microsoft_Http_Client::PUT, $headers);
  143. if ($response->isSuccessful()) {
  144. return new Microsoft_WindowsAzure_Storage_QueueInstance(
  145. $queueName,
  146. $metadata
  147. );
  148. } else {
  149. throw new Microsoft_WindowsAzure_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
  150. }
  151. }
  152. /**
  153. * Get queue
  154. *
  155. * @param string $queueName Queue name
  156. * @return Microsoft_WindowsAzure_Storage_QueueInstance
  157. * @throws Microsoft_WindowsAzure_Exception
  158. */
  159. public function getQueue($queueName = '')
  160. {
  161. if ($queueName === '') {
  162. throw new Microsoft_WindowsAzure_Exception('Queue name is not specified.');
  163. }
  164. if (!self::isValidQueueName($queueName)) {
  165. throw new Microsoft_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
  166. }
  167. // Perform request
  168. $response = $this->_performRequest($queueName, '?comp=metadata', Microsoft_Http_Client::GET);
  169. if ($response->isSuccessful()) {
  170. // Parse metadata
  171. $metadata = $this->_parseMetadataHeaders($response->getHeaders());
  172. // Return queue
  173. $queue = new Microsoft_WindowsAzure_Storage_QueueInstance(
  174. $queueName,
  175. $metadata
  176. );
  177. $queue->ApproximateMessageCount = intval($response->getHeader('x-ms-approximate-message-count'));
  178. return $queue;
  179. } else {
  180. throw new Microsoft_WindowsAzure_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
  181. }
  182. }
  183. /**
  184. * Get queue metadata
  185. *
  186. * @param string $queueName Queue name
  187. * @return array Key/value pairs of meta data
  188. * @throws Microsoft_WindowsAzure_Exception
  189. */
  190. public function getQueueMetadata($queueName = '')
  191. {
  192. if ($queueName === '') {
  193. throw new Microsoft_WindowsAzure_Exception('Queue name is not specified.');
  194. }
  195. if (!self::isValidQueueName($queueName)) {
  196. throw new Microsoft_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
  197. }
  198. return $this->getQueue($queueName)->Metadata;
  199. }
  200. /**
  201. * Set queue metadata
  202. *
  203. * Calling the Set Queue Metadata operation overwrites all existing metadata that is associated with the queue. It's not possible to modify an individual name/value pair.
  204. *
  205. * @param string $queueName Queue name
  206. * @param array $metadata Key/value pairs of meta data
  207. * @throws Microsoft_WindowsAzure_Exception
  208. */
  209. public function setQueueMetadata($queueName = '', $metadata = array())
  210. {
  211. if ($queueName === '') {
  212. throw new Microsoft_WindowsAzure_Exception('Queue name is not specified.');
  213. }
  214. if (!self::isValidQueueName($queueName)) {
  215. throw new Microsoft_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
  216. }
  217. if (count($metadata) == 0) {
  218. return;
  219. }
  220. // Create metadata headers
  221. $headers = array();
  222. $headers = array_merge($headers, $this->_generateMetadataHeaders($metadata));
  223. // Perform request
  224. $response = $this->_performRequest($queueName, '?comp=metadata', Microsoft_Http_Client::PUT, $headers);
  225. if (!$response->isSuccessful()) {
  226. throw new Microsoft_WindowsAzure_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
  227. }
  228. }
  229. /**
  230. * Delete queue
  231. *
  232. * @param string $queueName Queue name
  233. * @throws Microsoft_WindowsAzure_Exception
  234. */
  235. public function deleteQueue($queueName = '')
  236. {
  237. if ($queueName === '') {
  238. throw new Microsoft_WindowsAzure_Exception('Queue name is not specified.');
  239. }
  240. if (!self::isValidQueueName($queueName)) {
  241. throw new Microsoft_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
  242. }
  243. // Perform request
  244. $response = $this->_performRequest($queueName, '', Microsoft_Http_Client::DELETE);
  245. if (!$response->isSuccessful()) {
  246. throw new Microsoft_WindowsAzure_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
  247. }
  248. }
  249. /**
  250. * List queues
  251. *
  252. * @param string $prefix Optional. Filters the results to return only queues whose name begins with the specified prefix.
  253. * @param int $maxResults Optional. Specifies the maximum number of queues to return per call to Azure storage. This does NOT affect list size returned by this function. (maximum: 5000)
  254. * @param string $marker Optional string value that identifies the portion of the list to be returned with the next list operation.
  255. * @param int $currentResultCount Current result count (internal use)
  256. * @return array
  257. * @throws Microsoft_WindowsAzure_Exception
  258. */
  259. public function listQueues($prefix = null, $maxResults = null, $marker = null, $currentResultCount = 0)
  260. {
  261. // Build query string
  262. $queryString = '?comp=list';
  263. if (!is_null($prefix)) {
  264. $queryString .= '&prefix=' . $prefix;
  265. }
  266. if (!is_null($maxResults)) {
  267. $queryString .= '&maxresults=' . $maxResults;
  268. }
  269. if (!is_null($marker)) {
  270. $queryString .= '&marker=' . $marker;
  271. }
  272. // Perform request
  273. $response = $this->_performRequest('', $queryString, Microsoft_Http_Client::GET);
  274. if ($response->isSuccessful()) {
  275. $xmlQueues = $this->_parseResponse($response)->Queues->Queue;
  276. $xmlMarker = (string)$this->_parseResponse($response)->NextMarker;
  277. $queues = array();
  278. if (!is_null($xmlQueues)) {
  279. for ($i = 0; $i < count($xmlQueues); $i++) {
  280. $queues[] = new Microsoft_WindowsAzure_Storage_QueueInstance(
  281. (string)$xmlQueues[$i]->QueueName
  282. );
  283. }
  284. }
  285. $currentResultCount = $currentResultCount + count($queues);
  286. if (!is_null($maxResults) && $currentResultCount < $maxResults) {
  287. if (!is_null($xmlMarker) && $xmlMarker != '') {
  288. $queues = array_merge($queues, $this->listQueues($prefix, $maxResults, $xmlMarker, $currentResultCount));
  289. }
  290. }
  291. if (!is_null($maxResults) && count($queues) > $maxResults) {
  292. $queues = array_slice($queues, 0, $maxResults);
  293. }
  294. return $queues;
  295. } else {
  296. throw new Microsoft_WindowsAzure_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
  297. }
  298. }
  299. /**
  300. * Put message into queue
  301. *
  302. * @param string $queueName Queue name
  303. * @param string $message Message
  304. * @param int $ttl Message Time-To-Live (in seconds). Defaults to 7 days if the parameter is omitted.
  305. * @throws Microsoft_WindowsAzure_Exception
  306. */
  307. public function putMessage($queueName = '', $message = '', $ttl = null)
  308. {
  309. if ($queueName === '') {
  310. throw new Microsoft_WindowsAzure_Exception('Queue name is not specified.');
  311. }
  312. if (!self::isValidQueueName($queueName)) {
  313. throw new Microsoft_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
  314. }
  315. if (strlen($message) > self::MAX_MESSAGE_SIZE) {
  316. throw new Microsoft_WindowsAzure_Exception('Message is too big. Message content should be < 8KB.');
  317. }
  318. if ($message == '') {
  319. throw new Microsoft_WindowsAzure_Exception('Message is not specified.');
  320. }
  321. if (!is_null($ttl) && ($ttl <= 0 || $ttl > self::MAX_MESSAGE_SIZE)) {
  322. throw new Microsoft_WindowsAzure_Exception('Message TTL is invalid. Maximal TTL is 7 days (' . self::MAX_MESSAGE_SIZE . ' seconds) and should be greater than zero.');
  323. }
  324. // Build query string
  325. $queryString = '';
  326. if (!is_null($ttl)) {
  327. $queryString .= '?messagettl=' . $ttl;
  328. }
  329. // Build body
  330. $rawData = '';
  331. $rawData .= '<QueueMessage>';
  332. $rawData .= ' <MessageText>' . base64_encode($message) . '</MessageText>';
  333. $rawData .= '</QueueMessage>';
  334. // Perform request
  335. $response = $this->_performRequest($queueName . '/messages', $queryString, Microsoft_Http_Client::POST, array(), false, $rawData);
  336. if (!$response->isSuccessful()) {
  337. throw new Microsoft_WindowsAzure_Exception('Error putting message into queue.');
  338. }
  339. }
  340. /**
  341. * Get queue messages
  342. *
  343. * @param string $queueName Queue name
  344. * @param string $numOfMessages Optional. A nonzero integer value that specifies the number of messages to retrieve from the queue, up to a maximum of 32. By default, a single message is retrieved from the queue with this operation.
  345. * @param int $visibilityTimeout Optional. An integer value that specifies the message's visibility timeout in seconds. The maximum value is 2 hours. The default message visibility timeout is 30 seconds.
  346. * @param string $peek Peek only?
  347. * @return array
  348. * @throws Microsoft_WindowsAzure_Exception
  349. */
  350. public function getMessages($queueName = '', $numOfMessages = 1, $visibilityTimeout = null, $peek = false)
  351. {
  352. if ($queueName === '') {
  353. throw new Microsoft_WindowsAzure_Exception('Queue name is not specified.');
  354. }
  355. if (!self::isValidQueueName($queueName)) {
  356. throw new Microsoft_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
  357. }
  358. if ($numOfMessages < 1 || $numOfMessages > 32 || intval($numOfMessages) != $numOfMessages) {
  359. throw new Microsoft_WindowsAzure_Exception('Invalid number of messages to retrieve.');
  360. }
  361. if (!is_null($visibilityTimeout) && ($visibilityTimeout <= 0 || $visibilityTimeout > 7200)) {
  362. throw new Microsoft_WindowsAzure_Exception('Visibility timeout is invalid. Maximum value is 2 hours (7200 seconds) and should be greater than zero.');
  363. }
  364. // Build query string
  365. $query = array();
  366. if ($peek) {
  367. $query[] = 'peekonly=true';
  368. }
  369. if ($numOfMessages > 1) {
  370. $query[] = 'numofmessages=' . $numOfMessages;
  371. }
  372. if (!$peek && !is_null($visibilityTimeout)) {
  373. $query[] = 'visibilitytimeout=' . $visibilityTimeout;
  374. }
  375. $queryString = '?' . implode('&', $query);
  376. // Perform request
  377. $response = $this->_performRequest($queueName . '/messages', $queryString, Microsoft_Http_Client::GET);
  378. if ($response->isSuccessful()) {
  379. // Parse results
  380. $result = $this->_parseResponse($response);
  381. if (!$result) {
  382. return array();
  383. }
  384. $xmlMessages = null;
  385. if (count($result->QueueMessage) > 1) {
  386. $xmlMessages = $result->QueueMessage;
  387. } else {
  388. $xmlMessages = array($result->QueueMessage);
  389. }
  390. $messages = array();
  391. for ($i = 0; $i < count($xmlMessages); $i++) {
  392. $messages[] = new Microsoft_WindowsAzure_Storage_QueueMessage(
  393. (string)$xmlMessages[$i]->MessageId,
  394. (string)$xmlMessages[$i]->InsertionTime,
  395. (string)$xmlMessages[$i]->ExpirationTime,
  396. ($peek ? '' : (string)$xmlMessages[$i]->PopReceipt),
  397. ($peek ? '' : (string)$xmlMessages[$i]->TimeNextVisible),
  398. base64_decode((string)$xmlMessages[$i]->MessageText)
  399. );
  400. }
  401. return $messages;
  402. } else {
  403. throw new Microsoft_WindowsAzure_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
  404. }
  405. }
  406. /**
  407. * Peek queue messages
  408. *
  409. * @param string $queueName Queue name
  410. * @param string $numOfMessages Optional. A nonzero integer value that specifies the number of messages to retrieve from the queue, up to a maximum of 32. By default, a single message is retrieved from the queue with this operation.
  411. * @return array
  412. * @throws Microsoft_WindowsAzure_Exception
  413. */
  414. public function peekMessages($queueName = '', $numOfMessages = 1)
  415. {
  416. return $this->getMessages($queueName, $numOfMessages, null, true);
  417. }
  418. /**
  419. * Clear queue messages
  420. *
  421. * @param string $queueName Queue name
  422. * @throws Microsoft_WindowsAzure_Exception
  423. */
  424. public function clearMessages($queueName = '')
  425. {
  426. if ($queueName === '') {
  427. throw new Microsoft_WindowsAzure_Exception('Queue name is not specified.');
  428. }
  429. if (!self::isValidQueueName($queueName)) {
  430. throw new Microsoft_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
  431. }
  432. // Perform request
  433. $response = $this->_performRequest($queueName . '/messages', '', Microsoft_Http_Client::DELETE);
  434. if (!$response->isSuccessful()) {
  435. throw new Microsoft_WindowsAzure_Exception('Error clearing messages from queue.');
  436. }
  437. }
  438. /**
  439. * Delete queue message
  440. *
  441. * @param string $queueName Queue name
  442. * @param Microsoft_WindowsAzure_Storage_QueueMessage $message Message to delete from queue. A message retrieved using "peekMessages" can NOT be deleted!
  443. * @throws Microsoft_WindowsAzure_Exception
  444. */
  445. public function deleteMessage($queueName = '', Microsoft_WindowsAzure_Storage_QueueMessage $message)
  446. {
  447. if ($queueName === '') {
  448. throw new Microsoft_WindowsAzure_Exception('Queue name is not specified.');
  449. }
  450. if (!self::isValidQueueName($queueName)) {
  451. throw new Microsoft_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
  452. }
  453. if ($message->PopReceipt == '') {
  454. throw new Microsoft_WindowsAzure_Exception('A message retrieved using "peekMessages" can NOT be deleted! Use "getMessages" instead.');
  455. }
  456. // Perform request
  457. $response = $this->_performRequest($queueName . '/messages/' . $message->MessageId, '?popreceipt=' . $message->PopReceipt, Microsoft_Http_Client::DELETE);
  458. if (!$response->isSuccessful()) {
  459. throw new Microsoft_WindowsAzure_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
  460. }
  461. }
  462. /**
  463. * Is valid queue name?
  464. *
  465. * @param string $queueName Queue name
  466. * @return boolean
  467. */
  468. public static function isValidQueueName($queueName = '')
  469. {
  470. if (preg_match("/^[a-z0-9][a-z0-9-]*$/", $queueName) === 0) {
  471. return false;
  472. }
  473. if (strpos($queueName, '--') !== false) {
  474. return false;
  475. }
  476. if (strtolower($queueName) != $queueName) {
  477. return false;
  478. }
  479. if (strlen($queueName) < 3 || strlen($queueName) > 63) {
  480. return false;
  481. }
  482. if (substr($queueName, -1) == '-') {
  483. return false;
  484. }
  485. return true;
  486. }
  487. /**
  488. * Get error message from Microsoft_Http_Response
  489. *
  490. * @param Microsoft_Http_Response $response Repsonse
  491. * @param string $alternativeError Alternative error message
  492. * @return string
  493. */
  494. protected function _getErrorMessage(Microsoft_Http_Response $response, $alternativeError = 'Unknown error.')
  495. {
  496. $response = $this->_parseResponse($response);
  497. if ($response && $response->Message) {
  498. return (string)$response->Message;
  499. } else {
  500. return $alternativeError;
  501. }
  502. }
  503. }