Statistics
| Branch: | Revision:

root / asi-http-request-with-pithos / Classes / Pithos / ASIPithosAccountRequest.m @ 0be02d23

History | View | Annotate | Download (13 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 = [NSMutableDictionary dictionary];
124
        NSMutableDictionary *groupsHeaders = [self getHeadersDictionaryForPrefix:@"X-Account-Group-"];
125
        for (NSString *groupName in groupsHeaders) {
126
            [groups setObject:[[groupsHeaders objectForKey:groupName] componentsSeparatedByString:@","]
127
                       forKey:groupName];
128
        }
129
        [groups retain];
130
    }
131
    return groups;
132
}
133

    
134
- (NSDictionary *)policy {
135
    if (policy == nil) {
136
        policy = [self getHeadersDictionaryForPrefix:@"X-Account-Policy-"];
137
        [policy retain];
138
    }
139
    return policy;
140
}
141

    
142
- (NSDictionary *)metadata {
143
    if (metadata == nil) {
144
        metadata = [self getHeadersDictionaryForPrefix:@"X-Account-Meta-"];
145
        [metadata retain];
146
    }
147
    return metadata;
148
}
149

    
150
- (NSDate *)lastModified {
151
    NSString *lastModifiedString = [[self responseHeaders] objectForKey:@"Last-Modified"];
152
    if (lastModifiedString)
153
        return [[self dateFormatterWithFormatId:1] dateFromString:lastModifiedString];
154
    return nil;
155
}
156

    
157
- (ASIPithosAccount *)account {
158
    ASIPithosAccount *account = [ASIPithosAccount account];
159
    account.containerCount = [self containerCount];
160
    account.bytesUsed = [self bytesUsed];
161
    account.untilTimestamp = [self untilTimestamp];
162
    account.groups = (NSMutableDictionary *)[self groups];
163
    account.policy = (NSMutableDictionary *)[self policy];
164
    account.metadata = (NSMutableDictionary *)[self metadata];
165
    account.lastModified = [self lastModified];
166
    return account;
167
}
168

    
169

    
170
#pragma mark -
171
#pragma mark GET
172

    
173
// GET storageURL
174
+ (id)listContainersRequestWithPithos:(ASIPithos *)pithos {
175
	return [self storageRequestWithMethod:@"GET" pithos:pithos queryString:@"?format=xml"];
176
}
177

    
178
// GET storageURL?[limit=limit]&[marker=marker]&[shared=]&[until=untilTimestamp]
179
+ (id)listContainersRequestWithPithos:(ASIPithos *)pithos 
180
                                limit:(NSUInteger)limit 
181
                               marker:(NSString *)marker 
182
                               shared:(BOOL)shared 
183
                                until:(NSDate *)untilTimestamp {
184
	NSString *queryString = @"?format=xml";
185
	if (limit && (limit > 0))
186
		queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&limit=%lu", limit]];
187
	if (marker)
188
		queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&marker=%@", [self encodeToPercentEscape:marker]]];
189
	if (shared)
190
		queryString = [queryString stringByAppendingString:@"&shared="];    
191
    if (untilTimestamp)
192
        queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&until=%d", (int)[untilTimestamp timeIntervalSince1970]]];
193
	
194
	return [self storageRequestWithMethod:@"GET" pithos:pithos queryString:queryString];
195
}
196

    
197
// GET storageURL?[limit=limit]&[marker=marker]&[shared=]&[until=untilTimestamp] [If-Modified-Since]
198
+ (id)listContainersRequestWithPithos:(ASIPithos *)pithos 
199
                                limit:(NSUInteger)limit 
200
                              marker:(NSString *)marker 
201
                              shared:(BOOL)shared 
202
                               until:(NSDate *)untilTimestamp 
203
                     ifModifiedSince:(NSDate *)sinceTimestamp {
204
    ASIPithosAccountRequest *request = [self listContainersRequestWithPithos:pithos limit:limit marker:marker shared:shared until:untilTimestamp];
205
    [request addRequestIfModifiedSinceHeader:sinceTimestamp];
206
    return request;
207
}
208

    
209
// GET storageURL?[limit=limit]&[marker=marker]&[shared=]&[until=untilTimestamp] [If-Unmodified-Since]
210
+ (id)listContainersRequestWithPithos:(ASIPithos *)pithos 
211
                                limit:(NSUInteger)limit 
212
                              marker:(NSString *)marker 
213
                              shared:(BOOL)shared 
214
                               until:(NSDate *)untilTimestamp 
