root / asi-http-request-with-pithos / Classes / Pithos / ASIPithosContainerRequest.m @ be116d22
History | View | Annotate | Download (18.7 kB)
1 |
// ASIPithosContainerRequest.m |
---|---|
2 |
// Based on ASICloudFilesContainerRequest.m |
3 |
// |
4 |
// Copyright 2011 GRNET S.A. All rights reserved. |
5 |
// |
6 |
// Redistribution and use in source and binary forms, with or |
7 |
// without modification, are permitted provided that the following |
8 |
// conditions are met: |
9 |
// |
10 |
// 1. Redistributions of source code must retain the above |
11 |
// copyright notice, this list of conditions and the following |
12 |
// disclaimer. |
13 |
// |
14 |
// 2. Redistributions in binary form must reproduce the above |
15 |
// copyright notice, this list of conditions and the following |
16 |
// disclaimer in the documentation and/or other materials |
17 |
// provided with the distribution. |
18 |
// |
19 |
// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
20 |
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
21 |
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
22 |
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
23 |
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
26 |
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
27 |
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
28 |
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
29 |
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
30 |
// POSSIBILITY OF SUCH DAMAGE. |
31 |
// |
32 |
// The views and conclusions contained in the software and |
33 |
// documentation are those of the authors and should not be |
34 |
// interpreted as representing official policies, either expressed |
35 |
// or implied, of GRNET S.A. |
36 |
|
37 |
#import "ASIPithosContainerRequest.h" |
38 |
#import "ASIPithosContainer.h" |
39 |
#import "ASIPithosObject.h" |
40 |
|
41 |
@implementation ASIPithosContainerRequest |
42 |
@synthesize containerName; |
43 |
@synthesize currentElement, currentContent, currentObject; |
44 |
|
45 |
#pragma mark - |
46 |
#pragma mark Constructors |
47 |
|
48 |
+ (id)storageRequestWithMethod:(NSString *)method containerName:(NSString *)containerName queryString:(NSString *)queryString { |
49 |
NSString *urlString = [NSString stringWithFormat:@"%@/%@", [self storageURL], containerName]; |
50 |
if (queryString) |
51 |
urlString = [urlString stringByAppendingString:queryString]; |
52 |
|
53 |
ASIPithosContainerRequest *request = [[[self alloc] initWithURL:[NSURL URLWithString:urlString]] autorelease]; |
54 |
[request setRequestMethod:method]; |
55 |
[request addRequestHeader:@"X-Auth-Token" value:[self authToken]]; |
56 |
request.containerName = containerName; |
57 |
return request; |
58 |
} |
59 |
|
60 |
+ (id)storageRequestWithMethod:(NSString *)method containerName:(NSString *)containerName { |
61 |
return [self storageRequestWithMethod:method containerName:containerName queryString:nil]; |
62 |
} |
63 |
|
64 |
#pragma mark - |
65 |
#pragma mark Memory Management |
66 |
|
67 |
- (void)dealloc { |
68 |
[currentObject release]; |
69 |
[currentContent release]; |
70 |
[currentElement release]; |
71 |
[objects release]; |
72 |
[metadata release]; |
73 |
[policy release]; |
74 |
[containerName release]; |
75 |
[super dealloc]; |
76 |
} |
77 |
|
78 |
#pragma mark - |
79 |
#pragma mark HEAD |
80 |
|
81 |
// HEAD storageURL/container |
82 |
+ (id)containerMetadataRequestWithContainerName:(NSString *)containerName { |
83 |
return [self storageRequestWithMethod:@"HEAD" containerName:containerName]; |
84 |
} |
85 |
|
86 |
// HEAD storageURL/container?[until=untilTimestamp] |
87 |
+ (id)containerMetadataRequestWithContainerName:(NSString *)containerName until:(NSDate *)untilTimestamp { |
88 |
NSString *queryString = nil; |
89 |
if (untilTimestamp) |
90 |
queryString = [NSString stringWithFormat:@"?until=%d", (int)[untilTimestamp timeIntervalSince1970]]; |
91 |
|
92 |
return [self storageRequestWithMethod:@"HEAD" containerName:containerName queryString:queryString]; |
93 |
} |
94 |
|
95 |
// HEAD storageURL/container?[until=untilTimestamp] [If-Modified-Since] |
96 |
+ (id)containerMetadataRequestWithContainerName:(NSString *)containerName |
97 |
until:(NSDate *)untilTimestamp |
98 |
ifModifiedSince:(NSDate *)sinceTimestamp { |
99 |
ASIPithosContainerRequest *request = [self containerMetadataRequestWithContainerName:containerName until:untilTimestamp]; |
100 |
[request addRequestIfModifiedSinceHeader:sinceTimestamp]; |
101 |
return request; |
102 |
} |
103 |
|
104 |
// HEAD storageURL/container?[until=untilTimestamp] [If-Unmodified-Since] |
105 |
+ (id)containerMetadataRequestWithContainerName:(NSString *)containerName |
106 |
until:(NSDate *)untilTimestamp |
107 |
ifUnmodifiedSince:(NSDate *)sinceTimestamp { |
108 |
ASIPithosContainerRequest *request = [self containerMetadataRequestWithContainerName:containerName until:untilTimestamp]; |
109 |
[request addRequestIfUnmodifiedSinceHeader:sinceTimestamp]; |
110 |
return request; |
111 |
} |
112 |
|
113 |
- (NSUInteger)objectCount { |
114 |
return [[[self responseHeaders] objectForKey:@"X-Container-Object-Count"] intValue]; |
115 |
} |
116 |
|
117 |
- (NSUInteger)bytesUsed { |
118 |
return [[[self responseHeaders] objectForKey:@"X-Container-Bytes-Used"] intValue]; |
119 |
} |
120 |
|
121 |
- (NSUInteger)blockSize { |
122 |
return [[[self responseHeaders] objectForKey:@"X-Container-Block-Size"] intValue]; |
123 |
} |
124 |
|
125 |
- (NSString *)blockHash { |
126 |
return [[self responseHeaders] objectForKey:@"X-Container-Block-Hash"]; |
127 |
} |
128 |
|
129 |
- (NSArray *)objectMeta { |
130 |
return [[[self responseHeaders] objectForKey:@"X-Container-Object-Meta"] componentsSeparatedByString:@","]; |
131 |
} |
132 |
|
133 |
- (NSDate *)untilTimestamp { |
134 |
NSString *untilTimestampString = [[self responseHeaders] objectForKey:@"X-Container-Until-Timestamp"]; |
135 |
if (untilTimestampString) |
136 |
return [[self dateFormatterWithFormatId:1] dateFromString:untilTimestampString]; |
137 |
return nil; |
138 |
} |
139 |
|
140 |
- (NSDictionary *)policy { |
141 |
if (policy == nil) { |
142 |
policy = [self getHeadersDictionaryForPrefix:@"X-Container-Policy-"]; |
143 |
[policy retain]; |
144 |
} |
145 |
return policy; |
146 |
} |
147 |
|
148 |
- (NSDictionary *)metadata { |
149 |
if (metadata == nil) { |
150 |
metadata = [self getHeadersDictionaryForPrefix:@"X-Container-Meta-"]; |
151 |
[metadata retain]; |
152 |
} |
153 |
return metadata; |
154 |
} |
155 |
|
156 |
- (NSDate *)lastModified { |
157 |
NSString *lastModifiedString = [[self responseHeaders] objectForKey:@"Last-Modified"]; |
158 |
if (lastModifiedString) |
159 |
return [[self dateFormatterWithFormatId:1] dateFromString:lastModifiedString]; |
160 |
return nil; |
161 |
} |
162 |
|
163 |
- (ASIPithosContainer *)container { |
164 |
ASIPithosContainer *container = [ASIPithosContainer container]; |
165 |
container.name = containerName; |
166 |
container.count = [self objectCount]; |
167 |
container.bytes = [self bytesUsed]; |
168 |
container.lastModified = [self lastModified]; |
169 |
container.untilTimestamp = [self untilTimestamp]; |
170 |
container.policy = (NSMutableDictionary *)[self policy]; |
171 |
container.metadata = (NSMutableDictionary *)[self metadata]; |
172 |
container.blockSize = [self blockSize]; |
173 |
container.blockHash = [self blockHash]; |
174 |
container.objectMeta = (NSMutableArray *)[self objectMeta]; |
175 |
[self objectMeta]; |
176 |
return container; |
177 |
} |
178 |
|
179 |
#pragma mark - |
180 |
#pragma mark GET |
181 |
|
182 |
// GET storageURL/container |
183 |
+ (id)listObjectsRequestWithContainerName:(NSString *)containerName { |
184 |
return [self storageRequestWithMethod:@"GET" containerName:containerName queryString:@"?format=xml"]; |
185 |
} |
186 |
|
187 |
// GET storageURL?[limit=limit]&[marker=marker]&[prefix=prefix]&[delimiter=delimiter]&[path=path]&[meta=meta]&[shared=]&[until=untilTimestamp] |
188 |
+ (id)listObjectsRequestWithContainerName:(NSString *)containerName |
189 |
limit:(NSUInteger)limit |
190 |
marker:(NSString *)marker |
191 |
prefix:(NSString *)prefix |
192 |
delimiter:(NSString *)delimiter |
193 |
path:(NSString *)path |
194 |
meta:(NSArray *)meta |
195 |
shared:(BOOL)shared |
196 |
until:(NSDate *)untilTimestamp { |
197 |
NSString *queryString = @"?format=xml"; |
198 |
if (limit && (limit > 0)) |
199 |
queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&limit=%lu", limit]]; |
200 |
if (marker) |
201 |
queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&marker=%@", [marker stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; |
202 |
if (prefix) |
203 |
queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&prefix=%@", [prefix stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; |
204 |
if (delimiter) |
205 |
queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&delimiter=%@", [delimiter stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; |
206 |
if (path) |
207 |
queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&path=%@", [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; |
208 |
if (meta) |
209 |
queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&meta=%@", [[meta componentsJoinedByString:@","] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; |
210 |
if (shared) |
211 |
queryString = [queryString stringByAppendingString:@"&shared="]; |
212 |
if (untilTimestamp) |
213 |
queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&until=%d", (int)[untilTimestamp timeIntervalSince1970]]]; |
214 |
|
215 |
return [self storageRequestWithMethod:@"GET" containerName:containerName queryString:queryString]; |
216 |
} |
217 |
|
218 |
// GET storageURL?[limit=limit]&[marker=marker]&[prefix=prefix]&[delimiter=delimiter]&[path=path]&[meta=meta]&[shared=]&[until=untilTimestamp] [If-Modified-Since] |
219 |
+ (id)listObjectsRequestWithContainerName:(NSString *)containerName |
220 |
limit:(NSUInteger)limit |
221 |
marker:(NSString *)marker |
222 |
prefix:(NSString *)prefix |
223 |
delimiter:(NSString *)delimiter |
224 |
path:(NSString *)path |
225 |
meta:(NSArray *)meta |
226 |
shared:(BOOL)shared |
227 |
until:(NSDate *)untilTimestamp |
228 |
ifModifiedSince:(NSDate *)sinceTimestamp { |
229 |
ASIPithosContainerRequest *request = [self listObjectsRequestWithContainerName:containerName |
230 |
limit:limit |
231 |
marker:marker |
232 |
prefix:prefix |
233 |
delimiter:delimiter |
234 |
path:path |
235 |
meta:meta |
236 |
shared:shared |
237 |
until:untilTimestamp]; |
238 |
[request addRequestIfModifiedSinceHeader:sinceTimestamp]; |
239 |
return request; |
240 |
} |
241 |
|
242 |
// GET storageURL?[limit=limit]&[marker=marker]&[prefix=prefix]&[delimiter=delimiter]&[path=path]&[meta=meta]&[shared=]&[until=untilTimestamp] [If-Unmodified-Since] |
243 |
+ (id)listObjectsRequestWithContainerName:(NSString *)containerName |
244 |
limit:(NSUInteger)limit |
245 |
marker:(NSString *)marker |
246 |
prefix:(NSString *)prefix |
247 |
delimiter:(NSString *)delimiter |
248 |
path:(NSString *)path |
249 |
meta:(NSArray *)meta |
250 |
shared:(BOOL)shared |
251 |
until:(NSDate *)untilTimestamp |
252 |
ifUnmodifiedSince:(NSDate *)sinceTimestamp { |
253 |
ASIPithosContainerRequest *request = [self listObjectsRequestWithContainerName:containerName |
254 |
limit:limit |
255 |
marker:marker |
256 |
prefix:prefix |
257 |
delimiter:delimiter |
258 |
path:path |
259 |
meta:meta |
260 |
shared:shared |
261 |
until:untilTimestamp]; |
262 |
[request addRequestIfUnmodifiedSinceHeader:sinceTimestamp]; |
263 |
return request; |
264 |
} |
265 |
|
266 |
|
267 |
- (NSArray *)objects { |
268 |
if (objects == nil) { |
269 |
objects = [[NSMutableArray alloc] init]; |
270 |
|
271 |
NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:[self responseData]] autorelease]; |
272 |
[parser setDelegate:self]; |
273 |
[parser setShouldProcessNamespaces:NO]; |
274 |
[parser setShouldReportNamespacePrefixes:NO]; |
275 |
[parser setShouldResolveExternalEntities:NO]; |
276 |
[parser parse]; |
277 |
} |
278 |
return objects; |
279 |
} |
280 |
|
281 |
#pragma mark - |
282 |
#pragma mark PUT |
283 |
|
284 |
// PUT storageURL/container |
285 |
+ (id)createOrUpdateContainerRequestWithContainerName:(NSString *)containerName { |
286 |
return [self storageRequestWithMethod:@"PUT" containerName:containerName]; |
287 |
} |
288 |
|
289 |
+ (id)createOrUpdateContainerRequestWithContainerName:(NSString *)containerName policy:(NSDictionary *)policy metadata:(NSDictionary *)metadata { |
290 |
ASIPithosContainerRequest *request = [self storageRequestWithMethod:@"PUT" containerName:containerName]; |
291 |
|
292 |
if (policy) { |
293 |
for (NSString *key in [policy keyEnumerator]) { |
294 |
[request addRequestHeader:[NSString stringWithFormat:@"X-Container-Policy-%@", key] value:[policy objectForKey:key]]; |
295 |
} |
296 |
} |
297 |
|
298 |
if (metadata) { |
299 |
for (NSString *key in [metadata keyEnumerator]) { |
300 |
[request addRequestHeader:[NSString stringWithFormat:@"X-Container-Meta-%@", key] value:[metadata objectForKey:key]]; |
301 |
} |
302 |
} |
303 |
|
304 |
return request; |
305 |
} |
306 |
|
307 |
#pragma mark - |
308 |
#pragma mark POST |
309 |
|
310 |
// POST storageURL/container |
311 |
+ (id)updateContainerMetadataRequestWithContainerName:(NSString *)containerName { |
312 |
return [self storageRequestWithMethod:@"POST" containerName:containerName]; |
313 |
} |
314 |
|
315 |
// POST storageURL/container?[update=] |
316 |
+ (id)updateContainerMetadataRequestWithContainerName:(NSString *)containerName |
317 |
policy:(NSDictionary *)policy |
318 |
metadata:(NSDictionary *)metadata |
319 |
update:(BOOL)update { |
320 |
NSString *queryString = nil; |
321 |
if (update) |
322 |
queryString = @"?update="; |
323 |
ASIPithosContainerRequest *request = [self storageRequestWithMethod:@"POST" |
324 |
containerName:containerName |
325 |
queryString:queryString]; |
326 |
if (policy) { |
327 |
for (NSString *key in [policy keyEnumerator]) { |
328 |
[request addRequestHeader:[NSString stringWithFormat:@"X-Container-Policy-%@", key] value:[policy objectForKey:key]]; |
329 |
} |
330 |
} |
331 |
|
332 |
if (metadata) { |
333 |
for (NSString *key in [metadata keyEnumerator]) { |
334 |
[request addRequestHeader:[NSString stringWithFormat:@"X-Container-Meta-%@", key] value:[metadata objectForKey:key]]; |
335 |
} |
336 |
} |
337 |
|
338 |
return request; |
339 |
} |
340 |
|
341 |
#pragma mark - |
342 |
#pragma mark DELETE |
343 |
|
344 |
// DELETE storageURL/container |
345 |
+ (id)deleteContainerRequestWithContainerName:(NSString *)containerName { |
346 |
return [self storageRequestWithMethod:@"DELETE" containerName:containerName]; |
347 |
} |
348 |
|
349 |
// DELETE storageURL/container?[until=untilTimestamp] |
350 |
+ (id)deleteContainerRequestWithContainerName:(NSString *)containerName until:(NSDate *)untilTimestamp { |
351 |
NSString *queryString = nil; |
352 |
if (untilTimestamp) |
353 |
queryString = [NSString stringWithFormat:@"?until=%d", (int)[untilTimestamp timeIntervalSince1970]]; |
354 |
return [self storageRequestWithMethod:@"DELETE" containerName:containerName queryString:queryString]; |
355 |
} |
356 |
|
357 |
#pragma mark - |
358 |
#pragma mark NSXMLParserDelegate |
359 |
|
360 |
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { |
361 |
self.currentElement = elementName; |
362 |
if ([elementName isEqualToString:@"object"]) { |
363 |
self.currentObject = [ASIPithosObject object]; |
364 |
} else if ([elementName isEqualToString:@"subdir"]) { |
365 |
[objects addObject:[ASIPithosObject subdirWithName:[attributeDict valueForKey:@"name"]]]; |
366 |
} |
367 |
|
368 |
self.currentContent = @""; |
369 |
} |
370 |
|
371 |
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { |
372 |
if ([elementName isEqualToString:@"name"]) { |
373 |
currentObject.name = currentContent; |
374 |
} else if ([elementName isEqualToString:@"hash"]) { |
375 |
currentObject.hash = currentContent; |
376 |
} else if ([elementName isEqualToString:@"bytes"]) { |
377 |
currentObject.bytes = [currentContent intValue]; |
378 |
} else if ([elementName isEqualToString:@"content_type"]) { |
379 |
currentObject.contentType = currentContent; |
380 |
} else if ([elementName isEqualToString:@"content_encoding"]) { |
381 |
currentObject.contentEncoding = currentContent; |
382 |
} else if ([elementName isEqualToString:@"content_disposition"]) { |
383 |
currentObject.contentDisposition = currentContent; |
384 |
} else if ([elementName isEqualToString:@"last_modified"]) { |
385 |
currentObject.lastModified = [[self dateFormatterWithFormatId:0] dateFromString:currentContent]; |
386 |
} else if ([elementName isEqualToString:@"x_object_version"]) { |
387 |
currentObject.version = currentContent; |
388 |
} else if ([elementName isEqualToString:@"x_object_version_timestamp"]) { |
389 |
currentObject.versionTimestamp = [NSDate dateWithTimeIntervalSince1970:[currentContent intValue]]; |
390 |
} else if ([elementName isEqualToString:@"x_object_modified_by"]) { |
391 |
currentObject.modifiedBy = currentContent; |
392 |
} else if ([elementName isEqualToString:@"x_object_manifest"]) { |
393 |
currentObject.manifest = currentContent; |
394 |
} else if ([elementName isEqualToString:@"x_object_sharing"]) { |
395 |
currentObject.sharing = currentContent; |
396 |
} else if ([elementName isEqualToString:@"x_object_shared_by"]) { |
397 |
currentObject.sharedBy = currentContent; |
398 |
} else if ([elementName isEqualToString:@"x_object_public"]) { |
399 |
currentObject.publicURI = currentContent; |
400 |
} else if ([elementName rangeOfString:@"x_object_meta_"].location == 0) { |
401 |
if (currentObject.metadata == nil) |
402 |
currentObject.metadata = [NSMutableDictionary dictionary]; |
403 |
[currentObject.metadata setObject:currentContent forKey:[elementName substringFromIndex:14]]; |
404 |
} else if ([elementName isEqualToString:@"object"]) { |
405 |
[objects addObject:currentObject]; |
406 |
self.currentObject = nil; |
407 |
} |
408 |
} |
409 |
|
410 |
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { |
411 |
self.currentContent = [currentContent stringByAppendingString:string]; |
412 |
} |
413 |
|
414 |
@end |