web/lib/Zend/Service/WindowsAzure/Storage/Queue.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Service_WindowsAzure
    16  * @package    Zend_Service_WindowsAzure
    17  * @subpackage Storage
    17  * @subpackage Storage
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://todo     name_todo
    19  * @license    http://todo     name_todo
    20  * @version    $Id: Queue.php 23167 2010-10-19 17:53:31Z mabe $
    20  * @version    $Id: Queue.php 24593 2012-01-05 20:35:02Z matthew $
    21  */
    21  */
    22 
    22 
    23 /**
    23 /**
    24  * @see Zend_Service_WindowsAzure_Credentials_SharedKey
    24  * see Zend_Service_WindowsAzure_Storage
    25  */
       
    26 require_once 'Zend/Service/WindowsAzure/Credentials/SharedKey.php';
       
    27 
       
    28 /**
       
    29  * @see Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract
       
    30  */
       
    31 require_once 'Zend/Service/WindowsAzure/RetryPolicy/RetryPolicyAbstract.php';
       
    32 
       
    33 /**
       
    34  * @see Zend_Http_Client
       
    35  */
       
    36 require_once 'Zend/Http/Client.php';
       
    37 
       
    38 /**
       
    39  * @see Zend_Http_Response
       
    40  */
       
    41 require_once 'Zend/Http/Response.php';
       
    42 
       
    43 /**
       
    44  * @see Zend_Service_WindowsAzure_Storage
       
    45  */
    25  */
    46 require_once 'Zend/Service/WindowsAzure/Storage.php';
    26 require_once 'Zend/Service/WindowsAzure/Storage.php';
    47 
    27 
    48 /**
    28 /**
    49  * Zend_Service_WindowsAzure_Storage_QueueInstance
    29  * @see Zend_Service_WindowsAzure_Storage_QueueInstance
    50  */
    30  */
    51 require_once 'Zend/Service/WindowsAzure/Storage/QueueInstance.php';
    31 require_once 'Zend/Service/WindowsAzure/Storage/QueueInstance.php';
    52 
    32 
    53 /**
    33 /**
    54  * Zend_Service_WindowsAzure_Storage_QueueMessage
    34  * @see Zend_Service_WindowsAzure_Storage_QueueMessage
    55  */
    35  */
    56 require_once 'Zend/Service/WindowsAzure/Storage/QueueMessage.php';
    36 require_once 'Zend/Service/WindowsAzure/Storage/QueueMessage.php';
    57 
       
    58 /**
       
    59  * @see Zend_Service_WindowsAzure_Exception
       
    60  */
       
    61 require_once 'Zend/Service/WindowsAzure/Exception.php';
       
    62 
       
    63 
    37 
    64 /**
    38 /**
    65  * @category   Zend
    39  * @category   Zend
    66  * @package    Zend_Service_WindowsAzure
    40  * @package    Zend_Service_WindowsAzure
    67  * @subpackage Storage
    41  * @subpackage Storage
    68  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    42  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    69  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    43  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    70  */
    44  */
    71 class Zend_Service_WindowsAzure_Storage_Queue extends Zend_Service_WindowsAzure_Storage
    45 class Zend_Service_WindowsAzure_Storage_Queue extends Zend_Service_WindowsAzure_Storage
    72 {
    46 {
    73 	/**
    47 	/**
   104 	 * @return boolean
    78 	 * @return boolean
   105 	 */
    79 	 */
   106 	public function queueExists($queueName = '')
    80 	public function queueExists($queueName = '')
   107 	{
    81 	{
   108 		if ($queueName === '') {
    82 		if ($queueName === '') {
   109 			throw new Zend_Service_WindowsAzure_Exception('Queue name is not specified.');
    83 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   110 		}
    84 			throw new Zend_Service_WindowsAzure_Exception('Queue name is not specified.');
   111 		if (!self::isValidQueueName($queueName)) {
    85 		}
       
    86 		if (!self::isValidQueueName($queueName)) {
       
    87 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   112 		    throw new Zend_Service_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
    88 		    throw new Zend_Service_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
   113 		}
    89 		}
   114 			
    90 			
   115 		// List queues
    91 		// List queues
   116         $queues = $this->listQueues($queueName, 1);
    92         $queues = $this->listQueues($queueName, 1);
   132 	 * @throws Zend_Service_WindowsAzure_Exception
   108 	 * @throws Zend_Service_WindowsAzure_Exception
   133 	 */
   109 	 */
   134 	public function createQueue($queueName = '', $metadata = array())
   110 	public function createQueue($queueName = '', $metadata = array())
   135 	{
   111 	{
   136 		if ($queueName === '') {
   112 		if ($queueName === '') {
   137 			throw new Zend_Service_WindowsAzure_Exception('Queue name is not specified.');
   113 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   138 		}
   114 			throw new Zend_Service_WindowsAzure_Exception('Queue name is not specified.');
   139 		if (!self::isValidQueueName($queueName)) {
   115 		}
       
   116 		if (!self::isValidQueueName($queueName)) {
       
   117 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   140 		    throw new Zend_Service_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
   118 		    throw new Zend_Service_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
   141 		}
   119 		}
   142 			
   120 			
   143 		// Create metadata headers
   121 		// Create metadata headers
   144 		$headers = array();
   122 		$headers = array();
   145 		$headers = array_merge($headers, $this->_generateMetadataHeaders($metadata)); 
   123 		$headers = array_merge($headers, $this->_generateMetadataHeaders($metadata)); 
   146 		
   124 		
   147 		// Perform request
   125 		// Perform request
   148 		$response = $this->_performRequest($queueName, '', Zend_Http_Client::PUT, $headers);	
   126 		$response = $this->_performRequest($queueName, '', Zend_Http_Client::PUT, $headers);	
   149 		if ($response->isSuccessful()) {
   127 		if ($response->isSuccessful()) {
       
   128 			
   150 		    return new Zend_Service_WindowsAzure_Storage_QueueInstance(
   129 		    return new Zend_Service_WindowsAzure_Storage_QueueInstance(
   151 		        $queueName,
   130 		        $queueName,
   152 		        $metadata
   131 		        $metadata
   153 		    );
   132 		    );
   154 		} else {
   133 		} else {
       
   134 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   155 			throw new Zend_Service_WindowsAzure_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
   135 			throw new Zend_Service_WindowsAzure_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
       
   136 		}
       
   137 	}
       
   138 	
       
   139 	/**
       
   140 	 * Create queue if it does not exist
       
   141 	 *
       
   142 	 * @param string $queueName Queue name
       
   143 	 * @param array  $metadata  Key/value pairs of meta data
       
   144 	 * @throws Zend_Service_WindowsAzure_Exception
       
   145 	 */
       
   146 	public function createQueueIfNotExists($queueName = '', $metadata = array())
       
   147 	{
       
   148 		if (!$this->queueExists($queueName)) {
       
   149 			$this->createQueue($queueName, $metadata);
   156 		}
   150 		}
   157 	}
   151 	}
   158 	
   152 	
   159 	/**
   153 	/**
   160 	 * Get queue
   154 	 * Get queue
   164 	 * @throws Zend_Service_WindowsAzure_Exception
   158 	 * @throws Zend_Service_WindowsAzure_Exception
   165 	 */
   159 	 */
   166 	public function getQueue($queueName = '')
   160 	public function getQueue($queueName = '')
   167 	{
   161 	{
   168 		if ($queueName === '') {
   162 		if ($queueName === '') {
   169 			throw new Zend_Service_WindowsAzure_Exception('Queue name is not specified.');
   163 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   170 		}
   164 			throw new Zend_Service_WindowsAzure_Exception('Queue name is not specified.');
   171 		if (!self::isValidQueueName($queueName)) {
   165 		}
       
   166 		if (!self::isValidQueueName($queueName)) {
       
   167 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   172 		    throw new Zend_Service_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
   168 		    throw new Zend_Service_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
   173 		}
   169 		}
   174 		    
   170 		    
   175 		// Perform request
   171 		// Perform request
   176 		$response = $this->_performRequest($queueName, '?comp=metadata', Zend_Http_Client::GET);	
   172 		$response = $this->_performRequest($queueName, '?comp=metadata', Zend_Http_Client::GET);	
   177 		if ($response->isSuccessful()) {
   173 		if ($response->isSuccessful()) {
   178 		    // Parse metadata
   174 		    // Parse metadata
   179 		    $metadata = $this->_parseMetadataHeaders($response->getHeaders());
   175 		    $metadata = $this->_parseMetadataHeaders($response->getHeaders());
   180 
   176 			
   181 		    // Return queue
   177 		    // Return queue
   182 		    $queue = new Zend_Service_WindowsAzure_Storage_QueueInstance(
   178 		    $queue = new Zend_Service_WindowsAzure_Storage_QueueInstance(
   183 		        $queueName,
   179 		        $queueName,
   184 		        $metadata
   180 		        $metadata
   185 		    );
   181 		    );
   186 		    $queue->ApproximateMessageCount = intval($response->getHeader('x-ms-approximate-message-count'));
   182 		    $queue->ApproximateMessageCount = intval($response->getHeader('x-ms-approximate-message-count'));
   187 		    return $queue;
   183 		    return $queue;
   188 		} else {
   184 		} else {
       
   185 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   189 		    throw new Zend_Service_WindowsAzure_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
   186 		    throw new Zend_Service_WindowsAzure_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
   190 		}
   187 		}
   191 	}
   188 	}
   192 	
   189 	
   193 	/**
   190 	/**
   198 	 * @throws Zend_Service_WindowsAzure_Exception
   195 	 * @throws Zend_Service_WindowsAzure_Exception
   199 	 */
   196 	 */
   200 	public function getQueueMetadata($queueName = '')
   197 	public function getQueueMetadata($queueName = '')
   201 	{
   198 	{
   202 		if ($queueName === '') {
   199 		if ($queueName === '') {
   203 			throw new Zend_Service_WindowsAzure_Exception('Queue name is not specified.');
   200 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   204 		}
   201 			throw new Zend_Service_WindowsAzure_Exception('Queue name is not specified.');
   205 		if (!self::isValidQueueName($queueName)) {
   202 		}
       
   203 		if (!self::isValidQueueName($queueName)) {
       
   204 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   206 		    throw new Zend_Service_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
   205 		    throw new Zend_Service_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
   207 		}
   206 		}
   208 			
   207 			
   209 	    return $this->getQueue($queueName)->Metadata;
   208 	    return $this->getQueue($queueName)->Metadata;
   210 	}
   209 	}
   219 	 * @throws Zend_Service_WindowsAzure_Exception
   218 	 * @throws Zend_Service_WindowsAzure_Exception
   220 	 */
   219 	 */
   221 	public function setQueueMetadata($queueName = '', $metadata = array())
   220 	public function setQueueMetadata($queueName = '', $metadata = array())
   222 	{
   221 	{
   223 		if ($queueName === '') {
   222 		if ($queueName === '') {
   224 			throw new Zend_Service_WindowsAzure_Exception('Queue name is not specified.');
   223 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   225 		}
   224 			throw new Zend_Service_WindowsAzure_Exception('Queue name is not specified.');
   226 		if (!self::isValidQueueName($queueName)) {
   225 		}
       
   226 		if (!self::isValidQueueName($queueName)) {
       
   227 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   227 		    throw new Zend_Service_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
   228 		    throw new Zend_Service_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
   228 		}
   229 		}
   229 		if (count($metadata) == 0) {
   230 		if (count($metadata) == 0) {
   230 		    return;
   231 		    return;
   231 		}
   232 		}
   236 		
   237 		
   237 		// Perform request
   238 		// Perform request
   238 		$response = $this->_performRequest($queueName, '?comp=metadata', Zend_Http_Client::PUT, $headers);
   239 		$response = $this->_performRequest($queueName, '?comp=metadata', Zend_Http_Client::PUT, $headers);
   239 
   240 
   240 		if (!$response->isSuccessful()) {
   241 		if (!$response->isSuccessful()) {
       
   242 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   241 			throw new Zend_Service_WindowsAzure_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
   243 			throw new Zend_Service_WindowsAzure_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
   242 		}
   244 		}
   243 	}
   245 	}
   244 	
   246 	
   245 	/**
   247 	/**
   249 	 * @throws Zend_Service_WindowsAzure_Exception
   251 	 * @throws Zend_Service_WindowsAzure_Exception
   250 	 */
   252 	 */
   251 	public function deleteQueue($queueName = '')
   253 	public function deleteQueue($queueName = '')
   252 	{
   254 	{
   253 		if ($queueName === '') {
   255 		if ($queueName === '') {
   254 			throw new Zend_Service_WindowsAzure_Exception('Queue name is not specified.');
   256 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   255 		}
   257 			throw new Zend_Service_WindowsAzure_Exception('Queue name is not specified.');
   256 		if (!self::isValidQueueName($queueName)) {
   258 		}
       
   259 		if (!self::isValidQueueName($queueName)) {
       
   260 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   257 		    throw new Zend_Service_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
   261 		    throw new Zend_Service_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
   258 		}
   262 		}
   259 			
   263 			
   260 		// Perform request
   264 		// Perform request
   261 		$response = $this->_performRequest($queueName, '', Zend_Http_Client::DELETE);
   265 		$response = $this->_performRequest($queueName, '', Zend_Http_Client::DELETE);
   262 		if (!$response->isSuccessful()) {
   266 		if (!$response->isSuccessful()) {
       
   267 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   263 			throw new Zend_Service_WindowsAzure_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
   268 			throw new Zend_Service_WindowsAzure_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
   264 		}
   269 		}
   265 	}
   270 	}
   266 	
   271 	
   267 	/**
   272 	/**
   277 	 */
   282 	 */
   278 	public function listQueues($prefix = null, $maxResults = null, $marker = null, $include = null, $currentResultCount = 0)
   283 	public function listQueues($prefix = null, $maxResults = null, $marker = null, $include = null, $currentResultCount = 0)
   279 	{
   284 	{
   280 	    // Build query string
   285 	    // Build query string
   281 		$queryString = array('comp=list');
   286 		$queryString = array('comp=list');
   282         if ($prefix !== null) {
   287         if (!is_null($prefix)) {
   283 	        $queryString[] = 'prefix=' . $prefix;
   288 	        $queryString[] = 'prefix=' . $prefix;
   284         }
   289         }
   285 	    if ($maxResults !== null) {
   290 	    if (!is_null($maxResults)) {
   286 	        $queryString[] = 'maxresults=' . $maxResults;
   291 	        $queryString[] = 'maxresults=' . $maxResults;
   287 	    }
   292 	    }
   288 	    if ($marker !== null) {
   293 	    if (!is_null($marker)) {
   289 	        $queryString[] = 'marker=' . $marker;
   294 	        $queryString[] = 'marker=' . $marker;
   290 	    }
   295 	    }
   291 		if ($include !== null) {
   296 		if (!is_null($include)) {
   292 	        $queryString[] = 'include=' . $include;
   297 	        $queryString[] = 'include=' . $include;
   293 	    }
   298 	    }
   294 	    $queryString = self::createQueryStringFromArray($queryString);
   299 	    $queryString = self::createQueryStringFromArray($queryString);
   295 	        
   300 	        
   296 		// Perform request
   301 		// Perform request
   298 		if ($response->isSuccessful()) {
   303 		if ($response->isSuccessful()) {
   299 			$xmlQueues = $this->_parseResponse($response)->Queues->Queue;
   304 			$xmlQueues = $this->_parseResponse($response)->Queues->Queue;
   300 			$xmlMarker = (string)$this->_parseResponse($response)->NextMarker;
   305 			$xmlMarker = (string)$this->_parseResponse($response)->NextMarker;
   301 
   306 
   302 			$queues = array();
   307 			$queues = array();
   303 			if ($xmlQueues !== null) {
   308 			if (!is_null($xmlQueues)) {
       
   309 				
   304 				for ($i = 0; $i < count($xmlQueues); $i++) {
   310 				for ($i = 0; $i < count($xmlQueues); $i++) {
   305 					$queues[] = new Zend_Service_WindowsAzure_Storage_QueueInstance(
   311 					$queues[] = new Zend_Service_WindowsAzure_Storage_QueueInstance(
   306 						(string)$xmlQueues[$i]->Name,
   312 						(string)$xmlQueues[$i]->Name,
   307 						$this->_parseMetadataElement($xmlQueues[$i])
   313 						$this->_parseMetadataElement($xmlQueues[$i])
   308 					);
   314 					);
   309 				}
   315 				}
   310 			}
   316 			}
   311 			$currentResultCount = $currentResultCount + count($queues);
   317 			$currentResultCount = $currentResultCount + count($queues);
   312 			if ($maxResults !== null && $currentResultCount < $maxResults) {
   318 			if (!is_null($maxResults) && $currentResultCount < $maxResults) {
   313     			if ($xmlMarker !== null && $xmlMarker != '') {
   319     			if (!is_null($xmlMarker) && $xmlMarker != '') {
   314     			    $queues = array_merge($queues, $this->listQueues($prefix, $maxResults, $xmlMarker, $include, $currentResultCount));
   320     			    $queues = array_merge($queues, $this->listQueues($prefix, $maxResults, $xmlMarker, $include, $currentResultCount));
   315     			}
   321     			}
   316 			}
   322 			}
   317 			if ($maxResults !== null && count($queues) > $maxResults) {
   323 			if (!is_null($maxResults) && count($queues) > $maxResults) {
   318 			    $queues = array_slice($queues, 0, $maxResults);
   324 			    $queues = array_slice($queues, 0, $maxResults);
   319 			}
   325 			}
   320 			    
   326 			    
   321 			return $queues;
   327 			return $queues;
   322 		} else {
   328 		} else {
       
   329 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   323 			throw new Zend_Service_WindowsAzure_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
   330 			throw new Zend_Service_WindowsAzure_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
   324 		}
   331 		}
   325 	}
   332 	}
   326 	
   333 	
   327 	/**
   334 	/**
   333 	 * @throws Zend_Service_WindowsAzure_Exception
   340 	 * @throws Zend_Service_WindowsAzure_Exception
   334 	 */
   341 	 */
   335 	public function putMessage($queueName = '', $message = '', $ttl = null)
   342 	public function putMessage($queueName = '', $message = '', $ttl = null)
   336 	{
   343 	{
   337 		if ($queueName === '') {
   344 		if ($queueName === '') {
   338 			throw new Zend_Service_WindowsAzure_Exception('Queue name is not specified.');
   345 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   339 		}
   346 			throw new Zend_Service_WindowsAzure_Exception('Queue name is not specified.');
   340 		if (!self::isValidQueueName($queueName)) {
   347 		}
       
   348 		if (!self::isValidQueueName($queueName)) {
       
   349 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   341 		    throw new Zend_Service_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
   350 		    throw new Zend_Service_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
   342 		}
   351 		}
   343 		if (strlen($message) > self::MAX_MESSAGE_SIZE) {
   352 		if (strlen($message) > self::MAX_MESSAGE_SIZE) {
       
   353 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   344 		    throw new Zend_Service_WindowsAzure_Exception('Message is too big. Message content should be < 8KB.');
   354 		    throw new Zend_Service_WindowsAzure_Exception('Message is too big. Message content should be < 8KB.');
   345 		}
   355 		}
   346 		if ($message == '') {
   356 		if ($message == '') {
       
   357 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   347 		    throw new Zend_Service_WindowsAzure_Exception('Message is not specified.');
   358 		    throw new Zend_Service_WindowsAzure_Exception('Message is not specified.');
   348 		}
   359 		}
   349 		if ($ttl !== null && ($ttl <= 0 || $ttl > self::MAX_MESSAGE_SIZE)) {
   360 		if (!is_null($ttl) && ($ttl <= 0 || $ttl > self::MAX_MESSAGE_SIZE)) {
       
   361 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   350 		    throw new Zend_Service_WindowsAzure_Exception('Message TTL is invalid. Maximal TTL is 7 days (' . self::MAX_MESSAGE_SIZE . ' seconds) and should be greater than zero.');
   362 		    throw new Zend_Service_WindowsAzure_Exception('Message TTL is invalid. Maximal TTL is 7 days (' . self::MAX_MESSAGE_SIZE . ' seconds) and should be greater than zero.');
   351 		}
   363 		}
   352 		    
   364 		    
   353 	    // Build query string
   365 	    // Build query string
   354 		$queryString = array();
   366 		$queryString = array();
   355         if ($ttl !== null) {
   367         if (!is_null($ttl)) {
   356 	        $queryString[] = 'messagettl=' . $ttl;
   368 	        $queryString[] = 'messagettl=' . $ttl;
   357         }
   369         }
   358 	    $queryString = self::createQueryStringFromArray($queryString);
   370 	    $queryString = self::createQueryStringFromArray($queryString);
   359 	        
   371 	        
   360 	    // Build body
   372 	    // Build body
   365 	        
   377 	        
   366 		// Perform request
   378 		// Perform request
   367 		$response = $this->_performRequest($queueName . '/messages', $queryString, Zend_Http_Client::POST, array(), false, $rawData);
   379 		$response = $this->_performRequest($queueName . '/messages', $queryString, Zend_Http_Client::POST, array(), false, $rawData);
   368 
   380 
   369 		if (!$response->isSuccessful()) {
   381 		if (!$response->isSuccessful()) {
       
   382 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   370 			throw new Zend_Service_WindowsAzure_Exception('Error putting message into queue.');
   383 			throw new Zend_Service_WindowsAzure_Exception('Error putting message into queue.');
   371 		}
   384 		}
   372 	}
   385 	}
   373 	
   386 	
   374 	/**
   387 	/**
   382 	 * @throws Zend_Service_WindowsAzure_Exception
   395 	 * @throws Zend_Service_WindowsAzure_Exception
   383 	 */
   396 	 */
   384 	public function getMessages($queueName = '', $numOfMessages = 1, $visibilityTimeout = null, $peek = false)
   397 	public function getMessages($queueName = '', $numOfMessages = 1, $visibilityTimeout = null, $peek = false)
   385 	{
   398 	{
   386 		if ($queueName === '') {
   399 		if ($queueName === '') {
   387 			throw new Zend_Service_WindowsAzure_Exception('Queue name is not specified.');
   400 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   388 		}
   401 			throw new Zend_Service_WindowsAzure_Exception('Queue name is not specified.');
   389 		if (!self::isValidQueueName($queueName)) {
   402 		}
       
   403 		if (!self::isValidQueueName($queueName)) {
       
   404 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   390 		    throw new Zend_Service_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
   405 		    throw new Zend_Service_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
   391 		}
   406 		}
   392 		if ($numOfMessages < 1 || $numOfMessages > 32 || intval($numOfMessages) != $numOfMessages) {
   407 		if ($numOfMessages < 1 || $numOfMessages > 32 || intval($numOfMessages) != $numOfMessages) {
       
   408 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   393 		    throw new Zend_Service_WindowsAzure_Exception('Invalid number of messages to retrieve.');
   409 		    throw new Zend_Service_WindowsAzure_Exception('Invalid number of messages to retrieve.');
   394 		}
   410 		}
   395 		if ($visibilityTimeout !== null && ($visibilityTimeout <= 0 || $visibilityTimeout > 7200)) {
   411 		if (!is_null($visibilityTimeout) && ($visibilityTimeout <= 0 || $visibilityTimeout > 7200)) {
       
   412 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   396 		    throw new Zend_Service_WindowsAzure_Exception('Visibility timeout is invalid. Maximum value is 2 hours (7200 seconds) and should be greater than zero.');
   413 		    throw new Zend_Service_WindowsAzure_Exception('Visibility timeout is invalid. Maximum value is 2 hours (7200 seconds) and should be greater than zero.');
   397 		}
   414 		}
   398 		    
   415 		    
   399 	    // Build query string
   416 	    // Build query string
   400 		$queryString = array();
   417 		$queryString = array();
   402     	    $queryString[] = 'peekonly=true';
   419     	    $queryString[] = 'peekonly=true';
   403     	}
   420     	}
   404     	if ($numOfMessages > 1) {
   421     	if ($numOfMessages > 1) {
   405 	        $queryString[] = 'numofmessages=' . $numOfMessages;
   422 	        $queryString[] = 'numofmessages=' . $numOfMessages;
   406     	}
   423     	}
   407     	if (!$peek && $visibilityTimeout !== null) {
   424     	if (!$peek && !is_null($visibilityTimeout)) {
   408 	        $queryString[] = 'visibilitytimeout=' . $visibilityTimeout;
   425 	        $queryString[] = 'visibilitytimeout=' . $visibilityTimeout;
   409     	}   
   426     	}   
   410 	    $queryString = self::createQueryStringFromArray($queryString);
   427 	    $queryString = self::createQueryStringFromArray($queryString);
   411 	        
   428 	        
   412 		// Perform request
   429 		// Perform request
   422 		    if (count($result->QueueMessage) > 1) {
   439 		    if (count($result->QueueMessage) > 1) {
   423     		    $xmlMessages = $result->QueueMessage;
   440     		    $xmlMessages = $result->QueueMessage;
   424     		} else {
   441     		} else {
   425     		    $xmlMessages = array($result->QueueMessage);
   442     		    $xmlMessages = array($result->QueueMessage);
   426     		}
   443     		}
   427 
   444 			
   428 			$messages = array();
   445 			$messages = array();
   429 			for ($i = 0; $i < count($xmlMessages); $i++) {
   446 			for ($i = 0; $i < count($xmlMessages); $i++) {
   430 				$messages[] = new Zend_Service_WindowsAzure_Storage_QueueMessage(
   447 				$messages[] = new Zend_Service_WindowsAzure_Storage_QueueMessage(
   431 					(string)$xmlMessages[$i]->MessageId,
   448 					(string)$xmlMessages[$i]->MessageId,
   432 					(string)$xmlMessages[$i]->InsertionTime,
   449 					(string)$xmlMessages[$i]->InsertionTime,
   438 			    );
   455 			    );
   439 			}
   456 			}
   440 			    
   457 			    
   441 			return $messages;
   458 			return $messages;
   442 		} else {
   459 		} else {
       
   460 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   443 			throw new Zend_Service_WindowsAzure_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
   461 			throw new Zend_Service_WindowsAzure_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
   444 		}
   462 		}
   445 	}
   463 	}
   446 	
   464 	
   447 	/**
   465 	/**
   456 	{
   474 	{
   457 	    return $this->getMessages($queueName, $numOfMessages, null, true);
   475 	    return $this->getMessages($queueName, $numOfMessages, null, true);
   458 	}
   476 	}
   459 	
   477 	
   460 	/**
   478 	/**
       
   479 	 * Checks to see if a given queue has messages
       
   480 	 *
       
   481 	 * @param string $queueName         Queue name
       
   482 	 * @return boolean
       
   483 	 * @throws Zend_Service_WindowsAzure_Exception
       
   484 	 */
       
   485 	public function hasMessages($queueName = '')
       
   486 	{
       
   487 		return count($this->peekMessages($queueName)) > 0;
       
   488 	}
       
   489 	
       
   490 	/**
   461 	 * Clear queue messages
   491 	 * Clear queue messages
   462 	 *
   492 	 *
   463 	 * @param string $queueName         Queue name
   493 	 * @param string $queueName         Queue name
   464 	 * @throws Zend_Service_WindowsAzure_Exception
   494 	 * @throws Zend_Service_WindowsAzure_Exception
   465 	 */
   495 	 */
   466 	public function clearMessages($queueName = '')
   496 	public function clearMessages($queueName = '')
   467 	{
   497 	{
   468 		if ($queueName === '') {
   498 		if ($queueName === '') {
   469 			throw new Zend_Service_WindowsAzure_Exception('Queue name is not specified.');
   499 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   470 		}
   500 			throw new Zend_Service_WindowsAzure_Exception('Queue name is not specified.');
   471 		if (!self::isValidQueueName($queueName)) {
   501 		}
       
   502 		if (!self::isValidQueueName($queueName)) {
       
   503 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   472 		    throw new Zend_Service_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
   504 		    throw new Zend_Service_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
   473 		}
   505 		}
   474 
   506 
   475 		// Perform request
   507 		// Perform request
   476 		$response = $this->_performRequest($queueName . '/messages', '', Zend_Http_Client::DELETE);	
   508 		$response = $this->_performRequest($queueName . '/messages', '', Zend_Http_Client::DELETE);	
   477 		if (!$response->isSuccessful()) {
   509 		if (!$response->isSuccessful()) {
       
   510 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   478 			throw new Zend_Service_WindowsAzure_Exception('Error clearing messages from queue.');
   511 			throw new Zend_Service_WindowsAzure_Exception('Error clearing messages from queue.');
   479 		}
   512 		}
   480 	}
   513 	}
   481 	
   514 	
   482 	/**
   515 	/**
   483 	 * Delete queue message
   516 	 * Delete queue message
   484 	 *
   517 	 *
   485 	 * @param string $queueName         					Queue name
   518 	 * @param string $queueName Queue name
   486 	 * @param Zend_Service_WindowsAzure_Storage_QueueMessage $message Message to delete from queue. A message retrieved using "peekMessages" can NOT be deleted!
   519 	 * @param Zend_Service_WindowsAzure_Storage_QueueMessage $message Message to delete from queue. A message retrieved using "peekMessages" can NOT be deleted!
   487 	 * @throws Zend_Service_WindowsAzure_Exception
   520 	 * @throws Zend_Service_WindowsAzure_Exception
   488 	 */
   521 	 */
   489 	public function deleteMessage($queueName = '', Zend_Service_WindowsAzure_Storage_QueueMessage $message)
   522 	public function deleteMessage($queueName = '', Zend_Service_WindowsAzure_Storage_QueueMessage $message)
   490 	{
   523 	{
   491 		if ($queueName === '') {
   524 		if ($queueName === '') {
   492 			throw new Zend_Service_WindowsAzure_Exception('Queue name is not specified.');
   525 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   493 		}
   526 			throw new Zend_Service_WindowsAzure_Exception('Queue name is not specified.');
   494 		if (!self::isValidQueueName($queueName)) {
   527 		}
       
   528 		if (!self::isValidQueueName($queueName)) {
       
   529 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   495 		    throw new Zend_Service_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
   530 		    throw new Zend_Service_WindowsAzure_Exception('Queue name does not adhere to queue naming conventions. See http://msdn.microsoft.com/en-us/library/dd179349.aspx for more information.');
   496 		}
   531 		}
   497 		if ($message->PopReceipt == '') {
   532 		if ($message->PopReceipt == '') {
       
   533 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   498 		    throw new Zend_Service_WindowsAzure_Exception('A message retrieved using "peekMessages" can NOT be deleted! Use "getMessages" instead.');
   534 		    throw new Zend_Service_WindowsAzure_Exception('A message retrieved using "peekMessages" can NOT be deleted! Use "getMessages" instead.');
   499 		}
   535 		}
   500 
   536 
   501 		// Perform request
   537 		// Perform request
   502 		$response = $this->_performRequest($queueName . '/messages/' . $message->MessageId, '?popreceipt=' . $message->PopReceipt, Zend_Http_Client::DELETE);	
   538 		$response = $this->_performRequest($queueName . '/messages/' . $message->MessageId, '?popreceipt=' . urlencode($message->PopReceipt), Zend_Http_Client::DELETE);	
   503 		if (!$response->isSuccessful()) {
   539 		if (!$response->isSuccessful()) {
       
   540 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   504 			throw new Zend_Service_WindowsAzure_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
   541 			throw new Zend_Service_WindowsAzure_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
   505 		}
   542 		}
   506 	}
   543 	}
   507 	
   544 	
   508 	/**
   545 	/**