-
Notifications
You must be signed in to change notification settings - Fork 0
/
OTRestResponse.m
192 lines (151 loc) · 4.82 KB
/
OTRestResponse.m
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// OTRestResponse.m
// OTRestFramework
//
// Created by Blake Watters on 7/28/09.
// Copyright 2009 Two Toasters. All rights reserved.
//
#import "OTRestResponse.h"
#import "OTRestNotifications.h"
#import "SBJSON.h"
@implementation OTRestResponse
@synthesize payload = _payload, request = _request, failureError = _failureError;
- (id)init {
if (self = [super init]) {
_payload = [[NSMutableData alloc] init];
_failureError = nil;
}
return self;
}
- (id)initWithRestRequest:(OTRestRequest*)request {
if (self = [self init]) {
_request = [request retain];
}
return self;
}
- (void)dealloc {
[_httpURLResponse release];
[_payload release];
[_request release];
[_failureError release];
[super dealloc];
}
// Handle basic auth
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge previousFailureCount] == 0) {
NSURLCredential *newCredential;
newCredential=[NSURLCredential credentialWithUser:[NSString stringWithFormat:@"%@", _request.username]
password:[NSString stringWithFormat:@"%@", _request.password]
persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:newCredential
forAuthenticationChallenge:challenge];
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_payload appendData:data];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response {
_httpURLResponse = [response retain];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSDate* receivedAt = [NSDate date];
NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:[_request HTTPMethod], @"HTTPMethod", [_request URL], @"URL", receivedAt, @"receivedAt", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:kOTRestResponseReceivedNotification object:self userInfo:userInfo];
[[_request delegate] performSelector:[_request callback] withObject:self];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
_failureError = [error retain];
[[_request delegate] performSelector:[_request callback] withObject:self];
}
- (NSString*)localizedStatusCodeString {
return [NSHTTPURLResponse localizedStringForStatusCode:[self statusCode]];
}
- (NSString*)payloadString {
return [[[NSString alloc] initWithData:_payload encoding:NSUTF8StringEncoding] autorelease];
}
- (DocumentRoot*)payloadXMLDocument {
return [DocumentRoot parseXML:[self payloadString]];
}
- (NSDictionary*)payloadJSONDictionary {
return [[[[SBJSON alloc] init] autorelease] objectWithString:[self payloadString]];
}
- (NSString*)failureErrorDescription {
if ([self isFailure]) {
return [_failureError localizedDescription];
} else {
return nil;
}
}
- (NSURL*)URL {
return [_httpURLResponse URL];
}
- (NSString*)MIMEType {
return [_httpURLResponse MIMEType];
}
- (NSInteger)statusCode {
return [_httpURLResponse statusCode];
}
- (NSDictionary*)allHeaderFields {
return [_httpURLResponse allHeaderFields];
}
- (BOOL)isFailure {
return (nil != _failureError);
}
- (BOOL)isInvalid {
return ([self statusCode] < 100 || [self statusCode] > 600);
}
- (BOOL)isInformational {
return ([self statusCode] >= 100 && [self statusCode] < 200);
}
- (BOOL)isSuccessful {
return ([self statusCode] >= 200 && [self statusCode] < 300);
}
- (BOOL)isRedirection {
return ([self statusCode] >= 300 && [self statusCode] < 400);
}
- (BOOL)isClientError {
return ([self statusCode] >= 400 && [self statusCode] < 500);
}
- (BOOL)isServerError {
return ([self statusCode] >= 500 && [self statusCode] < 600);
}
- (BOOL)isError {
return ([self isClientError] || [self isServerError]);
}
- (BOOL)isOK {
return ([self statusCode] == 200);
}
- (BOOL)isCreated {
return ([self statusCode] == 201);
}
- (BOOL)isForbidden {
return ([self statusCode] == 403);
}
- (BOOL)isNotFound {
return ([self statusCode] == 404);
}
- (BOOL)isRedirect {
return ([self statusCode] == 301 || [self statusCode] == 302 || [self statusCode] == 303 || [self statusCode] == 307);
}
- (BOOL)isEmpty {
return ([self statusCode] == 201 || [self statusCode] == 204 || [self statusCode] == 304);
}
- (NSString*)contentType {
return ([[self allHeaderFields] objectForKey:@"Content-Type"]);
}
- (NSString*)contentLength {
return ([[self allHeaderFields] objectForKey:@"Content-Length"]);
}
- (NSString*)location {
return ([[self allHeaderFields] objectForKey:@"Location"]);
}
- (BOOL)isXML {
return [[self contentType] isEqualToString:@"application/xml"];
}
- (BOOL)isJSON {
return [[self contentType] isEqualToString:@"application/json"];
}
@end