diff -r 000000000000 -r 4eba9c11703f web/Zend/Service/WindowsAzure/Storage/BatchStorageAbstract.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/Zend/Service/WindowsAzure/Storage/BatchStorageAbstract.php Mon Dec 13 18:29:26 2010 +0100 @@ -0,0 +1,197 @@ +isInBatch()) { + throw new Zend_Service_WindowsAzure_Exception('Only one batch can be active at a time.'); + } + $this->_currentBatch = $batch; + } + + /** + * Get current batch + * + * @return Zend_Service_WindowsAzure_Storage_Batch + */ + public function getCurrentBatch() + { + return $this->_currentBatch; + } + + /** + * Is there a current batch? + * + * @return boolean + */ + public function isInBatch() + { + return $this->_currentBatch !== null; + } + + /** + * Starts a new batch operation set + * + * @return Zend_Service_WindowsAzure_Storage_Batch + * @throws Zend_Service_WindowsAzure_Exception + */ + public function startBatch() + { + return new Zend_Service_WindowsAzure_Storage_Batch($this, $this->getBaseUrl()); + } + + /** + * Perform batch using Zend_Http_Client channel, combining all batch operations into one request + * + * @param array $operations Operations in batch + * @param boolean $forTableStorage Is the request for table storage? + * @param boolean $isSingleSelect Is the request a single select statement? + * @param string $resourceType Resource type + * @param string $requiredPermission Required permission + * @return Zend_Http_Response + */ + public function performBatch($operations = array(), $forTableStorage = false, $isSingleSelect = false, $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN, $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ) + { + // Generate boundaries + $batchBoundary = 'batch_' . md5(time() . microtime()); + $changesetBoundary = 'changeset_' . md5(time() . microtime()); + + // Set headers + $headers = array(); + + // Add version header + $headers['x-ms-version'] = $this->_apiVersion; + + // Add dataservice headers + $headers['DataServiceVersion'] = '1.0;NetFx'; + $headers['MaxDataServiceVersion'] = '1.0;NetFx'; + + // Add content-type header + $headers['Content-Type'] = 'multipart/mixed; boundary=' . $batchBoundary; + + // Set path and query string + $path = '/$batch'; + $queryString = ''; + + // Set verb + $httpVerb = Zend_Http_Client::POST; + + // Generate raw data + $rawData = ''; + + // Single select? + if ($isSingleSelect) { + $operation = $operations[0]; + $rawData .= '--' . $batchBoundary . "\n"; + $rawData .= 'Content-Type: application/http' . "\n"; + $rawData .= 'Content-Transfer-Encoding: binary' . "\n\n"; + $rawData .= $operation; + $rawData .= '--' . $batchBoundary . '--'; + } else { + $rawData .= '--' . $batchBoundary . "\n"; + $rawData .= 'Content-Type: multipart/mixed; boundary=' . $changesetBoundary . "\n\n"; + + // Add operations + foreach ($operations as $operation) + { + $rawData .= '--' . $changesetBoundary . "\n"; + $rawData .= 'Content-Type: application/http' . "\n"; + $rawData .= 'Content-Transfer-Encoding: binary' . "\n\n"; + $rawData .= $operation; + } + $rawData .= '--' . $changesetBoundary . '--' . "\n"; + + $rawData .= '--' . $batchBoundary . '--'; + } + + // Generate URL and sign request + $requestUrl = $this->_credentials->signRequestUrl($this->getBaseUrl() . $path . $queryString, $resourceType, $requiredPermission); + $requestHeaders = $this->_credentials->signRequestHeaders($httpVerb, $path, $queryString, $headers, $forTableStorage, $resourceType, $requiredPermission); + + // Prepare request + $this->_httpClientChannel->resetParameters(true); + $this->_httpClientChannel->setUri($requestUrl); + $this->_httpClientChannel->setHeaders($requestHeaders); + $this->_httpClientChannel->setRawData($rawData); + + // Execute request + $response = $this->_retryPolicy->execute( + array($this->_httpClientChannel, 'request'), + array($httpVerb) + ); + + return $response; + } +}