root / asi-http-request-with-pithos / Classes / S3 / ASIS3BucketRequest.m @ be116d22
History | View | Annotate | Download (6 kB)
1 |
// |
---|---|
2 |
// ASIS3BucketRequest.m |
3 |
// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest |
4 |
// |
5 |
// Created by Ben Copsey on 16/03/2010. |
6 |
// Copyright 2010 All-Seeing Interactive. All rights reserved. |
7 |
// |
8 |
|
9 |
#import "ASIS3BucketRequest.h" |
10 |
#import "ASIS3BucketObject.h" |
11 |
|
12 |
|
13 |
// Private stuff |
14 |
@interface ASIS3BucketRequest () |
15 |
@property (retain, nonatomic) ASIS3BucketObject *currentObject; |
16 |
@property (retain) NSMutableArray *objects; |
17 |
@property (retain) NSMutableArray *commonPrefixes; |
18 |
@property (assign) BOOL isTruncated; |
19 |
@end |
20 |
|
21 |
@implementation ASIS3BucketRequest |
22 |
|
23 |
- (id)initWithURL:(NSURL *)newURL |
24 |
{ |
25 |
self = [super initWithURL:newURL]; |
26 |
[self setObjects:[[[NSMutableArray alloc] init] autorelease]]; |
27 |
[self setCommonPrefixes:[[[NSMutableArray alloc] init] autorelease]]; |
28 |
return self; |
29 |
} |
30 |
|
31 |
+ (id)requestWithBucket:(NSString *)theBucket |
32 |
{ |
33 |
ASIS3BucketRequest *request = [[[self alloc] initWithURL:nil] autorelease]; |
34 |
[request setBucket:theBucket]; |
35 |
return request; |
36 |
} |
37 |
|
38 |
+ (id)requestWithBucket:(NSString *)theBucket subResource:(NSString *)theSubResource |
39 |
{ |
40 |
ASIS3BucketRequest *request = [[[self alloc] initWithURL:nil] autorelease]; |
41 |
[request setBucket:theBucket]; |
42 |
[request setSubResource:theSubResource]; |
43 |
return request; |
44 |
} |
45 |
|
46 |
+ (id)PUTRequestWithBucket:(NSString *)theBucket |
47 |
{ |
48 |
ASIS3BucketRequest *request = [self requestWithBucket:theBucket]; |
49 |
[request setRequestMethod:@"PUT"]; |
50 |
return request; |
51 |
} |
52 |
|
53 |
|
54 |
+ (id)DELETERequestWithBucket:(NSString *)theBucket |
55 |
{ |
56 |
ASIS3BucketRequest *request = [self requestWithBucket:theBucket]; |
57 |
[request setRequestMethod:@"DELETE"]; |
58 |
return request; |
59 |
} |
60 |
|
61 |
- (void)dealloc |
62 |
{ |
63 |
[currentObject release]; |
64 |
[objects release]; |
65 |
[commonPrefixes release]; |
66 |
[prefix release]; |
67 |
[marker release]; |
68 |
[delimiter release]; |
69 |
[subResource release]; |
70 |
[bucket release]; |
71 |
[super dealloc]; |
72 |
} |
73 |
|
74 |
- (NSString *)canonicalizedResource |
75 |
{ |
76 |
if ([self subResource]) { |
77 |
return [NSString stringWithFormat:@"/%@/?%@",[self bucket],[self subResource]]; |
78 |
} |
79 |
return [NSString stringWithFormat:@"/%@/",[self bucket]]; |
80 |
} |
81 |
|
82 |
- (void)buildURL |
83 |
{ |
84 |
NSString *baseURL; |
85 |
if ([self subResource]) { |
86 |
baseURL = [NSString stringWithFormat:@"%@://%@.%@/?%@",[self requestScheme],[self bucket],[[self class] S3Host],[self subResource]]; |
87 |
} else { |
88 |
baseURL = [NSString stringWithFormat:@"%@://%@.%@",[self requestScheme],[self bucket],[[self class] S3Host]]; |
89 |
} |
90 |
NSMutableArray *queryParts = [[[NSMutableArray alloc] init] autorelease]; |
91 |
if ([self prefix]) { |
92 |
[queryParts addObject:[NSString stringWithFormat:@"prefix=%@",[[self prefix] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; |
93 |
} |
94 |
if ([self marker]) { |
95 |
[queryParts addObject:[NSString stringWithFormat:@"marker=%@",[[self marker] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; |
96 |
} |
97 |
if ([self delimiter]) { |
98 |
[queryParts addObject:[NSString stringWithFormat:@"delimiter=%@",[[self delimiter] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; |
99 |
} |
100 |
if ([self maxResultCount] > 0) { |
101 |
[queryParts addObject:[NSString stringWithFormat:@"max-keys=%hi",[self maxResultCount]]]; |
102 |
} |
103 |
if ([queryParts count]) { |
104 |
NSString* template = @"%@?%@"; |
105 |
if ([[self subResource] length] > 0) { |
106 |
template = @"%@&%@"; |
107 |
} |
108 |
[self setURL:[NSURL URLWithString:[NSString stringWithFormat:template,baseURL,[queryParts componentsJoinedByString:@"&"]]]]; |
109 |
} else { |
110 |
[self setURL:[NSURL URLWithString:baseURL]]; |
111 |
|
112 |
} |
113 |
} |
114 |
|
115 |
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict |
116 |
{ |
117 |
if ([elementName isEqualToString:@"Contents"]) { |
118 |
[self setCurrentObject:[ASIS3BucketObject objectWithBucket:[self bucket]]]; |
119 |
} |
120 |
[super parser:parser didStartElement:elementName namespaceURI:namespaceURI qualifiedName:qName attributes:attributeDict]; |
121 |
} |
122 |
|
123 |
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName |
124 |
{ |
125 |
if ([elementName isEqualToString:@"Contents"]) { |
126 |
[objects addObject:currentObject]; |
127 |
[self setCurrentObject:nil]; |
128 |
} else if ([elementName isEqualToString:@"Key"]) { |
129 |
[[self currentObject] setKey:[self currentXMLElementContent]]; |
130 |
} else if ([elementName isEqualToString:@"LastModified"]) { |
131 |
[[self currentObject] setLastModified:[[ASIS3Request S3ResponseDateFormatter] dateFromString:[self currentXMLElementContent]]]; |
132 |
} else if ([elementName isEqualToString:@"ETag"]) { |
133 |
[[self currentObject] setETag:[self currentXMLElementContent]]; |
134 |
} else if ([elementName isEqualToString:@"Size"]) { |
135 |
[[self currentObject] setSize:(unsigned long long)[[self currentXMLElementContent] longLongValue]]; |
136 |
} else if ([elementName isEqualToString:@"ID"]) { |
137 |
[[self currentObject] setOwnerID:[self currentXMLElementContent]]; |
138 |
} else if ([elementName isEqualToString:@"DisplayName"]) { |
139 |
[[self currentObject] setOwnerName:[self currentXMLElementContent]]; |
140 |
} else if ([elementName isEqualToString:@"Prefix"] && [[self currentXMLElementStack] count] > 2 && [[[self currentXMLElementStack] objectAtIndex:[[self currentXMLElementStack] count]-2] isEqualToString:@"CommonPrefixes"]) { |
141 |
[[self commonPrefixes] addObject:[self currentXMLElementContent]]; |
142 |
} else if ([elementName isEqualToString:@"IsTruncated"]) { |
143 |
[self setIsTruncated:[[self currentXMLElementContent] isEqualToString:@"true"]]; |
144 |
} else { |
145 |
// Let ASIS3Request look for error messages |
146 |
[super parser:parser didEndElement:elementName namespaceURI:namespaceURI qualifiedName:qName]; |
147 |
} |
148 |
} |
149 |
|
150 |
#pragma mark NSCopying |
151 |
|
152 |
- (id)copyWithZone:(NSZone *)zone |
153 |
{ |
154 |
ASIS3BucketRequest *newRequest = [super copyWithZone:zone]; |
155 |
[newRequest setBucket:[self bucket]]; |
156 |
[newRequest setSubResource:[self subResource]]; |
157 |
[newRequest setPrefix:[self prefix]]; |
158 |
[newRequest setMarker:[self marker]]; |
159 |
[newRequest setMaxResultCount:[self maxResultCount]]; |
160 |
[newRequest setDelimiter:[self delimiter]]; |
161 |
return newRequest; |
162 |
} |
163 |
|
164 |
@synthesize bucket; |
165 |
@synthesize subResource; |
166 |
@synthesize currentObject; |
167 |
@synthesize objects; |
168 |
@synthesize commonPrefixes; |
169 |
@synthesize prefix; |
170 |
@synthesize marker; |
171 |
@synthesize maxResultCount; |
172 |
@synthesize delimiter; |
173 |
@synthesize isTruncated; |
174 |
|
175 |
@end |