5 // Created by Mike Mayo on 12/24/10.
6 // The OpenStack project is provided under the Apache 2.0 license.
9 #import "GetObjectsRequest.h"
10 #import "OpenStackAccount.h"
12 #import "StorageObject.h"
13 #import "AccountManager.h"
16 #import "NSString+Conveniences.h"
18 @implementation GetObjectsRequest
20 @synthesize container;
22 #pragma mark - Constructors
24 + (id)request:(OpenStackAccount *)account method:(NSString *)method url:(NSURL *)url {
25 GetObjectsRequest *request = [[[self
26 alloc] initWithURL:url] autorelease];
27 request.account = account;
28 request.requestMethod = method;
29 [request addRequestHeader:@"X-Auth-Token" value:account.authToken];
30 [request addRequestHeader:@"Content-Type" value:@"application/json"];
31 request.timeOutSeconds = 60;
32 request.numberOfTimesToRetryOnTimeout = 5;
33 request.validatesSecureCertificate = !account.ignoreSSLErrors;
37 + (id)filesRequest:(OpenStackAccount *)account method:(NSString *)method path:(NSString *)path marker:(NSString *)marker {
38 NSString *urlString = [account.filesURL description];
39 if (account.sharingAccount) {
40 urlString = [NSString stringWithFormat:@"%@%@",
41 [urlString substringToIndex:[urlString rangeOfString:account.username].location],
42 account.sharingAccount];
44 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@?format=json%@%@",
47 (account.shared ? @"&shared=" : @""),
48 (marker ? [NSString stringWithFormat:@"&marker=%@", marker] : @"")]];
49 return [self request:account method:method url:url];
52 + (id)filesRequest:(OpenStackAccount *)account method:(NSString *)method path:(NSString *)path {
53 return [self filesRequest:account method:method path:path marker:nil];
56 + (id)request:(OpenStackAccount *)account container:(Container *)container {
57 return [self request:account container:container marker:nil objectsBuffer:nil];
60 + (id)request:(OpenStackAccount *)account container:(Container *)container
61 marker:(NSString *)marker objectsBuffer:(NSMutableDictionary *)objectsBuffer {
62 GetObjectsRequest *request = [self filesRequest:account
64 path:[NSString stringWithFormat:@"/%@", [NSString encodeToPercentEscape:container.name]]
65 marker:[NSString encodeToPercentEscape:marker]];
66 request.container = container;
67 if (objectsBuffer == nil)
68 objectsBuffer = [NSMutableDictionary dictionary];
69 request.userInfo = [NSMutableDictionary dictionaryWithObject:objectsBuffer forKey:@"objectsBuffer"];
73 #pragma mark - ASIHTTPRequest Overrides
75 - (void)requestFinished {
76 if ([self isSuccess]) {
77 Container *aContainer = self.container;
78 NSMutableDictionary *objects = [self objects];
79 NSMutableDictionary *objectsBuffer = [self.userInfo objectForKey:@"objectsBuffer"];
80 [objectsBuffer addEntriesFromDictionary:objects];
81 if (objects.count < 10000) {
82 aContainer.rootFolder = [Folder folder];
83 aContainer.rootFolder.objects = objectsBuffer;
84 [self.account persist];
85 NSNotification *notification = [NSNotification notificationWithName:@"getObjectsSucceeded" object:self.container
86 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
87 aContainer, @"container",
90 [[NSNotificationCenter defaultCenter] postNotification:notification];
92 SBJSON *parser = [[[SBJSON alloc] init] autorelease];
93 NSArray *jsonObjects = [parser objectWithString:[self responseString]];
94 NSString *marker = [[jsonObjects lastObject] objectForKey:@"name"];
95 [self.account.manager getObjects:self.container afterMarker:marker objectsBuffer:objectsBuffer];
98 NSNotification *notification = [NSNotification notificationWithName:@"getObjectsFailed" object:self.container userInfo:[NSDictionary dictionaryWithObject:self forKey:@"request"]];
99 [[NSNotificationCenter defaultCenter] postNotification:notification];
102 [super requestFinished];
105 - (void)failWithError:(NSError *)theError {
106 NSNotification *notification = [NSNotification notificationWithName:@"getObjectsFailed" object:self.container userInfo:[NSDictionary dictionaryWithObject:self forKey:@"request"]];
107 [[NSNotificationCenter defaultCenter] postNotification:notification];
108 [super failWithError:theError];
111 #pragma mark - Memory Management