215
                     ifUnmodifiedSince:(NSDate *)sinceTimestamp {
216
    ASIPithosAccountRequest *request = [self listContainersRequestWithPithos:pithos limit:limit marker:marker shared:shared until:untilTimestamp];
217
    [request addRequestIfUnmodifiedSinceHeader:sinceTimestamp];
218
    return request;
219
}
220

    
221

    
222
- (NSArray *)containers {
223
    if (containers == nil) {
224
        containers = [[NSMutableArray alloc] init];
225
        
226
        NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:[self responseData]] autorelease];
227
        [parser setDelegate:self];
228
        [parser setShouldProcessNamespaces:NO];
229
        [parser setShouldReportNamespacePrefixes:NO];
230
        [parser setShouldResolveExternalEntities:NO];
231
        [parser parse];
232
    }    
233
	return containers;
234
}
235

    
236
#pragma mark -
237
#pragma mark POST
238

    
239

    
240
// POST storageURL
241
+ (id)updateAccountMetadataRequestWithPithos:(ASIPithos *)pithos {
242
    return [self storageRequestWithMethod:@"POST" pithos:pithos];
243
}
244

    
245
// POST storageURL?[update=] [X-Account-Group-*] [X-Account-Meta-*]
246
+ (id)updateAccountMetadataRequestWithPithos:(ASIPithos *)pithos 
247
                                      groups:(NSDictionary *)groups 
248
                                    metadata:(NSDictionary *)metadata 
249
                                      update:(BOOL)update {
250
    NSString *queryString = nil;
251
    if (update)
252
        queryString = @"?update=";
253
    ASIPithosAccountRequest *request = [self storageRequestWithMethod:@"POST" pithos:pithos queryString:queryString];
254
	
255
    if (groups) {
256
        for (NSString *key in groups) {
257
            [request addRequestHeader:[self encodeToPercentEscape:[NSString stringWithFormat:@"X-Account-Group-%@", key]] 
258
                                value:[self encodeToPercentEscape:[[groups objectForKey:key] componentsJoinedByString:@","]]];
259
        }
260
    }
261
    
262
    if (metadata) {
263
        for (NSString *key in metadata) {
264
            [request addRequestHeader:[self encodeToPercentEscape:[NSString stringWithFormat:@"X-Account-Meta-%@", key]] 
265
                                value:[self encodeToPercentEscape:[metadata objectForKey:key]]];
266
        }
267
    }
268
    
269
	return request;
270
}
271

    
272
#pragma mark -
273
#pragma mark NSXMLParserDelegate
274

    
275
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
276
    self.currentElement = elementName;
277
	if ([elementName isEqualToString:@"container"]) {
278
		self.currentContainer = [ASIPithosContainer container];
279
	} else if ([elementName isEqualToString:@"x_container_policy"]) {
280
        if (currentContainer.policy == nil)
281
            currentContainer.policy = [NSMutableDictionary dictionary];
282
        self.currentDictionary = currentContainer.policy;
283
    }
284
	self.currentContent = @"";
285
}
286

    
287
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
288
	if ([elementName isEqualToString:@"name"]) {
289
        currentContainer.name = currentContent;
290
	} else if ([elementName isEqualToString:@"count"]) {
291
        currentContainer.count = [currentContent integerValue];
292
	} else if ([elementName isEqualToString:@"bytes"]) {
293
        currentContainer.bytes = [currentContent integerValue];
294
    } else if ([elementName isEqualToString:@"last_modified"]) {
295
        currentContainer.lastModified = [[self dateFormatterWithFormatId:0] dateFromString:
296
                                         [currentContent stringByReplacingOccurrencesOfString:@":" 
297
                                                                                   withString:@"" 
298
                                                                                      options:NSBackwardsSearch 
299
                                                                                        range:NSMakeRange(([currentContent length] - 3), 1)]];
300
    } else if ([elementName isEqualToString:@"x_container_until_timestamp"]) {
301
        currentContainer.untilTimestamp = [NSDate dateWithTimeIntervalSince1970:[currentContent doubleValue]];
302
    } else if ([elementName isEqualToString:@"key"]) {
303
        self.currentKey = currentContent;
304
    } else if ([elementName isEqualToString:@"value"]) {
305
        [currentDictionary setObject:currentContent forKey:currentKey];
306
        self.currentKey = nil;
307
    } else if ([elementName isEqualToString:@"x_container_policy"]) {
308
        self.currentDictionary = nil;
309
	} else if ([elementName isEqualToString:@"container"]) {
310
		[containers addObject:currentContainer];
311
        self.currentContainer = nil;
312
	}
313
}
314

    
315
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
316
    self.currentContent = [currentContent stringByAppendingString:string];
317
}
318
    
319
@end