root / Classes / GetObjectsRequest.m @ e06c24cf
History | View | Annotate | Download (5.3 kB)
1 |
// |
---|---|
2 |
// GetObjectsRequest.m |
3 |
// OpenStack |
4 |
// |
5 |
// Created by Mike Mayo on 12/24/10. |
6 |
// The OpenStack project is provided under the Apache 2.0 license. |
7 |
// |
8 |
|
9 |
#import "GetObjectsRequest.h" |
10 |
#import "OpenStackAccount.h" |
11 |
#import "Container.h" |
12 |
#import "StorageObject.h" |
13 |
#import "AccountManager.h" |
14 |
#import "Folder.h" |
15 |
#import "SBJSON.h" |
16 |
#import "NSString+Conveniences.h" |
17 |
|
18 |
@implementation GetObjectsRequest |
19 |
|
20 |
@synthesize container; |
21 |
|
22 |
+ (id)request:(OpenStackAccount *)account method:(NSString *)method url:(NSURL *)url { |
23 |
GetObjectsRequest *request = [[[GetObjectsRequest alloc] initWithURL:url] autorelease]; |
24 |
request.account = account; |
25 |
[request setRequestMethod:method]; |
26 |
[request addRequestHeader:@"X-Auth-Token" value:[account authToken]]; |
27 |
[request addRequestHeader:@"Content-Type" value:@"application/json"]; |
28 |
return request; |
29 |
} |
30 |
|
31 |
+ (id)filesRequest:(OpenStackAccount *)account method:(NSString *)method path:(NSString *)path marker:(NSString *)marker { |
32 |
NSString *now = [[[NSDate date] description] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
33 |
|
34 |
NSString *filesUrl; |
35 |
NSString *urlString = [account.filesURL description]; |
36 |
if (account.sharingAccount) { |
37 |
NSRange authUserRange = [urlString rangeOfString:account.username]; |
38 |
filesUrl = [NSString stringWithFormat:@"%@%@", |
39 |
[urlString substringToIndex:authUserRange.location], |
40 |
account.sharingAccount]; |
41 |
} else |
42 |
filesUrl = urlString; |
43 |
|
44 |
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@?format=json%@%@&now=%@", |
45 |
filesUrl, |
46 |
path, |
47 |
account.shared ? @"&shared=" : @"", |
48 |
marker ? [NSString stringWithFormat:@"&marker=%@", marker] : @"", |
49 |
now]]; |
50 |
|
51 |
return [GetObjectsRequest request:account method:method url:url]; |
52 |
} |
53 |
|
54 |
+ (id)filesRequest:(OpenStackAccount *)account method:(NSString *)method path:(NSString *)path { |
55 |
return [GetObjectsRequest filesRequest:account method:method path:path marker:nil]; |
56 |
} |
57 |
|
58 |
+ (GetObjectsRequest *)request:(OpenStackAccount *)account container:(Container *)container { |
59 |
return [GetObjectsRequest request:account container:container marker:nil objectsBuffer:nil]; |
60 |
} |
61 |
|
62 |
+ (GetObjectsRequest *)request:(OpenStackAccount *)account |
63 |
container:(Container *)container |
64 |
marker:(NSString *)marker |
65 |
objectsBuffer:(NSMutableDictionary *)objectsBuffer |
66 |
{ |
67 |
GetObjectsRequest *request = [GetObjectsRequest filesRequest:account method:@"GET" |
68 |
path:[NSString stringWithFormat:@"/%@", [NSString encodeToPercentEscape:container.name]] |
69 |
marker:[NSString encodeToPercentEscape:marker]]; |
70 |
request.account = account; |
71 |
request.container = container; |
72 |
if (objectsBuffer == nil) |
73 |
objectsBuffer = [NSMutableDictionary dictionary]; |
74 |
[request setUserInfo:[NSMutableDictionary dictionaryWithObject:objectsBuffer forKey:@"objectsBuffer"]]; |
75 |
[request setTimeOutSeconds:60]; |
76 |
[request setNumberOfTimesToRetryOnTimeout:5]; |
77 |
return request; |
78 |
} |
79 |
|
80 |
|
81 |
- (void)requestFinished { |
82 |
if ([self isSuccess]) { |
83 |
Container *aContainer = self.container; //[self.userInfo objectForKey:@"container"]; |
84 |
NSMutableDictionary *objects = [self objects]; |
85 |
NSMutableDictionary *objectsBuffer = [self.userInfo objectForKey:@"objectsBuffer"]; |
86 |
|
87 |
SBJSON *parser = [[SBJSON alloc] init]; |
88 |
NSArray *jsonObjects = [parser objectWithString:[self responseString]]; |
89 |
NSUInteger numberOfObjectsInResponse = [jsonObjects count]; |
90 |
[parser release]; |
91 |
|
92 |
[objectsBuffer addEntriesFromDictionary:objects]; |
93 |
if (numberOfObjectsInResponse < 10000) { |
94 |
aContainer.rootFolder = [Folder folder]; |
95 |
aContainer.rootFolder.objects = objectsBuffer; |
96 |
[self.account persist]; |
97 |
NSNotification *notification = [NSNotification notificationWithName:@"getObjectsSucceeded" object:self.container userInfo: |
98 |
[NSDictionary dictionaryWithObjectsAndKeys:aContainer,@"container", |
99 |
self, @"request",nil]]; |
100 |
[[NSNotificationCenter defaultCenter] postNotification:notification]; |
101 |
} else { |
102 |
NSString *marker = [[jsonObjects lastObject] objectForKey:@"name"]; |
103 |
[self.account.manager getObjects:self.container afterMarker:marker objectsBuffer:objectsBuffer]; |
104 |
} |
105 |
} else { |
106 |
NSNotification *notification = [NSNotification notificationWithName:@"getObjectsFailed" object:self.container userInfo:[NSDictionary dictionaryWithObject:self forKey:@"request"]]; |
107 |
[[NSNotificationCenter defaultCenter] postNotification:notification]; |
108 |
} |
109 |
|
110 |
[super requestFinished]; |
111 |
} |
112 |
|
113 |
- (void)failWithError:(NSError *)theError { |
114 |
NSNotification *notification = [NSNotification notificationWithName:@"getObjectsFailed" object:self.container userInfo:[NSDictionary dictionaryWithObject:self forKey:@"request"]]; |
115 |
[[NSNotificationCenter defaultCenter] postNotification:notification]; |
116 |
[super failWithError:theError]; |
117 |
} |
118 |
|
119 |
- (void)dealloc { |
120 |
[container release]; |
121 |
[super dealloc]; |
122 |
} |
123 |
|
124 |
@end |