root / asi-http-request-with-pithos / Classes / Pithos / ASIPithosAccountRequest.m @ be116d22
History | View | Annotate | Download (10.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 "ASIPithosContainer.h" |
39 |
|
40 |
@implementation ASIPithosAccountRequest |
41 |
@synthesize currentElement, currentContent, currentContainer; |
42 |
|
43 |
#pragma mark - |
44 |
#pragma mark Constructors |
45 |
|
46 |
+ (id)storageRequestWithMethod:(NSString *)method queryString:(NSString *)queryString { |
47 |
NSString *urlString = [NSString stringWithString:[self storageURL]]; |
48 |
if (queryString) |
49 |
urlString = [urlString stringByAppendingString:queryString]; |
50 |
|
51 |
ASIPithosAccountRequest *request = [[[self alloc] initWithURL:[NSURL URLWithString:urlString]] autorelease]; |
52 |
[request setRequestMethod:method]; |
53 |
[request addRequestHeader:@"X-Auth-Token" value:[self authToken]]; |
54 |
return request; |
55 |
} |
56 |
|
57 |
+ (id)storageRequestWithMethod:(NSString *)method { |
58 |
return [self storageRequestWithMethod:method queryString:nil]; |
59 |
} |
60 |
|
61 |
#pragma mark - |
62 |
#pragma mark Memory Management |
63 |
|
64 |
- (void)dealloc { |
65 |
[currentContainer release]; |
66 |
[currentContent release]; |
67 |
[currentElement release]; |
68 |
[containers release]; |
69 |
[metadata 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 *)metadata { |
128 |
if (metadata == nil) { |
129 |
metadata = [self getHeadersDictionaryForPrefix:@"X-Account-Meta-"]; |
130 |
[metadata retain]; |
131 |
} |
132 |
return metadata; |
133 |
} |
134 |
|
135 |
- (NSDate *)lastModified { |
136 |
NSString *lastModifiedString = [[self responseHeaders] objectForKey:@"Last-Modified"]; |
137 |
if (lastModifiedString) |
138 |
return [[self dateFormatterWithFormatId:1] dateFromString:lastModifiedString]; |
139 |
return nil; |
140 |
} |
141 |
|
142 |
#pragma mark - |
143 |
#pragma mark GET |
144 |
|
145 |
// GET storageURL |
146 |
+ (id)listContainersRequest { |
147 |
return [self storageRequestWithMethod:@"GET" queryString:@"?format=xml"]; |
148 |
} |
149 |
|
150 |
// GET storageURL?[limit=limit]&[marker=marker]&[shared=]&[until=untilTimestamp] |
151 |
+ (id)listContainersRequestWithLimit:(NSUInteger)limit |
152 |
marker:(NSString *)marker |
153 |
shared:(BOOL)shared |
154 |
until:(NSDate *)untilTimestamp { |
155 |
NSString *queryString = @"?format=xml"; |
156 |
if (limit && (limit > 0)) |
157 |
queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&limit=%lu", limit]]; |
158 |
if (marker) |
159 |
queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&marker=%@", [marker stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; |
160 |
if (shared) |
161 |
queryString = [queryString stringByAppendingString:@"&shared="]; |
162 |
if (untilTimestamp) |
163 |
queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&until=%d", (int)[untilTimestamp timeIntervalSince1970]]]; |
164 |
|
165 |
return [self storageRequestWithMethod:@"GET" queryString:queryString]; |
166 |
} |
167 |
|
168 |
// GET storageURL?[limit=limit]&[marker=marker]&[shared=]&[until=untilTimestamp] [If-Modified-Since] |
169 |
+ (id)listContainersRequestWithLimit:(NSUInteger)limit |
170 |
marker:(NSString *)marker |
171 |
shared:(BOOL)shared |
172 |
until:(NSDate *)untilTimestamp |
173 |
ifModifiedSince:(NSDate *)sinceTimestamp { |
174 |
ASIPithosAccountRequest *request = [self listContainersRequestWithLimit:limit marker:marker shared:shared until:untilTimestamp]; |
175 |
[request addRequestIfModifiedSinceHeader:sinceTimestamp]; |
176 |
return request; |
177 |
} |
178 |
|
179 |
// GET storageURL?[limit=limit]&[marker=marker]&[shared=]&[until=untilTimestamp] [If-Unmodified-Since] |
180 |
+ (id)listContainersRequestWithLimit:(NSUInteger)limit |
181 |
marker:(NSString *)marker |
182 |
shared:(BOOL)shared |
183 |
until:(NSDate *)untilTimestamp |
184 |
ifUnmodifiedSince:(NSDate *)sinceTimestamp { |
185 |
ASIPithosAccountRequest *request = [self listContainersRequestWithLimit:limit marker:marker shared:shared until:untilTimestamp]; |
186 |
[request addRequestIfUnmodifiedSinceHeader:sinceTimestamp]; |
187 |
return request; |
188 |
} |
189 |
|
190 |
|
191 |
- (NSArray *)containers { |
192 |
if (containers == nil) { |
193 |
containers = [[NSMutableArray alloc] init]; |
194 |
|
195 |
NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:[self responseData]] autorelease]; |
196 |
[parser setDelegate:self]; |
197 |
[parser setShouldProcessNamespaces:NO]; |
198 |
[parser setShouldReportNamespacePrefixes:NO]; |
199 |
[parser setShouldResolveExternalEntities:NO]; |
200 |
[parser parse]; |
201 |
} |
202 |
return containers; |
203 |
} |
204 |
|
205 |
#pragma mark - |
206 |
#pragma mark POST |
207 |
|
208 |
|
209 |
// POST storageURL |
210 |
+ (id)updateAccountMetadataRequest { |
211 |
return [self storageRequestWithMethod:@"POST"]; |
212 |
} |
213 |
|
214 |
// POST storageURL?[update=] [X-Account-Group-*] [X-Account-Meta-*] |
215 |
+ (id)updateAccountMetadataRequestWithGroups:(NSDictionary *)groups metadata:(NSDictionary *)metadata update:(BOOL)update { |
216 |
NSString *queryString = nil; |
217 |
if (update) |
218 |
queryString = @"?update="; |
219 |
ASIPithosAccountRequest *request = [self storageRequestWithMethod:@"POST" queryString:queryString]; |
220 |
|
221 |
if (groups) { |
222 |
for (NSString *key in [groups keyEnumerator]) { |
223 |
[request addRequestHeader:[NSString stringWithFormat:@"X-Account-Group-%@", key] value:[groups objectForKey:key]]; |
224 |
} |
225 |
} |
226 |
|
227 |
if (metadata) { |
228 |
for (NSString *key in [metadata keyEnumerator]) { |
229 |
[request addRequestHeader:[NSString stringWithFormat:@"X-Account-Meta-%@", key] value:[metadata objectForKey:key]]; |
230 |
} |
231 |
} |
232 |
|
233 |
return request; |
234 |
} |
235 |
|
236 |
#pragma mark - |
237 |
#pragma mark NSXMLParserDelegate |
238 |
|
239 |
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { |
240 |
self.currentElement = elementName; |
241 |
if ([elementName isEqualToString:@"container"]) { |
242 |
self.currentContainer = [ASIPithosContainer container]; |
243 |
} |
244 |
self.currentContent = @""; |
245 |
} |
246 |
|
247 |
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { |
248 |
if ([elementName isEqualToString:@"name"]) { |
249 |
currentContainer.name = currentContent; |
250 |
} else if ([elementName isEqualToString:@"count"]) { |
251 |
currentContainer.count = [currentContent intValue]; |
252 |
} else if ([elementName isEqualToString:@"bytes"]) { |
253 |
currentContainer.bytes = [currentContent intValue]; |
254 |
} else if ([elementName isEqualToString:@"last_modified"]) { |
255 |
currentContainer.lastModified = [[self dateFormatterWithFormatId:0] dateFromString:currentContent]; |
256 |
} else if ([elementName isEqualToString:@"x_container_until_timestamp"]) { |
257 |
currentContainer.untilTimestamp = [NSDate dateWithTimeIntervalSince1970:[currentContent intValue]]; |
258 |
} else if ([elementName rangeOfString:@"x_container_policy_"].location == 0) { |
259 |
if (currentContainer.policy == nil) |
260 |
currentContainer.policy = [NSMutableDictionary dictionary]; |
261 |
[currentContainer.policy setObject:currentContent forKey:[elementName substringFromIndex:19]]; |
262 |
} else if ([elementName rangeOfString:@"x_container_meta_"].location == 0) { |
263 |
if (currentContainer.metadata == nil) |
264 |
currentContainer.metadata = [NSMutableDictionary dictionary]; |
265 |
[currentContainer.metadata setObject:currentContent forKey:[elementName substringFromIndex:17]]; |
266 |
} else if ([elementName isEqualToString:@"container"]) { |
267 |
[containers addObject:currentContainer]; |
268 |
self.currentContainer = nil; |
269 |
} |
270 |
} |
271 |
|
272 |
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { |
273 |
self.currentContent = [currentContent stringByAppendingString:string]; |
274 |
} |
275 |
|
276 |
@end |