Statistics
| Branch: | Revision:

root / asi-http-request-with-pithos / Classes / Pithos / ASIPithosAccountRequest.m @ 13bc2a4e

History | View | Annotate | Download (11.1 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 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
    [containers release];
67
    [metadata release];
68
    [groups release];
69
	[super dealloc];
70
}
71

    
72
#pragma mark -
73
#pragma mark HEAD
74

    
75
// HEAD storageURL
76
+ (id)accountMetadataRequest {
77
	return [self storageRequestWithMethod:@"HEAD"];
78
}
79

    
80
// HEAD storageURL?[until=untilTimestamp]
81
+ (id)accountMetadataRequestUntil:(NSDate *)untilTimestamp {
82
    NSString *queryString = nil;
83
    if (untilTimestamp)
84
        queryString = [NSString stringWithFormat:@"?until=%d", (int)[untilTimestamp timeIntervalSince1970]];
85
	return [self storageRequestWithMethod:@"HEAD" queryString:queryString];
86
}
87

    
88
// HEAD storageURL?[until=untilTimestamp] [If-Modified-Since]
89
+ (id)accountMetadataRequestUntil:(NSDate *)untilTimestamp ifModifiedSince:(NSDate *)sinceTimestamp {
90
    ASIPithosAccountRequest *request = [self accountMetadataRequestUntil:untilTimestamp];
91
    [request addRequestIfModifiedSinceHeader:sinceTimestamp];
92
    return request;
93
}
94

    
95
// HEAD storageURL?[until=untilTimestamp] [If-Unmodified-Since]
96
+ (id)accountMetadataRequestUntil:(NSDate *)untilTimestamp ifUnmodifiedSince:(NSDate *)sinceTimestamp {
97
    ASIPithosAccountRequest *request = [self accountMetadataRequestUntil:untilTimestamp];
98
    [request addRequestIfUnmodifiedSinceHeader:sinceTimestamp];
99
    return request;
100
}
101

    
102
- (NSUInteger)containerCount {
103
	return [[[self responseHeaders] objectForKey:@"X-Account-Container-Count"] intValue];
104
}
105

    
106
- (NSUInteger)bytesUsed {
107
	return [[[self responseHeaders] objectForKey:@"X-Account-Bytes-Used"] intValue];
108
}
109

    
110
- (NSDate *)untilTimestamp {
111
    NSString *untilTimestampString = [[self responseHeaders] objectForKey:@"X-Account-Until-Timestamp"];
112
    if (untilTimestampString)
113
        return [[self dateFormatterWithFormatId:1] dateFromString:untilTimestampString];
114
    return nil;
115
}
116

    
117
- (NSDictionary *)groups {
118
    if (groups == nil) {
119
        groups = [self getHeadersDictionaryForPrefix:@"X-Account-Group-"];
120
        [groups retain];
121
    }
122
    return groups;
123
}
124

    
125
- (NSDictionary *)metadata {
126
    if (metadata == nil) {
127
        metadata = [self getHeadersDictionaryForPrefix:@"X-Account-Meta-"];
128
        [metadata retain];
129
    }
130
    return metadata;
131
}
132

    
133
- (NSDate *)lastModified {
134
    NSString *lastModifiedString = [[self responseHeaders] objectForKey:@"Last-Modified"];
135
    if (lastModifiedString)
136
        return [[self dateFormatterWithFormatId:1] dateFromString:lastModifiedString];
137
    return nil;
138
}
139

    
140
#pragma mark -
141
#pragma mark GET
142

    
143
// GET storageURL
144
+ (id)listContainersRequest {
145
	return [self storageRequestWithMethod:@"GET" queryString:@"?format=xml"];
146
}
147

    
148
// GET storageURL?[limit=limit]&[marker=marker]&[shared=]&[until=untilTimestamp]
149
+ (id)listContainersRequestWithLimit:(NSUInteger)limit 
150
                              marker:(NSString *)marker 
151
                              shared:(BOOL)shared 
152
                               until:(NSDate *)untilTimestamp {
153
	NSString *queryString = @"?format=xml";
154
	if (limit && (limit > 0))
155
		queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&limit=%lu", limit]];
156
	if (marker)
157
		queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&marker=%@", [marker stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
158
	if (shared)
159
		queryString = [queryString stringByAppendingString:@"&shared="];    
160
    if (untilTimestamp)
161
        queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&until=%d", (int)[untilTimestamp timeIntervalSince1970]]];
162
	
163
	return [self storageRequestWithMethod:@"GET" queryString:queryString];
164
}
165

    
166
// GET storageURL?[limit=limit]&[marker=marker]&[shared=]&[until=untilTimestamp] [If-Modified-Since]
167
+ (id)listContainersRequestWithLimit:(NSUInteger)limit 
168
                              marker:(NSString *)marker 
169
                              shared:(BOOL)shared 
170
                               until:(NSDate *)untilTimestamp 
