root / asi-http-request-with-pithos / Classes / Pithos / ASIPithosAccountRequest.m @ 7ac1bf11
History | View | Annotate | Download (12.8 kB)
1 |
// ASIPithosAccountRequest.m |
---|---|
2 |
// Based on ASICloudFilesContainerRequest.m |
3 |
// |
4 |
// Copyright 2011-2012 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 "ASIPithosAccountRequest.h" |
38 |
#import "ASIPithos.h" |
39 |
#import "ASIPithosAccount.h" |
40 |
#import "ASIPithosContainer.h" |
41 |
|
42 |
@implementation ASIPithosAccountRequest |
43 |
@synthesize currentContainer, currentDictionary, currentKey; |
44 |
|
45 |
#pragma mark - |
46 |
#pragma mark Constructors |
47 |
|
48 |
+ (id)storageRequestWithMethod:(NSString *)method pithos:(ASIPithos *)pithos queryString:(NSString *)queryString { |
49 |
NSString *urlString = [NSString stringWithString:pithos.storageURL]; |
50 |
if (queryString) |
51 |
urlString = [urlString stringByAppendingString:queryString]; |
52 |
|
53 |
ASIPithosAccountRequest *request = [[[self alloc] initWithURL:[NSURL URLWithString:urlString]] autorelease]; |
54 |
[request setRequestMethod:method]; |
55 |
[request addRequestHeader:@"X-Auth-Token" value:pithos.authToken]; |
56 |
return request; |
57 |
} |
58 |
|
59 |
+ (id)storageRequestWithMethod:(NSString *)method pithos:(ASIPithos *)pithos { |
60 |
return [self storageRequestWithMethod:method pithos:pithos queryString:nil]; |
61 |
} |
62 |
|
63 |
#pragma mark - |
64 |
#pragma mark Memory Management |
65 |
|
66 |
- (void)dealloc { |
67 |
[currentKey release]; |
68 |
[currentContainer release]; |
69 |
[containers release]; |
70 |
[metadata release]; |
71 |
[policy release]; |
72 |
[groups release]; |
73 |
[super dealloc]; |
74 |
} |
75 |
|
76 |
#pragma mark - |
77 |
#pragma mark HEAD |
78 |
|
79 |
// HEAD storageURL |
80 |
+ (id)accountMetadataRequestWithPithos:(ASIPithos *)pithos { |
81 |
return [self storageRequestWithMethod:@"HEAD" pithos:pithos]; |
82 |
} |
83 |
|
84 |
// HEAD storageURL?[until=untilTimestamp] |
85 |
+ (id)accountMetadataRequestWithPithos:(ASIPithos *)pithos until:(NSDate *)untilTimestamp { |
86 |
NSString *queryString = nil; |
87 |
if (untilTimestamp) |
88 |
queryString = [NSString stringWithFormat:@"?until=%d", (int)[untilTimestamp timeIntervalSince1970]]; |
89 |
return [self storageRequestWithMethod:@"HEAD" pithos:pithos queryString:queryString]; |
90 |
} |
91 |
|
92 |
// HEAD storageURL?[until=untilTimestamp] [If-Modified-Since] |
93 |
+ (id)accountMetadataRequestWithPithos:(ASIPithos *)pithos until:(NSDate *)untilTimestamp ifModifiedSince:(NSDate *)sinceTimestamp { |
94 |
ASIPithosAccountRequest *request = [self accountMetadataRequestWithPithos:pithos until:untilTimestamp]; |
95 |
[request addRequestIfModifiedSinceHeader:sinceTimestamp]; |
96 |
return request; |
97 |
} |
98 |
|
99 |
// HEAD storageURL?[until=untilTimestamp] [If-Unmodified-Since] |
100 |
+ (id)accountMetadataRequestWithPithos:(ASIPithos *)pithos until:(NSDate *)untilTimestamp ifUnmodifiedSince:(NSDate *)sinceTimestamp { |
101 |
ASIPithosAccountRequest *request = [self accountMetadataRequestWithPithos:pithos until:untilTimestamp]; |
102 |
[request addRequestIfUnmodifiedSinceHeader:sinceTimestamp]; |
103 |
return request; |
104 |
} |
105 |
|
106 |
- (NSUInteger)containerCount { |
107 |
return [[[self responseHeaders] objectForKey:@"X-Account-Container-Count"] integerValue]; |
108 |
} |
109 |
|
110 |
- (NSUInteger)bytesUsed { |
111 |
return [[[self responseHeaders] objectForKey:@"X-Account-Bytes-Used"] integerValue]; |
112 |
} |
113 |
|
114 |
- (NSDate *)untilTimestamp { |
115 |
NSString *untilTimestampString = [[self responseHeaders] objectForKey:@"X-Account-Until-Timestamp"]; |
116 |
if (untilTimestampString) |
117 |
return [[self dateFormatterWithFormatId:1] dateFromString:untilTimestampString]; |
118 |
return nil; |
119 |
} |
120 |
|
121 |
- (NSDictionary *)groups { |
122 |
if (groups == nil) { |
123 |
groups = [self getHeadersDictionaryForPrefix:@"X-Account-Group-"]; |
124 |
[groups retain]; |
125 |
} |
126 |
return groups; |
127 |
} |
128 |
|
129 |
- (NSDictionary *)policy { |
130 |
if (policy == nil) { |
131 |
policy = [self getHeadersDictionaryForPrefix:@"X-Account-Policy-"]; |
132 |
[policy retain]; |
133 |
} |
134 |
return policy; |
135 |
} |
136 |
|
137 |
- (NSDictionary *)metadata { |
138 |
if (metadata == nil) { |
139 |
metadata = [self getHeadersDictionaryForPrefix:@"X-Account-Meta-"]; |
140 |
[metadata retain]; |
141 |
} |
142 |
return metadata; |
143 |
} |
144 |
|
145 |
- (NSDate *)lastModified { |
146 |
NSString *lastModifiedString = [[self responseHeaders] objectForKey:@"Last-Modified"]; |
147 |
if (lastModifiedString) |
148 |
return [[self dateFormatterWithFormatId:1] dateFromString:lastModifiedString]; |
149 |
return nil; |
150 |
} |
151 |
|
152 |
- (ASIPithosAccount *)account { |
153 |
ASIPithosAccount *account = [ASIPithosAccount account]; |
154 |
account.containerCount = [self containerCount]; |
155 |
account.bytesUsed = [self bytesUsed]; |
156 |
account.untilTimestamp = [self untilTimestamp]; |
157 |
account.groups = (NSMutableDictionary *)[self groups]; |
158 |
account.policy = (NSMutableDictionary *)[self policy]; |
159 |
account.metadata = (NSMutableDictionary *)[self metadata]; |
160 |
account.lastModified = [self lastModified]; |
161 |
return account; |
162 |
} |
163 |
|
164 |
|
165 |
#pragma mark - |
166 |
#pragma mark GET |
167 |
|
168 |
// GET storageURL |
169 |
+ (id)listContainersRequestWithPithos:(ASIPithos *)pithos { |
170 |
return [self storageRequestWithMethod:@"GET" pithos:pithos queryString:@"?format=xml"]; |
171 |
} |
172 |
|
173 |
// GET storageURL?[limit=limit]&[marker=marker]&[shared=]&[until=untilTimestamp] |
174 |
+ (id)listContainersRequestWithPithos:(ASIPithos *)pithos |
175 |
limit:(NSUInteger)limit |
176 |
marker:(NSString *)marker |
177 |
shared:(BOOL)shared |
178 |
until:(NSDate *)untilTimestamp { |
179 |
NSString *queryString = @"?format=xml"; |
180 |
if (limit && (limit > 0)) |
181 |
queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&limit=%lu", limit]]; |
182 |
if (marker) |
183 |
queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&marker=%@", [self encodeToPercentEscape:marker]]]; |
184 |
if (shared) |
185 |
queryString = [queryString stringByAppendingString:@"&shared="]; |
186 |
if (untilTimestamp) |
187 |
queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&until=%d", (int)[untilTimestamp timeIntervalSince1970]]]; |
188 |
|
189 |
return [self storageRequestWithMethod:@"GET" pithos:pithos queryString:queryString]; |
190 |
} |
191 |
|
192 |
// GET storageURL?[limit=limit]&[marker=marker]&[shared=]&[until=untilTimestamp] [If-Modified-Since] |
193 |
+ (id)listContainersRequestWithPithos:(ASIPithos *)pithos |
194 |
limit:(NSUInteger)limit |
195 |
marker:(NSString *)marker |
196 |
shared:(BOOL)shared |
197 |
until:(NSDate *)untilTimestamp |
198 |
ifModifiedSince:(NSDate *)sinceTimestamp { |
199 |
ASIPithosAccountRequest *request = [self listContainersRequestWithPithos:pithos limit:limit marker:marker shared:shared until:untilTimestamp]; |
200 |
[request addRequestIfModifiedSinceHeader:sinceTimestamp]; |
201 |
return request; |
202 |
} |
203 |
|
204 |
// GET storageURL?[limit=limit]&[marker=marker]&[shared=]&[until=untilTimestamp] [If-Unmodified-Since] |
205 |
+ (id)listContainersRequestWithPithos:(ASIPithos *)pithos |
206 |
limit:(NSUInteger)limit |
207 |
marker:(NSString *)marker |
208 |
shared:(BOOL)shared |
209 |
until:(NSDate *)untilTimestamp |
210 |
ifUnmodifiedSince:(NSDate *)sinceTimestamp { |
211 |
ASIPithosAccountRequest *request = [self listContainersRequestWithPithos:pithos limit:limit marker:marker shared:shared until:untilTimestamp]; |
212 |
[request addRequestIfUnmodifiedSinceHeader:sinceTimestamp]; |
213 |
return request; |
214 |
} |
215 |
|
216 |
|
217 |
- (NSArray *)containers { |
218 |
if (containers == nil) { |
219 |
containers = [[NSMutableArray alloc] init]; |
220 |
|
221 |
NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:[self responseData]] autorelease]; |
222 |
[parser setDelegate:self]; |
223 |
[parser setShouldProcessNamespaces:NO]; |
224 |
[parser setShouldReportNamespacePrefixes:NO]; |
225 |
[parser setShouldResolveExternalEntities:NO]; |
226 |
[parser parse]; |
227 |
} |
228 |
return containers; |
229 |
} |
230 |
|
231 |
#pragma mark - |
232 |
#pragma mark POST |
233 |
|
234 |
|
235 |
// POST storageURL |
236 |
+ (id)updateAccountMetadataRequestWithPithos:(ASIPithos *)pithos { |
237 |
return [self storageRequestWithMethod:@"POST" pithos:pithos]; |
238 |
} |
239 |
|
240 |
// POST storageURL?[update=] [X-Account-Group-*] [X-Account-Meta-*] |
241 |
+ (id)updateAccountMetadataRequestWithPithos:(ASIPithos *)pithos |
242 |
groups:(NSDictionary *)groups |
243 |
metadata:(NSDictionary *)metadata |
244 |
update:(BOOL)update { |
245 |
NSString *queryString = nil; |
246 |
if (update) |
247 |
queryString = @"?update="; |
248 |
ASIPithosAccountRequest *request = [self storageRequestWithMethod:@"POST" pithos:pithos queryString:queryString]; |
249 |
|
250 |
if (groups) { |
251 |
for (NSString *key in [groups keyEnumerator]) { |
252 |
[request addRequestHeader:[self encodeToPercentEscape:[NSString stringWithFormat:@"X-Account-Group-%@", key]] |
253 |
value:[self encodeToPercentEscape:[groups objectForKey:key]]]; |
254 |
} |
255 |
} |
256 |
|
257 |
if (metadata) { |
258 |
for (NSString *key in [metadata keyEnumerator]) { |
259 |
[request addRequestHeader:[self encodeToPercentEscape:[NSString stringWithFormat:@"X-Account-Meta-%@", key]] |
260 |
value:[self encodeToPercentEscape:[metadata objectForKey:key]]]; |
261 |
} |
262 |
} |
263 |
|
264 |
return request; |
265 |
} |
266 |
|
267 |
#pragma mark - |
268 |
#pragma mark NSXMLParserDelegate |
269 |
|
270 |
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { |
271 |
self.currentElement = elementName; |
272 |
if ([elementName isEqualToString:@"container"]) { |
273 |
self.currentContainer = [ASIPithosContainer container]; |
274 |
} else if ([elementName isEqualToString:@"x_container_policy"]) { |
275 |
if (currentContainer.policy == nil) |
276 |
currentContainer.policy = [NSMutableDictionary dictionary]; |
277 |
self.currentDictionary = currentContainer.policy; |
278 |
} |
279 |
self.currentContent = @""; |
280 |
} |
281 |
|
282 |
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { |
283 |
if ([elementName isEqualToString:@"name"]) { |
284 |
currentContainer.name = currentContent; |
285 |
} else if ([elementName isEqualToString:@"count"]) { |
286 |
currentContainer.count = [currentContent integerValue]; |
287 |
} else if ([elementName isEqualToString:@"bytes"]) { |
288 |
currentContainer.bytes = [currentContent integerValue]; |
289 |
} else if ([elementName isEqualToString:@"last_modified"]) { |
290 |
currentContainer.lastModified = [[self dateFormatterWithFormatId:0] dateFromString: |
291 |
[currentContent stringByReplacingOccurrencesOfString:@":" |
292 |
withString:@"" |
293 |
options:NSBackwardsSearch |
294 |
range:NSMakeRange(([currentContent length] - 3), 1)]]; |
295 |
} else if ([elementName isEqualToString:@"x_container_until_timestamp"]) { |
296 |
currentContainer.untilTimestamp = [NSDate dateWithTimeIntervalSince1970:[currentContent doubleValue]]; |
297 |
} else if ([elementName isEqualToString:@"key"]) { |
298 |
self.currentKey = currentContent; |
299 |
} else if ([elementName isEqualToString:@"value"]) { |
300 |
[currentDictionary setObject:currentContent forKey:currentKey]; |
301 |
self.currentKey = nil; |
302 |
} else if ([elementName isEqualToString:@"x_container_policy"]) { |
303 |
self.currentDictionary = nil; |
304 |
} else if ([elementName isEqualToString:@"container"]) { |
305 |
[containers addObject:currentContainer]; |
306 |
self.currentContainer = nil; |
307 |
} |
308 |
} |
309 |
|
310 |
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { |
311 |
self.currentContent = [currentContent stringByAppendingString:string]; |
312 |
} |
313 |
|
314 |
@end |