Revision 6ba383d0

b/asi-http-request-with-pithos/Classes/Pithos/ASIPithosAccount.h
1
//  ASIPithosAccount.h
2
//  pithos-macos
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 <Foundation/Foundation.h>
38

  
39
@interface ASIPithosAccount : NSObject {
40
	NSString *name;
41
	NSDate *lastModified;
42
}
43

  
44
@property (retain) NSString *name;
45
@property (retain) NSDate *lastModified;
46

  
47
+ (id)account;
48

  
49
@end
b/asi-http-request-with-pithos/Classes/Pithos/ASIPithosAccount.m
1
//  ASIPithosAccount.m
2
//  pithos-macos
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 "ASIPithosAccount.h"
38

  
39
@implementation ASIPithosAccount
40
@synthesize name, lastModified;
41

  
42
+ (id)account {
43
	ASIPithosAccount *account = [[[self alloc] init] autorelease];
44
	return account;
45
}
46

  
47
- (NSString *)description {
48
    return [NSString stringWithFormat:@"name: %@, lastModified: %@", name, lastModified];
49
}
50

  
51
-(void) dealloc {
52
    [lastModified release];
53
	[name release];
54
	[super dealloc];
55
}
56

  
57
@end
b/asi-http-request-with-pithos/Classes/Pithos/ASIPithosAccountRequest.h
49 49
    NSMutableArray *containers;
50 50

  
51 51
	// Internally used while parsing the response
52
	NSString *currentContent;
53
	NSString *currentElement;
54 52
	ASIPithosContainer *currentContainer;