171
                     ifModifiedSince:(NSDate *)sinceTimestamp {
172
    ASIPithosAccountRequest *request = [self listContainersRequestWithLimit:limit marker:marker shared:shared until:untilTimestamp];
173
    [request addRequestIfModifiedSinceHeader:sinceTimestamp];
174
    return request;
175
}
176

    
177
// GET storageURL?[limit=limit]&[marker=marker]&[shared=]&[until=untilTimestamp] [If-Unmodified-Since]
178
+ (id)listContainersRequestWithLimit:(NSUInteger)limit 
179
                              marker:(NSString *)marker 
180
                              shared:(BOOL)shared 
181
                               until:(NSDate *)untilTimestamp 
182
                     ifUnmodifiedSince:(NSDate *)sinceTimestamp {
183
    ASIPithosAccountRequest *request = [self listContainersRequestWithLimit:limit marker:marker shared:shared until:untilTimestamp];
184
    [request addRequestIfUnmodifiedSinceHeader:sinceTimestamp];
185
    return request;
186
}
187

    
188

    
189
- (NSArray *)containers {
190
    if (containers == nil) {
191
        containers = [[NSMutableArray alloc] init];
192
        
193
        NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:[self responseData]] autorelease];
194
        [parser setDelegate:self];
195
        [parser setShouldProcessNamespaces:NO];
196
        [parser setShouldReportNamespacePrefixes:NO];
197
        [parser setShouldResolveExternalEntities:NO];
198
        [parser parse];
199
    }    
200
	return containers;
201
}
202

    
203
#pragma mark -
204
#pragma mark POST
205

    
206

    
207
// POST storageURL
208
+ (id)updateAccountMetadataRequest {
209
    return [self storageRequestWithMethod:@"POST"];
210
}
211

    
212
// POST storageURL?[update=] [X-Account-Group-*] [X-Account-Meta-*]
213
+ (id)updateAccountMetadataRequestWithGroups:(NSDictionary *)groups metadata:(NSDictionary *)metadata update:(BOOL)update {
214
    NSString *queryString = nil;
215
    if (update)
216
        queryString = @"?update=";
217
    ASIPithosAccountRequest *request = [self storageRequestWithMethod:@"POST" queryString:queryString];
218
	
219
    if (groups) {
220
        for (NSString *key in [groups keyEnumerator]) {
221
            [request addRequestHeader:[NSString stringWithFormat:@"X-Account-Group-%@", key] value:[groups objectForKey:key]];
222
        }
223
    }
224
    
225
    if (metadata) {
226
        for (NSString *key in [metadata keyEnumerator]) {
227
            [request addRequestHeader:[NSString stringWithFormat:@"X-Account-Meta-%@", key] value:[metadata objectForKey:key]];
228
        }
229
    }
230
    
231
	return request;
232
}
233

    
234
#pragma mark -
235
#pragma mark NSXMLParserDelegate
236

    
237
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
238
    self.currentElement = elementName;
239
	if ([elementName isEqualToString:@"container"]) {
240
		self.currentContainer = [ASIPithosContainer container];
241
	}
242
	self.currentContent = @"";
243
}
244

    
245
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
246
	if ([elementName isEqualToString:@"name"]) {
247
        currentContainer.name = currentContent;
248
	} else if ([elementName isEqualToString:@"count"]) {
249
        currentContainer.count = [currentContent intValue];
250
	} else if ([elementName isEqualToString:@"bytes"]) {
251
        currentContainer.bytes = [currentContent intValue];
252
    } else if ([elementName isEqualToString:@"last_modified"]) {
253
        currentContainer.lastModified = [[self dateFormatterWithFormatId:0] dateFromString:
254
                                         [currentContent stringByReplacingOccurrencesOfString:@":" 
255
                                                                                   withString:@"" 
256
                                                                                      options:NSBackwardsSearch 
257
                                                                                        range:NSMakeRange(([currentContent length] - 3), 1)]];
258
    } else if ([elementName isEqualToString:@"x_container_until_timestamp"]) {
259
        currentContainer.untilTimestamp = [NSDate dateWithTimeIntervalSince1970:[currentContent intValue]];
260
    } else if ([elementName rangeOfString:@"x_container_policy_"].location == 0) {
261
        if (currentContainer.policy == nil)
262
            currentContainer.policy = [NSMutableDictionary dictionary];
263
        [currentContainer.policy setObject:currentContent forKey:[elementName substringFromIndex:19]];
264
    } else if ([elementName rangeOfString:@"x_container_meta_"].location == 0) {    
265
        if (currentContainer.metadata == nil)
266
            currentContainer.metadata = [NSMutableDictionary dictionary];
267
        [currentContainer.metadata setObject:currentContent forKey:[elementName substringFromIndex:17]];
268
	} else if ([elementName isEqualToString:@"container"]) {
269
		[containers addObject:currentContainer];
270
        self.currentContainer = nil;
271
	}
272
}
273

    
274
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
275
    self.currentContent = [currentContent stringByAppendingString:string];
276
}
277
    
278
@end