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