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