|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Service_WindowsAzure |
|
17 * @subpackage Storage |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: BatchStorageAbstract.php 23167 2010-10-19 17:53:31Z mabe $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_Service_WindowsAzure_Storage |
|
25 */ |
|
26 require_once 'Zend/Service/WindowsAzure/Storage.php'; |
|
27 |
|
28 /** |
|
29 * @see Zend_Service_WindowsAzure_Credentials_CredentialsAbstract |
|
30 */ |
|
31 require_once 'Zend/Service/WindowsAzure/Credentials/CredentialsAbstract.php'; |
|
32 |
|
33 /** |
|
34 * @see Zend_Service_WindowsAzure_Exception |
|
35 */ |
|
36 require_once 'Zend/Service/WindowsAzure/Exception.php'; |
|
37 |
|
38 /** |
|
39 * @see Zend_Service_WindowsAzure_Storage_Batch |
|
40 */ |
|
41 require_once 'Zend/Service/WindowsAzure/Storage/Batch.php'; |
|
42 |
|
43 /** |
|
44 * @see Zend_Http_Client |
|
45 */ |
|
46 require_once 'Zend/Http/Client.php'; |
|
47 |
|
48 /** |
|
49 * @see Zend_Http_Response |
|
50 */ |
|
51 require_once 'Zend/Http/Response.php'; |
|
52 |
|
53 /** |
|
54 * @category Zend |
|
55 * @package Zend_Service_WindowsAzure |
|
56 * @subpackage Storage |
|
57 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
58 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
59 */ |
|
60 abstract class Zend_Service_WindowsAzure_Storage_BatchStorageAbstract |
|
61 extends Zend_Service_WindowsAzure_Storage |
|
62 { |
|
63 /** |
|
64 * Current batch |
|
65 * |
|
66 * @var Zend_Service_WindowsAzure_Storage_Batch |
|
67 */ |
|
68 protected $_currentBatch = null; |
|
69 |
|
70 /** |
|
71 * Set current batch |
|
72 * |
|
73 * @param Zend_Service_WindowsAzure_Storage_Batch $batch Current batch |
|
74 * @throws Zend_Service_WindowsAzure_Exception |
|
75 */ |
|
76 public function setCurrentBatch(Zend_Service_WindowsAzure_Storage_Batch $batch = null) |
|
77 { |
|
78 if ($batch !== null && $this->isInBatch()) { |
|
79 throw new Zend_Service_WindowsAzure_Exception('Only one batch can be active at a time.'); |
|
80 } |
|
81 $this->_currentBatch = $batch; |
|
82 } |
|
83 |
|
84 /** |
|
85 * Get current batch |
|
86 * |
|
87 * @return Zend_Service_WindowsAzure_Storage_Batch |
|
88 */ |
|
89 public function getCurrentBatch() |
|
90 { |
|
91 return $this->_currentBatch; |
|
92 } |
|
93 |
|
94 /** |
|
95 * Is there a current batch? |
|
96 * |
|
97 * @return boolean |
|
98 */ |
|
99 public function isInBatch() |
|
100 { |
|
101 return $this->_currentBatch !== null; |
|
102 } |
|
103 |
|
104 /** |
|
105 * Starts a new batch operation set |
|
106 * |
|
107 * @return Zend_Service_WindowsAzure_Storage_Batch |
|
108 * @throws Zend_Service_WindowsAzure_Exception |
|
109 */ |
|
110 public function startBatch() |
|
111 { |
|
112 return new Zend_Service_WindowsAzure_Storage_Batch($this, $this->getBaseUrl()); |
|
113 } |
|
114 |
|
115 /** |
|
116 * Perform batch using Zend_Http_Client channel, combining all batch operations into one request |
|
117 * |
|
118 * @param array $operations Operations in batch |
|
119 * @param boolean $forTableStorage Is the request for table storage? |
|
120 * @param boolean $isSingleSelect Is the request a single select statement? |
|
121 * @param string $resourceType Resource type |
|
122 * @param string $requiredPermission Required permission |
|
123 * @return Zend_Http_Response |
|
124 */ |
|
125 public function performBatch($operations = array(), $forTableStorage = false, $isSingleSelect = false, $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN, $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ) |
|
126 { |
|
127 // Generate boundaries |
|
128 $batchBoundary = 'batch_' . md5(time() . microtime()); |
|
129 $changesetBoundary = 'changeset_' . md5(time() . microtime()); |
|
130 |
|
131 // Set headers |
|
132 $headers = array(); |
|
133 |
|
134 // Add version header |
|
135 $headers['x-ms-version'] = $this->_apiVersion; |
|
136 |
|
137 // Add dataservice headers |
|
138 $headers['DataServiceVersion'] = '1.0;NetFx'; |
|
139 $headers['MaxDataServiceVersion'] = '1.0;NetFx'; |
|
140 |
|
141 // Add content-type header |
|
142 $headers['Content-Type'] = 'multipart/mixed; boundary=' . $batchBoundary; |
|
143 |
|
144 // Set path and query string |
|
145 $path = '/$batch'; |
|
146 $queryString = ''; |
|
147 |
|
148 // Set verb |
|
149 $httpVerb = Zend_Http_Client::POST; |
|
150 |
|
151 // Generate raw data |
|
152 $rawData = ''; |
|
153 |
|
154 // Single select? |
|
155 if ($isSingleSelect) { |
|
156 $operation = $operations[0]; |
|
157 $rawData .= '--' . $batchBoundary . "\n"; |
|
158 $rawData .= 'Content-Type: application/http' . "\n"; |
|
159 $rawData .= 'Content-Transfer-Encoding: binary' . "\n\n"; |
|
160 $rawData .= $operation; |
|
161 $rawData .= '--' . $batchBoundary . '--'; |
|
162 } else { |
|
163 $rawData .= '--' . $batchBoundary . "\n"; |
|
164 $rawData .= 'Content-Type: multipart/mixed; boundary=' . $changesetBoundary . "\n\n"; |
|
165 |
|
166 // Add operations |
|
167 foreach ($operations as $operation) |
|
168 { |
|
169 $rawData .= '--' . $changesetBoundary . "\n"; |
|
170 $rawData .= 'Content-Type: application/http' . "\n"; |
|
171 $rawData .= 'Content-Transfer-Encoding: binary' . "\n\n"; |
|
172 $rawData .= $operation; |
|
173 } |
|
174 $rawData .= '--' . $changesetBoundary . '--' . "\n"; |
|
175 |
|
176 $rawData .= '--' . $batchBoundary . '--'; |
|
177 } |
|
178 |
|
179 // Generate URL and sign request |
|
180 $requestUrl = $this->_credentials->signRequestUrl($this->getBaseUrl() . $path . $queryString, $resourceType, $requiredPermission); |
|
181 $requestHeaders = $this->_credentials->signRequestHeaders($httpVerb, $path, $queryString, $headers, $forTableStorage, $resourceType, $requiredPermission); |
|
182 |
|
183 // Prepare request |
|
184 $this->_httpClientChannel->resetParameters(true); |
|
185 $this->_httpClientChannel->setUri($requestUrl); |
|
186 $this->_httpClientChannel->setHeaders($requestHeaders); |
|
187 $this->_httpClientChannel->setRawData($rawData); |
|
188 |
|
189 // Execute request |
|
190 $response = $this->_retryPolicy->execute( |
|
191 array($this->_httpClientChannel, 'request'), |
|
192 array($httpVerb) |
|
193 ); |
|
194 |
|
195 return $response; |
|
196 } |
|
197 } |