55 53
}
56 54

  
57
@property (retain) NSString *currentElement;
58
@property (retain) NSString *currentContent;
59 55
@property (retain) ASIPithosContainer *currentContainer;
60 56

  
61 57
// HEAD storageURL
b/asi-http-request-with-pithos/Classes/Pithos/ASIPithosAccountRequest.m
38 38
#import "ASIPithosContainer.h"
39 39

  
40 40
@implementation ASIPithosAccountRequest
41
@synthesize currentElement, currentContent, currentContainer;
41
@synthesize currentContainer;
42 42

  
43 43
#pragma mark -
44 44
#pragma mark Constructors
......
63 63

  
64 64
- (void)dealloc {
65 65
	[currentContainer release];
66
    [currentContent release];
67
    [currentElement release];
68 66
    [containers release];
69 67
    [metadata release];
70 68
    [groups release];
b/asi-http-request-with-pithos/Classes/Pithos/ASIPithosContainerRequest.h
52 52
    NSMutableArray *objects;
53 53

  
54 54
	// Internally used while parsing the response
55
	NSString *currentContent;
56
	NSString *currentElement;
57 55
	ASIPithosObject *currentObject;
58 56
}
59 57

  
60 58
@property (retain) NSString *containerName;
61
@property (retain) NSString *currentElement;
62
@property (retain) NSString *currentContent;
63 59
@property (retain) ASIPithosObject *currentObject;
64 60

  
65 61
// HEAD storageURL/container
b/asi-http-request-with-pithos/Classes/Pithos/ASIPithosContainerRequest.m
40 40

  
41 41
@implementation ASIPithosContainerRequest
42 42
@synthesize containerName;
43
@synthesize currentElement, currentContent, currentObject;
43
@synthesize currentObject;
44 44

  
45 45
#pragma mark -
46 46
#pragma mark Constructors
......
66 66

  
67 67
- (void)dealloc {
68 68
	[currentObject release];
69
    [currentContent release];
70
    [currentElement release];
71 69
    [objects release];
72 70
    [metadata release];
73 71
    [policy release];
b/asi-http-request-with-pithos/Classes/Pithos/ASIPithosObjectRequest.h
66 66
    NSMutableArray *versions;
67 67
    
68 68
	// Internally used while parsing the response
69
	NSString *currentContent;
70
	NSString *currentElement;
71 69
    ASIPithosObject *currentObject;
72 70
}
73 71

  
74 72
@property (assign) NSUInteger objectRequestType;
75 73
@property (retain) NSString *containerName;
76 74
@property (retain) NSString *objectName;
77
@property (retain) NSString *currentElement;
78
@property (retain) NSString *currentContent;
79 75
@property (retain) ASIPithosObject *currentObject;
80 76

  
81 77
// HEAD storageURL/container/object
b/asi-http-request-with-pithos/Classes/Pithos/ASIPithosObjectRequest.m
52 52
@implementation ASIPithosObjectRequest
53 53
@synthesize objectRequestType;
54 54
@synthesize containerName, objectName;
55
@synthesize currentElement, currentContent, currentObject;
55
@synthesize currentObject;
56 56

  
57 57
#pragma mark -
58 58
#pragma mark Constructors
......
79 79

  
80 80
- (void)dealloc {
81 81
    [currentObject release];
82
    [currentContent release];
83
    [currentElement release];
84 82
    [versions release];
85 83
    [hashes release];
86 84
    [blockHash release];
b/asi-http-request-with-pithos/Classes/Pithos/ASIPithosRequest.h
38 38
#import <Foundation/Foundation.h>
39 39
#import "ASIHTTPRequest.h"
40 40

  
41
@interface ASIPithosRequest : ASIHTTPRequest {
41
#if !TARGET_OS_IPHONE || (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_4_0)
42
#import "ASINSXMLParserCompat.h"
43
#endif
44

  
45
@class ASIPithosAccount;
46

  
47
@interface ASIPithosRequest : ASIHTTPRequest <NSXMLParserDelegate> {
48
    NSMutableArray *sharingAccounts;
49
    
50
	// Internally used while parsing the response
51
	NSString *currentContent;
52
	NSString *currentElement;
53
	ASIPithosAccount *currentAccount;
42 54
}
43 55

  
56
@property (retain) NSString *currentElement;
57
@property (retain) NSString *currentContent;
58
@property (retain) ASIPithosAccount *currentAccount;
59

  
44 60
+ (id)authenticationRequest;
45 61
+ (NSError *)authenticate;
46 62

  
63
// GET authURL
64
+ (id)listSharingAccountsRequest;
65
// GET authURL?[limit=limit]&[marker=marker]
66
+ (id)listSharingAccountsRequestWith:(NSUInteger)limit marker:(NSString *)marker;
67

  
47 68
+ (NSString *)authURL;
48 69
+ (void)setAuthURL:(NSString *)newAuthURL;
49
+ (NSString *)username;
50
+ (void)setUsername:(NSString *)newUsername;
51
+ (NSString *)apiKey;
52
+ (void)setApiKey:(NSString *)newApiKey;
70
+ (NSString *)authUser;
71
+ (void)setAuthUser:(NSString *)newAuthUser;
72
+ (NSString *)authKey;
73
+ (void)setAuthKey:(NSString *)newAuthKey;
53 74

  
75
+ (NSString *)storageURLPrefix;
76
+ (void)setStorageURLPrefix:(NSString *)newStorageURLPrefix;
54 77
+ (NSString *)storageURL;
55 78
+ (void)setStorageURL:(NSString *)newStorageURL;
56 79
+ (NSString *)authToken;
57 80
+ (void)setAuthToken:(NSString *)newAuthToken;
58 81

  
82
+ (NSString *)storageURLWithAuthUser:(NSString *)anAuthUser;
83

  
84
+ (NSString *)publicURLPrefix;
85
+ (void)setPublicURLPrefix:(NSString *)newPublicURLPrefix;
86

  
59 87
// Cached DateFormatters
60 88
- (NSDateFormatter *)dateFormatterWithFormatId:(NSUInteger)formatId;
61 89

  
b/asi-http-request-with-pithos/Classes/Pithos/ASIPithosRequest.m
36 36
// or implied, of GRNET S.A.
37 37

  
38 38
#import "ASIPithosRequest.h"
39
#import "ASIPithosAccount.h"
39 40

  
40 41
static NSString *authURL = nil;
41
static NSString *username = nil;
42
static NSString *apiKey = nil;
42
static NSString *authUser = nil;
43
static NSString *authKey = nil;
43 44

  
45
static NSString *storageURLPrefix = nil;
44 46
static NSString *storageURL = nil;
45 47
static NSString *authToken = nil;
46 48

  
49
static NSString *publicURLPrefix = nil;
50

  
47 51
static NSRecursiveLock *accessDetailsLock = nil;
48 52

  
49 53
@implementation ASIPithosRequest
54
@synthesize currentElement, currentContent, currentAccount;
50 55

  
51 56
+ (void)initialize {
52 57
	if (self == [ASIPithosRequest class]) {
......
55 60
}
56 61

  
57 62
#pragma mark -
63
#pragma mark Constructors
64

  
65
+ (id)authRequestWithMethod:(NSString *)method queryString:(NSString *)queryString useAuthToken:(BOOL)useAuthToken {
66
    NSString *urlString = [NSString stringWithString:authURL];
67
    if (queryString)
68
        urlString = [urlString stringByAppendingString:queryString];
69
    
70
	ASIPithosRequest *request = [[[self alloc] initWithURL:[NSURL URLWithString:urlString]] autorelease];
71
	[request setRequestMethod:method];
72
    if (useAuthToken)
73
        [request addRequestHeader:@"X-Auth-Token" value:authToken];
74
	return request;
75
}
76

  
77
+ (id)authRequestWithMethod:(NSString *)method useAuthToken:(BOOL)useAuthToken {
78
	return [self authRequestWithMethod:method queryString:nil useAuthToken:useAuthToken];
79
}
80

  
81
#pragma mark -
82
#pragma mark Memory Management
83

  
84
- (void)dealloc {
85
	[currentAccount release];
86
    [currentContent release];
87
    [currentElement release];
88
    [sharingAccounts release];
89
	[super dealloc];
90
}
91

  
92
#pragma mark -
58 93
#pragma mark Properties
59 94

  
60 95
+ (NSString *)authURL {
......
68 103
	[accessDetailsLock unlock];
69 104
}
70 105

  
71
+ (NSString *)username {
72
	return username;
106
+ (NSString *)authUser {
107
	return authUser;
73 108
}
74 109

  
75
+ (void)setUsername:(NSString *)newUsername {
110
+ (void)setAuthUser:(NSString *)newAuthUser {
76 111
	[accessDetailsLock lock];
77
	[username release];
78
	username = [newUsername retain];
112
	[authUser release];
113
	authUser = [newAuthUser retain];
79 114
	[accessDetailsLock unlock];
80 115
}
81 116

  
82
+ (NSString *)apiKey {
83
	return apiKey;
117
+ (NSString *)authKey {
118
	return authKey;
84 119
}
85 120

  
86
+ (void)setApiKey:(NSString *)newApiKey {
121
+ (void)setAuthKey:(NSString *)newAuthKey {
87 122
	[accessDetailsLock lock];
88
	[apiKey release];
89
	apiKey = [newApiKey retain];
123
	[authKey release];
124
	authKey = [newAuthKey retain];
125
	[accessDetailsLock unlock];
126
}
127

  
128
+ (NSString *)storageURLPrefix {
129
    return storageURLPrefix;
130
}
131

  
132
+ (void)setStorageURLPrefix:(NSString *)newStorageURLPrefix {
133
	[accessDetailsLock lock];
134
	[storageURLPrefix release];
135
	storageURLPrefix = [newStorageURLPrefix retain];
90 136
	[accessDetailsLock unlock];
91 137
}
92 138

  
93 139
+ (NSString *)storageURL {
94
	return storageURL;
140
    if (storageURL)
141
        return storageURL;
142
    else if (storageURLPrefix && authUser)
143
        return [NSString stringWithFormat:@"%@/%@", storageURLPrefix, [authUser stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
144
    return nil;
95 145
}
96 146

  
97 147
+ (void)setStorageURL:(NSString *)newStorageURL {
......
112 162
	[accessDetailsLock unlock];
113 163
}
114 164

  
165
+ (NSString *)storageURLWithAuthUser:(NSString *)anAuthUser {
166
    if (storageURLPrefix)
167
        return [NSString stringWithFormat:@"%@/%@", storageURLPrefix, [anAuthUser stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
168
    return nil;
169
}
170

  
171
+ (NSString *)publicURLPrefix {
172
    return publicURLPrefix;
173
}
174

  
175
+ (void)setPublicURLPrefix:(NSString *)newPublicURLPrefix {
176
    [accessDetailsLock lock];
177
	[publicURLPrefix release];
178
	publicURLPrefix = [newPublicURLPrefix retain];
179
	[accessDetailsLock unlock];
180
}
181

  
115 182
#pragma mark -
116 183
#pragma mark Authentication
117 184

  
118 185
+ (id)authenticationRequest {
119 186
	[accessDetailsLock lock];
120
	ASIPithosRequest *request = [[[self alloc] initWithURL:[NSURL URLWithString:authURL]] autorelease];
121
	[request addRequestHeader:@"X-Auth-User" value:username];
122
	[request addRequestHeader:@"X-Auth-Key" value:apiKey];
187
    ASIPithosRequest *request = [self authRequestWithMethod:@"GET" useAuthToken:NO];
188
	[request addRequestHeader:@"X-Auth-User" value:authUser];
189
	[request addRequestHeader:@"X-Auth-Key" value:authKey];
123 190
	[accessDetailsLock unlock];
124 191
	return request;
125 192
}
......
139 206
}
140 207

  
141 208
#pragma mark -
209
#pragma mark GET
210

  
211
// GET authURL
212
+ (id)listSharingAccountsRequest {
213
    return [self authRequestWithMethod:@"GET" queryString:@"?format=xml" useAuthToken:YES];
214
}
215

  
216
// GET authURL?[limit=limit]&[marker=marker]
217
+ (id)listSharingAccountsRequestWith:(NSUInteger)limit marker:(NSString *)marker {
218
    NSString *queryString = @"?format=xml";
219
	if (limit && (limit > 0))
220
		queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&limit=%lu", limit]];
221
	if (marker)
222
		queryString = [queryString stringByAppendingString:[NSString stringWithFormat:@"&marker=%@", [marker stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
223
    
224
    return [self authRequestWithMethod:@"GET" queryString:queryString useAuthToken:YES];
225
}
226

  
227
- (NSArray *)sharingAccounts {
228
    if (sharingAccounts == nil) {
229
        sharingAccounts = [[NSMutableArray alloc] init];
230
        
231
        NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:[self responseData]] autorelease];
232
        [parser setDelegate:self];
233
        [parser setShouldProcessNamespaces:NO];
234
        [parser setShouldReportNamespacePrefixes:NO];
235
        [parser setShouldResolveExternalEntities:NO];
236
        [parser parse];
237
    }    
238
	return sharingAccounts;
239
}
240

  
241
#pragma mark -
242
#pragma mark NSXMLParserDelegate
243

  
244
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
245
	self.currentElement = elementName;
246
	if ([elementName isEqualToString:@"account"]) {
247
		self.currentAccount = [ASIPithosAccount account];
248
	}
249
    
250
	self.currentContent = @"";
251
}
252

  
253
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
254
	if ([elementName isEqualToString:@"name"]) {
255
        currentAccount.name = currentContent;
256
    } else if ([elementName isEqualToString:@"last_modified"]) {
257
        currentAccount.lastModified = [[self dateFormatterWithFormatId:0] dateFromString:currentContent];
258
	} else if ([elementName isEqualToString:@"account"]) {
259
		[sharingAccounts addObject:currentAccount];
260
        self.currentAccount = nil;
261
	}
262
}
263

  
264
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
265
    self.currentContent = [currentContent stringByAppendingString:string];
266
}
267

  
268
#pragma mark -
142 269
#pragma mark Date Formatters
143 270

  
144 271
// We store our date formatters in the calling thread's dictionary

Also available in: Unified diff